Skip to content

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

Open
adriaespii wants to merge 1 commit into
fxceless:mainfrom
adriaespii:fix/lane-uniform-lds-read
Open

adriaespii wants to merge 1 commit into
fxceless:mainfrom
adriaespii:fix/lane-uniform-lds-read

Conversation

@adriaespii

Copy link
Copy Markdown

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.

Comment thread crates/victory-shader/src/emit/lower.rs Outdated
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@rostaddotcc

Copy link
Copy Markdown

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.

…form

Fixes two gaps in lane_uniform that prevented wave-uniform VGPRs (such as the BVH builder's prefix-sum loop in fxceless#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 fxceless#16.
Assisted-by: Antigravity (Gemini 4 Neon)
@adriaespii
adriaespii force-pushed the fix/lane-uniform-lds-read branch from d964ec7 to 87d674f Compare November 24, 2026 21:24
@adriaespii

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Author

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

  1. Scope relative to Ray-tracing builder runs on an RTX 5070 (32-lane) - then the title crashes on f16 box bounds #16: This PR only covers part 1 of Ray-tracing builder runs on an RTX 5070 (32-lane) - then the title crashes on f16 box bounds #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 Shaders: lower v_cvt_f32_f64 and model v_add_f64 whole-wave sums #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.

@CDR-0000

Copy link
Copy Markdown

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.

@arminkhaleghirod

Copy link
Copy Markdown

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.

@Revens2

Revens2 commented Nov 25, 2026

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants