Skip to content

Shaders: walk a predecessor's live-out registers once instead of searching it per register - #2520

Open
liandraraiane wants to merge 1 commit into
Marvisak:mainfrom
liandraraiane:perf/shader-translation-profiling
Open

liandraraiane wants to merge 1 commit into
Marvisak:mainfrom
liandraraiane:perf/shader-translation-profiling

Conversation

@liandraraiane

Copy link
Copy Markdown

The IR's renaming looked up every live register in its predecessor's sorted live-out list by binary search, and again for every phi argument. Both sides are in register order, so one walk along the list
serves them all. The live-out lists are also sized before they are filled.

Change: crates/victory-shader/src/ir.rs (build_bounded), plus a new ir_build benchmark.

Measurement

cargo bench -p victory-shader --bench ir_build (synthetic programs, no title files or GPU), median of 9, release build:

Program main this PR
128 registers, 500 divergent ifs 10.5 ms 7.6 ms
128 registers, 2000 divergent ifs 47.2 ms 36.0 ms

With 8 registers the change is within noise.

Tests

  • cargo test -p victory-shader --lib: 144 passed, 2 ignored (Ubuntu 24.04).
  • The built IR (phis, predecessors, inputs, reads, writes) hashes the same as on main for the benchmark's programs.

Not tested

  • Real title shaders: I have none, so I cannot say how much of a whole translation this is. The gain only shows with many live registers and many blocks.
  • Windows, GPU runs and the Windows CI checks (clippy -D warnings, GPU tests).
  • Loops and irreducible flow in the equality check. Only the existing tests cover them.

AI use

Claude Code (Claude Sonnet 5.5) profiled, wrote the change and the benchmark, and drafted this description. A person reviewed it and opened the PR.

…ching it per register

The IR's renaming looked up every live register in its predecessor's
sorted live-out list by binary search, and again for every phi argument.
Both sides are in register order, so one walk along the list serves them
all; the live-out lists are also sized before they are filled.

New ir_build benchmark (synthetic, no title files or GPU), median:
  128 registers, 500 divergent ifs:   10.5 ms -> 7.6 ms
  128 registers, 2000 divergent ifs:  47.2 ms -> 36.0 ms

The built IR hashes the same as on main for the benchmark's programs.
Tested on Ubuntu only (cargo test -p victory-shader --lib: 144 passed).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Verification (Windows 11, WARP/CPU only) — head be6ae54e88af9a4dea8f94889469a803f0ca8a88

Tested: the PR head, which is origin/main 940d32f plus the PR commits (merge-base 940d32f; clean merge, fast-forward). Windows 11 Pro 10.0.26200, rustc/cargo 1.98.1. VICTORY_WARP=1 and VICTORY_D3D12_WARP=1 set so nothing runs on a physical GPU.

command result
cargo test -p victory-shader --locked --lib -- --test-threads=1 245 passed, 2 failed, 3 ignored
cargo test -p victory-shader --locked --lib ir:: -- --test-threads=1 9 passed, 0 failed
cargo clippy -p victory-shader --all-targets --locked -- -D warnings clean (includes the new bench)
cargo xtask policy asset policy: 183 files ok
cargo bench -p victory-shader --locked --bench ir_build runs; medians below

The 2 failures are the WARP baseline: gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out fail identically on origin/main 940d32f under forced WARP (245 passed, 2 failed, 3 ignored there too; this PR adds no tests, so the counts match).

Bench (single run each on this machine, so only indicative; the PR's bench file copied onto origin/main for the "before" column), median of 9:

program main PR
8 regs, 500 ifs 4.93 ms 4.00 ms
64 regs, 500 ifs 9.47 ms 6.67 ms
128 regs, 500 ifs 12.94 ms 10.08 ms
8 regs, 2000 ifs 16.71 ms 15.26 ms
128 regs, 2000 ifs 55.03 ms 37.80 ms

Diff read: members() yields registers in 0..REGS order and Reg derives Ord in the same variant order the old binary_search_by_key relied on, so the forward walk in lookup_from is equivalent to the search (a register missing from outs leaves the cursor on the next larger entry and returns NONE, as before). Phis are only pushed in that one members loop, so they are in register order when the per-predecessor walk fills args; arg order still follows block.preds. The debug_assert was rewritten to the all-phis form with the same meaning. No findings. Coverage note: the change is only checked by the existing IR/emit tests (no new unit test pins the ordering assumption), which pass.

AI use: Claude Code (Claude Sonnet 5.5).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

In build_bounded (crates/victory-shader/src/ir.rs), the per-register binary search in a predecessor's live-out list becomes one forward walk (lookup_from). This applies to single-predecessor blocks and to phi arguments, and the walk restarts for each predecessor. I checked the ordering it relies on: outs is built by members in index order, the live-in registers and the phis (pushed only in that members loop) come in the same order, and a missing register returns NONE as before. The ir_build bench target is synthetic and adds no dependency.

The speed-up is measured on synthetic programs only (about 25 % with 128 live registers). The gain on real translations is not measured.

CI: check passing.


Generated by Claude Code

@rezioliver-collab

Copy link
Copy Markdown

On an Apple M1, the full IR dump matched byte for byte between 940d32f (base) and be6ae54e (PR) for both loop shapes in Wave32 and Wave64, including the order of every phi argument.

Synthetic GFX10 shader IR blocks Phi nodes Wave32 Wave64
Single-entry loop with a back edge 3 2 Identical (15,848 bytes) Identical (16,225 bytes)
Two-entry loop, after node splitting 5 4 Identical (32,386 bytes) Identical (32,766 bytes)

I built each commit separately on macOS 14.7 with native Rust 1.98.1 (dev profile). I passed the same synthetic instruction words to ir::build with 16 VGPRs at each wave size, then compared the complete Program debug dumps directly, preserving block, predecessor and operand order.

Both fixtures carry s2 and s9 through the loop. For each phi, I also checked that each argument was the value last written to that register by the corresponding predecessor. For example, in block 2 of the two-entry Wave32 case, predecessors [0, 1, 4] supplied s2 SSA value IDs [4, 22, 7] in that order on both commits.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
… once instead of searching it per register

@fuzeofficlal fuzeofficlal 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.

Reviewed and verified against main (63bcca43) on Windows 11 — clean optimization and ready to merge (note: overlaps with #2957 which replaces ir.rs's SSA builder):

  • Reg's derived Ord (S(0..127) < Scc < V(0..255)) matches index(reg) and members() enumeration order, so outs[p] and block.phis are strictly ascending and the two-pointer lookup_from(&outs[p], &mut walk, reg) walk cannot miss any live-out entry.

Local verification:

  • cargo test --locked -p victory-shader --lib -- ir::: 9/9 passed
  • cargo bench --locked -p victory-shader --bench ir_build: ran cleanly (2.38ms–29.22ms across 8..128 regs / 500..2000 ifs)
  • cargo clippy --all-targets -- -D warnings + cargo xtask policy (192 files ok): 0 warnings

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
… once instead of searching it per register
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…ut registers once instead of searching it per register
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