rdna2spirv: mask v_mbcnt_lo/hi shift count to < 32 to avoid SPIR-V OpShiftLeftLogical UB (SHADER-MBCNT-SHIFT-IN-RANGE) #2121

Merged
ShotTechOp merged 1 commit from upstream-pr/2121 into main 2026-11-28 17:59:04 +00:00
ShotTechOp commented 2026-11-25 21:33:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ShotTechOp on 2026-11-25T21:33:09Z.


Claimed in #1608 as SHADER-MBCNT-SHIFT-IN-RANGE.

What and why

  • In tools/rdna2spirv.py (v_mbcnt_lo_u32_b32, v_mbcnt_hi_u32_b32), the lowering emitted:
    %inrange = OpULessThan %bool %rel %c32
    %one     = OpShiftLeftLogical %uint %c1 %rel
    %m1      = OpISub %uint %one %c1
    %big     = OpSelect %uint %inrange %m1 ...
    
  • Under SPIR-V §3.42.10 (OpShiftLeftLogical), the result is undefined whenever Shift >= 32, even when the shifted value sits on the unselected side of OpSelect (the same SPIR-V rule tools/rdna2spirv.py already guards against in v_perm_b32, v_alignbit_b32, v_alignbyte_b32, fmt_dec_component, and buffer_load_format).
    • For v_mbcnt_lo_u32_b32 in wave64, lanes 32..63 had rel = 32..63 >= 32.
    • For v_mbcnt_hi_u32_b32 in both wave32 and wave64, lanes 0..31 had rel = lane - 32 = 0xFFFFFFE0..0xFFFFFFFF >= 32.
    • On spirv_wave_interp, this produced _Poison("OpShiftLeftLogical by ... >= 32") in 32 lanes of every wave.
  • Masking rel with 31 (OpBitwiseAnd %uint %rel %c31) before OpShiftLeftLogical is an identity whenever inrange is true (0 <= rel <= 31) and keeps every OpShiftLeftLogical shift operand strictly < 32 on all lanes.

Testing

  • AI-assisted: yes.
  • Environment: Windows 11, Python 3.12 (no GPU / LLVM / SPIRV-Tools installed locally).
  • New regression suite: tests/semantic/test_mbcnt_shift_defined.py — executes translated v_mbcnt_lo_u32_b32 and v_mbcnt_hi_u32_b32 in wave32 and wave64 (plain scalar masks and lane-mask vcc, plus paired wave64 thread-rank) on spirv_wave_interp._Wave and asserts both exact per-lane outputs and zero _Poison values across all lanes.
  • RED control on origin/main: tests/semantic/test_mbcnt_shift_defined.py fails on origin/main with FAILED (failures=4) (_Poison('OpShiftLeftLogical by 32 >= 32') / _Poison('OpShiftLeftLogical by 4294967264 >= 32') across 32 lanes per wave).
  • GREEN checks on branch:
    • python -I tests/semantic/test_mbcnt_shift_defined.py -v (3/3 OK)
    • python -m unittest discover -s tests/semantic -p test_mbcnt_lo_hi.py (28/28 OK)
    • python -I tests/test_text_write_line_endings.py (3/3 OK)
    • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py
    • python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py
    • git diff --check
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5499536984 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2121). Originally posted by [ShotTechOp](https://github.com/ShotTechOp) on 2026-11-25T21:33:09Z. --- Claimed in #1608 as `SHADER-MBCNT-SHIFT-IN-RANGE`. ## What and why - In `tools/rdna2spirv.py` (`v_mbcnt_lo_u32_b32`, `v_mbcnt_hi_u32_b32`), the lowering emitted: ```spirv %inrange = OpULessThan %bool %rel %c32 %one = OpShiftLeftLogical %uint %c1 %rel %m1 = OpISub %uint %one %c1 %big = OpSelect %uint %inrange %m1 ... ``` - Under SPIR-V §3.42.10 (`OpShiftLeftLogical`), the result is undefined whenever `Shift >= 32`, even when the shifted value sits on the unselected side of `OpSelect` (the same SPIR-V rule `tools/rdna2spirv.py` already guards against in `v_perm_b32`, `v_alignbit_b32`, `v_alignbyte_b32`, `fmt_dec_component`, and `buffer_load_format`). - For `v_mbcnt_lo_u32_b32` in wave64, lanes `32..63` had `rel = 32..63 >= 32`. - For `v_mbcnt_hi_u32_b32` in both wave32 and wave64, lanes `0..31` had `rel = lane - 32 = 0xFFFFFFE0..0xFFFFFFFF >= 32`. - On `spirv_wave_interp`, this produced `_Poison("OpShiftLeftLogical by ... >= 32")` in 32 lanes of every wave. - Masking `rel` with `31` (`OpBitwiseAnd %uint %rel %c31`) before `OpShiftLeftLogical` is an identity whenever `inrange` is true (`0 <= rel <= 31`) and keeps every `OpShiftLeftLogical` shift operand strictly `< 32` on all lanes. ## Testing - **AI-assisted**: yes. - **Environment**: Windows 11, Python 3.12 (no GPU / LLVM / SPIRV-Tools installed locally). - **New regression suite**: `tests/semantic/test_mbcnt_shift_defined.py` — executes translated `v_mbcnt_lo_u32_b32` and `v_mbcnt_hi_u32_b32` in wave32 and wave64 (plain scalar masks and lane-mask `vcc`, plus paired wave64 thread-rank) on `spirv_wave_interp._Wave` and asserts both exact per-lane outputs and zero `_Poison` values across all lanes. - **RED control on `origin/main`**: `tests/semantic/test_mbcnt_shift_defined.py` fails on `origin/main` with `FAILED (failures=4)` (`_Poison('OpShiftLeftLogical by 32 >= 32')` / `_Poison('OpShiftLeftLogical by 4294967264 >= 32')` across 32 lanes per wave). - **GREEN checks on branch**: - `python -I tests/semantic/test_mbcnt_shift_defined.py -v` (3/3 OK) - `python -m unittest discover -s tests/semantic -p test_mbcnt_lo_hi.py` (28/28 OK) - `python -I tests/test_text_write_line_endings.py` (3/3 OK) - `python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py` - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.py` - `git diff --check`
ecox982745 commented 2026-11-25 21:47:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-25T21:47:45Z.


I checked 4d683b684 and its parent b9320a118 on an RTX 4090, Windows 11, NVIDIA 616.64. Each revision produced 4,000/4,000 correct output words over 25 compute dispatches. All five modules per revision passed spirv-val --target-env vulkan1.2. In the disassembled binaries the head masked the shift count with 31 at 11/11 tested MBCNT shift sites. The parent did so at 0/11. GPU readbacks were byte-identical between revisions, including the untouched guard words.

Shader wave and workgroup size Prefix counts checked per revision
Native wave32, 32 and 48 threads, MBCNT low 400
Emulated wave64, 48, 64 and 128 threads, MBCNT low, high and low followed by high 3,600

I set the wave-size bit in each synthetic FSSM header, checked the generated index and pinned the Vulkan subgroup to 32. Five input sets included empty and full masks, isolated high bits, alternating masks and an accumulator that wraps. Each active thread wrote its result to a distinct buffer word. The CPU reference counted set bits strictly below that lane in each logical wave.

One detail for the test description: the PR's high-half interpreter case translates a wave64 shader and runs it with 32 and 64 interpreter lanes. The translator currently treats v_mbcnt_hi_u32_b32 as a wave64 marker. For native wave32 coverage here I checked v_mbcnt_lo_u32_b32 through the partial second wave as well.

AI-assisted with OpenAI Codex (gpt-6.2-astra). All inputs were hand-written synthetic compute shaders.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7175548492 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2121#issuecomment-7175548492). Originally posted by [ecox982745](https://github.com/ecox982745) on 2026-11-25T21:47:45Z. --- I checked `4d683b684` and its parent `b9320a118` on an RTX 4090, Windows 11, NVIDIA 616.64. Each revision produced **4,000/4,000 correct output words** over 25 compute dispatches. All five modules per revision passed `spirv-val --target-env vulkan1.2`. In the disassembled binaries the head masked the shift count with 31 at **11/11** tested MBCNT shift sites. The parent did so at **0/11**. GPU readbacks were byte-identical between revisions, including the untouched guard words. | Shader wave and workgroup size | Prefix counts checked per revision | |---|---:| | Native wave32, 32 and 48 threads, MBCNT low | 400 | | Emulated wave64, 48, 64 and 128 threads, MBCNT low, high and low followed by high | 3,600 | I set the wave-size bit in each synthetic FSSM header, checked the generated index and pinned the Vulkan subgroup to 32. Five input sets included empty and full masks, isolated high bits, alternating masks and an accumulator that wraps. Each active thread wrote its result to a distinct buffer word. The CPU reference counted set bits strictly below that lane in each logical wave. One detail for the test description: the PR's high-half interpreter case translates a **wave64 shader** and runs it with 32 and 64 interpreter lanes. The translator currently treats `v_mbcnt_hi_u32_b32` as a wave64 marker. For native wave32 coverage here I checked `v_mbcnt_lo_u32_b32` through the partial second wave as well. AI-assisted with OpenAI Codex (`gpt-6.2-astra`). All inputs were hand-written synthetic compute shaders.
qhukz13 commented 2026-11-26 00:36:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-26T00:36:22Z.


Checked at 4d683b684 on Linux (Python 3, no GPU; ecox982745's RTX 4090 run above covers the hardware side).

  • tests/semantic/test_mbcnt_shift_defined.py: 3 tests pass. With the mask taken out again (OpShiftLeftLogical %uint %c1 {rel} instead of {rel_s}) it fails 4 checks on _Poison, so it pins the change.
  • The masked count equals rel wherever inrange selects the shifted value (rel < 32), so the result is unchanged on every lane; only the unselected side stops being undefined.
  • The other 1 << x sites next to it are already in range: v_bfm_b32 masks its width with & 31 before the shift (tools/rdna2spirv.py just below the mbcnt block).

Looks right to me.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7178929710 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2121#issuecomment-7178929710). Originally posted by [qhukz13](https://github.com/qhukz13) on 2026-11-26T00:36:22Z. --- Checked at `4d683b684` on Linux (Python 3, no GPU; ecox982745's RTX 4090 run above covers the hardware side). - `tests/semantic/test_mbcnt_shift_defined.py`: 3 tests pass. With the mask taken out again (`OpShiftLeftLogical %uint %c1 {rel}` instead of `{rel_s}`) it fails 4 checks on `_Poison`, so it pins the change. - The masked count equals `rel` wherever `inrange` selects the shifted value (`rel < 32`), so the result is unchanged on every lane; only the unselected side stops being undefined. - The other `1 << x` sites next to it are already in range: `v_bfm_b32` masks its width with `& 31` before the shift (`tools/rdna2spirv.py` just below the mbcnt block). Looks right to me. AI-assisted (Claude Code).
Tauwni commented 2026-11-26 13:55:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tauwni on 2026-11-26T13:55:31Z.


Tested 4d683b684 merged onto main a73a57e13 (it merges cleanly), on an Apple M3 (macOS 26.5.1). I ran the full scripts/run_gpu_tests.py in auto mode, with test programs built from main. I tested the merge rather than the branch alone because a branch from before 9f98dcc61 fails ps_tex against main's vk_bindless_test (the tagged descriptor table), which says nothing about this change.

Driver main main + this PR
MoltenVK 1.4.2 195 pass, 70 skip 195 pass, 70 skip
MoltenVK, LEONIDA_W64_EMULATE=1 229 pass, 36 skip 229 pass, 36 skip
KosmicKrisp (Mesa 26.3.1) 190 pass, 73 skip 190 pass, 73 skip

The counts are identical to main on all three. No new failure. The ones left are the same on both sides: cs_fma_fused (#1724), ps_ssharp (#1774) and cs_imgatomic_ext (#1518) on MoltenVK; KosmicKrisp's (NAN)-constant fixtures (#2229) and cs_imgatomic_ext there.

AI-assisted (Claude Code). No game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7194027614 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2121#issuecomment-7194027614). Originally posted by [Tauwni](https://github.com/Tauwni) on 2026-11-26T13:55:31Z. --- Tested `4d683b684` merged onto `main` `a73a57e13` (it merges cleanly), on an Apple M3 (macOS 26.5.1). I ran the full `scripts/run_gpu_tests.py` in auto mode, with test programs built from `main`. I tested the merge rather than the branch alone because a branch from before `9f98dcc61` fails `ps_tex` against `main`'s `vk_bindless_test` (the tagged descriptor table), which says nothing about this change. | Driver | `main` | `main` + this PR | |---|---|---| | MoltenVK 1.4.2 | 195 pass, 70 skip | 195 pass, 70 skip | | MoltenVK, `LEONIDA_W64_EMULATE=1` | 229 pass, 36 skip | 229 pass, 36 skip | | KosmicKrisp (Mesa 26.3.1) | 190 pass, 73 skip | 190 pass, 73 skip | The counts are identical to `main` on all three. No new failure. The ones left are the same on both sides: `cs_fma_fused` (#1724), `ps_ssharp` (#1774) and `cs_imgatomic_ext` (#1518) on MoltenVK; KosmicKrisp's `(NAN)`-constant fixtures (#2229) and `cs_imgatomic_ext` there. AI-assisted (Claude Code). No game data.
zmasi commented 2026-11-28 13:04:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T13:04:56Z.


Confirmed on physical gfx906, native subgroup 64 (RADV Mesa 26.0.8). Main d48a957bb and 4d683b684 merged with it each matched 20/20 GPU readbacks: 15,040 checked data words per build, all 20 dump pairs byte-identical, guards intact, and no shader-run validation errors or synchronization hazards. The fixtures cover both halves, chained prefix rank, compare-generated masks, sparse EXEC and wrapping addends.

The difference is in the generated SPIR-V, not the observed rank. At all 32 MBCNT shift sites in the four main modules, the count is out of range on one half of the wave: lanes 32–63 for the low instruction, lanes 0–31 for the high instruction. All 32 sites in the merge mask the count to 0–31. The strict interpreter independently found an undefined intermediate in all 64 lanes across the chained pair on main and none on the merge; both returned ranks 0–63.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239676562 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2121#issuecomment-7239676562). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T13:04:56Z. --- Confirmed on physical gfx906, native subgroup 64 (RADV Mesa 26.0.8). Main `d48a957bb` and `4d683b684` merged with it each matched **20/20 GPU readbacks**: 15,040 checked data words per build, all 20 dump pairs byte-identical, guards intact, and no shader-run validation errors or synchronization hazards. The fixtures cover both halves, chained prefix rank, compare-generated masks, sparse EXEC and wrapping addends. The difference is in the generated SPIR-V, not the observed rank. At all 32 MBCNT shift sites in the four main modules, the count is out of range on one half of the wave: lanes 32–63 for the low instruction, lanes 0–31 for the high instruction. All 32 sites in the merge mask the count to 0–31. The strict interpreter independently found an undefined intermediate in all 64 lanes across the chained pair on main and none on the merge; both returned ranks 0–63. AI-assisted.
MoonEyes commented 2026-11-28 17:59:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:59:00Z.


Red/green: tests/semantic/test_mbcnt_shift_defined.py fails 4 assertions on main (unmasked OpShiftLeftLogical count >= 32 in v_mbcnt) and passes with the PR.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243437338 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2121#issuecomment-7243437338). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:59:00Z. --- Red/green: tests/semantic/test_mbcnt_shift_defined.py fails 4 assertions on main (unmasked OpShiftLeftLogical count >= 32 in v_mbcnt) and passes with the PR. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
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!2121
No description provided.