Skip to content

rdna2spirv, lane_masks: handle lane-mask destinations and sources in s_bitset0/1_b32 (SHADER-BITSET32-MASK-AND-DST) - #2355

Open
ShotTechOp wants to merge 1 commit into
ProjectLeonida:mainfrom
ShotTechOp:fix/bitset-mask-and-dst
Open

ShotTechOp wants to merge 1 commit into
ProjectLeonida:mainfrom
ShotTechOp:fix/bitset-mask-and-dst

Conversation

@ShotTechOp

Copy link
Copy Markdown
Contributor

Item: SHADER-BITSET32-MASK-AND-DST

Problem

In tools/rdna2spirv.py, s_bitset0_b32 and s_bitset1_b32 always applied old & ~(1 << (b & 31)) / old | (1 << (b & 31)) directly to self.src_u(a[0]) and wrote back via self.dst(a[0], r), while tools/lane_masks.py dropped mask tracking for a[0] on both opcodes:

  1. When a[0] holds a lane mask (vcc_lo/vcc_hi holding a mask, or a mask SGPR tracked by lane_masks.py), each invocation holds only its own lane's bit (0 or 0xFFFFFFFF), so ORing 1 << k turned every inactive lane non-zero (activating all 32 lanes) and ANDing ~(1 << k) left the targeted lane non-zero (0xFFFFFFDF etc.).
  2. In tools/lane_masks.py, mutating a lane mask with s_bitset0/1_b32 dropped a[0] from state, causing downstream mask consumers (s_bcnt1_i32_b32, s_ff1_i32_b32, s_bcnt1_i32_b64, etc.) to read per-lane 0/0xFFFFFFFF as scalar data instead of collecting the wave mask.
  3. When the bit-index source a[1] holds a lane mask (e.g. s_bitset1_b32 s10, vcc_lo), self.src_u(a[1]) read each lane's 0/0xFFFFFFFF instead of the wave's 32-bit mask word, producing divergent per-lane shifts (bit 0 vs bit 31).

Fix

  • tools/lane_masks.py: Preserve MASK / MASK_LO / MASK_HI (and wave64 vcc mask status for vcc_lo/vcc_hi) when s_bitset0_b32 or s_bitset1_b32 mutates an existing lane mask; clear tracking to plain data when mutating non-mask or EITHER constants.
  • tools/rdna2spirv.py: Merge s_bitset0_b32 and s_bitset1_b32 into one handler. Materialize a lane-mask bit-index source via Gen.lane_mask_word. When self.holds_lane_mask(a[0]), compare wave_lane() against (b & 31) (or (b & 31) + 32 for a wave64 high half) and update only the addressed lane via Gen.put_mask (keeping the untouched half of EXEC/VCC in wave64 and updating %vcc_hi when a[0] is vcc_hi).
  • docs/design.md: Document the s_bitset0_b32/s_bitset1_b32 lane-mask contract row.
  • tests/semantic/test_bitset32_mask.py (and updated tests/semantic/test_scalar_alu_imm_bitset.py comment/loop): Add wave32, wave64, and --wave64-emulate tests for EXEC, VCC, mask SGPRs, and lane-mask bit-index sources.

Verification

AI-assisted: yes.
Environment: Windows 11, Python 3.12.10, CPU-only (no game files, no spirv-val, not GPU-verified).

  • GREEN (branch):
    • python -I tests/semantic/test_bitset32_mask.py -v -> 8/8 OK (wave32, wave64 native, --wave64-emulate)
    • python -I tests/semantic/test_scalar_alu_imm_bitset.py -> 17/17 OK
    • python -I tests/semantic/test_salu_bitset_b64.py -> 5/5 OK
    • python -I tests/semantic/test_salu_b32_lane_masks.py -> 8/8 OK (1 skipped: spirv-val absent)
    • python -I tests/test_lane_masks.py -> OK
    • python -I tests/test_text_write_line_endings.py -> 3/3 OK
    • python -m compileall -q tools/rdna2spirv.py tools/lane_masks.py tests/semantic/test_bitset32_mask.py tests/semantic/test_scalar_alu_imm_bitset.py -> 0
    • python -I scripts/check_no_game_data.py ... -> 0
    • git diff --check -> 0
  • RED control (origin/main fbf100b1):
    • Running tests/semantic/test_bitset32_mask.py against origin/main's tools/ fails 6/8 assertions (test_wave32_vcc_lo_and_mask_sgpr_bitset, test_wave64_vcc_and_mask_sgpr_halves_bitset native & emulate, test_lane_mask_bit_index_source_uses_wave_mask_word, and both LaneMasksBitset32Tests preservation checks).

…s_bitset0/1_b32 (SHADER-BITSET32-MASK-AND-DST)
@ecox982745

Copy link
Copy Markdown

The bit-set fix passed all 66 synthetic GPU dispatches on an RTX 4090 at 69726d1ce. The same inputs matched the CPU reference in 23 of 66 dispatches on parent fbf100b1b.

Compute mode Parent PR
Native wave32 9/21 21/21
Wave64 emulated on 32-lane subgroups 14/45 45/45

One case exposes the bug with a single active lane. Lane 0 sets VCC bit 0, then s_mov_b32 s10, vcc_lo saves the word 1. After s_bitset1_b32 s10, s10, the word should be 3 and its population count should be 2 in every lane. The parent returned 32 in lane 0 and 1 in lane 1. The PR returned 2 in all 32 lanes. The corresponding saved-high-half cases also passed in wave64 emulation.

The remaining probes covered setting and clearing EXEC and VCC bits, using either VCC half as the bit-index source, wrapped indices including 32 and 63, and ordinary scalar controls. I checked every output word and the untouched tail of each buffer. All 13 PR modules passed spirv-val --target-env vulkan1.2. The eight committed CPU tests also passed.

Windows 11, NVIDIA 616.64, Vulkan validation enabled, synthetic shader listings. AI-assisted verification by OpenAI Codex (GPT-6).

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows 11: 30/30 bitset semantic tests passed, including tracked lane-mask operands and the immediate-bitset cases. No findings.

AI-assisted.

@wavehs wavehs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS arm64 check of #2355 (SHADER-BITSET32-MASK-AND-DST). Translator plus lane_masks.py and one semantic test; the GPU part is MoltenVK/Kosmickrisp.

Tested head 69726d1ce merged onto main bdbc7e923 as merge b56572d3e (clean; branch 21 commits behind, git merge-tree clean).

Environment: Apple M3 Pro, arm64 (subgroups 4..32), macOS 27.2.1, CPython 3.15.2, SPIRV-Tools v2026.4, llvm-mc 23.1.5, MoltenVK 1.4.0; KosmicKrisp and lavapipe = Mesa 26.3.0.

The new test passes, and the control bites

  • python -I tests/semantic/test_bitset32_mask.py: Ran 8 tests ... OK at the head and after merging onto bdbc7e923; the updated tests/semantic/test_scalar_alu_imm_bitset.py still passes.
  • Negative control (test kept, tools/rdna2spirv.py and tools/lane_masks.py put back to main's): FAILED (failures=6) - mask bit-index source, wave32 vcc_lo + mask SGPR, wave64 VCC/SGPR halves in native and --wave64-emulate, and the two lane_masks checks that a mask kind survives. So both the emission and the tracking are pinned.

What I checked in the code

  • The mask path updates only the addressed lane: hit = (SubgroupLocalInvocationId == (S0 & 31) [+32 for a wave64 high half]), then put_mask writes 0 / ~0 or keeps the old per-lane bit. The untouched half of a wave64 EXEC/VCC is preserved, and SCC is not written, which matches the row and the ISA. The non-mask path keeps the old bit math, and s_bitset0_b32 now shares it with s_bitset1_b32 instead of a separate block.
  • The lane_masks.step addition keeps a mask kind on a mask destination and drops it to data otherwise.
  • Nit, not a failure: for a wave64 vcc_lo/vcc_hi write when state["vcc"] is a full MASK, step returns without changing the kind. Native reads go through the live %vcc/%vcc_hi, and the new wave64 test reads the result through s_bcnt1_i32_b64 vcc in both native and --wave64-emulate and passes, so I could not make it stale; a one-line comment saying why the full-mask kind is kept would save the next reader the same detour.

No regressions

  • Semantic suite: 500 files / 6,367 tests / 0 failed at main bdbc7e923; 501 files / 6,375 tests / 0 failed at the merge (+1 file, +8 tests).
  • GPU fixtures (268): MoltenVK 195 PASS / 3 FAIL / 70 SKIP, KosmicKrisp 190 / 5 / 73, identical at main and the merge after normalising the per-run temp directory; same pre-existing failures (cs_fma_fused, cs_imgatomic_ext, ps_ssharp on MV; cs_bvh, cs_bvh32, cs_imgatomic_ext, ps_cndmask_sign_mods, ps_valu16_mad_sel on KK).

Finding
The mask-destination path has no GPU fixture. cs_w32_salu_ext.asm is the only fixture with s_bitset0/1_b32, and it runs them on plain SGPRs (s65, s66); cs_w32_salu64.asm has the _b64 form on scalar pairs. Nothing on a GPU writes a lane mask (exec_lo, vcc_lo/vcc_hi, a mask SGPR) with s_bitset and then counts the mask, so the put_mask/wave_lane half logic rests on the interpreter, which the doc row already says ("not GPU-verified"). Adding four stores to an existing cs_w32_* fixture (mask before, bitset, s_bcnt1, store) would cover it on any GPU, and would catch a wrong half split in wave64 the way cs_w64x_partial_scmp.asm does for compares.

python -I scripts/check_no_game_data.py on the changed files: clean. git diff --check: clean.

No game files. AI-assisted.

@tiguan26 tiguan26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving 69726d1.

s_bitset0/1_b32 on a lane mask now follows the translator's mask model: a lane holds only its own bit, so setting or clearing bit n changes lane n's value alone (n + 32 for the high half in wave64), and the destination stays a tracked mask in lane_masks.step instead of being dropped to data. On plain data the two ops keep their shift-and-or/and-not form, now in one branch. The updated test_scalar_alu_imm_bitset.py case follows from that: these ops no longer clear mask tracking.

test_bitset32_mask.py 8 passed (4 subtests), test_scalar_alu_imm_bitset.py 17 passed (265 subtests). Merged onto main d1dff7949 together with #2397, #2416 and the other three of this series below (no conflicts), on Linux with Python 3.13. The full translator suite (run_semantic_tests.py tests/semantic) on that tree has only the 8 failures main has here (7 in test_fspx_dot4_op_sel_disasm, test_int32_to_f64), and tests/test_translate.py only main's own ps_sdwa_f16_const.asm (an llvm-mc version difference in this environment). AI-assisted (Claude Code, Claude Opus 5.5).

@Tauwni

Tauwni commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Tested 69726d1ce merged onto main ec91d695c on a Mac (profile S), where wave64 is always emulated on 32-wide subgroups: Apple M3, macOS 26.5.1, MoltenVK 1.4.2, KosmicKrisp and llvmpipe (Mesa 26.3.1). No game files.

scripts/run_gpu_tests.py, auto mode, test programs from main:

Driver main main + this PR
MoltenVK 196/269 196/269
MoltenVK, LEONIDA_W64_EMULATE=1 230/269 230/269
KosmicKrisp 191/269 191/269
llvmpipe 177/269 177/269

The failures are the same on both sides (the known ones: #1724, #1518 and #1774 on MoltenVK, #2229's on KosmicKrisp). The translator suite passes with no failures.

Evidence: MEASURED, 1 run each. This only shows that nothing changes on Apple drivers. It says nothing about the game.

AI-assisted (Claude Code, Claude Opus 5.5).

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified head 69726d1ce on Windows 11 (semantic suites + dual-GPU control + live translation repros).

The straight-line mask destination/source handling passes its semantic suites (test_bitset32_mask.py 8/8, test_scalar_alu_imm_bitset.py 17/17, test_salu_bitset_b64.py 5/5, test_wave64_emulate_barriers.py 13/13) and cs_w32_salu_ext passes on both RTX 5080 and AMD Radeon.

Blocking finding — missing wave64_emulate_opt.py mask-reader classification causes translation crashes under --wave64-emulate:
s_bitset0_b32 / s_bitset1_b32 now call lane_mask_word for a tracked bit-index operand (including exec_lo and tracked mask SGPRs; tools/rdna2spirv.py:9287-9293), which under --wave64-emulate can invoke x_round and emit a workgroup barrier. However, tools/wave64_emulate_opt.py does not classify s_bitset0_b32 / s_bitset1_b32 as operand-1 mask readers (unlike s_flbit_* at tools/wave64_emulate_opt.py:81-86, 326-335, 1232-1247, 1469-1493). As a result, both the local execz fold and the sync-free multi-wave planner admit blocks containing that exchange, tripping hard translation assertions inside _translate:

  1. 64-thread s_cbranch_execz guard (v_cmp_lt_u32_e64 lanes 0..15 -> s_and_saveexec_b64 -> s_cbranch_execz -> s_bitset1_b32 s14, exec_lo):
    • Live repro on 69726d1ce:
      AssertionError: wave64 emulation: x_split inside a folded lane-local region (tools/rdna2spirv.py:18985).
  2. 128-thread divergent s_cbranch_scc0 guard (v_cmp_eq_u32_e64 s[6:7], v0, 5, wave-0-only branch executing s_bitset1_b32 s14, s6):
    • Live repro on 69726d1ce:
      AssertionError: wave64 emulation: unexpected OpControlBarrier in sync-free multi-wave block (tools/rdna2spirv.py:19100).

Requested fix: Register s_bitset0_b32 and s_bitset1_b32 alongside s_flbit_* for operand-1 mask-reader detection in tools/wave64_emulate_opt.py, and add both guarded --wave64-emulate regressions (single-wave execz fold + 128-thread multi-wave divergent branch).

No game data used. AI-assisted review (Codex static audit + Gemini live verification).

@sodove

sodove commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2355 | head 69726d1ceca0b8579023c6aaf7bab95ed29891b0 | python3 -I tests/semantic/test_bitset32_mask.py -v, python3 -I tests/semantic/test_scalar_alu_imm_bitset.py -v | red: 25 tests, 6 assertion failures | green: 25 passed | Red → green

The added lane-mask cases failed on main while the pre-existing scalar bitset tests passed on both sides.

Pinned main 4938673668d807bf365b61284bf22b1ff8d02cc4, merged into the PR head. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Full suite: not run. AI-assisted: OpenAI Codex. Batch claim.

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

VERIFY-2355-VEGA20-RADV: five synthetic compute fixtures, same inputs and independent per-lane integer reference on 2d4735682 and on its merge with 69726d1ce. Both sets of modules dispatched on physical gfx906 / RADV Mesa 26.0.8, native subgroup 64. Main returned 685 wrong words out of 1,920; the merge returned 0/1,920 wrong.

Case Main wrong / checked Merge wrong / checked
VCC bit set/clear 298/448 0/448
Mask held in scalar registers 128/448 0/448
Mask used as the bit index 259/384 0/384
Ordinary scalar controls and EXEC cases 0/640 0/640

No unwritten results or changed guard words; zero Khronos validation errors or synchronization hazards. The PR’s eight targeted semantic tests and 17 adjacent scalar tests passed on the merge. The native evidence is for fixed wave64, not wave32 emulation.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants