Shaders: a relative move's program is built at its full range only if no bound is proven #2077

Open
0xkn0wh3r3 wants to merge 1 commit from upstream-pr/2079 into main
0xkn0wh3r3 commented 2026-11-25 04:25:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T04:25:35Z.


The problem

Translating a program with a relative move (v_movrel* or v_swaprel) cost nearly three times as much in range::build as the same program without one.

range::build built the program with every relative move at its full range, and its structure, before trying to prove bounds. It uses that full-range program only where no bound is proven (and to give up at once where its structure fails). The bounded rounds then build the program and its structure again, so the first build was mostly wasted work.

Synthetic 256-block program: 20.7 ms with a relative move against 7.2 ms without.

The fix

  • The moves are now found in the decoded instructions, not in the built program.
  • The program at full range is built only after the rounds prove nothing.
  • The code is decoded once. ir::build_decoded takes the decoded list, so a program without relative moves decodes and builds once as before, and each round saves a decode.
  • On the first round only the moves the program reaches are kept, as when they were taken from the built program.

One case changes: a program whose full-range structure fails but whose bounded form proves every bound now translates instead of being refused. The bounds hold by the same induction; only the fallback was refused.

Measured with synthetic wave32 compute programs, release build, three rounds each:

program before after
256 blocks, with a relative move 20.7-22.9 ms 14.8-15.7 ms
64 blocks, with a relative move 3.5-4.4 ms 2.3-2.8 ms
without one unchanged (same allocations, times within noise)

The DXIL is byte-identical for every program (FNV hash of the output).

Testing

  • cargo test -p victory-shader --lib -- --test-threads=1 on an RTX 5070 Ti (driver 621.24), including range.rs's bound tests: all pass except emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros and emit::synthetic_tests::gathers_at_a_level_match_the_hardware, which fail the same way on main on NVIDIA (the test host's views, #1835).
  • cargo clippy -p victory-shader --all-targets: clean.
  • Timings are from a synthetic benchmark outside the tree (hand-encoded programs, one variant with a v_movrels_b32 indexed by a bounded M0).
  • No game shaders here, so how many of the title's programs have relative moves is not known.
  • #1757 and #1802 edit other parts of range.rs (operand access, shift bounds); this touches only build.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5485156293 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T04:25:35Z. --- ## The problem Translating a program with a relative move (`v_movrel*` or `v_swaprel`) cost nearly three times as much in `range::build` as the same program without one. `range::build` built the program with every relative move at its full range, and its structure, before trying to prove bounds. It uses that full-range program only where no bound is proven (and to give up at once where its structure fails). The bounded rounds then build the program and its structure again, so the first build was mostly wasted work. Synthetic 256-block program: 20.7 ms with a relative move against 7.2 ms without. ## The fix - The moves are now found in the decoded instructions, not in the built program. - The program at full range is built only after the rounds prove nothing. - The code is decoded once. `ir::build_decoded` takes the decoded list, so a program without relative moves decodes and builds once as before, and each round saves a decode. - On the first round only the moves the program reaches are kept, as when they were taken from the built program. One case changes: a program whose full-range structure fails but whose bounded form proves every bound now translates instead of being refused. The bounds hold by the same induction; only the fallback was refused. Measured with synthetic wave32 compute programs, release build, three rounds each: | program | before | after | |---|---|---| | 256 blocks, with a relative move | 20.7-22.9 ms | 14.8-15.7 ms | | 64 blocks, with a relative move | 3.5-4.4 ms | 2.3-2.8 ms | | without one | | unchanged (same allocations, times within noise) | The DXIL is byte-identical for every program (FNV hash of the output). ## Testing - `cargo test -p victory-shader --lib -- --test-threads=1` on an RTX 5070 Ti (driver 621.24), including `range.rs`'s bound tests: all pass except `emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros` and `emit::synthetic_tests::gathers_at_a_level_match_the_hardware`, which fail the same way on `main` on NVIDIA (the test host's views, #1835). - `cargo clippy -p victory-shader --all-targets`: clean. - Timings are from a synthetic benchmark outside the tree (hand-encoded programs, one variant with a `v_movrels_b32` indexed by a bounded M0). ## Not covered / Related - No game shaders here, so how many of the title's programs have relative moves is not known. - #1757 and #1802 edit other parts of `range.rs` (operand access, shift bounds); this touches only `build`.
DenRakEiw commented 2026-11-25 04:31:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:31:04Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib range:: — 3 passed, 0 failed
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean refinement avoiding full-range rebuilds when a relative move's index bound is already proven. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051882298 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079#pullrequestreview-6051882298). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:31:04Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-shader --lib range::` — 3 passed, 0 failed - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean refinement avoiding full-range rebuilds when a relative move's index bound is already proven. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 04:34:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T04:34:45Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

144 passed / 0 failed. Relative move's program built at its full range only when needed — movrel program-size fix.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051907592 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079#pullrequestreview-6051907592). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T04:34:45Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 144 passed / 0 failed. Relative move's program built at its full range only when needed — movrel program-size fix.
sinan-cmyk commented 2026-11-25 10:07:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T09:07:40Z.


Offline measurement on a retail dump

Result: no count change against main.

I merged head 955cde3d onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7157911172 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079#issuecomment-7157911172). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T09:07:40Z. --- ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 955cde3d onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 11:05:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T10:30:33Z.


Automated triage for the maintainers.

Merge candidate (unchanged). The push since the last triage (955cde3 to 99d33d4) is a rebase onto current main. The diff to crates/victory-shader/src/range.rs and crates/victory-shader/src/ir.rs is identical, and it merges cleanly. range::build finds relative moves in the decoded list and decodes once (ir::build_decoded, which ir::build/build_bounded now delegate to with the same arguments). It builds the full-range program only when the bounded rounds prove nothing. The one behaviour change, translating a program whose bounds are all proven even though its full-range structure fails, follows from the induction in the function's doc comment. The retail census in the comments showed no change in validated counts.

CI: passing (check).

Recommending a look, not approving.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7159791316 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079#issuecomment-7159791316). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T10:30:33Z. --- <!-- victory-triage v1 head=99d33d41679076941778d17fc016f39530803625 verdict=merge-candidate at=2026-11-26T01:20:52Z --> Automated triage for the maintainers. **Merge candidate (unchanged).** The push since the last triage (`955cde3` to `99d33d4`) is a rebase onto current `main`. The diff to `crates/victory-shader/src/range.rs` and `crates/victory-shader/src/ir.rs` is identical, and it merges cleanly. `range::build` finds relative moves in the decoded list and decodes once (`ir::build_decoded`, which `ir::build`/`build_bounded` now delegate to with the same arguments). It builds the full-range program only when the bounded rounds prove nothing. The one behaviour change, translating a program whose bounds are all proven even though its full-range structure fails, follows from the induction in the function's doc comment. The retail census in the comments showed no change in validated counts. CI: passing (`check`). Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-26 19:50:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:50:47Z.

Review: APPROVED


Looks good to me — clean optimization avoiding unnecessary full-range IR/structure builds unless bound inference fails to prove a bound, while sharing a single gfx10::decode across rounds.

Verified locally on Windows 11 with head 99d33d41 cleanly merged onto main (63bcca43):

  • Audited crates/victory-shader/src/ir.rs:138-153 and crates/victory-shader/src/range.rs:41-99: range::build scans the decoded instruction list once for relative moves, restricts assumed to reached instructions on round 0, preserves exact endpoint bounds (vgprs.min(u16::from(reg) + u16::from(m) + 1)), and falls back to full-range ir::build_decoded(..., &HashMap::new()) at range.rs:97 only when bounds cannot be proven.
  • cargo run -p xtask -- policy (191 files ok) and cargo clippy --locked -p victory-shader --all-targets -- -D warnings (both default and --all-features) pass.
  • cargo test --locked -p victory-shader --lib ir::: 9 passed; 0 failed (0.12s).
  • cargo test --locked -p victory-shader --lib range::: 3 passed; 0 failed (0.00s).
  • rustfmt --edition 2024 --check passes on range.rs, and the newly added lines in ir.rs (144-152) are formatting-clean (only pre-existing lines 340 and 841 in ir.rs report diffs).
<!-- greyfinch-sync:Marvisak/VIctory:review:6079776930 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079#pullrequestreview-6079776930). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T19:50:47Z. **Review:** APPROVED --- Looks good to me — clean optimization avoiding unnecessary full-range IR/structure builds unless bound inference fails to prove a bound, while sharing a single `gfx10::decode` across rounds. Verified locally on Windows 11 with head `99d33d41` cleanly merged onto `main` (`63bcca43`): - Audited `crates/victory-shader/src/ir.rs:138-153` and `crates/victory-shader/src/range.rs:41-99`: `range::build` scans the decoded instruction list once for relative moves, restricts `assumed` to reached instructions on round `0`, preserves exact endpoint bounds (`vgprs.min(u16::from(reg) + u16::from(m) + 1)`), and falls back to full-range `ir::build_decoded(..., &HashMap::new())` at `range.rs:97` only when bounds cannot be proven. - `cargo run -p xtask -- policy` (`191 files ok`) and `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (both default and `--all-features`) pass. - `cargo test --locked -p victory-shader --lib ir::`: `9 passed; 0 failed` (`0.12s`). - `cargo test --locked -p victory-shader --lib range::`: `3 passed; 0 failed` (`0.00s`). - `rustfmt --edition 2024 --check` passes on `range.rs`, and the newly added lines in `ir.rs` (`144-152`) are formatting-clean (only pre-existing lines `340` and `841` in `ir.rs` report diffs).
andrewpozdnakov7-jpg commented 2026-11-26 22:14:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T22:14:01Z.


I repeated the #3357/#2079 comparison on Windows x64 (MSVC, locked release builds). I tested 99d33d4 merged onto 63bcca4 (tree a390ba8), #3357 at f389683, and 63bcca4 itself. Each build ran six fresh processes in balanced order. The inputs were the bounded M0 loop from #3357, repeated 1, 8 or 32 times with one final s_endpgm, paired with direct-move controls.

Relative-move motifs main, µs/build #3357 #2079
1 99.39 93.11 65.69
8 998.21 957.46 747.32
32 19,116.15 19,545.56 18,393.68

All three builds produced identical generated IR and the checked structural fields for all six inputs. Direct-move medians were close. #2079 is faster on the one- and eight-motif relative inputs, consistent with the M4 Pro report. The 32-motif sample ranges overlap, so the median ranking there is weaker. These are CPU microbenchmarks of generated shaders.

For the two pending approaches, the Windows result supports the bounded-first change on the smaller relative-move cases.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204486905 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079#issuecomment-7204486905). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T22:14:01Z. --- I repeated the #3357/#2079 comparison on Windows x64 (MSVC, locked release builds). I tested `99d33d4` merged onto `63bcca4` (tree `a390ba8`), #3357 at `f389683`, and `63bcca4` itself. Each build ran six fresh processes in balanced order. The inputs were the [bounded M0 loop from #3357](https://github.com/Marvisak/VIctory/blob/f389683feca50ed93c1436a4542378db6635fdf1/crates/victory-shader/src/range.rs), repeated 1, 8 or 32 times with one final `s_endpgm`, paired with direct-move controls. | Relative-move motifs | main, µs/build | #3357 | #2079 | |---:|---:|---:|---:| | 1 | 99.39 | 93.11 | 65.69 | | 8 | 998.21 | 957.46 | 747.32 | | 32 | 19,116.15 | 19,545.56 | 18,393.68 | All three builds produced identical generated IR and the checked structural fields for all six inputs. Direct-move medians were close. #2079 is faster on the one- and eight-motif relative inputs, consistent with [the M4 Pro report](https://github.com/Marvisak/VIctory/pull/3357#issuecomment-7203978461). The 32-motif sample ranges overlap, so the median ranking there is weaker. These are CPU microbenchmarks of generated shaders. For the two pending approaches, the Windows result supports the bounded-first change on the smaller relative-move cases.
andrewpozdnakov7-jpg commented 2026-11-28 00:01:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T00:01:32Z.


I checked this range-analysis change at 99d33d4 with the relative-source emitter change from #3630 on main 63bcca4. The tested combination keeps both exact PR commits.

With app-local WARP and required DXIL validation, the combined branch passed all 160 generated dispatches. Its eight generated DXIL containers were byte-identical to #3630 alone, including the 56-register and relative source/destination cases. The nine-register regression, two existing relative-move tests, and all three range-analysis tests also passed. Strict all-features shader Clippy and the asset policy check passed.

These results cover compatibility between the two changes. The earlier range-build benchmark remains the separate timing measurement for this PR.

If you reuse the emitter change, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg [email protected] if folding it into another commit.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7230586162 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2079#issuecomment-7230586162). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T00:01:32Z. --- I checked this range-analysis change at 99d33d4 with the relative-source emitter change from #3630 on main 63bcca4. The [tested combination](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/402203b8ec7a6e41e4c2676b88812b716b481b2b) keeps both exact PR commits. With app-local WARP and required DXIL validation, the combined branch passed all 160 generated dispatches. Its eight generated DXIL containers were byte-identical to #3630 alone, including the 56-register and relative source/destination cases. The nine-register regression, two existing relative-move tests, and all three range-analysis tests also passed. Strict all-features shader Clippy and the asset policy check passed. These results cover compatibility between the two changes. The [earlier range-build benchmark](https://github.com/Marvisak/VIctory/pull/2079#issuecomment-7204486905) remains the separate timing measurement for this PR. If you reuse the emitter change, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding it into another commit.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2079:upstream-pr/2079'
git switch 'upstream-pr/2079'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2079'
git switch 'upstream-pr/2079'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2079'
git switch 'upstream-pr/2079'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2079'
git switch 'main'
git merge --squash 'upstream-pr/2079'
git switch 'main'
git merge --ff-only 'upstream-pr/2079'
git switch 'main'
git merge 'upstream-pr/2079'
git push origin 'main'
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!2077
No description provided.