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

Closed
liandraraiane wants to merge 1 commit from upstream-pr/2520 into main
liandraraiane commented 2026-11-25 15:13:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by liandraraiane on 2026-11-25T15:13:44Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5494043075 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2520). Originally posted by [liandraraiane](https://github.com/liandraraiane) on 2026-11-25T15:13:44Z. --- 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.
oni-swr commented 2026-11-25 16:09:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T15:27:43Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166878631 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2520#issuecomment-7166878631). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T15:27:43Z. --- **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).
Marvisak commented 2026-11-25 19:06:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T18:32:12Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171041531 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2520#issuecomment-7171041531). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T18:32:12Z. --- <!-- victory-triage v1 head=be6ae54e88af9a4dea8f94889469a803f0ca8a88 verdict=merge-candidate at=2026-11-25T18:32:01Z --> 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](https://claude.ai/code)_
rezioliver-collab commented 2026-11-25 22:04:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-25T21:03:38Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7174555560 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2520#issuecomment-7174555560). Originally posted by [rezioliver-collab](https://github.com/rezioliver-collab) on 2026-11-25T21:03:38Z. --- 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.
fuzeofficlal commented 2026-11-26 02:05:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T02:05:05Z.

Review: COMMENTED


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
<!-- greyfinch-sync:Marvisak/VIctory:review:6067588931 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2520#pullrequestreview-6067588931). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T02:05:05Z. **Review:** COMMENTED --- 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
Dukestar69 commented 2026-11-27 00:50:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-27T00:50:47Z.

Review: APPROVED


LGTM — verified on Windows:

  • Linear merge walk (lookup_from) over sorted outs[p] and register-ordered members / block.phis is exact and avoids redundant binary searches and allocations
  • cargo test -p victory-shader --lib ir:: passed (9/9)
  • cargo clippy -p victory-shader --all-targets -- -D warnings (including benches/ir_build.rs) passed with 0 warnings
  • cargo xtask policy passed (192 files ok)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6082075226 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2520#pullrequestreview-6082075226). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-27T00:50:47Z. **Review:** APPROVED --- LGTM — verified on Windows: - Linear merge walk (`lookup_from`) over sorted `outs[p]` and register-ordered `members` / `block.phis` is exact and avoids redundant binary searches and allocations - `cargo test -p victory-shader --lib ir::` passed (9/9) - `cargo clippy -p victory-shader --all-targets -- -D warnings` (including `benches/ir_build.rs`) passed with 0 warnings - `cargo xtask policy` passed (`192 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!2518
No description provided.