Skip to content

victory-shader: build the per-instruction read/write lists once (ir::build -13%) - #1033

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
pes7:perf/ir-pairs
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
pes7:perf/ir-pairs

Conversation

@pes7

@pes7 pes7 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

ir::build allocates four times per instruction for lists of two to four registers. This builds them once, for 13% off the SSA pass and 4.7% off a translation, with byte-identical output.

effects() collects each instruction's register lists as Vec<Reg> - two allocations - and the caller immediately maps them into Vec<(Reg, Value)>, the shape the IR wants - two more allocations - and frees the first pair. On a 26k-instruction program that is ~100k allocations and ~50k copies that nothing reads.

What changes

effects(), add_regs(), the mask/indexed helpers and the branch arms push (Reg, NONE) directly, the placeholder the SSA pass fills in. Two allocations per instruction instead of four. Inst, Phi and everything downstream are untouched; the placeholder value is the same NONE it was mapped to a line later.

Measured

The bench is the one added in #1002 (crates/victory-shader/src/emit/bench_tests.rs, synthetic GFX10 compute programs, no game data). Ten interleaved rounds per arm, best of ten, nanoseconds per instruction:

instructions ir::build before after ratio
256 308 269 0.873
1 024 375 324 0.864
4 096 407 340 0.835
16 384 402 353 0.878
instructions whole translation before after ratio
256 898 872 0.971
1 024 898 825 0.919
4 096 951 875 0.920
16 384 956 912 0.954

gfx10::decode (105-113 ns), structure, resource, io and lower are unchanged, which is the check that the change did what it claims and nothing else.

The bench lives in #1002 and the numbers above were measured on top of it, but this branch is only the ir.rs change - one file, +25/-25 - and applies to main on its own; nothing here depends on #1002 landing first.

Correctness

  • DXIL byte-identical at all four sizes (FNV-1a over the emitted bytes, printed by the bench).
  • cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored.
  • Both CI clippy configurations with -D warnings; cargo xtask policy: 173 files ok.

No game data used.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pure refactor: effects() now returns (Reg, Value) pairs with NONE placeholders directly instead of Reg vecs + unset closure; the vector rule (VGPR writes auto-added to reads, 'lanes disabled in EXEC keep the old value') is preserved via .cloned(). No behavior change, existing 96 tests cover it. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p <crate> --locked. Shader execution tests (valu_tests/host) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. victory-shader: 96 passed, 0 failed.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 94cc038: cargo test -p victory-shader passes, 120 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@pes7

pes7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto today's main (70564ae), which conflicts on ir.rs: build became build_bounded and effects now takes m0_max: Option<u8> to bound relative moves (v_movrel*). Both survive - the pairs this PR returns are still pairs, and the bound still cuts the range:

let indexed = |op: &Operand, out: &mut Vec<(Reg, Value)>| match *op {
    Operand::Vector { reg, .. } if u16::from(reg) < vgprs => {
        let end = m0_max.map_or(vgprs, |m| vgprs.min(u16::from(reg) + u16::from(m) + 1));
        out.extend((u16::from(reg)..end).map(|r| (Reg::V(r as u8), NONE)));

Head is now 266f671. cargo test -p victory-shader --locked: 209 passed, 0 failed; both clippy configurations with -D warnings pass, as does cargo xtask policy. The diff against this PR is unchanged (+25/-25, one file).

@catminou4 - your approval is still shown on the PR, but the head moved because of the rebase, so re-check if you want the approval to be for this exact tree; the change itself is the same one you reviewed.

pes7 added a commit to pes7/VIctory that referenced this pull request Nov 24, 2026
`Instruction::words` is a `Vec<u32>` built with `collect()`, so every
decoded instruction allocates for the one to five dwords it has and reads
them back through a pointer. A counter over the pipeline put that at 1.17
allocations and 149 bytes per instruction in decode - the largest single
allocation source there.

The longest instruction the decoder produces is an MIMG with its three
extra address dwords, five in all, so keep them in `[u32; 5]` with a
length byte and return a slice from `words()`. The struct does not grow:
`Vec` is 24 bytes, the array and the byte are 24 with padding. A longer
instruction is now a clean `Isa` error rather than a silent truncation.

Measured standalone against `ffe310e` with the synthetic bench from Marvisak#1002,
twelve interleaved rounds per arm, nanoseconds per instruction:

  decode  48 -> 31,  99 -> 54, 105 -> 82, 104 -> 85        (0.55-0.82)
  whole   977 -> 934, 970 -> 921, 1001 -> 981, 1046 -> 1033 (0.949-0.988)

The emitted DXIL is byte-identical at all four sizes. Honest about the
shape: decode is 18-45% faster, the whole translation is 1-5%, and at
16k instructions that gap is inside this machine's run-to-run spread.
Applied on top of Marvisak#1002 + Marvisak#1033 + Marvisak#1180 the same change measured
0.929-0.957 of the translation.

cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored.
Both clippy configurations with -D warnings; cargo xtask policy.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. The head (d809fb7) has not changed since the last review and still merges cleanly. effects() and add_regs() in crates/victory-shader/src/ir.rs push (Reg, NONE) pairs directly, so build_bounded no longer converts each Vec<Reg> through the unset closure. The resulting IR is the same: NONE is the placeholder the closure added, the seen.contains dedup and the m0_max-bounded indexed range are unchanged, and both functions are private to this file.

Reply to the author: thanks for the corpus measurement (-3.2% wall time, output identical on all 26,084 programs). It confirms the earlier reading, so the verdict stays. #1623 is a separate decoder change and does not overlap.

CI: passing.


Generated by Claude Code

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against origin/main (940d32f):

  • Building per-instruction read/write lists in a single pass merges cleanly and passes all IR tests.
  • cargo test -p victory-shader ir::tests: 9/9 passed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

ir::build fills every instruction's register lists twice. `effects()`
collects `Vec<Reg>` (two allocations), and the caller immediately maps
each one into `Vec<(Reg, Value)>` (two more), freeing the first pair. For
lists that are almost always two to four entries, that is four
allocations and two copies per instruction, on every instruction of every
program.

Have `effects()` and its helpers collect the `(Reg, Value)` pairs
directly, with the placeholder the SSA pass fills in. Two allocations per
instruction instead of four; `Inst` and everything downstream are
unchanged.

Measured with the synthetic bench in Marvisak#1002 (no game data), ten
interleaved rounds per arm, nanoseconds per instruction, at 256/1k/4k/16k
instructions:

  ir::build   308 -> 269, 375 -> 324, 407 -> 340, 402 -> 353  (0.835-0.878)
  translation 898 -> 872, 898 -> 825, 951 -> 875, 956 -> 912  (0.919-0.971)

gfx10::decode, structure, lower and the rest are unchanged. The emitted
DXIL is byte-identical at all four sizes.

cargo test --workspace --locked: 342 passed, 0 failed, 7 ignored.
Both clippy configurations clean; cargo xtask policy ok.
@pes7

pes7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Real corpus, same method as the note on #1002: the Xbox build's shaders.rpf - 26 084 programs, 40 601 701 IR instructions - victory translate <shaders.rpf> --list, three binaries running at the same time on one thread each (--jobs 1) on a 32-core machine, so they share identical conditions and the control is common:

binary wall against main
main 940d32f 600 s -
this PR d809fb7 581 s -3.2%
#1623 246c928 593 s -1.2%

So the claim holds on real programs and not only on the bench: -3.2% for the whole translation here, against the -4.7% the PR measured on synthetic programs (a real program is 1 557 instructions on average, so the SSA share is a little smaller there).

The output is identical to main on all 26 084 programs - every count in the summary and every emit:/resources: line matches; only the (e.g. ...) examples differ between runs, because the thread that finishes first supplies the example.

For the record, the same measurement says the earlier Fx-hasher change (#1002) is -0.3%, i.e. neutral, and I have closed it: whatever lower's tables cost on synthetic programs, on this corpus they are not what the translation spends its time on.

@Marvisak
Marvisak merged commit b9c8a8b into Marvisak:main Nov 25, 2026
1 check passed
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants