d3d12-shader: Wave64 loop convergence, lane provenance, and wide/sub-dword paired LDS (D3D12-SHADER-PAIRED-WAVE64-EXACTNESS) #2662
No reviewers
Labels
No labels
accessibility
area: ci
area: d3d12
area: docs
area: launcher
area: runtime (legacy)
area: shader translator
area: tools
area: viewer
bug
dependencies
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs in-game run
needs rebase
needs-triage
question
ready-for-agent
submodules
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!2662
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2662"
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?
@zmasi on GitHub · 2026-11-30 00:09 UTC · original
zmasi:fix/d3d12-shader-paired-water-and-wave64→main· +1914 −207 · 6 files · 4 commits · merged 2026-11-30 05:45 UTCClaimed in the coordination issue as: D3D12-SHADER-PAIRED-WAVE64-EXACTNESS (#2570, #2570)
What and why
Fixes
D3D12-SHADER-PAIRED-WAVE64-EXACTNESSacrossd3d12/crates/leonida-shader/src/emit/{mod.rs, wave.rs, paired.rs, lower.rs, paired_tests.rs, wave64_tests.rs}:LEONIDA_LOOPS_WAVE=1), partial-grouphost_widemasking + dead-half-wave exit, and fixpoint loop-region tracking:loops_wavedefaulted to0whenLEONIDA_LOOPS_WAVEwas unset, leaving threads that exited per-lane loops on earlier iterations in independent sub-warps on 32-lane hosts (NVIDIA), which corrupted post-loopwaveActiveBallot/exchange_halves/v_readfirstlane/v_mbcnttile appends (CS_TransmissionVolumetrics_GenerateTiles1aa247ed3653b616and boundary tile classifiers).loops_wavenow defaults to1for Wave64 compute programs on 32-lane hosts while keepingLEONIDA_LOOPS_WAVE=0as an explicit opt-out (aftertrustno1-coder/VIctory-NVIDIA64d4931, GPL-3.0).Lower::wave_wide, thehost_widebranch now masksminebyself.partial_active(And(mine, a)forany,Or(mine, Xor(a, true))for!any) and combines withwaveAnyTrue(a)when an entire 32-lane host half-wave is unlaunched (waveAnyTrue(a) == false) so dead half-waves exit on iteration 1 and reach post-loop barriers cleanly instead of spinning toLOOP_LIMIT.lower::lane_regions(all_lane_blocks,branches_around) treated per-lane loop exit,continue, or guarded-breakblocks as wave-converged whenever post-dominated across later iterations.lane_regionsnow intersects each retreating edge's backward walk fromlatchwith forward reachability fromhead(keeping exact natural loops on reducible CFGs and exact loop SCCs on irreducible CFGs without leaking into entry block0), merges loop bodies sharinghead, and closes the non-post-dominated walk to a fixpoint overfull_by_headwhenever a per-lane branch reachesheador exitsfull_bodydirectly or through intermediate blocks (while keeping diamondif/elsejoins and nested loops inside uniform outer loops unexpanded).Sym::Own(Value, u32)&Shape::Lane(Value, u32)):wave.rstracked initial lane values asSym::Own(Value)without their source lane index0..wave, miscompiling fixed-lane broadcasts inside whole-wave sections (s_or_saveexec_b64 -1..v_readlane_b32 s_dst, v_src, <lane>) into per-lane identity reads (Shape::Value(v_src)).Sym::Own(Value, u32)andShape::Lane(Value, u32)now preserve the exact source console lane0..63, lowered across both halves inpaired_shapeand throughconsole_lane_read(with half-wave refusal and cachedevery_lane_blockfull-wave convergence checks) inLower::shape(afteredsuuu/VI-BR-PORTcf3394bandTaynanGT/Vice-Tay5e588e8, GPL-3.0).paired_crossv_readlane_b32withLower::valu:paired_instinterceptedv_readlane_b32beforeLower::valu, bypassingResources::restored_lanesand post-sectionSection::reads.paired_crossnow lowers both across lower and upper paired halves (self.both/put_lanes) before general lane shuffles and appliesmemory_guardinv_readfirstlane_b32andds_bpermute_b32.paired.rsalongside_rtnatomics,ds_cmpst_rtn_b32, andds_subCAS loops:supports_paired_ldsandpaired_ldstods_read_b64/_b96/_b128,ds_write_b64/_b96/_b128,ds_read2_b64,ds_write2_b64,ds_read2st64_b32/_b64,ds_write2st64_b32/_b64, and sub-dwordds_read_{u8,i8,u16,i16}/ds_write_{b8,b16}(plus_d16and_d16_hi), using atomicRmw::And+Rmw::Orfor sub-dword writes so lower and upper console lanes sharing an LDS dword never clobber each other, preserving multi-wavebarriers_on_every_way,_rtninteger atomics,ds_cmpst_rtn_b32, andLower::lds_int_subcompare-exchange loops fords_sub_u32/ds_sub_rtn_u32/0 - xadds, and guarding every dword access byindex < dwordsso out-of-bounds reads/returns yield0and out-of-bounds writes drop cleanly (afterTaynanGT/Vice-Tay5e588e8,fxceless/victory-fork, andvoidopcode/vilocityadd4873/5c8149f, GPL-3.0).USER_DISPATCH_THREADS_X) Wave64 compute programs and 1-DBeforeWiderescue inpaired:spill::is_spilledrefusal inemit::pairedand decouples theBeforeWidepairedfallback from the 2-Dtilecheck (while keepingwiden()'s barrier-heavy fallback gated ontileunlessLEONIDA_WAVE_WIDE_LAST=all) so refused 1-D[64, 1, 1]groups with divergent cross-half LDS regions translate cleanly.partial_activemasks inLower::run(counted = true)against console group widthcs.threads[0]without early-exiting host threads needed at LDS barriers, swapspartial_activeinLaneState/swap_lanes, and applies per-halfpartial_active/launchedacrossmask_dword,scc_some,wave_wide, andpaired_f64_constant_sum.Tested
origin/main):all_lane_blocksleft per-lane loop latches, shared-latch / directcontinueloop bodies, and guarded-breakblockstrue;whole_wave_section_fixed_lane_broadcast_preserves_lane_provenancereturned0x12340000 + tinstead of0x12340000 + lane; unmaskedhost_wideloops in partial thread-countedhalves/exchangedispatches spun toLOOP_LIMIT; andpaired_readlane_after_whole_wave_section_and_restored_parked_scalar,paired_lds_wide_strided_and_subdword_accesses_across_halves,the_lds_operations_the_paired_form_takes, andpaired_partial_thread_counted_dispatch_masks_both_halves_and_keeps_barriersfailed withUnsupported/ assertion errors.cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib paired_tests -- --test-threads=4(default &--features host) ->22/22passed (GenerateTiles64-group post-loop append + 1-D refused group rescue, multi-wavebarriers_on_every_way, fixed-lane whole-wave broadcasts acrosslane in [0, 19, 31, 32, 37, 63]andstart in [0, 16, 40],Section::reads+restored_lanes,ds_read_b64/_b96/_b128+ OOB +2st64_b32/_b64+ concurrentb8/u8/i8/b16/u16/i16+_d16/_d16_hi+ds_sub_rtn_u32CAS loop +ds_cmpst_rtn_b32+ OOB return0, spilled partial thread-counted[2, 1, 1]dispatches in bothpairedandhost_widehalves/exchangemodes including iteration-1 dead-half-wave exit proof, and direct/trampolined/shared-latchcontinue, guardedbreak, irreducible 2-entry loop, and diamond/nested-loop non-expansion CFG checks).cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib wave64_tests -- --test-threads=4(default &--features host) ->30/30passed;lds_subtracts(--features host) ->2/2passed;emit::tests->23/23passed.cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --all-targets(default &--features host) ->0warnings on touched code.python -I scripts/check_no_game_data.py->0issues.GPUs tested: NVIDIA GeForce RTX 3090 (driver 610.88) and Microsoft WARP, Windows 11 Pro (26200)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a:d3d12/crates/leonida-shaderDXIL backend;docs/design.mdcoverstools/rdna2spirv.py)Fixes six Wave64-on-Wave32 translation and lowering gaps across `d3d12/crates/leonida-shader/src/emit/{mod.rs, wave.rs, paired.rs, lower.rs}`: 1. Host half-wave loop convergence & natural-loop region tracking: - Default `LEONIDA_LOOPS_WAVE` to `1` when unset (`0` opts out) so Wave64 compute shaders on 32-lane hosts keep each host half-wave converged across per-lane loops before post-loop ballots, exchanges, and first-lane reads (`CS_TransmissionVolumetrics_GenerateTiles` and tile classifiers; after trustno1-coder/VIctory-NVIDIA 64d4931, GPL-3.0). - Teach `lower::lane_regions` (`all_lane_blocks`, `branches_around`) to include natural-loop bodies exited per lane so loop headers, mid-loop breaks, and latches are never misclassified as executed by every lane on subsequent iterations, while reconverged post-dominating blocks after a single-exit loop remain wave-converged. 2. Fixed-lane provenance in whole-wave sections (`wave.rs`, `paired.rs`, `lower.rs`): - Track source lane provenance in `Sym::Own(Value, u32)` and emit `Shape::Lane(Value, u32)` when a whole-wave section broadcasts a fixed console lane (`v_readlane_b32` into SGPR or copied VGPR) instead of collapsing to each lane's own value (`Shape::Value`). - Lower `Shape::Lane` across lower/upper paired halves in `paired_shape` and via `console_lane_read` with half-wave and full-wave convergence checks in `Lower::shape` (after edsuuu/VI-BR-PORT cf3394b and TaynanGT/Vice-Tay 5e588e8, GPL-3.0). 3. Paired `v_readlane_b32` parity with `Lower::valu` (`paired.rs`): - Handle `Resources::restored_lanes` (uniform scalars parked in scratch VGPRs clobbered by unmodelled whole-wave instructions) and post-section `Section::reads` before general cross-lane shuffles in `paired_cross`. 4. Bounds-checked multi-dword, strided-64, and sub-dword paired LDS (`paired.rs`): - Extend `supports_paired_lds` and `paired_lds` to `_b64`/`_b96`/`_b128`, `ds_read2`/`ds_write2` (`_b32`/`_b64`), `ds_read2st64`/`ds_write2st64` (`_b32`/`_b64`), and sub-dword `_u8`/`_i8`/`_u16`/`_i16`/`_b8`/`_b16` (including `_d16` and `_d16_hi`), using `Rmw::And` + `Rmw::Or` for sub-dword writes so paired halves sharing an LDS dword never clobber each other, and guarding every dword index by `index < dwords` (after TaynanGT/Vice-Tay 5e588e8 and fxceless/victory-fork, GPL-3.0). 5. Spilled & thread-counted (`USER_DISPATCH_THREADS_X`) paired compute programs (`mod.rs`, `paired.rs`, `lower.rs`): - Allow spilled Wave64 compute programs in `emit::paired` and decouple the `BeforeWide` `paired` fallback from the 2-D `tile` gate so refused 1-D Wave64 groups with divergent cross-half LDS regions are rescued cleanly. - Compute per-half `partial_active` masks in `Lower::run(counted = true)` against console group width `cs.threads[0]` without early-exiting host threads needed at LDS barriers, swap `partial_active` in `swap_lanes`, and apply `partial_active` / `launched` / `memory_guard` across `mask_dword`, `scc_some`, `wave_wide`, `paired_f64_constant_sum`, and `paired_cross`. Verification (Windows 11 + NVIDIA GeForce RTX 3090 D3D12 & WARP): - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib paired_tests -- --test-threads=4`: 20/20 passed (6 new Red->Green hardware + unit tests). - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader -- --test-threads=4`: 359 passed, 0 failed. - `cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --all-targets`: 0 warnings on touched code. - `python -I scripts/check_no_game_data.py`: 0 issues. AI-assisted.Follow-up fixes for Wave64-on-Wave32 lowering and loop-region analysis: 1. `Lower::wave_wide` `host_wide` partial-group masking (`lower.rs`): - Mask `mine` by `self.partial_active` (`And(mine, a)` for `any`, `Or(mine, Xor(a, true))` for `!any`) in the `host_wide` branch so unlaunched threads kept in the wave for barriers (`USER_DISPATCH_THREADS_X` in spilled `halves`/`exchange` Wave64 compute programs) never hold `waveAnyTrue` true until `LOOP_LIMIT` or keep `waveAllTrue` false. 2. Multi-latch & irreducible loop region precision (`lower.rs`): - Intersect each retreating edge's backward walk from `latch` with forward reachability from `head` so irreducible multi-entry loops capture only the exact loop SCC without leaking into entry block `0` or preheaders. - Merge all loop bodies sharing `head` so per-lane `continue` back-edges (direct or trampolined) before a uniform latch mark post-dominating loop-body blocks `all_lane_blocks == false` and include the `continue` branch in `branches_around`. 3. Paired readlane parity, cached `all_lane_blocks`, and expanded tests (`paired.rs`, `lower.rs`, `paired_tests.rs`, `wave64_tests.rs`): - Evaluate `restored_lanes` and post-section `Section::reads` across both paired halves via `self.both` / `put_lanes` instead of mirroring `lo`. - Cache `all_lane_blocks(self.x.st)` on `Lower` (`every_lane_block`) and co-locate `console_lane_read` right below `Lower::shape`. - Update `wave64_tests` mode-`0` assertion text and add runtime RTX + WARP coverage for `ds_write_b8`/`ds_read_u8`/`ds_read_i8`, `_d16`/`_d16_hi` half preservation, `ds_write_b128`/`ds_read_b96`/`ds_read_b128`, `ds_write2st64_b64`/`ds_read2st64_b64`, counted `host_wide` loops (`s_cbranch_execnz` & `s_cbranch_execz`), per-lane `continue` before a uniform latch with cross-half LDS, and irreducible 2-entry loop CFGs. Verification (Windows 11 + NVIDIA GeForce RTX 3090 D3D12 & Microsoft WARP): - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib paired_tests -- --test-threads=4`: 21/21 passed. - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader -- --test-threads=4`: 360 passed, 0 failed. - `cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --all-targets`: 0 warnings on touched code. - `python -I scripts/check_no_game_data.py`: 0 issues. AI-assisted.Follow-up fixes for Wave64-on-Wave32 loop-region analysis and partial-group `host_wide` loops: 1. Fixpoint closure over `full_by_head` in `lower::lane_regions` (`lower.rs`): - Replace the pre-walk `sub_loops` check with a monotone fixpoint over `full_by_head` (`expanded`), expanding any enclosing loop `full_body` with `full_body[d]` whenever the non-post-dominated walk reaches `head`, leaves `full_body`, or reaches a block with an edge to `head` or outside `full_body`. - Captures shared-latch per-lane `continue` branches and guarded `break` exits inside per-lane branches while keeping diamond `if/else` joins and nested per-lane loops inside uniform outer loops unexpanded. 2. Dead half-wave early exit in `Lower::wave_wide` (`lower.rs`): - When an entire 32-lane host half-wave is unlaunched (`waveAnyTrue(a) == false`, e.g. lanes `32..63` when `USER_DISPATCH_THREADS_X <= 32`), combine `host_wide`'s branch result with `waveAnyTrue(a)` (`And` when `taken` cycles back to `b` and `!any`, `Or(!waveAnyTrue(a))` when `not_taken` cycles back to `b` and `any`) so dead half-waves exit on iteration 1 and reach post-loop barriers cleanly instead of spinning to `LOOP_LIMIT`. 3. Regression coverage (`paired_tests.rs`): - Verify live on RTX 3090 + WARP that lane 40's `v6` equals `1` when `active_in_group <= 32` and `4` when `active_in_group > 32`, and add unit assertions for shared-latch `continue`, guarded `break` inside a per-lane branch, diamond `if/else` non-expansion, and nested-loop non-expansion. Verification (Windows 11 + NVIDIA GeForce RTX 3090 D3D12 & Microsoft WARP): - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib paired_tests -- --test-threads=4`: 21/21 passed. - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib wave64_tests -- --test-threads=4`: 30/30 passed. - `cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --all-targets`: 0 warnings on touched code. - `python -I scripts/check_no_game_data.py`: 0 issues. AI-assisted.@ecox982745 on GitHub · 2026-11-30 00:47 UTC · original
Native D3D12 Wave64 verification on RTX 4090 (NVIDIA 616.64, Windows 11 build 26300, MSVC, Rust 1.98.1, SDK 26100 DXIL validator). I merged
ab78da371ontob8bd37111(tree9be32c2d3).cargo test --locked -p leonida-shader --features host --lib:paired_tests22/22,wave64_tests30/30,lds_subtracts2/2. These filters mix CPU and GPU assertions.b8bd37111under the same toolchain and driver.Synthetic inputs only. AI-assisted with OpenAI Codex (GPT-6.2 Sol).
@merzleckeier on GitHub · 2026-11-30 00:50 UTC · original
Independent Wave64 verification on Wine and RADV. I merged
ab78da371intomainb8bd37111(tree9be32c2d3) and built the Win64leonida-shadertests with--features host. Wine 11.22 and VKD3D-Proton71cae091, RTX 4070 SUPER (620.73.04) and Radeon Raphael (RADV 26.3.1).paired_testsand 30wave64_testspassed on each GPU. Thewave64_teststotal includes three optional native-Wave64 hardware paths that loggednot runon NVIDIA and one 32-lane-only flag-dispatch path skipped on RADV.mainand the merged tree.Synthetic fixtures only. AI-assisted with OpenAI Codex.