Shaders: track uniform LDS reads and exact operand widths in lane_uniform (#16) #36

Open
github-bot wants to merge 1 commit from github-bot/gh-36 into main AGit
Owner

@⁠adriaespii on GitHub · 2026-11-24 20:44 UTC · original
adriaespii:fix/lane-uniform-lds-read → main · +207 −19 · 1 files · 1 commits · open

Addresses the v_readfirstlane_b32 wave-uniformity gap reported in #16 (part 1) without needing an opt-in override flag.

What changed

In crates/victory-shader/src/emit/lower.rs (lane_uniform):

  • Track unguarded LDS reads (ds_read*, excluding _d16): when ds_read* (gds: false, no guard) reads from a VGPR address already in same, mark its destination register(s) as wave-uniform. _d16 forms are excluded because they preserve the other half of the destination VGPR.
  • Fix Operand::Vector read range: filter inst.reads by reg..reg + dwords instead of x >= reg. Because ir.rs includes the previous value of the destination VGPR in inst.reads (for EXEC guards), x >= reg swept up the old uninitialized/per-lane destination register whenever dst > src (e.g. v_add_nc_u32 v8, s17, v2) and poisoned uniformity. Also check all dwords for Operand::Scalar.
  • Unit test (first_lane_read_of_uniform_lds_load_lowers_on_wave32): verifies under TEST_HOST_WAVE = 32 that a divergent ds_read_b32 from a uniform address + v_add_nc_u32 + v_readfirstlane_b32 lowers cleanly, while the same shader with a per-lane address (v0) is still refused with a first-lane read where not every lane runs.

Tested

  • Hardware: AMD Radeon RX 9070 (driver 32.0.32015.7029), AMD Ryzen 5 7600, Windows 11 Pro, Rust 1.98.1 (x86_64-pc-windows-msvc)
  • cargo clippy --workspace --all-targets -- -D warnings: 0 warnings
  • cargo test --workspace: 496 passed, 0 failed, 7 ignored (victory-shader: 211 passed, 0 failed, 2 ignored)

Not tested

  • Retail XboxBvhBuilder.dll shaders or live game runs (no game files on this machine)

AI use

  • Antigravity (Gemini 4 Neon) diagnosed the two lane_uniform bugs, wrote the fix and unit test, and ran clippy and the workspace test suite.
**@⁠adriaespii** on GitHub · 2026-11-24 20:44 UTC · [original](https://github.com/fxceless/victory-fork/pull/36) `adriaespii:fix/lane-uniform-lds-read` → `main` · +207 −19 · 1 files · 1 commits · open Addresses the `v_readfirstlane_b32` wave-uniformity gap reported in #16 (part 1) without needing an opt-in override flag. ### What changed In `crates/victory-shader/src/emit/lower.rs` (`lane_uniform`): - **Track unguarded LDS reads (`ds_read*`, excluding `_d16`):** when `ds_read*` (`gds: false`, no guard) reads from a VGPR address already in `same`, mark its destination register(s) as wave-uniform. `_d16` forms are excluded because they preserve the other half of the destination VGPR. - **Fix `Operand::Vector` read range:** filter `inst.reads` by `reg..reg + dwords` instead of `x >= reg`. Because `ir.rs` includes the previous value of the destination VGPR in `inst.reads` (for EXEC guards), `x >= reg` swept up the old uninitialized/per-lane destination register whenever `dst > src` (e.g. `v_add_nc_u32 v8, s17, v2`) and poisoned uniformity. Also check all `dwords` for `Operand::Scalar`. - **Unit test (`first_lane_read_of_uniform_lds_load_lowers_on_wave32`):** verifies under `TEST_HOST_WAVE = 32` that a divergent `ds_read_b32` from a uniform address + `v_add_nc_u32` + `v_readfirstlane_b32` lowers cleanly, while the same shader with a per-lane address (`v0`) is still refused with `a first-lane read where not every lane runs`. ### Tested - Hardware: AMD Radeon RX 9070 (driver 32.0.32015.7029), AMD Ryzen 5 7600, Windows 11 Pro, Rust 1.98.1 (`x86_64-pc-windows-msvc`) - `cargo clippy --workspace --all-targets -- -D warnings`: 0 warnings - `cargo test --workspace`: 496 passed, 0 failed, 7 ignored (`victory-shader`: 211 passed, 0 failed, 2 ignored) ### Not tested - Retail `XboxBvhBuilder.dll` shaders or live game runs (no game files on this machine) ### AI use - Antigravity (Gemini 4 Neon) diagnosed the two `lane_uniform` bugs, wrote the fix and unit test, and ran `clippy` and the workspace test suite.
Fixes two gaps in lane_uniform that prevented wave-uniform VGPRs (such as the BVH builder's prefix-sum loop in #16) from lowering v_readfirstlane_b32 on 32-lane hosts without a cross-half exchange:

- Handle unguarded non-d16 ds_read* instructions when their address VGPR is already wave-uniform.
- Bound Operand::Vector reads to reg..reg + dwords in u16 (so operands ending at v255 are not truncated by u8 saturation, and the old value of a higher-numbered destination register kept for EXEC guards no longer poisons uniformity); also check all dwords of Operand::Scalar in u16.

Part of #16.
Assisted-by: Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:34:58 +00:00
Author
Owner

@⁠rostaddotcc on GitHub · 2026-11-24 21:20 UTC · original

Verified at d964ec7 on a Linux host (CachyOS, x86-64, no GPU needed for these):

  • cargo test -p victory-shader — 171 passed, 0 failed, 2 ignored (173 listed).
  • cargo test -p victory-pipeline — 4 passed.
  • cargo xtask policy — 169 files ok.
  • cargo xwin build --release -p victory --target x86_64-pc-windows-msvc — clean, 16.8 s.

The change adds one test for 113 lines in lane_uniform. Worth saying in the description whether that test covers #16's own shapes — the f16 box bounds and the v_cvt_f32_f64 / whole-wave v_add_f64 sums — or only the uniform-read machinery. #16's reporter is on a 32-lane host, so nobody with a wave64 machine can confirm the fix by running the game; the unit test is the only evidence there is, and it should name the case it settles.

One thing I can check here that a wave64 host cannot: whether the wave64 path is unchanged for programs that were already correct. If the diff can perturb uniform tracking on a 64-lane host, the 26 698-program corpus is the place to see it — I have that translate run on disk and can diff the emitted containers before/after this patch on this machine (~10 min, no game). Say the word and I will post the count.

**@⁠rostaddotcc** on GitHub · 2026-11-24 21:20 UTC · [original](https://github.com/fxceless/victory-fork/pull/36#issuecomment-7144799829) Verified at `d964ec7` on a Linux host (CachyOS, x86-64, no GPU needed for these): - `cargo test -p victory-shader` — 171 passed, 0 failed, 2 ignored (173 listed). - `cargo test -p victory-pipeline` — 4 passed. - `cargo xtask policy` — 169 files ok. - `cargo xwin build --release -p victory --target x86_64-pc-windows-msvc` — clean, 16.8 s. The change adds one test for 113 lines in `lane_uniform`. Worth saying in the description whether that test covers #16's own shapes — the `f16` box bounds and the `v_cvt_f32_f64` / whole-wave `v_add_f64` sums — or only the uniform-read machinery. #16's reporter is on a 32-lane host, so nobody with a wave64 machine can confirm the fix by running the game; the unit test is the only evidence there is, and it should name the case it settles. One thing I can check here that a wave64 host cannot: whether the *wave64* path is unchanged for programs that were already correct. If the diff can perturb uniform tracking on a 64-lane host, the 26 698-program corpus is the place to see it — I have that translate run on disk and can diff the emitted containers before/after this patch on this machine (~10 min, no game). Say the word and I will post the count.
Author
Owner

@⁠adriaespii on GitHub · 2026-11-24 21:24 UTC · original

Thanks for the great catch and for verifying the 52/52 builder translation + DXIL validation @⁠ryotdev!

Fixed in 87d674f (rebased onto main 4e17478):

  • Widened the start..end register range to u16 for both Operand::Vector and Operand::Scalar so operands touching v255 (v255 single-word and v[254:255] pairs) are not truncated by u8 saturation.
  • Added regression test lane_uniform_handles_v255_and_high_pair_operands covering both the uniform v255 case and the varying v[254:255] high-word shift case.
  • Re-verified on AMD Radeon RX 9070 (Windows 11, Rust 1.98.1): cargo clippy --workspace --all-targets -- -D warnings (0 warnings) and cargo test --workspace (498 passed, 0 failed, 7 ignored; victory-shader: 212 passed, 0 failed, 2 ignored). Ready for another look!

AI use: Antigravity (Gemini 4 Neon) applied the fix, wrote the regression test, and ran clippy/tests.

**@⁠adriaespii** on GitHub · 2026-11-24 21:24 UTC · [original](https://github.com/fxceless/victory-fork/pull/36#issuecomment-7144885502) Thanks for the great catch and for verifying the 52/52 builder translation + DXIL validation @⁠ryotdev! Fixed in `87d674f` (rebased onto `main` `4e17478`): - Widened the `start..end` register range to `u16` for both `Operand::Vector` and `Operand::Scalar` so operands touching `v255` (`v255` single-word and `v[254:255]` pairs) are not truncated by `u8` saturation. - Added regression test `lane_uniform_handles_v255_and_high_pair_operands` covering both the uniform `v255` case and the varying `v[254:255]` high-word shift case. - Re-verified on AMD Radeon RX 9070 (Windows 11, Rust 1.98.1): `cargo clippy --workspace --all-targets -- -D warnings` (0 warnings) and `cargo test --workspace` (498 passed, 0 failed, 7 ignored; `victory-shader`: 212 passed, 0 failed, 2 ignored). Ready for another look! *AI use: Antigravity (Gemini 4 Neon) applied the fix, wrote the regression test, and ran clippy/tests.*
Author
Owner

@⁠adriaespii on GitHub · 2026-11-24 21:26 UTC · original

Thanks for checking on Linux @⁠rostaddotcc! To clarify both points:

  1. Scope relative to #16: This PR only covers part 1 of #16 (the v_readfirstlane_b32 wave-uniformity refusal on 32-lane hosts, which @ryotdev confirmed takes XboxBvhBuilder.dll from 49/52 to 52/52 emitted and DXIL-validated). It does not touch v_cvt_f32_f64 / v_add_f64 (covered by #9) or the f16 box-bounds crash.
  2. Wave64 host path: On a 64-lane host (host_max_wave() >= 64), exchange is None, so lane_uniform is never called (uniform stays empty). The emitted wave64 containers are unchanged by construction.

AI use: Antigravity (Gemini 4 Neon) drafted this reply.

**@⁠adriaespii** on GitHub · 2026-11-24 21:26 UTC · [original](https://github.com/fxceless/victory-fork/pull/36#issuecomment-7144940188) Thanks for checking on Linux @⁠rostaddotcc! To clarify both points: 1. **Scope relative to #16:** This PR only covers **part 1 of #16** (the `v_readfirstlane_b32` wave-uniformity refusal on 32-lane hosts, which `@ryotdev` confirmed takes `XboxBvhBuilder.dll` from 49/52 to 52/52 emitted and DXIL-validated). It does **not** touch `v_cvt_f32_f64` / `v_add_f64` (covered by #9) or the `f16` box-bounds crash. 2. **Wave64 host path:** On a 64-lane host (`host_max_wave() >= 64`), `exchange` is `None`, so `lane_uniform` is never called (`uniform` stays empty). The emitted wave64 containers are unchanged by construction. *AI use: Antigravity (Gemini 4 Neon) drafted this reply.*
Author
Owner

@⁠CDR-0000 on GitHub · 2026-11-24 21:40 UTC · original

NVIDIA test of this PR at 87d674f: RTX 3080 Ti (32-lane waves) — 52/52 builder programs with host pipelines on the card, no change elsewhere in the retail pack, and the builder starts in game.

Setup: RTX 3080 Ti 12 GB, driver 32.0.16.1742; Ryzen 7 7800X3D; 32 GB; Windows 11 Pro 26300; Rust 1.98.1 MSVC, release. Tree: fork main 4e17478 + upstream #454 + this PR, merged cleanly.

without this PR with this PR (87d674f)
XboxBvhBuilder.dll, VICTORY_HOST_MAX_WAVE=32 victory translate --validate --pipelines 49 of 52 emitted (3 × "a first-lane read where not every lane runs", @0x99160 …) 52 of 52 emitted, 52 validated, 52 host pipelines created on the card
whole retail pack (26,148 programs), same wave size, --validate compute 2718 of 2734 compute 2721 of 2734 (the +3 above); VS/PS 14168/14057 and mesh 8762/8657 identical
cargo test -p victory-shader on the card — 212 passed, 0 failed, 2 ignored (incl. first_lane_read_of_uniform_lds_load_lowers_on_wave32, lane_uniform_handles_v255_and_high_pair_operands)
in game, VICTORY_BVH_BUILDER=1 (3 runs, at d964ec7) "builder stays off" "the ray-tracing structure builder runs: all of its programs have host pipelines pipelines=53"

The title then crashes 7–14 s later in the prologue — the #16 crash, which also happens on a 64-lane RX 9070 XT without this PR, so it is outside this PR's scope; details and a write-back trace in #16.

Written with Claude Code (Claude Opus 5.5); the runs are mine.

**@⁠CDR-0000** on GitHub · 2026-11-24 21:40 UTC · [original](https://github.com/fxceless/victory-fork/pull/36#issuecomment-7145238392) **NVIDIA test of this PR at `87d674f`: RTX 3080 Ti (32-lane waves) — 52/52 builder programs with host pipelines on the card, no change elsewhere in the retail pack, and the builder starts in game.** **Setup:** RTX 3080 Ti 12 GB, driver 32.0.16.1742; Ryzen 7 7800X3D; 32 GB; Windows 11 Pro 26300; Rust 1.98.1 MSVC, release. Tree: fork `main` `4e17478` + upstream #454 + this PR, merged cleanly. | | without this PR | with this PR (`87d674f`) | |---|---|---| | `XboxBvhBuilder.dll`, `VICTORY_HOST_MAX_WAVE=32 victory translate --validate --pipelines` | 49 of 52 emitted (3 × "a first-lane read where not every lane runs", `@0x99160` …) | **52 of 52 emitted, 52 validated, 52 host pipelines created on the card** | | whole retail pack (26,148 programs), same wave size, `--validate` | compute 2718 of 2734 | compute 2721 of 2734 (the +3 above); VS/PS 14168/14057 and mesh 8762/8657 identical | | `cargo test -p victory-shader` on the card | — | 212 passed, 0 failed, 2 ignored (incl. `first_lane_read_of_uniform_lds_load_lowers_on_wave32`, `lane_uniform_handles_v255_and_high_pair_operands`) | | in game, `VICTORY_BVH_BUILDER=1` (3 runs, at `d964ec7`) | "builder stays off" | "the ray-tracing structure builder runs: all of its programs have host pipelines pipelines=53" | The title then crashes 7–14 s later in the prologue — the #16 crash, which also happens on a 64-lane RX 9070 XT without this PR, so it is outside this PR's scope; details and a write-back trace in #16. Written with Claude Code (Claude Opus 5.5); the runs are mine.
Author
Owner

@⁠arminkhaleghirod on GitHub · 2026-11-24 21:42 UTC · original

Merged onto main 90228a7 and ran victory translate --validate over the full title (26,148 programs) on Windows 11 / RTX 4080. Changes against main, by failure reason:

No change: every failure count is the same as main, and no new reason appears. So no regression in translation. What this PR changes at run time isn't covered by this check.

**@⁠arminkhaleghirod** on GitHub · 2026-11-24 21:42 UTC · [original](https://github.com/fxceless/victory-fork/pull/36#issuecomment-7145286756) Merged onto main 90228a7 and ran `victory translate --validate` over the full title (26,148 programs) on Windows 11 / RTX 4080. Changes against main, by failure reason: No change: every failure count is the same as main, and no new reason appears. So no regression in translation. What this PR changes at run time isn't covered by this check.
Author
Owner

@⁠Revens2 on GitHub · 2026-11-25 05:13 UTC · original

Build check only: this PR merges cleanly on main c96b4f2 and the integration build of eight PRs (#115 #66 #36 #144 #147 #141 #139 #130) compiles (diagnostics profile). In the game, that combo was run alone 3 times (900 s, VICTORY_RELAY=2, scripted input): one run to the end (median 4.95 fps, about 4.9k draws per frame, against 4.84-5.04 fps on main), one title breakpoint 0x80000003 at about 157 s after a failed dstorage request (the pattern is explained on #115), one device removed (0x887A0005) at about 160 s that did not repeat in the two other runs. Nothing is proven for this PR alone: it was not run on its own, cargo test was not run on the combo, and nothing was checked on the title side of its change. Not verified.

**@⁠Revens2** on GitHub · 2026-11-25 05:13 UTC · [original](https://github.com/fxceless/victory-fork/pull/36#issuecomment-7153564477) Build check only: this PR merges cleanly on main c96b4f2 and the integration build of eight PRs (#115 #66 #36 #144 #147 #141 #139 #130) compiles (diagnostics profile). In the game, that combo was run alone 3 times (900 s, `VICTORY_RELAY=2`, scripted input): one run to the end (median 4.95 fps, about 4.9k draws per frame, against 4.84-5.04 fps on main), one title breakpoint `0x80000003` at about 157 s after a failed dstorage request (the pattern is explained on #115), one device removed (`0x887A0005`) at about 160 s that did not repeat in the two other runs. Nothing is proven for this PR alone: it was not run on its own, `cargo test` was not run on the combo, and nothing was checked on the title side of its change. Not verified.
Author
Owner

@⁠ryotdev on GitHub · 2026-11-24 21:02 UTC · original
On crates/victory-shader/src/emit/lower.rs line 483 (outdated):

@@ -465,34 +465,51 @@ fn lane_uniform(p: &Program, st: &Structured, all: &[bool]) -> HashSet<Value> {
         }
         for (i, inst) in block.insts.iter().enumerate() {
             let name = inst.name();
-            let ops: Vec<Operand> = match &inst.fields {
+            if st.guards[b][i].is_some() {
+                continue; // lanes outside EXEC keep another value
+            }
+            let alike = match &inst.fields {
                 Fields::Valu(v)
                     if v.dpp.is_none()
                         && v.sdwa.is_none()
                         && !name.starts_with("v_mbcnt")
                         && !name.starts_with("v_cmpx")
                         && !name.contains("lane") =>
                 {
-                    v.src.clone()
-                }
-                _ => continue,
-            };
-            if st.guards[b][i].is_some() {
-                continue; // lanes outside EXEC keep another value
-            }
-            let alike = ops.iter().all(|op| match *op {
-                Operand::Vector { reg, .. } => {
-                    // every dword of the operand the instruction reads from that register on
-                    let read: Vec<Value> =
-                        inst.reads.iter().filter(|r| matches!(r.0, Reg::V(x) if x >= reg)).map(|r| r.1).collect();
-                    !read.is_empty() && read.iter().all(|x| same.contains(x))
+                    v.src.iter().all(|op| match *op {
+                        Operand::Vector { reg, dwords } => {
+                            // every dword of the operand the instruction reads (not the old value
+                            // of a higher-numbered destination register that IR keeps for guards)
+                            let end = reg.saturating_add(dwords);

For a legal source pair v[254:255], the exclusive end is 256. The u8 addition saturates to 255 and drops the high word. A shift that copies a per-lane v255 into its low result then gets classified as wave-uniform. A single-word read of a uniform v255 also fails the proof in the other direction.

I put a cherry-pickable fix and a hand-written regression test directly on top of your PR. It checks each decoded source dword, including the last register. The varying-high-word case fails on this head. With the fix, both the varying and constant cases pass, and the shader library suite passes 172 tests with 2 ignored. Fix and regression test

I also ran the builder through the offline translator with VICTORY_HOST_WAVE=32 and DXIL validation. Main at 4e17478 emitted and validated 49/52 compute programs. This PR reached 52/52, and all 52 outputs remained byte-identical after the fix. This measures shader generation and validation. GPU execution, lighting and frame times remain untested here.

AI-assisted review and test.

**@⁠ryotdev** on GitHub · 2026-11-24 21:02 UTC · [original](https://github.com/fxceless/victory-fork/pull/36#discussion_r4686506424) On `crates/victory-shader/src/emit/lower.rs` line 483 (outdated): ```diff @@ -465,34 +465,51 @@ fn lane_uniform(p: &Program, st: &Structured, all: &[bool]) -> HashSet<Value> { } for (i, inst) in block.insts.iter().enumerate() { let name = inst.name(); - let ops: Vec<Operand> = match &inst.fields { + if st.guards[b][i].is_some() { + continue; // lanes outside EXEC keep another value + } + let alike = match &inst.fields { Fields::Valu(v) if v.dpp.is_none() && v.sdwa.is_none() && !name.starts_with("v_mbcnt") && !name.starts_with("v_cmpx") && !name.contains("lane") => { - v.src.clone() - } - _ => continue, - }; - if st.guards[b][i].is_some() { - continue; // lanes outside EXEC keep another value - } - let alike = ops.iter().all(|op| match *op { - Operand::Vector { reg, .. } => { - // every dword of the operand the instruction reads from that register on - let read: Vec<Value> = - inst.reads.iter().filter(|r| matches!(r.0, Reg::V(x) if x >= reg)).map(|r| r.1).collect(); - !read.is_empty() && read.iter().all(|x| same.contains(x)) + v.src.iter().all(|op| match *op { + Operand::Vector { reg, dwords } => { + // every dword of the operand the instruction reads (not the old value + // of a higher-numbered destination register that IR keeps for guards) + let end = reg.saturating_add(dwords); ``` For a legal source pair `v[254:255]`, the exclusive end is 256. The `u8` addition saturates to 255 and drops the high word. A shift that copies a per-lane `v255` into its low result then gets classified as wave-uniform. A single-word read of a uniform `v255` also fails the proof in the other direction. I put a cherry-pickable fix and a hand-written regression test directly on top of your PR. It checks each decoded source dword, including the last register. The varying-high-word case fails on this head. With the fix, both the varying and constant cases pass, and the shader library suite passes 172 tests with 2 ignored. [Fix and regression test](https://github.com/ryotdev/victory-gta6/commit/8d354928dc4d4718c672c39bd64d4ae7de3a73c2) I also ran the builder through the offline translator with `VICTORY_HOST_WAVE=32` and DXIL validation. Main at `4e17478` emitted and validated 49/52 compute programs. This PR reached 52/52, and all 52 outputs remained byte-identical after the fix. This measures shader generation and validation. GPU execution, lighting and frame times remain untested here. AI-assisted review and test.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/36/head:github-bot/gh-36'
git switch 'github-bot/gh-36'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-36'
git switch 'github-bot/gh-36'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-36'
git switch 'github-bot/gh-36'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-36'
git switch 'main'
git merge --squash 'github-bot/gh-36'
git switch 'main'
git merge --ff-only 'github-bot/gh-36'
git switch 'main'
git merge 'github-bot/gh-36'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.