Shaders: select v_readfirstlane_b32 source lane from the EXEC guard #2828
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2830"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T21:38:52Z.
Problem
Lower::valudrops the branchless lane guard (st.guards[b][i]) for every instruction with a scalar destination (Some(Operand::Scalar { .. }) => None). While scalar destinations are indeed written unconditionally regardless ofEXEC(already enforced byLower::write_i),v_readfirstlane_b32still usesEXECto select which source lane to read (lowest active bit ofEXEC, or lane 0 whenEXEC == 0; comparecrates/victory-shader/src/range.rs:162-164).When
EXECis narrowed without branching (e.g., viav_cmpx_*ors_and_saveexec_b32+s_xor_b32), droppingguardcausesv_readfirstlane_b32to emit an unguardeddx.op.waveReadLaneFirst.i32over the full host wave, silently reading inactive lane 0 instead of the first active guarded lane.Fix
Lower::valu, keepself.guard(b, i)?for non-waterfallv_readfirstlane_b32(waterfall reads andv_readlane_b32still skipself.guard).Lower::valu_op("v_readfirstlane_b32"), whenguardisSome(g), compute the lowest active lane fromdx.op.waveActiveBallot(g)withdx.op.unaryBits.i32(FirstbitLo, falling back to lane0when both ballot words are zero) and read viadx.op.waveReadLaneAt.i32. Unguardedv_readfirstlane_b32keeps emittingdx.op.waveReadLaneFirst.i32,helper_reads_ownis preserved, andwrite_istill writes the scalar destination unconditionally.Verification
emit::lower::tests::readfirstlane_uses_the_exec_guard_to_select_a_lane(28/28inemit::lower::tests): confirms guardedv_readfirstlane_b32emitswaveActiveBallot+waveReadLaneAt.i32(and setswave_ops), while unguarded emitswaveReadLaneFirst.i32.dxil.dlltestemit::valu_tests::guarded_readfirstlane_broadcasts_after_exec_is_restored: verifiesv_cmpx_le_u32(lanes2..),s_and_saveexec_b32+s_xor_b32(lanes3..), and empty-EXECv_cmpx_eq_u32(falls back to lane 0) after restoringEXEC, passing on RTX 5090, WARP, and Windows SDK10.0.26100.0dxil.dll.cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default and--features host): 0 warnings.cargo xtask policy: 184 files ok.Host Wave-Width Scope
Consistent with
main's unguardedv_readfirstlane_b32lowering (waveReadLaneFirst.i32), this guarded lowering (waveActiveBallot+firstbitlow+waveReadLaneAt.i32) operates within each host wave and does not addv_readfirstlane_b32toconsole_lanes(WaveSize), avoidingWaveSize(64)PSO rejection on 32-wide hosts; shaders that already require console wave width continue to pinWaveSizeviaconsole_lanes.Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T21:57:09Z.
Review: COMMENTED
Correct readfirstlane semantics: hardware v_readfirstlane_b32 reads lane ff1(EXEC), but waveReadLaneFirst under emulation uses the whole wave — a divergent-region call silently picked the wrong lane. Ballot-of-guard + cttz(lo) / cttz(hi)+32 reproduces ff1(EXEC) exactly, the empty-EXEC->lane0 fallback is deterministic, and the waterfall-reads carve-out keeps the proven per-lane fast path. LGTM.
Imported from Marvisak/VIctory. Originally posted by ligmaSec on 2026-11-25T21:47:18Z.
I tried the guarded read on a downstream tree with your ballot + lane selection from
36eb5f6, using pinned vkd3d-proton and Mesa llvmpipe (8-lane subgroups). The four-thread compute case passed a real UAV readback. With lane values equal to thread index + 1 and EXEC selecting lanes 2 and 3, main returned four 1s and the candidate returned four 3s. Unguarded and empty-EXEC controls both returned four 1s.One case for the numerical test is a console Wave32 group spanning two narrower host waves. With 16 threads, lane values equal to thread index + 1, and EXEC narrowed to thread indices >= 10, every thread must get 11 after restoring EXEC. The baseline gave eight 1s then eight 9s. The candidate gave eight 1s then eight 11s. The first host wave sees an empty ballot even though the console wave has active lanes. Both shaders had an unconstrained host wave size. An expected value calculated from each host wave’s own first value can miss this case.
This was synthetic, with the lowering adapted to our tree. Shapes already requiring the console wave width are a good independent case to keep. Could you cover the split-host-wave case or document the width assumption on this PR?
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T22:32:21Z.
Thanks for testing
36eb5f6on vkd3d-proton + llvmpipe (8-lane subgroups) and verifying the 4-thread UAV readback!Good catch on the split-host-wave case. That behavior matches
main's existing wave-width assumption forv_readfirstlane_b32: inemit/lower.rs(console_lanes),s.wave_size = Some(wave)is only emitted for compute shaders using explicit lane-index / swizzle / permute / DPP / opaque-mask ops (and single-wave LDS without barriers), not forv_readfirstlane_b32itself — so bothmain's unguardedwaveReadLaneFirst.i32and this PR's guardedwaveActiveBallot+firstbitlow+waveReadLaneAt.i32operate per host wave unless the shader already pinsWaveSize.Adding
v_readfirstlane_b32unconditionally toconsole_laneswould setWaveSize(64)on Wave64 compute shaders (which fails PSO creation on 32-wide NVIDIA hosts), and graphics/mesh stages cannot specifyWaveSizein DXIL anyway. I'll note this host-wave scope explicitly in the PR description so the assumption is clear.Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-25T23:33:33Z.
Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head
36eb5f6(on its base13f91ca).Tested
dxil.dllfrom Windows SDK 10.0.26100 (the loader's default), debug.cargo build --workspace --locked, thencargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 775 passed, 0 failed, 9 ignored.readfirstlane_uses_the_exec_guard_to_select_a_lane,guarded_readfirstlane_broadcasts_after_exec_is_restored.--skipleaves out tests that fail on main842b0cbon this card too, not because of this PR:heap_direct_texture_reads_mark_their_heap_indexends thevictory-shadertest process with0xc0000005(#2571), so without the skip the rest of that crate never reports;dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_viewsand the twostride_testshit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317);gathers_at_a_level_match_the_hardwarefails on lane 0 (#1744).Not tested
AI use
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T02:28:25Z.
Review: APPROVED
Tested merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP,dxil.dllvalidator active):cargo test -p victory-shader --lib emit::valu_tests::guarded_readfirstlane_broadcasts_after_exec_is_restored: 1 passed, 0 failed (RTX 5090 hardware + WARP).cargo test -p victory-shader --lib emit::lower::tests::readfirstlane_uses_the_exec_guard_to_select_a_lane: 1 passed, 0 failed.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T02:23:17Z.
Automated triage for the maintainers.
Lower::valudropped the branchless EXEC guard for every scalar destination.v_readfirstlane_b32still picks its source lane from EXEC (the lowest active lane, or lane 0 when EXEC is empty), so after av_cmpx/s_and_saveexecwithout a branch it read the wrong lane. With a guard it now ballots the guard, takes the lowest set bit (lane 0 when empty) and reads that lane. Waterfall reads and the unguarded case keepwaveReadLaneFirst. The tests check the emitted ops and run on hardware/WARP, and ligmaSec reproduced the fix on another host. The limitation for a console wave split across narrower host waves is the same as onmain.CI: passing.
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-26T18:38:34Z.
Wave64 hardware check of
36eb5f6.Checked against a hardware table measured on an AMD Radeon iGPU (gfx1036, RDNA 2, Ryzen 7 9800X3D) in native
wave64: 64 cross-lane instructions x 8 EXEC masks (all, lo32, hi32, all but lane 0, alternate lanes, lane 40 alone,
lanes 31+32, a sparse mask). Each case was assembled with the same assembler, run through VIctory as a wave64
compute program of 64 threads on the same Radeon through D3D12 (WaveSize 64, dxil.dll validated), and all 64
lanes compared with the hardware. EXEC is narrowed two ways: straight-line (
v_cmpx_ne_u32, lowered with a laneguard) and branched (
s_and_saveexec_b64+s_cbranch_execz, a DXILif). Windows 11, Rust pinned, debug.No game files were used.
Tested (merged with #1312 onto main
63bcca4; Radeon, wave64):v_readfirstlane_b32 s20, v4(v4 = lane + 1) is exact for all 8 masks with this PR.On main, 4 of 8 are wrong, every mask whose first lane is not 0: hi32 gives lanes 32-63 the value 1 instead
of 33; all-but-lane-0 gives 1 instead of 2; lane 40 alone gives 1 instead of 41; lanes 31+32 give 1 instead of 32.
this PR.
The host-wave width note, measured on AMD too: a wave64 program whose only lane operation is
v_readfirstlane_b32declares noWaveSize, and the AMD driver ran it as two waves of 32 on this 32..64 host.With full EXEC, lanes 32-63 got lane 32's value (0x21 instead of 0x1), on main and with this PR alike. So the scope
in the description applies on AMD hosts as well, not only on 32..32 ones. Programs that also read lane ids
(
v_mbcnt, DPP, swizzle, permute, readlane) declare WaveSize(64) and are exact.Not tested: NVIDIA for this PR (the table is wave64), the title.
AI use: Claude Code (Claude Opus 5.5) built the harness and the hardware table, ran them on the GPU above and wrote this report; the numbers come from those runs.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.