Skip to content

Shaders: include v_readfirstlane_b32 in wave64 exchange and check implicit reads in lane_uniform - #354

Open
AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/shader-wave64-readfirstlane-and-uniform
Open

AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/shader-wave64-readfirstlane-and-uniform

Conversation

@AscilCH

@AscilCH AscilCH commented Nov 24, 2026

Copy link
Copy Markdown

Problem

Two wave64-on-wave32 soundness bugs in crates/victory-shader/src/emit/lower.rs:

  1. Missing exchange for v_readfirstlane_b32 (Lower::new): console_lanes, crosses, and VICTORY_WAVE_WHY checked v_readlane_b32 and v_writelane_b32 but omitted v_readfirstlane_b32. On 32-lane hosts, a console wave64 compute shader using v_readfirstlane_b32 without another full-wave crossing op did not set up exchange LDS, so lanes 32..63 executed WaveReadLaneFirst on their own 32-lane half instead of across all 64 lanes.
  2. Implicit reads ignored in lane_uniform: lane_uniform only checked v.src and ignored implicit reads in inst.reads (e.g., accumulator VGPRs in v_fmac_*/v_mac_*/v_dot*c_*, preserved 16-bit destination halves, and implicit VCC in VOP2 v_cndmask_b32/v_add_co_ci_u32/v_div_fmas_f32) as well as movrel*. Whenever explicit sources were uniform, the per-lane result was falsely added to self.uniform, causing v_readfirstlane_b32 to skip exchange_halves.

Fix

  • Add non-waterfall v_readfirstlane_b32 to console_lanes, crosses, and VICTORY_WAVE_WHY (leaving waterfall descriptor reads unaffected since those lower per-lane).
  • Check every (reg, val) in inst.reads inside lane_uniform (and exclude movrel*) so implicit VGPR and VCC dependencies are properly tracked.
  • Add tests wave64_readfirstlane_exchanges_across_halves_on_32_lane_host and wave64_implicit_reads_are_not_marked_lane_uniform in emit::wave64_tests (asserting wave_size == Some(32) and half_exchanges > 0 for standalone v_readfirstlane_b32, v_fmac_f32, and VOP2 v_cndmask_b32, half_exchanges == 0 for truly uniform v_mov_b32, validated by dxil.dll, and executed on 32-lane hardware when supported).

Verification

  • cargo test -p victory-shader --locked wave64: 9 passed, 0 failed (dxil.dll validation + wave_size / half_exchanges assertions; note Intel UHD adapter 0 skips [WaveSize(32)] compute execution as with the existing wave64_tests)
  • cargo test -p victory-shader --locked waterfall: 21 passed, 0 failed (verified waterfall wave64 unaffected)
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic tests only, no game data.)

…licit reads in lane_uniform

Fix two wave64-on-wave32 soundness bugs in `crates/victory-shader/src/emit/lower.rs`:

1. `v_readfirstlane_b32` (when not a per-lane waterfall descriptor read)
   was omitted from `console_lanes`, `crosses`, and `VICTORY_WAVE_WHY` in
   `Lower::new`. A console wave64 compute shader whose cross-lane reads
   only used `v_readfirstlane_b32` (or paired it with half-local ops such
   as `ds_swizzle`/DPP) did not allocate half-wave `exchange` LDS, causing
   lanes 32..63 on 32-lane hosts to read lane 32's half instead of the
   64-lane wave's first active lane.
2. `lane_uniform` only inspected explicit `v.src` operands and ignored
   implicit register reads in `inst.reads` (such as the accumulator VGPR
   of `v_fmac_*`/`v_mac_*`/`v_dot*c_*`, preserved destination halves on
   16-bit writes, and implicit `VCC` on VOP2 `v_cndmask_b32`/`v_add_co_ci_u32`/
   `v_div_fmas_f32`), and did not exclude `movrel*`. A per-lane accumulator
   or `VCC`-selected VGPR whose explicit sources were uniform was falsely
   marked uniform, causing `v_readfirstlane_b32` to skip half-wave exchange.
   Inspect all `inst.reads` entries directly and exclude `movrel*`.

Add `wave64_readfirstlane_exchanges_across_halves_on_32_lane_host` and
`wave64_implicit_reads_are_not_marked_lane_uniform` in `emit::wave64_tests`.
@m335671

m335671 commented Nov 25, 2026

Copy link
Copy Markdown

Offline check on a 32-lane card. On current main, this PR makes the driver refuse 10 compute pipelines that it creates without it, and it adds none.

Tested

  • RTX 4070 12 GB (32-lane waves), driver 32.0.16.2124, Windows 11 Pro 26300, release build.
  • main 3973179, then main with this PR's head 6273077 merged in. The merge was clean.
  • VICTORY_HOST_MAX_WAVE=32 victory translate --list --pipelines over the title's whole shader pack, compared program by program by hash.
pipelines created
main 3973179 25,608
main + this PR 25,598

Each of the 10 goes from pipeline created to pipeline: The parameter is incorrect.:

  • meshlet_culling:CS_BatchCullPreSkinnedMeshlets, with its _Prev, _ParaboloidShadows and _ParaboloidShadows_Prev variants (0452fc2d2e2fc4c3 and 3 others)
  • subsurfacescatter:CS_SSSPrefilterFull / Half, each with its _MaterialTiles variant (41f021c4e168e70b, 7e96471a390eb947 and 2 others)
  • vfx_proj_decal_hit_detection:CS_ComputeHitDetectionResults and CS_ComputeHitDetectionSkinnedResults

None of the programs that main refuses comes up with it.

My guess, which I haven't checked: these programs already pass through v_readfirstlane_b32 and now take the half exchange as well, and the form the driver gets is no longer one it accepts. That would fit "the fix is right, but it needs a fallback when the exchange form can't be created". The pre-skinned meshlet culling is the one that matters most: if its dispatches are dropped, skinned meshes may not be culled or drawn.

Not tested: in the title (offline the result is already worse on this card), other GPUs, and the PR on its own base (2119b0e).

AI use: Claude Code (Claude Opus 5.5) built and ran the check on my machine, compared the listings and wrote this comment.

@HAL9000RELOADED

Copy link
Copy Markdown

RX 7900 XTX (RDNA3), Windows 10 + #402 for SM 6.6, main d510ee4: both new tests pass on the card with this PR. No other test changed. Run table: #129 (#129 (comment)).

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…rrier fence, W14-07 CAS stores, W14-08 wave64 readlane

Implements Wave 14 Lane 14C shader stability fixes:
- W14-01 (--store-page-guard, adapted from ethan00013/keylime@650550ffa, GPL-3.0): emit lazy %store_addr_valid(heap_base, va) checking the 64 KiB page-validity bitmap at heap_base+0x170000 and GPBM control header at heap_base+0x180000 (runtime/src/gpu_page_bitmap.h) on PSB stores/atomics; default off, zero bloat on read-only shaders.
- W14-05 (adapted from ethan00013/keylime@0afb8154c5, GPL-3.0): per-function tripped flag in add_loop_guards so hitting a nested inner loop cap breaks enclosing loops instead of multiplying limit*limit.
- W14-06 (adapted from Marvisak/VIctory#1799 & five-star-recomp@320612a4d7, GPL-3.0): downgrade s_barrier inside switch/dispatch form, divergent guards, or non-top-level mesh blocks to OpMemoryBarrier while preserving uniform top-level CS OpControlBarrier.
- W14-07 (adapted from Marvisak/VIctory#2208, GPL-3.0): replace OpAtomicAnd+OpAtomicOr in global_sub and buffer_sub with OpAtomicCompareExchange CAS loops.
- W14-08 (adapted from fxceless/victory-fork#354 GPL-3.0 & brcly/TuxBox#185 Apache-2.0): tighten uniform_vgprs/wave64_halves implicit-read tracking (DPP mods, vlist, block-0 back-edges, v_fmac_f64, v_dot*c_u32_*, v_cndmask_b16) and constant-lane/uniform v_readlane_b32 lowering.
- Add synthetic semantic & spirv-val regression suite tests/semantic/test_w14_shader_stability.py (zero game data).
@ArturoWolff

Copy link
Copy Markdown

There is one more ordinary two-result case worth covering in the uniformity analysis here: v_swap_b32. AscilCH's implicit-read work in #354 is what led me to check it. The destination's old value is an actual input to the value written back to the source, not merely an inactive-lane preservation dependency. AMD's RDNA 2 ISA, section 12.8, V_SWAP_B32 defines the exchange of both operands.

I tested the actual ir::build -> structure -> all_lane_blocks -> lane_uniform path with this entirely synthetic, unguarded sequence and its reverse register order:

v_mov_b32 v5, v0
v_mov_b32 v6, 42
v_swap_b32 v5, v6
s_endpgm

Here v0 is a varying input. The fixture also runs both input orders and both same-kind controls. Its independent CPU oracle swaps two initialized values for each of 64 lanes and checks which output is constant across the lanes. A 1 below means the output is proved uniform, in encoded destination/source order. These are analysis results, not GPU readbacks.

Destination/source Old input uniformity Expected output proof Main before #354 head Main with correction
v5/v6 01 10 11 11 10
v5/v6 10 01 00 00 01
v6/v5 01 10 00 11 10
v6/v5 10 01 00 00 01

All four controls (both varying or both uniform, in both register orders) agree with the oracle in all three builds. The two focused tests on each uncorrected revision return 1 pass / 1 intended assertion failure; on the corrected receiver both pass. The existing native library suite gives 171 passed / 3 ignored before and after, with identical named outcomes. The historical #354 head gives 170 passed / 2 ignored. All builds completed successfully.

Exact tested revisions: main 93db653ada3ee0b94b2334243d7af81c3d12f27a and #354 head 6273077e2b8d3583f681aeafbce1ce83040f936d. These are separate source snapshots, not a causal whole-PR A/B. The controlled correction is on the first revision, with the same test-only fixture and lockfile before/after. Native x86_64 Linux, Rust 1.98.1, independent Cargo targets, locked/offline builds. No game, display, or GPU access.

Strict Clippy for the corrected non-test library also exits 0. Strict native --lib --tests lint exits 101 on both the receiver and its corrected version, with the same four unused-import/variable/mutability diagnostics in the unchanged emit/mod.rs. I left those unrelated Windows-gated test warnings outside this correction.

The narrow correction I tested is to retain the existing VALU and guard eligibility checks, handle this instruction before the generic alike classification, and propagate each old SSA input only to the output it supplies. With v borrowed from the existing Fields::Valu match:

if name == "v_swap_b32" {
    if let (Some(&Operand::Vector { reg: src, .. }), Some(Operand::Vector { reg: dst, .. })) =
        (v.src.first(), v.dst)
    {
        for (out, input) in [(Reg::V(dst), Reg::V(src)), (Reg::V(src), Reg::V(dst))] {
            if inst.read(input).is_some_and(|x| same.contains(&x)) {
                same.extend(inst.write(out));
            }
        }
    }
    continue;
}

For #354 this needs to be before the generic implicit-read/overwritten-destination filtering. Simply requiring both old values to be uniform would avoid the false positive but still miss the uniform output of a mixed swap. The tested main's ascending-register read selection also makes the two register orders differ. The per-result rule handles both tested orders while retaining the existing guard eligibility checks. This matters because v_readfirstlane_b32 can consume this proof when deciding whether the two host halves need to exchange values.

Main advanced to e4245f3c1b0490250d9cf0946a2113000be98687 while closing this comparison. I checked that the text of lane_uniform is unchanged and that the correction applies cleanly there, but its surrounding translator has changed and that newer revision has not been built or executed in this comparison. This is only a small proven analysis correction; I have not established a connection to the ten pipeline refusals reported above, the two water programs, driver execution, lighting, or title frame time.

AI assistance: Hermes Agent, working on behalf of ArturoWolff. (GPT 6.2 Sol Max).

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.

4 participants