Skip to content

rdna2spirv: count and scan a mixed mask/data SGPR pair word by word (SHADER-823) - #7224

Open
Mayzkl wants to merge 3 commits into
ItsNotRexed:mainfrom
Mayzkl:shader-823-mixed-mask-pair
Open

Mayzkl wants to merge 3 commits into
ItsNotRexed:mainfrom
Mayzkl:shader-823-mixed-mask-pair

Conversation

@Mayzkl

@Mayzkl Mayzkl commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claim: #6750 (comment) (SHADER-823; first found by @lumenpearson, whose claim lapsed without code)

What and why

s_bcnt0/1_i32_b64 and s_ff0/1_i32_b64 read their source as one 64-bit value. When the SGPR pair holds a lane mask in one word and data in the other (v_cmp_eq_u32_e64 s[10:11], 0, v3 then s_mov_b32 s11, 5), holds_lane_mask is false, because only one word is a mask, so the translator loaded both words with load_reg. A mask register holds only this lane's 0 / ~0, so every lane counted its own pattern beside the scalar: lanes inside and outside the mask got different counts and first-set positions.

  • Gen.mixed_pair_words (tools/rdna2spirv.py): for such a pair, the mask word is read as the whole wave's bits (lane_mask_word: in wave64 the low or high half by the word's mask kind) and the data word as it is, in either order. Used by the b64 forms of s_bcnt0/1 and s_ff0/1. A whole mask still takes wave_mask first; a pair with no mask word is unchanged.
  • tools/wave64_emulate_opt.py: under --wave64-emulate that read is a cross-lane exchange, so _reads_mask_word counts the four b64 readers as cross-lane when either word of the pair is a lane mask, at the three _MASK_CHK_WORD_READER sites (block classification, the fused pass and _insn_may_barrier_with_meta). Without it, a lane-local fold or sync-free block could hold an exchange.
  • docs/design.md, "Whole-wave masks" row: the rule and how it was checked.

Tested

Windows 11, Python 3.14, Vulkan SDK 1.4.363.1, llvm-mc 21.1.8; no game files, no GPU needed.

  • New tests/semantic/test_salu64_mixed_mask_pair.py, 6 tests:
    • The four ops run on all 64 lanes of a wave64 (spirv_mini_interp.run_workgroup) for both word orders, 7 masks and 4 data words, and every lane must match the ISA model, SCC of s_bcnt1 included.
    • The emulate-opt predicate: a mixed pair is cross-lane, a data pair and the b32 forms stay lane-local, a whole mask is unchanged.
    • The --wave64-emulate translation of a mixed pair ORs mask bits into %x_buf (OpAtomicOr) and uses no subgroup op; the data-pair control does neither.
    • Against main's tools, the every-lane test fails, starting with the finder's case: a comparison mask beside the scalar 5.
  • Full python -m unittest discover -s tests/semantic: 5844 tests OK (3 skipped), on this branch rebased onto 7aba944f.
  • runtime/test/shaders/build.py with main and with this branch: 264/264 shaders translate and pass spirv-val, and all 264 .spv/.spvasm are byte-identical between the two. No fixture uses a mixed pair, so nothing else changes.
  • tests/test_semantic_translator_use.py, tests/semantic/test_wave64_emulate_opt.py, test_lane_mask_reductions.py, test_carry_exec_mask.py, test_andn2_exec_b64.py, tests/test_docs_repo_paths.py, tests/test_docs_design_rows.py: OK.

GPU: @shdann ran the emulated wave64 path on an AMD Radeon iGPU and an RTX 4090 with both word orders: 64/64 lanes match the ISA on both, 0/64 with the translator before the fix (see the comment below); docs/design.md cites it. s_flbit_i32_b64 of a mixed pair has the same per-lane load. It is outside this item, and I'll post it as a finding.

Overlap: #4596 (@lumenpearson's s_flbit / mixed s_cmp_*_u64 emulate detection) edits the same _MASK_CHK_WORD_READER area of tools/wave64_emulate_opt.py and conflicts textually with this branch. #4479 (mixed mask + constant pairs) already conflicts with main. Whichever lands first, I'll rebase this one.

AI-assisted (Claude Code).

Generated with Claude Code

…SHADER-823)

s_bcnt0/1_i32_b64 and s_ff0/1_i32_b64 of an SGPR pair with a lane mask in one
word and data in the other (v_cmp_eq_u32_e64 s[10:11] then s_mov_b32 s11, 5)
loaded both words as data. A mask register holds only this lane's 0 / ~0, so
every lane counted its own bit pattern beside the scalar and lanes in and out
of the mask got different results.

Gen.mixed_pair_words reads the mask word as the whole wave's bits
(lane_mask_word) and the data word as it is, in either order. A whole mask
still takes wave_mask first; a pair without a mask word is unchanged.

Under --wave64-emulate that read is a cross-lane exchange, so
tools/wave64_emulate_opt.py now counts the b64 readers as cross-lane when
either word of the pair is a lane mask (_reads_mask_word), and keeps them out
of lane-local folds and sync-free blocks.

tests/semantic/test_salu64_mixed_mask_pair.py runs the four ops on all 64
lanes of a wave64 for both word orders, 7 masks and 4 data words against the
ISA model, checks the emulate-opt predicate, and that the emulated
translation exchanges (OpAtomicOr into %x_buf) for the mixed pair only.
docs/design.md's "Whole-wave masks" row records the rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 866e6fe on Windows 11 with two Vulkan GPUs: pass. The AMD Radeon integrated GPU uses driver 32.0.21043.5001. The NVIDIA GeForce RTX 4090 uses driver 32.0.16.2101. I selected each adapter with VK_LOADER_DRIVERS_DISABLE and checked the printed device name.

I ran the emulated wave64 path with both word orders. I used the PR fixture program(order, 3, 1, 5, 0x80000001). I assembled and validated each module for Vulkan 1.2. I ran vk_compute_test.exe shader.spv 1 dump with VKC_OUT_WORDS=320 and checked all five words for each of the 64 lanes.

Mask word Expected words per lane AMD lanes that match RTX lanes that match
High 10, 54, 0, 1, 1 64/64 64/64
Low 10, 54, 3, 0, 1 64/64 64/64

I repeated the four dispatches with the translator from parent 7aba944f1. Zero lanes matched the expected tuple in each dispatch. The six tests in tests/semantic/test_salu64_mixed_mask_pair.py also pass on the PR with CPython 3.14.7.

AI-assisted.

@brothernog

Copy link
Copy Markdown

Tested 866e6fe on Linux (no GPU, no game files):

  • python -I tests/semantic/test_salu64_mixed_mask_pair.py: 6 OK.
  • With only the tools/ changes reverted, the every-lane test fails with wrong counts (e.g. pair
    0x0000000500000001, lane 0 gives (34,30,0,33,1) where the ISA gives (3,61,0,1,1)), and
    test_mixed_pair_is_cross_lane fails too, so the test exercises the translator, not a model.
  • scripts/run_semantic_tests.py tests/semantic: no new failures (the 3 in test_float_nan_fract also fail on main).
  • Both s_bcnt*_b64 and s_ff*_b64 paths and the three _reads_mask_word sites in wave64_emulate_opt.py are covered;
    the docs/design.md row matches the code.

Nit: tools/wave64_emulate_opt.py:762 has a collapsed backslash continuation (a run of spaces mid-line); wrapping it in
parentheses would read better. Heads-up: #4596 touches the same _MASK_CHK_WORD_READER area.

This review was AI-assisted.

…s the GPU check (SHADER-823 review)

The continuation in _reads_mask_word had collapsed into one line with a run of
spaces (@brothernog); it is a parenthesised condition now. docs/design.md's
"Whole-wave masks" row replaces "not GPU-verified" with @shdann's run of the
emulated path on an AMD Radeon iGPU and an RTX 4090: 64/64 lanes on both,
0/64 with the translator before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mayzkl

Mayzkl commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @shdann and @brothernog.

AI-assisted (Claude Code).

@limpan1212

Copy link
Copy Markdown

Reproduced a defect on 7ebbf1a19 (SHADER-823, CPU-only synthetic shaders via support.translate_lines + spirv_mini_interp.run_workgroup, Python 3.12.10, no GPU, no game files).

Defect

s_bcnt0/1_i32_b64 (tools/rdna2spirv.py:7485) and s_ff0/1_i32_b64 (tools/rdna2spirv.py:7600) check if self.holds_lane_mask(a[1]): before falling through to mixed_pair_words(a[1]). Because holds_lane_mask (tools/rdna2spirv.py:5729-5731) returns True for any SGPR pair whose words are both in cur_masks or cur_either, it intercepts non-canonical pairs that wave_mask(a[1]) cannot read in a single ballot:

  1. Wave32 b64 SGPR pairs where the non-mask word is 0/-1 (EITHER) or both words are lane masks: mask_src only selects r + 1 when self.wave64 is true (tools/rdna2spirv.py:5957), so wave_mask drops the high 32-bit word. Changing s11 from 5 (DATA, takes mixed_pair_words) to -1 (EITHER, takes wave_mask) breaks the count.
  2. Wave64 b64 SGPR pairs with misplaced, swapped, or duplicated mask halves ((MASK_HI, 0), (0, MASK_LO), (MASK_HI, MASK_LO), (MASK_LO, MASK_LO)): wave_mask reads lanes 0..31 from r and lanes 32..63 from r + 1 regardless of each word's MASK_LO/MASK_HI tag. Changing s11 from 5 (DATA) to 0 (EITHER) switches s[10:11] from mixed_pair_words to wave_mask and returns the wrong half's bits.

Reproduction

From a checkout of 7ebbf1a19:

import pathlib, struct, sys
sys.path.insert(0, str(pathlib.Path("tests/semantic").resolve()))
sys.path.insert(0, str(pathlib.Path("tools").resolve()))
import spirv_mini_interp as si, support

VSHARP = [0, 0, 0x1000, 0x31016FAC]

def fssm(wave64):
    h = bytearray(0x148)
    struct.pack_into("<I", h, 0xD4, 0 if wave64 else 0x20)
    struct.pack_into("<4I", h, 0x138, 0x8088, 64 if wave64 else 32, 1, 1)
    return bytes(h)

def run_case(wave64, setup_lines):
    n = 64 if wave64 else 32
    lines = [
        "v_mbcnt_lo_u32_b32 v1, -1, 0",
        *(["v_mbcnt_hi_u32_b32 v1, -1, v1"] if wave64 else []),
        *setup_lines,
        "s_bcnt1_i32_b64 s20, s[10:11]",
        "s_ff1_i32_b64 s21, s[10:11]",
        "v_mov_b32_e32 v20, s20",
        "v_mov_b32_e32 v21, s21",
        "v_mul_u32_u24_e32 v22, 8, v1",
        "buffer_store_dword v20, v22, s[0:3], 0 offen",
        "v_add_nc_u32_e32 v23, 4, v22",
        "buffer_store_dword v21, v23, s[0:3], 0 offen",
        "s_endpgm",
    ]
    spv = support.translate_lines("cs_repro", lines, fssm=fssm(wave64))
    mem = si.run_workgroup(
        spv, n, n,
        inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i},
        push_constants=VSHARP + [0] * 28,
        memory={},
    )
    vals = {(mem[8 * lane], mem[8 * lane + 4]) for lane in range(n)}
    assert len(vals) == 1, f"lanes disagreed: {vals}"
    return next(iter(vals))

# Wave32: s10 = mask(10 <= lane < 20) [10 bits, low=10]; s14 = mask(lane < 5) [5 bits, low=0]
w32_setup = [
    "v_cmp_le_u32_e64 s10, 10, v1",
    "v_cmp_gt_u32_e64 s12, 20, v1",
    "s_and_b32 s10, s10, s12",
    "v_cmp_gt_u32_e64 s14, 5, v1",
]
# Wave64: s[14:15] = mask(10 <= lane < 40) -> lo: 22 bits (low=10), hi: 8 bits (low=0)
#         s[16:17] = mask(2 <= lane < 34)  -> lo: 30 bits (low=2),  hi: 2 bits (low=0)
w64_setup = [
    "v_cmp_le_u32_e64 s[14:15], 10, v1",
    "v_cmp_gt_u32_e64 s[18:19], 40, v1",
    "s_and_b64 s[14:15], s[14:15], s[18:19]",
    "v_cmp_le_u32_e64 s[16:17], 2, v1",
    "v_cmp_gt_u32_e64 s[18:19], 34, v1",
    "s_and_b64 s[16:17], s[16:17], s[18:19]",
]

cases = [
    ("w32_mask_and_5",      False, w32_setup + ["s_mov_b32 s11, 5"],                         (12, 10)),
    ("w32_mask_and_minus1", False, w32_setup + ["s_mov_b32 s11, -1"],                        (42, 10)),
    ("w32_two_masks",       False, w32_setup + ["s_mov_b32 s11, s14"],                       (15, 10)),
    ("w32_zero_and_mask",   False, w32_setup + ["s_mov_b32 s10, 0", "s_mov_b32 s11, s14"],   (5, 32)),
    ("w64_hi_in_lo_with_5", True,  w64_setup + ["s_mov_b32 s10, s15", "s_mov_b32 s11, 5"],   (10, 0)),
    ("w64_hi_in_lo_with_0", True,  w64_setup + ["s_mov_b32 s10, s15", "s_mov_b32 s11, 0"],   (8, 0)),
    ("w64_lo_in_hi_with_0", True,  w64_setup + ["s_mov_b32 s10, 0",   "s_mov_b32 s11, s14"], (22, 42)),
    ("w64_swapped_hi_lo",   True,  w64_setup + ["s_mov_b32 s10, s15", "s_mov_b32 s11, s16"], (38, 0)),
    ("w64_dup_lo_in_both",  True,  w64_setup + ["s_mov_b32 s10, s14", "s_mov_b32 s11, s14"], (44, 10)),
]
for name, w64, setup, exp in cases:
    got = run_case(w64, setup)
    print(f"{name:22s} exp={exp} got={got} {'OK' if got == exp else 'FAIL'}")

Observed output on 7ebbf1a19

w32_mask_and_5         exp=(12, 10) got=(12, 10) OK
w32_mask_and_minus1    exp=(42, 10) got=(10, 10) FAIL
w32_two_masks          exp=(15, 10) got=(10, 10) FAIL
w32_zero_and_mask      exp=(5, 32) got=(0, 4294967295) FAIL
w64_hi_in_lo_with_5    exp=(10, 0) got=(10, 0) OK
w64_hi_in_lo_with_0    exp=(8, 0) got=(22, 10) FAIL
w64_lo_in_hi_with_0    exp=(22, 42) got=(8, 32) FAIL
w64_swapped_hi_lo      exp=(38, 0) got=(24, 10) FAIL
w64_dup_lo_in_both     exp=(44, 10) got=(30, 10) FAIL

Suggested fix

Align the wave_mask(a[1]) fast-path guard in s_bcnt0/1_i32_b64 and s_ff0/1_i32_b64 with s_cmp_eq_u64 (tools/rdna2spirv.py:9029-9037): only canonical full-wave masks (exec, mask vcc, or a wave64 (MASK_LO, MASK_HI) SGPR pair) should take wave_mask(a[1]), letting mixed_pair_words(a[1]) handle wave32 b64 SGPR pairs, pairs with EITHER (0/-1), and non-canonical wave64 half layouts word by word.

AI-assisted review.

main changed the row after "Whole-wave masks" in the same hunk; kept main's
version and re-added the SHADER-823 sentence to the Whole-wave masks row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 390d6be on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

  • python3 -m pytest tests/semantic/test_salu64_mixed_mask_pair.py -q — 6 passed, 64 subtests passed

AI-assisted (OpenCode).

@carlos-seiffert

Copy link
Copy Markdown

Tested 390d6be (merged onto main 976b3ec) on NVIDIA GeForce RTX 2060 (Turing), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 (10.0.26300), subgroup size 32: pass (no regression)

What I ran: python -I scripts/run_gpu_tests.py with the Vulkan tools built from 976b3ec (llvm-mingw + Ninja, -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, Vulkan-Headers 6802bb47), llvm-mc 23.1.6 and SPIRV-Tools from conda-forge.

main 976b3ec this PR on main
full GPU suite 192/241 passed, 48 skipped, 1 fail 192/241 passed, 48 skipped, 1 fail (same as main)

No GPU test changed status against main. This PR adds no GPU fixture of its own, so the run checks that the change breaks none of the existing GPU tests; it does not exercise the new rule directly.

Unit tests changed by the PR: tests/semantic/test_salu64_mixed_mask_pair.py: OK.

The one failure on both sides is ms_wave_info_s3, which fails on main on this GPU (10/10 runs) and is fixed by #7136 / #5741 / #5855; it is unrelated to this PR.

AI-assisted run (Claude Code); the numbers are from my machine.

@InCaseOf

Copy link
Copy Markdown

Tested 390d6be on RX 5700 XT (RDNA1, gfx1010, RADV NAVI10), Mesa 26.3.1, Linux 6.18: pass

  • scripts/run_gpu_tests.py: 213/241 passed, 28 skipped, 0 failed.
  • w64_graphics.py over all fixtures: 29/29 ok.
  • tests/semantic/test_salu64_mixed_mask_pair.py: Ran 6 tests in 3.868s OK
  • scripts/run_semantic_tests.py tests/semantic -j 8: 448 files, 5962 tests, 2 files failed (the same 2 files that fail on main here, nothing new).

LLVM llvm-mc 23.1.5, SPIRV-Tools v2026.4, Python 3.14; test programs built from main e36d4c4 unless rebuilt for the PR as noted. main e36d4c4 on the same GPU: run_gpu_tests.py 212/240, 28 skipped, 0 failed; w64_graphics.py 29/29; semantic suite 2 files failing (test_image_atomics_guard, test_image_store_d16). No game files.

AI-assisted (Claude Code).

@SpaghettiMaster545

Copy link
Copy Markdown

Tested 390d6bed3 merged onto main dde82f27e9 (clean merge). Windows 11 IoT Enterprise LTSC 2024 (26100), MSVC 14.50 (VS 2026) RelWithDebInfo + Ninja, Vulkan SDK 1.4.363.0 (SPIRV-Tools v2026.4), llvm-mc 23.1.6, Python 3.14.2. Test programs built from main dde82f27e9; this PR does not touch runtime/. GPUs: AMD Radeon Graphics (Ryzen 7 9800X3D iGPU, RDNA2 gfx1036), AMD 24.10.24 (Vulkan 2.0.302), with LEONIDA_PC_TAIL_UBO=1 (128-byte push constants); NVIDIA GeForce RTX 5070 Ti, 621.24. Synthetic tests only, no game files.

  • CPU: test_salu64_mixed_mask_pair 6 OK, test_wave64_emulate_opt 30 OK.

  • GPU method: each of the PR's 56 program(word, mul, add, mask, data) cases (both word orders × 7 masks × 4 data words) translated, validated for vulkan1.2 and run with vk_compute_test <spv> 1 dump (VKC_OUT_WORDS=320). All 64 lanes × 5 words compared with the test's model(). Also limpan1212's nine review cases against the ISA value. Every configuration ran twice, with identical results.

  • With this PR:

    • AMD native wave64 (--require-wave64): 56/56.
    • AMD --wave64-emulate: 56/56.
    • RTX 5070 Ti --wave64-emulate: 56/56.
    • NVIDIA native wave64 is skipped.
  • With main's translator: 20/56 on each of the three. The finder's case 0x5_01010101 gives (34, 30, 0, 33, 1) instead of (6, 58, 0, 1, 1) in 0/64 lanes.

  • limpan1212's review cases: 7 of the 9 still fail on both GPUs, native and emulated, with exactly the words from that review:

    • w32_mask_and_minus1 (10,10), want (42,10)
    • w32_two_masks (10,10), want (15,10)
    • w32_zero_and_mask (0,0xffffffff), want (5,32)
    • w64_hi_in_lo_with_0 (22,10), want (8,0)
    • w64_lo_in_hi_with_0 (8,32), want (22,42)
    • w64_swapped_hi_lo (24,10), want (38,0)
    • w64_dup_lo_in_both (30,10), want (44,10)

    main gives the same words for those 7, so this is not a regression, but it is still open at this head. The other two (w32_mask_and_5, w64_hi_in_lo_with_5) fail on main and pass with this PR.

Pass for the cases this PR claims; the noncanonical-pair cases from the review are confirmed on hardware and still open. AI-assisted (Claude Code).

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.

8 participants