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

Open
github-bot wants to merge 1 commit from github-bot/gh-2121 into main AGit
Owner

@⁠ShotTechOp on GitHub · 2026-11-25 21:33 UTC · original
ShotTechOp:fix/mbcnt-shift-in-range → main · +136 −2 · 2 files · 1 commits · open

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
**@⁠ShotTechOp** on GitHub · 2026-11-25 21:33 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2121) `ShotTechOp:fix/mbcnt-shift-in-range` → `main` · +136 −2 · 2 files · 1 commits · open 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`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:18:36 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 21:47 UTC · original

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.

**@⁠ecox982745** on GitHub · 2026-11-25 21:47 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2121#issuecomment-7175548492) 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.
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-26 00:36 UTC · original

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).

**@⁠qhukz13** on GitHub · 2026-11-26 00:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2121#issuecomment-7178929710) 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).
Author
Owner

@⁠Tauwni on GitHub · 2026-11-26 13:55 UTC · original

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.

**@⁠Tauwni** on GitHub · 2026-11-26 13:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2121#issuecomment-7194027614) 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2121/head:github-bot/gh-2121'
git switch 'github-bot/gh-2121'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2121'
git switch 'github-bot/gh-2121'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2121'
git switch 'github-bot/gh-2121'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2121'
git switch 'main'
git merge --squash 'github-bot/gh-2121'
git switch 'main'
git merge --ff-only 'github-bot/gh-2121'
git switch 'main'
git merge 'github-bot/gh-2121'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.