Shaders: lower ds_permute_b32 and ds_nop #2501

Manually merged
github merged 1 commit from github-bot/gh-2501 into main 2026-11-30 14:15:41 +00:00 AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-29 13:42 UTC · original
fuzeofficlal:fix/shader-ds-permute-b32 → main · +735 −2 · 3 files · 1 commits · merged 2026-11-30 14:12 UTC

Problem

gfx10::opcodes, gfx10::fields, and Lower::inst already recognize ds_permute_b32 (0x0b2) and ds_nop (0x014), whereas Lower::ds (crates/victory-shader/src/emit/lower.rs) only lowers ds_swizzle_b32 and ds_bpermute_b32 before requiring an LDS allocation and falling through to Err("an LDS operation"). Neither instruction accesses physical LDS memory.

Changes

  • ds_nop (0x014): Return Ok(()) as a no-op without requiring allocated LDS, and exclude ds_nop from has_memory_or_export_side_effect and split_lds barriers.
  • ds_permute_b32 (0x0b2): Lower forward cross-lane scatter within each 32-lane sub-vector (0..31, and 32..63 independently in Wave64 per RDNA2 ISA §10.4.4, matching self.s.wave_size == Some(64) so split Wave32 halves on NVIDIA also work without out-of-bounds lane reads) using waveActiveBallot and uniform-lane waveReadLaneAt: active lanes route vdata to ((vaddr + offset) >> 2) & 31 within their half, unwritten active destinations read 0, and inactive destination lanes retain their prior vdst value.
  • Add emit::ds_permute_tests covering Wave32 bijection/unaligned/offset/partial-EXEC/collision arbitration, Wave64 32-lane half isolation, zero-LDS ds_nop execution, and non-compute stage refusal.

Verification

  • cargo test --locked -p victory-shader --lib emit::ds_permute_tests:: -- --test-threads=1 — 4/4 pass on RTX 5090, AMD iGPU, WARP, and dxil.dll.
  • cargo test --locked -p victory-shader --lib emit::lds:: — all pass.
  • cargo clippy --locked -p victory-shader --all-targets — 0 new warnings in touched files.
  • cargo run --quiet --locked -p xtask -- policy --staged — pass.
**@⁠fuzeofficlal** on GitHub · 2026-11-29 13:42 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2501) `fuzeofficlal:fix/shader-ds-permute-b32` → `main` · +735 −2 · 3 files · 1 commits · merged 2026-11-30 14:12 UTC ### Problem `gfx10::opcodes`, `gfx10::fields`, and `Lower::inst` already recognize `ds_permute_b32` (`0x0b2`) and `ds_nop` (`0x014`), whereas `Lower::ds` (`crates/victory-shader/src/emit/lower.rs`) only lowers `ds_swizzle_b32` and `ds_bpermute_b32` before requiring an LDS allocation and falling through to `Err("an LDS operation")`. Neither instruction accesses physical LDS memory. ### Changes - **`ds_nop` (`0x014`)**: Return `Ok(())` as a no-op without requiring allocated LDS, and exclude `ds_nop` from `has_memory_or_export_side_effect` and `split_lds` barriers. - **`ds_permute_b32` (`0x0b2`)**: Lower forward cross-lane scatter within each 32-lane sub-vector (`0..31`, and `32..63` independently in Wave64 per RDNA2 ISA §10.4.4, matching `self.s.wave_size == Some(64)` so split Wave32 halves on NVIDIA also work without out-of-bounds lane reads) using `waveActiveBallot` and uniform-lane `waveReadLaneAt`: active lanes route `vdata` to `((vaddr + offset) >> 2) & 31` within their half, unwritten active destinations read `0`, and inactive destination lanes retain their prior `vdst` value. - Add `emit::ds_permute_tests` covering Wave32 bijection/unaligned/offset/partial-`EXEC`/collision arbitration, Wave64 32-lane half isolation, zero-LDS `ds_nop` execution, and non-compute stage refusal. ### Verification - `cargo test --locked -p victory-shader --lib emit::ds_permute_tests:: -- --test-threads=1` — 4/4 pass on RTX 5090, AMD iGPU, WARP, and `dxil.dll`. - `cargo test --locked -p victory-shader --lib emit::lds::` — all pass. - `cargo clippy --locked -p victory-shader --all-targets` — 0 new warnings in touched files. - `cargo run --quiet --locked -p xtask -- policy --staged` — pass.
Lower ds_permute_b32 via wave lane reads (preserving RDNA2 half-wave semantics on both native Wave64 and split Wave32 halves per ISA 10.4.4) and ds_nop as a no-op without requiring LDS allocation, with hardware/WARP/DXIL regression tests.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 13:44:41 +00:00
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-30 06:11 UTC · original

Checked ds_permute_b32 against a Radeon gfx1036 (RDNA 2) running the same kernels natively in wave32 and wave64, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12.

Setup

  • main at 41adb551 with this PR's head (63e4858) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory).
  • Kernels: hand-written GFX10 code, nothing from a title. A 64-thread group: two wave32 waves (o32) or one wave64 (o64). Data = 0x100 + 3 * lane; outputs v10.. stored per lane.
  • Hardware rows: the same machine code dispatched natively on the gfx1036.
  • Host runs: super::emit with a 32-lane host (5080 and Radeon) and with 64 host lanes (Radeon), and super::folded, each through dxil.dll validation (VICTORY_ADAPTER=radeon for the Radeon).

Cases (one ds_permute_b32 per output)

out address (bytes) gfx1036
v10 4 * ((lane + 1) & 63) lane 0 gets lane 31's data, lane 32 gets lane 63's: each 32-lane half on its own
v11 4 * (63 - lane) reversal inside each half
v12 the same with offset:4 one lane further
v13 20 for every lane lane 5 gets lane 31's data, lane 37 gets lane 63's (the highest lane wins); every other lane 0
v14 / v15 4 * lane + 0x100, 4 * lane - 4 the index wraps inside the half
v16 4 * lane + 3 the low two bits are dropped
v17 4 * (lane + 32) the same as 4 * lane
v18 / v19 rotate / reverse under a partial EXEC from SGPR constants (lo 0x5555F00F, hi 0x0FF0AAA5) lanes outside EXEC keep their old value; active lanes nobody writes read 0
v20 / v21 4 * (lane ^ 1), 4 * (lane ^ 33) the same values
v22 / v23 ds_bpermute_b32 of lane ^ 33 and of the reversal (control)
v24 after a ds_nop (control)

permute_cmpx (wave64): rotate, reverse, lane ^ 33 and "all to lane 5" under a partial EXEC made by v_cmpx_ne_u32 from a per-lane table (the same 64-bit mask), with v18..v21 preset to 0xDEAD0000 | lane.

Results (values that differ from the gfx1036)

case host main this PR
o32/permute (15 outputs) 5080, Radeon (32 lanes) refused (an LDS address) exact, both cards
o64/permute (15 outputs) 5080, Radeon (32 lanes) refused (an LDS address) 44 values differ, all in v18 / v19 (the SGPR-constant EXEC), lanes 33..62
o64/permute Radeon, 64 host lanes refused (LDS the program does not allocate) the same 44 values
o64/permute_cmpx (4 outputs) 5080, Radeon (32 lanes), Radeon (64 lanes) refused exact, all three
o64/permute, permute_cmpx super::folded, both cards refused (ds_(b)permute, fold.rs) refused, the same

The 64-bit forms translate for a 32-lane host as wave 32 (PSV0) and for the 64-lane Radeon as wave 64.

Verdict: the lowering matches the gfx1036. Every output of every case is exact in wave32 and in wave64, on both cards and both host widths, wherever EXEC comes from compares: the per-half routing, the highest lane winning a collision, 0 in active lanes nobody writes, the old value in lanes outside EXEC, the dropped low address bits, offset:, and ds_nop.

The 44 values in o64/permute are not the permute's. There EXEC is s_mov_b64 exec, s[30:31] from two s_mov_b32 constants (lo 0x5555F00F, hi 0x0FF0AAA5), and in the upper half the translation's active lanes follow the low dword's bits instead of the high dword's. Lane 33 (outside EXEC) is written with 0x160 where the gfx1036 keeps 0xDEAD0021; lane 53 (inside) keeps 0xDEAD0035 where the hardware writes 0x19C; the other differing lanes get their data from a sender whose EXEC bit is taken the same wrong way. With that mask the results are exactly what the low dword predicts. The same happens without any LDS instruction on main: s_mov_b64 exec, s[30:31] with s31 = 0, then a v_mov_b32 v41, 7, writes 7 into lanes 32..63 with super::emit on both cards and with 64 host lanes (the gfx1036 leaves them 0). So does an s_andn2_saveexec_b64 of S0 = (lo 0xFFFF0000, hi 0x0000FFFF) with EXEC on lanes 0..31: the new EXEC's high dword comes out 0xFFFF0000 (from S0's low dword), the gfx1036 0x0000FFFF; the folded form is exact there. Compilers build EXEC from compares, as permute_cmpx does, and that form is exact here.

Not covered: the folded form still refuses ds_permute_b32 (and ds_bpermute_b32) in fold.rs; this PR does not touch it.

(The upper-half value from EXEC built out of two s_mov_b32 constants looks like what #3402 fixes: "a lane mask written a dword at a time keeps its high dword".)

AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.

**@⁠Lyrnc** on GitHub · 2026-11-30 06:11 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2501#issuecomment-7270395057) Checked `ds_permute_b32` against a Radeon gfx1036 (RDNA 2) running the same kernels natively in wave32 and wave64, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12. **Setup** - `main` at 41adb551 with this PR's head (63e4858) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory). - Kernels: hand-written GFX10 code, nothing from a title. A 64-thread group: two wave32 waves (`o32`) or one wave64 (`o64`). Data = 0x100 + 3 * lane; outputs v10.. stored per lane. - Hardware rows: the same machine code dispatched natively on the gfx1036. - Host runs: `super::emit` with a 32-lane host (5080 and Radeon) and with 64 host lanes (Radeon), and `super::folded`, each through `dxil.dll` validation (`VICTORY_ADAPTER=radeon` for the Radeon). **Cases** (one `ds_permute_b32` per output) | out | address (bytes) | gfx1036 | |---|---|---| | v10 | 4 * ((lane + 1) & 63) | lane 0 gets lane 31's data, lane 32 gets lane 63's: each 32-lane half on its own | | v11 | 4 * (63 - lane) | reversal inside each half | | v12 | the same with `offset:4` | one lane further | | v13 | 20 for every lane | lane 5 gets lane 31's data, lane 37 gets lane 63's (the highest lane wins); every other lane 0 | | v14 / v15 | 4 * lane + 0x100, 4 * lane - 4 | the index wraps inside the half | | v16 | 4 * lane + 3 | the low two bits are dropped | | v17 | 4 * (lane + 32) | the same as 4 * lane | | v18 / v19 | rotate / reverse under a partial EXEC from SGPR constants (lo 0x5555F00F, hi 0x0FF0AAA5) | lanes outside EXEC keep their old value; active lanes nobody writes read 0 | | v20 / v21 | 4 * (lane ^ 1), 4 * (lane ^ 33) | the same values | | v22 / v23 | `ds_bpermute_b32` of lane ^ 33 and of the reversal (control) | | | v24 | after a `ds_nop` (control) | | `permute_cmpx` (wave64): rotate, reverse, lane ^ 33 and "all to lane 5" under a partial EXEC made by `v_cmpx_ne_u32` from a per-lane table (the same 64-bit mask), with v18..v21 preset to 0xDEAD0000 | lane. **Results** (values that differ from the gfx1036) | case | host | main | this PR | |---|---|---|---| | `o32/permute` (15 outputs) | 5080, Radeon (32 lanes) | refused (`an LDS address`) | exact, both cards | | `o64/permute` (15 outputs) | 5080, Radeon (32 lanes) | refused (`an LDS address`) | 44 values differ, all in v18 / v19 (the SGPR-constant EXEC), lanes 33..62 | | `o64/permute` | Radeon, 64 host lanes | refused (`LDS the program does not allocate`) | the same 44 values | | `o64/permute_cmpx` (4 outputs) | 5080, Radeon (32 lanes), Radeon (64 lanes) | refused | exact, all three | | `o64/permute`, `permute_cmpx` | `super::folded`, both cards | refused (`ds_(b)permute`, fold.rs) | refused, the same | The 64-bit forms translate for a 32-lane host as wave 32 (PSV0) and for the 64-lane Radeon as wave 64. **Verdict: the lowering matches the gfx1036.** Every output of every case is exact in wave32 and in wave64, on both cards and both host widths, wherever EXEC comes from compares: the per-half routing, the highest lane winning a collision, 0 in active lanes nobody writes, the old value in lanes outside EXEC, the dropped low address bits, `offset:`, and `ds_nop`. The 44 values in `o64/permute` are not the permute's. There EXEC is `s_mov_b64 exec, s[30:31]` from two `s_mov_b32` constants (lo 0x5555F00F, hi 0x0FF0AAA5), and in the upper half the translation's active lanes follow the low dword's bits instead of the high dword's. Lane 33 (outside EXEC) is written with 0x160 where the gfx1036 keeps 0xDEAD0021; lane 53 (inside) keeps 0xDEAD0035 where the hardware writes 0x19C; the other differing lanes get their data from a sender whose EXEC bit is taken the same wrong way. With that mask the results are exactly what the low dword predicts. The same happens without any LDS instruction on main: `s_mov_b64 exec, s[30:31]` with s31 = 0, then a `v_mov_b32 v41, 7`, writes 7 into lanes 32..63 with `super::emit` on both cards and with 64 host lanes (the gfx1036 leaves them 0). So does an `s_andn2_saveexec_b64` of S0 = (lo 0xFFFF0000, hi 0x0000FFFF) with EXEC on lanes 0..31: the new EXEC's high dword comes out 0xFFFF0000 (from S0's low dword), the gfx1036 0x0000FFFF; the folded form is exact there. Compilers build EXEC from compares, as `permute_cmpx` does, and that form is exact here. Not covered: the folded form still refuses `ds_permute_b32` (and `ds_bpermute_b32`) in `fold.rs`; this PR does not touch it. (The upper-half value from EXEC built out of two `s_mov_b32` constants looks like what #3402 fixes: "a lane mask written a dword at a time keeps its high dword".) AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.
github manually merged commit 7e587ff643 into main 2026-11-30 14:15:41 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!2501
No description provided.