rdna2spirv: mask v_mbcnt_lo/hi shift count to < 32 to avoid SPIR-V OpShiftLeftLogical UB (SHADER-MBCNT-SHIFT-IN-RANGE) #2121
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2121"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
tools/rdna2spirv.py(v_mbcnt_lo_u32_b32,v_mbcnt_hi_u32_b32), the lowering emitted:OpShiftLeftLogical), the result is undefined wheneverShift >= 32, even when the shifted value sits on the unselected side ofOpSelect(the same SPIR-V ruletools/rdna2spirv.pyalready guards against inv_perm_b32,v_alignbit_b32,v_alignbyte_b32,fmt_dec_component, andbuffer_load_format).v_mbcnt_lo_u32_b32in wave64, lanes32..63hadrel = 32..63 >= 32.v_mbcnt_hi_u32_b32in both wave32 and wave64, lanes0..31hadrel = lane - 32 = 0xFFFFFFE0..0xFFFFFFFF >= 32.spirv_wave_interp, this produced_Poison("OpShiftLeftLogical by ... >= 32")in 32 lanes of every wave.relwith31(OpBitwiseAnd %uint %rel %c31) beforeOpShiftLeftLogicalis an identity wheneverinrangeis true (0 <= rel <= 31) and keeps everyOpShiftLeftLogicalshift operand strictly< 32on all lanes.Testing
tests/semantic/test_mbcnt_shift_defined.py— executes translatedv_mbcnt_lo_u32_b32andv_mbcnt_hi_u32_b32in wave32 and wave64 (plain scalar masks and lane-maskvcc, plus paired wave64 thread-rank) onspirv_wave_interp._Waveand asserts both exact per-lane outputs and zero_Poisonvalues across all lanes.origin/main:tests/semantic/test_mbcnt_shift_defined.pyfails onorigin/mainwithFAILED (failures=4)(_Poison('OpShiftLeftLogical by 32 >= 32')/_Poison('OpShiftLeftLogical by 4294967264 >= 32')across 32 lanes per wave).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.pypython -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mbcnt_shift_defined.pygit diff --checkImported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-25T21:47:45Z.
I checked
4d683b684and its parentb9320a118on 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 passedspirv-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.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_b32as a wave64 marker. For native wave32 coverage here I checkedv_mbcnt_lo_u32_b32through the partial second wave as well.AI-assisted with OpenAI Codex (
gpt-6.2-astra). All inputs were hand-written synthetic compute shaders.Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-26T00:36:22Z.
Checked at
4d683b684on 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.relwhereverinrangeselects the shifted value (rel < 32), so the result is unchanged on every lane; only the unselected side stops being undefined.1 << xsites next to it are already in range:v_bfm_b32masks its width with& 31before the shift (tools/rdna2spirv.pyjust below the mbcnt block).Looks right to me.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by Tauwni on 2026-11-26T13:55:31Z.
Tested
4d683b684merged ontomaina73a57e13(it merges cleanly), on an Apple M3 (macOS 26.5.1). I ran the fullscripts/run_gpu_tests.pyin auto mode, with test programs built frommain. I tested the merge rather than the branch alone because a branch from before9f98dcc61failsps_texagainstmain'svk_bindless_test(the tagged descriptor table), which says nothing about this change.mainmain+ this PRLEONIDA_W64_EMULATE=1The counts are identical to
mainon all three. No new failure. The ones left are the same on both sides:cs_fma_fused(#1724),ps_ssharp(#1774) andcs_imgatomic_ext(#1518) on MoltenVK; KosmicKrisp's(NAN)-constant fixtures (#2229) andcs_imgatomic_extthere.AI-assisted (Claude Code). No game data.
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
d48a957bband4d683b684merged 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.
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
cb7a239ccin GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)