Shaders: v_writelane_b32 obeys EXEC guard and v_writelane_b32/v_readlane_b32 do not mask lane selectors (fix on a fork) #1044

Open
opened 2026-11-26 04:18:09 +00:00 by github-bot · 6 comments
Owner

@⁠AscilCH on GitHub · 2026-11-24 15:15 UTC · original

Pull requests are currently restricted to collaborators, so I am sharing this bug report and verified fix from a clean fork branch off main (ffe310e).

Bug

On RDNA2 (GFX10.3), v_writelane_b32 (VOP3 0x361) and v_readlane_b32 (VOP3 0x360) both ignore the EXEC mask and wrap their src1 lane selector modulo the wavefront size (src1[4:0] / & 31 in Wave32, src1[5:0] / & 63 in Wave64).

Two issues existed on main (ffe310e):

  1. EXEC guarding drops v_writelane_b32 writes to inactive lanes:

    • crates/victory-shader/src/ir.rs:931-935 unconditionally adds implicit EXEC reads (r.extend(mask(EXEC))) for all vector instructions, including v_readlane_b32 and v_writelane_b32.
    • crates/victory-shader/src/structure.rs:599-606 attaches an EXEC guard to v_writelane_b32 whenever EXEC is narrowed without a branch (including EXEC == 0, or when exec_lo is itself passed explicitly as src0 to spill EXEC into a VGPR lane).
    • crates/victory-shader/src/emit/lower.rs:4217-4232 writes v_writelane_b32's result via int(self, r) (which forwards guard to write_i), wrapping the write in select(guard, r, old) so writes targeting inactive lanes are silently discarded.
  2. Unmasked lane selectors:

    • crates/victory-shader/src/emit/lower.rs:4205-4232 passes raw src1 directly into read_lane(a, lane) (dx.op.waveReadLaneAt) and cmp(EQ, me, lane) without masking to wave_size - 1. Any src1 >= wave_size (including negative or wrapped values such as inline 32 or -1, or computed SGPR indices) either drops v_writelane_b32 writes or passes an out-of-range lane index to WaveReadLaneAt.

Reproduction (Before Fix)

With synthetic Wave32 RDNA2 compute shaders lowered to DXIL and executed on hardware (NVIDIA GeForce RTX 3050 6GB Laptop GPU, driver 32.0.16.1742, Windows 11, Windows SDK dxil.dll 10.0.26100.0):

  • Narrowed EXEC (lanes 16..31 active), v_writelane_b32 v1, 0xcafebabe, 0:
    lane 0 stayed 0x00001000 instead of 0xcafebabe (while control v_add_nc_u32 correctly obeyed EXEC on lanes 16..31).
  • Constant wrapped selectors (v_writelane_b32 with inline 32 -> lane 0 and -1 -> lane 31):
    lanes 0 and 31 stayed 0x00004000 and 0x0000401f instead of receiving s0 (0x13579bdf) and s1 (0x2468ace0).

Fix

  • crates/victory-shader/src/ir.rs: Skip implicit r.extend(mask(EXEC)) for v_readlane_b32 and v_writelane_b32 while keeping the destination VGPR read for untouched lanes and preserving explicit exec_lo source operands.
  • crates/victory-shader/src/structure.rs: Keep v_readlane_b32 and v_writelane_b32 unguarded (None) even if exec_lo appears as an explicit operand.
  • crates/victory-shader/src/emit/lower.rs:
    • Added Lower::mask_lane() to mask console lane selectors by self.wave() - 1 (0x1F in Wave32, 0x3F in Wave64), folding constants so DXIL never receives out-of-range lane constants (and emitting zero extra ops for in-range constants).
    • Applied mask_lane() to v_readlane_b32 (on the console-lane path, leaving dynamic MaskData::Host host-lane reads untouched) and v_writelane_b32.
    • Updated v_writelane_b32 to write via self.write_i(inst, r, None).
  • Added synthetic regression tests across four modules:
    • ir::tests::readlane_and_writelane_do_not_read_exec_implicitly (Wave32 & Wave64)
    • structure::tests::readlane_and_writelane_are_unguarded_under_narrowed_exec
    • emit::lower::tests::writelane_and_readlane_lower_and_validate_in_wave32_and_wave64 (Wave32 & Wave64 + dxil.dll validation)
    • emit::valu_tests::v_writelane_b32_ignores_exec_and_masks_lane (narrowed EXEC + control v_add_nc_u32, EXEC == 0, wrapped dynamic selectors 32/37/u32::MAX/0x1000_001F, and explicit exec_lo spill to inactive lane 0)
    • emit::valu_tests::v_readlane_and_writelane_wrap_constant_lane_selector (inline 32 and -1 on both v_writelane_b32 and v_readlane_b32)
    • valu_tests.rs enumerates SM 6.6 adapters supporting WaveSize(32), so hardware execution runs on capable GPUs while WARP-only CI (wave size 4) still runs full dxil.dll validation without failing on CreateComputePipelineState.

Verification

Tested on Windows 11, NVIDIA GeForce RTX 3050 6GB Laptop GPU (driver 32.0.16.1742), WARP, and Windows SDK dxil.dll (10.0.26100.0):

  • rustfmt --edition 2024 --check on ir.rs, valu_tests.rs, and modified lines of structure.rs/lower.rs: clean
  • cargo xtask policy: 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test -p victory-shader --locked -- v_writelane_b32_ignores_exec_and_masks_lane v_readlane_and_writelane_wrap_constant_lane_selector --nocapture --test-threads=1: 2 passed (RTX 3050 + dxil.dll)
  • VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1: 125 passed, 0 failed, 1 ignored
  • cargo test --workspace --locked -- --test-threads=1 --skip v_med3_f32_matches_rdna2_with_nan: 346 passed, 0 failed, 6 ignored (v_med3_f32_matches_rdna2_with_nan fails pre-existing on Adapter 0 Intel UHD Graphics and passes under VICTORY_WARP=1)
**@⁠AscilCH** on GitHub · 2026-11-24 15:15 UTC · [original](https://github.com/Marvisak/VIctory/issues/1044) Pull requests are currently restricted to collaborators, so I am sharing this bug report and verified fix from a clean fork branch off `main` (`ffe310e`). - **Branch:** https://github.com/AscilCH/VIctory/tree/fix/writelane-readlane-exec-and-lane-mask - **Commit:** https://github.com/AscilCH/VIctory/commit/79f162d899b984a5fe74d0dfd00aae388d4237ce ### Bug On RDNA2 (GFX10.3), `v_writelane_b32` (`VOP3 0x361`) and `v_readlane_b32` (`VOP3 0x360`) both **ignore the `EXEC` mask** and **wrap their `src1` lane selector modulo the wavefront size** (`src1[4:0]` / `& 31` in Wave32, `src1[5:0]` / `& 63` in Wave64). Two issues existed on `main` (`ffe310e`): 1. **EXEC guarding drops `v_writelane_b32` writes to inactive lanes:** - `crates/victory-shader/src/ir.rs:931-935` unconditionally adds implicit `EXEC` reads (`r.extend(mask(EXEC))`) for all vector instructions, including `v_readlane_b32` and `v_writelane_b32`. - `crates/victory-shader/src/structure.rs:599-606` attaches an `EXEC` guard to `v_writelane_b32` whenever `EXEC` is narrowed without a branch (including `EXEC == 0`, or when `exec_lo` is itself passed explicitly as `src0` to spill `EXEC` into a VGPR lane). - `crates/victory-shader/src/emit/lower.rs:4217-4232` writes `v_writelane_b32`'s result via `int(self, r)` (which forwards `guard` to `write_i`), wrapping the write in `select(guard, r, old)` so writes targeting inactive lanes are silently discarded. 2. **Unmasked lane selectors:** - `crates/victory-shader/src/emit/lower.rs:4205-4232` passes raw `src1` directly into `read_lane(a, lane)` (`dx.op.waveReadLaneAt`) and `cmp(EQ, me, lane)` without masking to `wave_size - 1`. Any `src1 >= wave_size` (including negative or wrapped values such as inline `32` or `-1`, or computed SGPR indices) either drops `v_writelane_b32` writes or passes an out-of-range lane index to `WaveReadLaneAt`. ### Reproduction (Before Fix) With synthetic Wave32 RDNA2 compute shaders lowered to DXIL and executed on hardware (NVIDIA GeForce RTX 3050 6GB Laptop GPU, driver `32.0.16.1742`, Windows 11, Windows SDK `dxil.dll` `10.0.26100.0`): - Narrowed `EXEC` (lanes `16..31` active), `v_writelane_b32 v1, 0xcafebabe, 0`: lane `0` stayed `0x00001000` instead of `0xcafebabe` (while control `v_add_nc_u32` correctly obeyed `EXEC` on lanes `16..31`). - Constant wrapped selectors (`v_writelane_b32` with inline `32` -> lane `0` and `-1` -> lane `31`): lanes `0` and `31` stayed `0x00004000` and `0x0000401f` instead of receiving `s0` (`0x13579bdf`) and `s1` (`0x2468ace0`). ### Fix - `crates/victory-shader/src/ir.rs`: Skip implicit `r.extend(mask(EXEC))` for `v_readlane_b32` and `v_writelane_b32` while keeping the destination VGPR read for untouched lanes and preserving explicit `exec_lo` source operands. - `crates/victory-shader/src/structure.rs`: Keep `v_readlane_b32` and `v_writelane_b32` unguarded (`None`) even if `exec_lo` appears as an explicit operand. - `crates/victory-shader/src/emit/lower.rs`: - Added `Lower::mask_lane()` to mask console lane selectors by `self.wave() - 1` (`0x1F` in Wave32, `0x3F` in Wave64), folding constants so DXIL never receives out-of-range lane constants (and emitting zero extra ops for in-range constants). - Applied `mask_lane()` to `v_readlane_b32` (on the console-lane path, leaving dynamic `MaskData::Host` host-lane reads untouched) and `v_writelane_b32`. - Updated `v_writelane_b32` to write via `self.write_i(inst, r, None)`. - Added synthetic regression tests across four modules: - `ir::tests::readlane_and_writelane_do_not_read_exec_implicitly` (Wave32 & Wave64) - `structure::tests::readlane_and_writelane_are_unguarded_under_narrowed_exec` - `emit::lower::tests::writelane_and_readlane_lower_and_validate_in_wave32_and_wave64` (Wave32 & Wave64 + `dxil.dll` validation) - `emit::valu_tests::v_writelane_b32_ignores_exec_and_masks_lane` (narrowed `EXEC` + control `v_add_nc_u32`, `EXEC == 0`, wrapped dynamic selectors `32`/`37`/`u32::MAX`/`0x1000_001F`, and explicit `exec_lo` spill to inactive lane `0`) - `emit::valu_tests::v_readlane_and_writelane_wrap_constant_lane_selector` (inline `32` and `-1` on both `v_writelane_b32` and `v_readlane_b32`) - `valu_tests.rs` enumerates SM 6.6 adapters supporting `WaveSize(32)`, so hardware execution runs on capable GPUs while WARP-only CI (wave size 4) still runs full `dxil.dll` validation without failing on `CreateComputePipelineState`. ### Verification Tested on Windows 11, NVIDIA GeForce RTX 3050 6GB Laptop GPU (driver `32.0.16.1742`), WARP, and Windows SDK `dxil.dll` (`10.0.26100.0`): - `rustfmt --edition 2024 --check` on `ir.rs`, `valu_tests.rs`, and modified lines of `structure.rs`/`lower.rs`: clean - `cargo xtask policy`: `154 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test -p victory-shader --locked -- v_writelane_b32_ignores_exec_and_masks_lane v_readlane_and_writelane_wrap_constant_lane_selector --nocapture --test-threads=1`: **2 passed** (RTX 3050 + `dxil.dll`) - `VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1`: **125 passed, 0 failed, 1 ignored** - `cargo test --workspace --locked -- --test-threads=1 --skip v_med3_f32_matches_rdna2_with_nan`: **346 passed, 0 failed, 6 ignored** (`v_med3_f32_matches_rdna2_with_nan` fails pre-existing on Adapter 0 Intel UHD Graphics and passes under `VICTORY_WARP=1`)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:18:10 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 15:17 UTC · original

Divergence report from the sibling runtime (ivall/xcl) — our emitter encodes the opposite assumptions on both axes of your finding, so your test matrix settles a second project, for free:\n\n- EXEC: our v_writelane_b32 emits an ungated if (lane_id == (b & wmask)) v = a; with an inline comment asserting "ignores EXEC" (emit.rs:3534 area) — if your rows show EXEC-gated writes on gfx103x, that comment is a load-bearing wrong belief.\n- Lane masking: we mask the selector — b & wmask (31/63) on both readlane and writelane — so lane 33 on a 32-lane wave wraps to lane 1; you claim the hardware does not mask. Those two behaviors differ observably exactly in your unmasks-the-selector rows: a selector with high bits set must hit the wrap on our side and (per you) nothing or lane-clamped on hardware.\n\nIf your harness (GPU + WARP rows) is still taking shape, two rows would close both questions: ① writer lane EXEC-off, selector = a lane whose EXEC is on, read back the target — does it change?; ② selector = wave_size + k with distinct low/high half contents — wrap, no-op, or clamp? We have no gfx103x box here, so we would adopt your measured values wholesale with attribution, same as we did for the 16-bit-compare op_sel quirk (#926 → our #1293). Posting before you land the fix so both projects converge on one measured table rather than two guessed ones.

**@⁠pickles976** on GitHub · 2026-11-24 15:17 UTC · [original](https://github.com/Marvisak/VIctory/issues/1044#issuecomment-7135787878) Divergence report from the sibling runtime (ivall/xcl) — our emitter encodes the **opposite assumptions on both axes** of your finding, so your test matrix settles a second project, for free:\n\n- **EXEC:** our `v_writelane_b32` emits an ungated `if (lane_id == (b & wmask)) v = a;` with an inline comment asserting "ignores EXEC" (`emit.rs:3534` area) — if your rows show EXEC-gated writes on gfx103x, that comment is a load-bearing wrong belief.\n- **Lane masking:** we **mask** the selector — `b & wmask` (31/63) on both readlane and writelane — so lane 33 on a 32-lane wave wraps to lane 1; you claim the hardware does **not** mask. Those two behaviors differ observably exactly in your unmasks-the-selector rows: a selector with high bits set must hit the wrap on our side and (per you) nothing or lane-clamped on hardware.\n\nIf your harness (GPU + WARP rows) is still taking shape, two rows would close both questions: ① writer lane EXEC-off, selector = a lane whose EXEC is on, read back the target — does it change?; ② selector = `wave_size + k` with distinct low/high half contents — wrap, no-op, or clamp? We have no gfx103x box here, so we would adopt your measured values wholesale with attribution, same as we did for the 16-bit-compare `op_sel` quirk (#926 → our #1293). Posting before you land the fix so both projects converge on one measured table rather than two guessed ones.
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-24 15:22 UTC · original

I compared 79f162d with #320 at cbcef55 using two synthetic Wave32 shaders. Both narrow EXEC to lanes 16–31 and write 0x12345678 to inactive lane 0. The second inserts s_cbranch_execz before the write. Since lanes 16–31 are still active, the guest wave should reach the write.

Lane 0 readback #1044 (79f162d) #320 (cbcef55)
Straight-line write 0x12345678 0x00001000
Write after s_cbranch_execz 0x00001000 0x12345678

The active-lane control wrote the expected value on both branches. RTX 4070 Ti and Radeon iGPU agreed. This gives two cases to retain when integrating the fixes. The test ran translated DXIL on native Windows.

AI-assisted with Codex.

**@⁠DrDarkDK** on GitHub · 2026-11-24 15:22 UTC · [original](https://github.com/Marvisak/VIctory/issues/1044#issuecomment-7135911414) I compared `79f162d` with #320 at `cbcef55` using two synthetic Wave32 shaders. Both narrow EXEC to lanes 16–31 and write `0x12345678` to inactive lane 0. The second inserts `s_cbranch_execz` before the write. Since lanes 16–31 are still active, the guest wave should reach the write. | Lane 0 readback | #1044 (`79f162d`) | #320 (`cbcef55`) | | --- | --- | --- | | Straight-line write | `0x12345678` | `0x00001000` | | Write after `s_cbranch_execz` | `0x00001000` | `0x12345678` | The active-lane control wrote the expected value on both branches. RTX 4070 Ti and Radeon iGPU agreed. This gives two cases to retain when integrating the fixes. The test ran translated DXIL on native Windows. *AI-assisted with Codex.*
Author
Owner

@⁠AscilCH on GitHub · 2026-11-24 15:25 UTC · original

@⁠pickles976 Quick clarification — the issue title describes VIctory's current bug on main, not RDNA2 hardware behavior. Our fix (79f162d) agrees with ivall/xcl on both axes per the GFX10.3 ISA spec:

  • v_writelane_b32 and v_readlane_b32 ignore EXEC.
  • Both mask src1 modulo wavefront size (src1 & 31 in Wave32, src1 & 63 in Wave64).

(Note also that our tests run translated DXIL on D3D12 hosts, not bare-metal RDNA2 hardware, so they verify the translator matches the GFX10.3 ISA rather than probing silicon directly.)

@⁠DrDarkDK Thanks for testing 79f162d side-by-side with #320 on RTX 4070 Ti and Radeon iGPU! That table pins down how the two changes complement each other:

  • Straight-line narrowed EXEC (no divergent branch): stays in per-lane mode, where #320 does not activate CONSOLE_FLOW and still guards v_writelane_b32; #1044 fixes that path as well as lane-selector masking (mask_lane) in per-lane lowering.
  • After s_cbranch_execz (divergent branch): per-lane DXIL control flow skips the branch body on inactive lane 0 altogether, which is what #320's wave-uniform CONSOLE_FLOW solves.

Agree both cases are worth keeping when integrating the two fixes.

**@⁠AscilCH** on GitHub · 2026-11-24 15:25 UTC · [original](https://github.com/Marvisak/VIctory/issues/1044#issuecomment-7136005216) @⁠pickles976 Quick clarification — the issue title describes **VIctory's current bug on `main`**, not RDNA2 hardware behavior. Our fix (`79f162d`) agrees with `ivall/xcl` on both axes per the GFX10.3 ISA spec: - `v_writelane_b32` and `v_readlane_b32` **ignore `EXEC`**. - Both **mask `src1` modulo wavefront size** (`src1 & 31` in Wave32, `src1 & 63` in Wave64). (Note also that our tests run translated DXIL on D3D12 hosts, not bare-metal RDNA2 hardware, so they verify the translator matches the GFX10.3 ISA rather than probing silicon directly.) @⁠DrDarkDK Thanks for testing `79f162d` side-by-side with `#320` on RTX 4070 Ti and Radeon iGPU! That table pins down how the two changes complement each other: - **Straight-line narrowed `EXEC`** (no divergent branch): stays in per-lane mode, where `#320` does not activate `CONSOLE_FLOW` and still guards `v_writelane_b32`; `#1044` fixes that path as well as lane-selector masking (`mask_lane`) in per-lane lowering. - **After `s_cbranch_execz`** (divergent branch): per-lane DXIL control flow skips the branch body on inactive lane 0 altogether, which is what `#320`'s wave-uniform `CONSOLE_FLOW` solves. Agree both cases are worth keeping when integrating the two fixes.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:57 UTC · original · edited 2026-11-25 09:58 UTC

Automated triage for the maintainers.

Confirmed on main. In crates/victory-shader/src/emit/lower.rs, the v_writelane_b32 arm compares lane_index() with the raw src1 and writes through int(self, r), which applies the instruction's EXEC guard. The v_readlane_b32 arm passes the raw src1 to read_lane. Neither masks the selector to the wave size, and the lane write doesn't bypass EXEC as the ISA says. Open PRs #1312 ("mask v_readlane/v_writelane lane index and bypass EXEC on writelane") and #1336 (which includes readlane/writelane semantics) carry this, so review can continue there.

One caution from DrDarkDK's comparison: with s_cbranch_execz before the write, 79f162d and #320 (lane reads and writes in the console flow) give opposite results. A fix should cover both cases.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:57 UTC · [original](https://github.com/Marvisak/VIctory/issues/1044#issuecomment-7155341606) · edited 2026-11-25 09:58 UTC <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-25T09:38:01Z at=2026-11-25T09:50:38Z --> Automated triage for the maintainers. Confirmed on `main`. In `crates/victory-shader/src/emit/lower.rs`, the `v_writelane_b32` arm compares `lane_index()` with the raw `src1` and writes through `int(self, r)`, which applies the instruction's EXEC guard. The `v_readlane_b32` arm passes the raw `src1` to `read_lane`. Neither masks the selector to the wave size, and the lane write doesn't bypass EXEC as the ISA says. Open PRs #1312 ("mask v_readlane/v_writelane lane index and bypass EXEC on writelane") and #1336 (which includes readlane/writelane semantics) carry this, so review can continue there. One caution from DrDarkDK's comparison: with `s_cbranch_execz` before the write, `79f162d` and #320 (lane reads and writes in the console flow) give opposite results. A fix should cover both cases. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠StExpeditus on GitHub · 2026-11-25 09:35 UTC · original

Validated the fork branch on Windows 11 Pro 26200 (Intel Core i5-14600K, RTX 5060 Ti, driver 621.24, Rust 1.98.1 MSVC), commit 79f162d: cargo test -p victory-shader --locked --no-fail-fast -> 125 passed, 0 failed, 1 ignored. Synthetic tests only, no game files.

**@⁠StExpeditus** on GitHub · 2026-11-25 09:35 UTC · [original](https://github.com/Marvisak/VIctory/issues/1044#issuecomment-7158538857) Validated the fork branch on Windows 11 Pro 26200 (Intel Core i5-14600K, RTX 5060 Ti, driver 621.24, Rust 1.98.1 MSVC), commit `79f162d`: `cargo test -p victory-shader --locked --no-fail-fast` -> **125 passed, 0 failed, 1 ignored**. Synthetic tests only, no game files.
Author
Owner

@⁠StExpeditus on GitHub · 2026-11-25 09:38 UTC · original

Addendum: this branch is far behind current main (the victory-shader delta is 33 files, ~16.4k lines), so the run above is the branch as-is on its old base; it needs a rebase before a clean cherry-pick.

**@⁠StExpeditus** on GitHub · 2026-11-25 09:38 UTC · [original](https://github.com/Marvisak/VIctory/issues/1044#issuecomment-7158593827) Addendum: this branch is far behind current main (the `victory-shader` delta is 33 files, ~16.4k lines), so the run above is the branch as-is on its old base; it needs a rebase before a clean cherry-pick.
This discussion has been locked. Commenting is limited to contributors.
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/Marvisak--VIctory#1044
No description provided.