d3d12-shader: Wave64 loop convergence, lane provenance, and wide/sub-dword paired LDS (D3D12-SHADER-PAIRED-WAVE64-EXACTNESS) #2662

Merged
zmasi merged 4 commits from upstream-pr/2662 into main 2026-11-30 05:45:09 +00:00
zmasi commented 2026-11-30 00:09:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-30T00:09:06Z.


Claimed in the coordination issue as: D3D12-SHADER-PAIRED-WAVE64-EXACTNESS (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7250778009, https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7264381245)

What and why

Fixes D3D12-SHADER-PAIRED-WAVE64-EXACTNESS across d3d12/crates/leonida-shader/src/emit/{mod.rs, wave.rs, paired.rs, lower.rs, paired_tests.rs, wave64_tests.rs}:

  1. Default host half-wave loop convergence (LEONIDA_LOOPS_WAVE=1), partial-group host_wide masking + dead-half-wave exit, and fixpoint loop-region tracking:
    • Previously, loops_wave defaulted to 0 when LEONIDA_LOOPS_WAVE was unset, leaving threads that exited per-lane loops on earlier iterations in independent sub-warps on 32-lane hosts (NVIDIA), which corrupted post-loop waveActiveBallot / exchange_halves / v_readfirstlane / v_mbcnt tile appends (CS_TransmissionVolumetrics_GenerateTiles 1aa247ed3653b616 and boundary tile classifiers). loops_wave now defaults to 1 for Wave64 compute programs on 32-lane hosts while keeping LEONIDA_LOOPS_WAVE=0 as an explicit opt-out (after trustno1-coder/VIctory-NVIDIA 64d4931, GPL-3.0).
    • In Lower::wave_wide, the host_wide branch now masks mine by self.partial_active (And(mine, a) for any, Or(mine, Xor(a, true)) for !any) and combines with waveAnyTrue(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 to LOOP_LIMIT.
    • Previously, lower::lane_regions (all_lane_blocks, branches_around) treated per-lane loop exit, continue, or guarded-break blocks as wave-converged whenever post-dominated across later iterations. lane_regions now intersects each retreating edge's backward walk from latch with forward reachability from head (keeping exact natural loops on reducible CFGs and exact loop SCCs on irreducible CFGs without leaking into entry block 0), merges loop bodies sharing head, and closes the non-post-dominated walk to a fixpoint over full_by_head whenever a per-lane branch reaches head or exits full_body directly or through intermediate blocks (while keeping diamond if/else joins and nested loops inside uniform outer loops unexpanded).
  2. Preserve source lane provenance in whole-wave sections (Sym::Own(Value, u32) & Shape::Lane(Value, u32)):
    • Previously, wave.rs tracked initial lane values as Sym::Own(Value) without their source lane index 0..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) and Shape::Lane(Value, u32) now preserve the exact source console lane 0..63, lowered across both halves in paired_shape and through console_lane_read (with half-wave refusal and cached every_lane_block full-wave convergence checks) in Lower::shape (after edsuuu/VI-BR-PORT cf3394b and TaynanGT/Vice-Tay 5e588e8, GPL-3.0).
  3. Align paired_cross v_readlane_b32 with Lower::valu:
    • paired_inst intercepted v_readlane_b32 before Lower::valu, bypassing Resources::restored_lanes and post-section Section::reads. paired_cross now lowers both across lower and upper paired halves (self.both / put_lanes) before general lane shuffles and applies memory_guard in v_readfirstlane_b32 and ds_bpermute_b32.
  4. Bounds-checked multi-dword, strided-64, and sub-dword LDS lowering in paired.rs alongside _rtn atomics, ds_cmpst_rtn_b32, and ds_sub CAS loops:
    • Extends supports_paired_lds and paired_lds to ds_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-dword ds_read_{u8,i8,u16,i16} / ds_write_{b8,b16} (plus _d16 and _d16_hi), using atomic Rmw::And + Rmw::Or for sub-dword writes so lower and upper console lanes sharing an LDS dword never clobber each other, preserving multi-wave barriers_on_every_way, _rtn integer atomics, ds_cmpst_rtn_b32, and Lower::lds_int_sub compare-exchange loops for ds_sub_u32 / ds_sub_rtn_u32 / 0 - x adds, and guarding every dword access by index < dwords so out-of-bounds reads/returns yield 0 and out-of-bounds writes drop cleanly (after TaynanGT/Vice-Tay 5e588e8, fxceless/victory-fork, and voidopcode/vilocity add4873 / 5c8149f, GPL-3.0).
  5. Support spilled and thread-counted (USER_DISPATCH_THREADS_X) Wave64 compute programs and 1-D BeforeWide rescue in paired:
    • Removes the spill::is_spilled refusal in emit::paired and decouples the BeforeWide paired fallback from the 2-D tile check (while keeping widen()'s barrier-heavy fallback gated on tile unless LEONIDA_WAVE_WIDE_LAST=all) so refused 1-D [64, 1, 1] groups with divergent cross-half LDS regions translate cleanly.
    • Computes independent lower and upper partial_active masks in Lower::run(counted = true) against console group width cs.threads[0] without early-exiting host threads needed at LDS barriers, swaps partial_active in LaneState / swap_lanes, and applies per-half partial_active / launched across mask_dword, scc_some, wave_wide, and paired_f64_constant_sum.

Tested

  • Red (origin/main):
    • Reproduced all target failures on Windows 11 + NVIDIA GeForce RTX 3090: all_lane_blocks left per-lane loop latches, shared-latch / direct continue loop bodies, and guarded-break blocks true; whole_wave_section_fixed_lane_broadcast_preserves_lane_provenance returned 0x12340000 + t instead of 0x12340000 + lane; unmasked host_wide loops in partial thread-counted halves/exchange dispatches spun to LOOP_LIMIT; and paired_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, and paired_partial_thread_counted_dispatch_masks_both_halves_and_keeps_barriers failed with Unsupported / assertion errors.
  • Green:
    • cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib paired_tests -- --test-threads=4 (default & --features host) -> 22/22 passed (GenerateTiles 64-group post-loop append + 1-D refused group rescue, multi-wave barriers_on_every_way, fixed-lane whole-wave broadcasts across lane in [0, 19, 31, 32, 37, 63] and start in [0, 16, 40], Section::reads + restored_lanes, ds_read_b64/_b96/_b128 + OOB + 2st64_b32/_b64 + concurrent b8/u8/i8/b16/u16/i16 + _d16/_d16_hi + ds_sub_rtn_u32 CAS loop + ds_cmpst_rtn_b32 + OOB return 0, spilled partial thread-counted [2, 1, 1] dispatches in both paired and host_wide halves/exchange modes including iteration-1 dead-half-wave exit proof, and direct/trampolined/shared-latch continue, guarded break, 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/30 passed; lds_subtracts (--features host) -> 2/2 passed; emit::tests -> 23/23 passed.
    • cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --all-targets (default & --features host) -> 0 warnings on touched code.
    • python -I scripts/check_no_game_data.py -> 0 issues.

GPUs tested: NVIDIA GeForce RTX 3090 (driver 610.88) and Microsoft WARP, Windows 11 Pro (26200)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: d3d12/crates/leonida-shader DXIL backend; docs/design.md covers tools/rdna2spirv.py)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5556644848 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2662). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-30T00:09:06Z. --- Claimed in the coordination issue as: D3D12-SHADER-PAIRED-WAVE64-EXACTNESS (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7250778009, https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7264381245) ### What and why Fixes `D3D12-SHADER-PAIRED-WAVE64-EXACTNESS` across `d3d12/crates/leonida-shader/src/emit/{mod.rs, wave.rs, paired.rs, lower.rs, paired_tests.rs, wave64_tests.rs}`: 1. **Default host half-wave loop convergence (`LEONIDA_LOOPS_WAVE=1`), partial-group `host_wide` masking + dead-half-wave exit, and fixpoint loop-region tracking**: - Previously, `loops_wave` defaulted to `0` when `LEONIDA_LOOPS_WAVE` was unset, leaving threads that exited per-lane loops on earlier iterations in independent sub-warps on 32-lane hosts (`NVIDIA`), which corrupted post-loop `waveActiveBallot` / `exchange_halves` / `v_readfirstlane` / `v_mbcnt` tile appends (`CS_TransmissionVolumetrics_GenerateTiles` `1aa247ed3653b616` and boundary tile classifiers). `loops_wave` now defaults to `1` for Wave64 compute programs on 32-lane hosts while keeping `LEONIDA_LOOPS_WAVE=0` as an explicit opt-out (after `trustno1-coder/VIctory-NVIDIA` `64d4931`, GPL-3.0). - In `Lower::wave_wide`, the `host_wide` branch now masks `mine` by `self.partial_active` (`And(mine, a)` for `any`, `Or(mine, Xor(a, true))` for `!any`) and combines with `waveAnyTrue(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 to `LOOP_LIMIT`. - Previously, `lower::lane_regions` (`all_lane_blocks`, `branches_around`) treated per-lane loop exit, `continue`, or guarded-`break` blocks as wave-converged whenever post-dominated across later iterations. `lane_regions` now intersects each retreating edge's backward walk from `latch` with forward reachability from `head` (keeping exact natural loops on reducible CFGs and exact loop SCCs on irreducible CFGs without leaking into entry block `0`), merges loop bodies sharing `head`, and closes the non-post-dominated walk to a fixpoint over `full_by_head` whenever a per-lane branch reaches `head` or exits `full_body` directly or through intermediate blocks (while keeping diamond `if/else` joins and nested loops inside uniform outer loops unexpanded). 2. **Preserve source lane provenance in whole-wave sections (`Sym::Own(Value, u32)` & `Shape::Lane(Value, u32)`)**: - Previously, `wave.rs` tracked initial lane values as `Sym::Own(Value)` without their source lane index `0..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)` and `Shape::Lane(Value, u32)` now preserve the exact source console lane `0..63`, lowered across both halves in `paired_shape` and through `console_lane_read` (with half-wave refusal and cached `every_lane_block` full-wave convergence checks) in `Lower::shape` (after `edsuuu/VI-BR-PORT` `cf3394b` and `TaynanGT/Vice-Tay` `5e588e8`, GPL-3.0). 3. **Align `paired_cross` `v_readlane_b32` with `Lower::valu`**: - `paired_inst` intercepted `v_readlane_b32` before `Lower::valu`, bypassing `Resources::restored_lanes` and post-section `Section::reads`. `paired_cross` now lowers both across lower and upper paired halves (`self.both` / `put_lanes`) before general lane shuffles and applies `memory_guard` in `v_readfirstlane_b32` and `ds_bpermute_b32`. 4. **Bounds-checked multi-dword, strided-64, and sub-dword LDS lowering in `paired.rs` alongside `_rtn` atomics, `ds_cmpst_rtn_b32`, and `ds_sub` CAS loops**: - Extends `supports_paired_lds` and `paired_lds` to `ds_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-dword `ds_read_{u8,i8,u16,i16}` / `ds_write_{b8,b16}` (plus `_d16` and `_d16_hi`), using atomic `Rmw::And` + `Rmw::Or` for sub-dword writes so lower and upper console lanes sharing an LDS dword never clobber each other, preserving multi-wave `barriers_on_every_way`, `_rtn` integer atomics, `ds_cmpst_rtn_b32`, and `Lower::lds_int_sub` compare-exchange loops for `ds_sub_u32` / `ds_sub_rtn_u32` / `0 - x` adds, and guarding every dword access by `index < dwords` so out-of-bounds reads/returns yield `0` and out-of-bounds writes drop cleanly (after `TaynanGT/Vice-Tay` `5e588e8`, `fxceless/victory-fork`, and `voidopcode/vilocity` `add4873` / `5c8149f`, GPL-3.0). 5. **Support spilled and thread-counted (`USER_DISPATCH_THREADS_X`) Wave64 compute programs and 1-D `BeforeWide` rescue in `paired`**: - Removes the `spill::is_spilled` refusal in `emit::paired` and decouples the `BeforeWide` `paired` fallback from the 2-D `tile` check (while keeping `widen()`'s barrier-heavy fallback gated on `tile` unless `LEONIDA_WAVE_WIDE_LAST=all`) so refused 1-D `[64, 1, 1]` groups with divergent cross-half LDS regions translate cleanly. - Computes independent lower and upper `partial_active` masks in `Lower::run(counted = true)` against console group width `cs.threads[0]` without early-exiting host threads needed at LDS barriers, swaps `partial_active` in `LaneState` / `swap_lanes`, and applies per-half `partial_active` / `launched` across `mask_dword`, `scc_some`, `wave_wide`, and `paired_f64_constant_sum`. ### Tested - **Red (`origin/main`)**: - Reproduced all target failures on Windows 11 + NVIDIA GeForce RTX 3090: `all_lane_blocks` left per-lane loop latches, shared-latch / direct `continue` loop bodies, and guarded-`break` blocks `true`; `whole_wave_section_fixed_lane_broadcast_preserves_lane_provenance` returned `0x12340000 + t` instead of `0x12340000 + lane`; unmasked `host_wide` loops in partial thread-counted `halves`/`exchange` dispatches spun to `LOOP_LIMIT`; and `paired_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`, and `paired_partial_thread_counted_dispatch_masks_both_halves_and_keeps_barriers` failed with `Unsupported` / assertion errors. - **Green**: - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib paired_tests -- --test-threads=4` (default & `--features host`) -> `22/22` passed (`GenerateTiles` 64-group post-loop append + 1-D refused group rescue, multi-wave `barriers_on_every_way`, fixed-lane whole-wave broadcasts across `lane in [0, 19, 31, 32, 37, 63]` and `start in [0, 16, 40]`, `Section::reads` + `restored_lanes`, `ds_read_b64`/`_b96`/`_b128` + OOB + `2st64_b32`/`_b64` + concurrent `b8`/`u8`/`i8`/`b16`/`u16`/`i16` + `_d16`/`_d16_hi` + `ds_sub_rtn_u32` CAS loop + `ds_cmpst_rtn_b32` + OOB return `0`, spilled partial thread-counted `[2, 1, 1]` dispatches in both `paired` and `host_wide` `halves`/`exchange` modes including iteration-1 dead-half-wave exit proof, and direct/trampolined/shared-latch `continue`, guarded `break`, 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/30` passed; `lds_subtracts` (`--features host`) -> `2/2` passed; `emit::tests` -> `23/23` passed. - `cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --all-targets` (default & `--features host`) -> `0` warnings on touched code. - `python -I scripts/check_no_game_data.py` -> `0` issues. GPUs tested: NVIDIA GeForce RTX 3090 (driver 610.88) and Microsoft WARP, Windows 11 Pro (26200) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: `d3d12/crates/leonida-shader` DXIL backend; `docs/design.md` covers `tools/rdna2spirv.py`)
ecox982745 commented 2026-11-30 00:47:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-30T00:47:17Z.


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 ab78da371 onto b8bd37111 (tree 9be32c2d3).

  • Exact tree, cargo test --locked -p leonida-shader --features host --lib: paired_tests 22/22, wave64_tests 30/30, lds_subtracts 2/2. These filters mix CPU and GPU assertions.
  • I reran the paired wide, strided, and sub-dword LDS fixture with adapter logging after each successful dispatch. DXGI resolved all seven hardware dispatches to the RTX 4090, and the fixture passed its output assertions.
  • Three older Wave64 tests returned early after the hardware rejected their pipeline. The same three returned early on unmodified b8bd37111 under the same toolchain and driver.

Synthetic inputs only. AI-assisted with OpenAI Codex (GPT-6.2 Sol).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7265577174 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2662#issuecomment-7265577174). Originally posted by [ecox982745](https://github.com/ecox982745) on 2026-11-30T00:47:17Z. --- 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 `ab78da371` onto `b8bd37111` (tree `9be32c2d3`). - Exact tree, `cargo test --locked -p leonida-shader --features host --lib`: `paired_tests` 22/22, `wave64_tests` 30/30, `lds_subtracts` 2/2. These filters mix CPU and GPU assertions. - I reran the paired wide, strided, and sub-dword LDS fixture with adapter logging after each successful dispatch. DXGI resolved all seven hardware dispatches to the RTX 4090, and the fixture passed its output assertions. - Three older Wave64 tests returned early after the hardware rejected their pipeline. The same three returned early on unmodified `b8bd37111` under the same toolchain and driver. Synthetic inputs only. AI-assisted with OpenAI Codex (GPT-6.2 Sol).
merzleckeier commented 2026-11-30 00:50:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T00:50:51Z.


Independent Wave64 verification on Wine and RADV. I merged ab78da371 into main b8bd37111 (tree 9be32c2d3) and built the Win64 leonida-shader tests with --features host. Wine 11.22 and VKD3D-Proton 71cae091, RTX 4070 SUPER (620.73.04) and Radeon Raphael (RADV 26.3.1).

  • MEASURED The unmodified build reported 22 paired_tests and 30 wave64_tests passed on each GPU. The wave64_tests total includes three optional native-Wave64 hardware paths that logged not run on NVIDIA and one 32-lane-only flag-dispatch path skipped on RADV.
  • MEASURED I added three test-only log lines after successful readback and reran the six new focused tests on both devices. Each GPU completed 81 hardware readbacks and passed the unchanged assertions, covering post-loop append, fixed-lane broadcasts, parked scalar restoration, wide/sub-dword LDS, partial groups and the dead-half-wave loop exit. The adapters reported wave limits 32–32 and 32–64 respectively. Wine’s WARP-labeled entries still used the selected physical adapter.
  • MEASURED The full native Linux shader unit suite reported 224 passed and 2 ignored on both plain main and the merged tree.

Synthetic fixtures only. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7265627437 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2662#issuecomment-7265627437). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T00:50:51Z. --- Independent Wave64 verification on Wine and RADV. I merged `ab78da371` into `main` `b8bd37111` (tree `9be32c2d3`) and built the Win64 `leonida-shader` tests with `--features host`. Wine 11.22 and VKD3D-Proton `71cae091`, RTX 4070 SUPER (620.73.04) and Radeon Raphael (RADV 26.3.1). - **MEASURED** The unmodified build reported 22 `paired_tests` and 30 `wave64_tests` passed on each GPU. The `wave64_tests` total includes three optional native-Wave64 hardware paths that logged `not run` on NVIDIA and one 32-lane-only flag-dispatch path skipped on RADV. - **MEASURED** I added three test-only log lines after successful readback and reran the six new focused tests on both devices. Each GPU completed 81 hardware readbacks and passed the unchanged assertions, covering post-loop append, fixed-lane broadcasts, parked scalar restoration, wide/sub-dword LDS, partial groups and the dead-half-wave loop exit. The adapters reported wave limits 32–32 and 32–64 respectively. Wine’s WARP-labeled entries still used the selected physical adapter. - **MEASURED** The full native Linux shader unit suite reported 224 passed and 2 ignored on both plain `main` and the merged tree. Synthetic fixtures only. AI-assisted with OpenAI Codex.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2662
No description provided.