Skip to content

rdna2spirv: fix 32-bit bitwise SALU ops on wave32 and wave64 half lane masks - #3225

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
SweatierKey:fix/salu-b32-lane-masks
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
SweatierKey:fix/salu-b32-lane-masks

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Fixes 32-bit bitwise SALU instructions (s_not_b32 and s_and/or/xor/andn2/orn2/nand/nor/xnor/cselect_b32) when operating on lane masks in wave32 and wave64 half-masks.

Claimed on #3047 (#3047 (comment)).

Bugs fixed (all reproduced on main 16282101)

  1. Wave32 s_not_b32 on lane masks: previously wrote per-lane SCC (OpSelect (r != 0)) instead of wave-wide scc_from_mask (divergent SCC across lanes, e.g. [0, 1, 1, ...] instead of [1, 1, 1, ...]), and constant-folded s_not_b32 exec_lo/vcc_lo, <imm> into a raw scalar constant instead of per-lane mask bits.
  2. Wave32 constant folding into vcc_lo: s_and_b32 / s_or_b32 / s_xor_b32 constant-fold guards only checked exec*, so s_and_b32 vcc_lo, 0x0f, 0x03 stored raw 0x03 into %vcc (making all 32 lanes true).
  3. Wave64 32-bit half-mask bitwise ops:
    • Writing exec_lo called set_exec and overwrote all 64 lanes (clobbering exec_hi); writing exec_hi raised Unsupported: destination exec_hi. Now both route through exec_half_write, preserving the untouched half.
    • Writing vcc_lo/vcc_hi with a non-0/-1 constant or scalar SGPR skipped per-lane bit extraction and dropped vcc from lane_masks.py, breaking subsequent vcc_hi/vcc_lo reads and s_bcnt1.
    • scc_logic fell back to per-lane scc_nonzero in wave64 instead of voting over the active 32-lane half (scc_any_half).
    • Cross-half writes (e.g. s_and_b32 exec_lo, exec_lo, vcc_hi) now collect the opposite half via wave_mask and extract bit lane & 31.

Changes

  • tools/rdna2spirv.py: add wave64 exec_lo/exec_hi handling in Gen.dst; unify 32-bit mask destination/source/SCC handling across s_not_b32 and MASK_SOP2_B32 (_mask_b32_dst_half, _mask_b32_src, _store_b32_mask_dst, sop2_b32_srcs, scc_logic).
  • tools/lane_masks.py: preserve vcc in the lane-mask state when a 32-bit bitwise op (s_not_b32 / BITWISE) writes vcc_lo (or vcc_hi while vcc holds a lane mask) with maskable operands; plain s_mov_b32 vcc_hi, <imm> and SGPR-destination data cases (s_and_b32 s20, exec_lo, 1) stay unchanged.
  • tests/semantic/test_salu_b32_lane_masks.py: multi-lane wave32 & wave64 semantic tests via spirv_mini_interp.run_workgroup + spirv-val --target-env vulkan1.2 (native and --wave64-emulate).
  • docs/design.md: update the Constant lane masks contract row.

Verification (CPU, synthetic tests only, no game files)

  • tests/semantic/test_salu_b32_lane_masks.py: 7/7 OK (incl. spirv-val native & --wave64-emulate)
  • Related suites: test_mask_src_sgpr_const (7/7), test_wave64_vcc_half_write (2/2), test_wave64_scmp_masks (16/16), test_scalar_alu_inv_logic (5/5), test_scalar_alu_and_smem_folding (6/6), test_salu64_bitwise_data (11/11), test_loop_ballot_barrier (15/15), test_lane_masks (ok), test_text_write_line_endings (2/2), test_ci_registration (5/5), test_docs_* (11/11) — all PASS
  • tools/cf_diff.py --programs 100 --wave both --spirv-val: 200 pass, 0 mismatch, 0 skipped, 0 harness errors

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md).
  • New translator rule documented in docs/design.md with verification status (CPU-checked, not yet GPU-verified).

@TheMrBeez

TheMrBeez commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested e630a4c (base 16282101) against native gfx1036 on two GPUs. The half-mask EXEC writes this PR adds are exact. AI-assisted run, synthetic shaders only, no game files.

A hand-written wave64 compute shader (64 threads, not part of this PR; the translator's index reports subgroup 64). vcc = lanes with (lane & 3) == 0, then three marks on a per-lane counter:

s_and_b32 exec_lo, exec_lo, vcc_hi     ; +1   (cross-half source)
s_mov_b64 exec, -1
s_not_b32 exec_hi, vcc_lo              ; +2
s_mov_b64 exec, -1
s_mov_b32 s24, 0x0000ffff
s_and_b32 exec_lo, exec_lo, s24        ; +16  (SGPR source, exec_hi untouched)

The reference is the same body run natively on an AMD Radeon iGPU (Ryzen 9 9950X3D, gfx1036, driver 25.10.30.02) through tools/hw_oracle: lanes 0–15 19,18,18,18…, 16–31 3,2,2,2…, 32–63 17,19,19,19….

translator gfx1036 iGPU (Vulkan, subgroup 64) RTX 5080 (621.01, --wave64-emulate)
base 16282101 refused: Unsupported: destination exec_hi same
this PR 64/64 lanes = hardware 64/64 lanes = hardware

Both modules pass spirv-val. tests/semantic/test_salu_b32_lane_masks.py: OK (1 skipped).

Out of scope here, already covered by #293: a 32-bit op that writes a plain SGPR from VCC (s_and_b32 s22, vcc_lo, 2 then s_cselect_b32) doesn't give the hardware's uniform word (s22 = 0, SCC = 0) on the base or on this PR. In wave64 the base gives 0/258 per lane and this PR 256/258 per lane (the same for vcc_lo, 1, vcc_lo, 5, vcc_hi, 2, vcc_hi, 1). In wave32 both give 258/256. The exec_lo form is right on both. #293's head gives the hardware words for all of these; the details are on #293.

Edited: an earlier version said the base and this PR give identical results for the SGPR-destination case in wave64. That came from test shaders the translator had taken for wave32. Rerun as true wave64 (above), they differ, and both differ from the hardware.

@LloydNicholson

Copy link
Copy Markdown
Contributor

GPU check of e630a4c on an RX 9060 XT (RDNA4, GFX1200), RADV Mesa 27.0.0-devel, Nobara Linux (Fedora 44): wave32 pass, wave64 3 of 4 cases pass, one case still wrong

The description says the change is CPU-checked only, so I wrote two small compute probes against it and ran them through runtime/test/shaders/build.py and vk_compute_test. They aren't in the repo. Each case marks the lanes it leaves on, and lane 0 stores (lane count) | (lowest lane) << 8. Setup: llvm-mc 22.1.8 and SPIRV-Tools 2026.1. Both probes pass spirv-val.

Wave32, subgroup 32. The expected words are derived from the ISA.

case expected this PR base 1628210
s_mov_b32 exec_lo, -1; s_not_b32 exec_lo, 0xfffffff0 → lanes 0-3 4 4 32
s_and_b32 vcc_lo, 0x0f, 0x03 then v_cndmask_b32_e32 ..., vcc_lo → lanes 0, 1 2 2 32
v_cmp_gt_u32 vcc_lo, 16, v0; s_not_b32 s12, vcc_lo; s_cselect_b32 s13, 1, 0 → SCC wave-wide, all lanes 32 32 4112 (lanes 16-31: per-lane SCC)
s_xor_b32 vcc_lo, 0xff, 0x0f then v_cndmask → lanes 4-7 1028 1028 32

The base column reproduces all three wave32 bugs from the description on hardware, and the PR fixes them.

Wave64, native subgroup 64 (--require-wave64), and with --wave64-emulate at subgroup 32, with the same words both ways. The base translator refuses this probe (Unsupported: destination exec_hi).

case expected this PR
s_mov_b64 exec, -1; s_and_b32 exec_lo, exec_lo, 0xff → lanes 0-7, 32-63 40 40
s_mov_b64 exec, -1; s_not_b32 exec_hi, 0xfffffff0; s_mov_b32 exec_lo, 0 → lanes 32-35 8196 8196
s_mov_b64 vcc, -1; s_and_b32 vcc_lo, vcc_lo, 0xf0 then v_cndmask_b32_e32 ..., vcc → lanes 4-7, 32-63 1060 64 (all lanes) ❌
v_cmp_gt_u32 vcc, 40, v0; s_not_b32 s12, vcc_hi; s_cselect_b32 s13, 1, 0 → all lanes 64 64

Still wrong: the third case. With VCC set by v_cmp_eq_u32_e32 vcc, v0, v0 instead of s_mov_b64 vcc, -1, the same s_and_b32 vcc_lo gives the correct 1060. Native and emulated agree, so the translator is at fault, not the driver.

Also run:

  • Full run_gpu_tests.py: 175/189 passed, 14 skipped, 0 failed, native and with LEONIDA_W64_EMULATE=1.
  • tests/semantic: OK. test_salu_b32_lane_masks.py: 7 OK.

AI-assisted (Claude Code).

@maxhurst131

Copy link
Copy Markdown
Contributor

Independent CPU check, AI-assisted.

Ran the full semantic suite on e630a4c9, not only the focused files:

  • Windows 11 build 26300, Python 3.10.11: python -I -m unittest discover -s tests/semantic -> 4,149 tests, OK (505 s), including the new test_salu_b32_lane_masks.py.
  • No GPU, no game files, no llvm-mc used by this suite.

This is a regression check across the whole translator suite; the PR's own wave32/wave64 cases are included, but I did not add a hardware measurement.

@SweatierKey
SweatierKey force-pushed the fix/salu-b32-lane-masks branch from e630a4c to 99c048f Compare November 22, 2026 20:18
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thank you @TheMrBeez, @LloydNicholson, and @maxhurst131 for the native gfx1036 / RTX 5080 / RX 9060 XT GPU checks and full 4,149-test semantic run!

Rebased onto Round 41 (f847748c) and fixed @LloydNicholson's wave64 s_mov_b64 vcc, -1; s_and_b32 vcc_lo, vcc_lo, 0xf0 case in 99c048f7:

  • Root cause: in tools/lane_masks.py, s_mov_b64 vcc, 0x3F promoted k = None to MASK, whereas s_mov_b64 vcc, -1 / 0 had k = EITHER and missed the promotion, so vcc was omitted from cur_masks and the following vcc_lo op fell back to the scalar path.
  • Fix: promote k in (None, EITHER) to MASK for 64-bit immediate copies and bitwise ops writing vcc/vcc_lo in tools/lane_masks.py, and added test_wave64_vcc_half_after_whole_vcc_const_write covering s_mov_b64 vcc, -1; s_and_b32 vcc_lo, vcc_lo, 0xf0 plus s_mov_b64 vcc, 0; s_or_b32 vcc_hi, vcc_hi, 0x0f and s_not_b32 vcc_hi.
  • Verification: test_salu_b32_lane_masks.py 8/8 OK (incl. spirv-val native & --wave64-emulate), test_lane_masks.py ok, related semantic + docs tests all PASS.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested e630a4c on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 143/189 passed, 46 skipped, 0 failed. main 1628210 gives 143/189, 46 skipped, 0 failed. All 194 test shaders translate and pass spirv-val.
  • tests/semantic/test_salu_b32_lane_masks.py: ran 7 tests in 12.298s; OK.

AI-assisted (Claude Code).

@WHOISJARDAS

Copy link
Copy Markdown
Contributor

Tested e630a4c on AMD Radeon RX 9070 XT (RDNA4, native wave32 and wave64), AMD 26.11.2 (LLPC), Windows 11: pass. The runtime was built from main 1628210, which is fine here because this PR doesn't change any runtime file.

Your semantic programs on the GPU. I ran tests/semantic/test_salu_b32_lane_masks.py with its translate_cs and run_cs wrapped, so every run_cs call also dispatched the same SPIR-V with vk_compute_test:

  • the same s4..s7, through VKC_SGPRS;
  • the whole buffer read back through VKC_DUMP.

Every word the interpreter wrote was compared with the GPU's buffer. Wave32 programs ran pinned to 32 lanes, and wave64 programs ran natively on 64 lanes (--require-wave64).

  • This PR: 7/7 tests OK. 113 programs ran (16 wave32, 97 wave64), 14,720 words were compared, and 0 differ from the interpreter.
  • Control: the same test file against main's translator (via LEONIDA_TOOLS_DIR) gives 19 subtest failures and 3 errors (Unsupported: destination exec_hi), as you describe. The 25 programs that still run return the same wrong words on the GPU as in the interpreter (2,880 words, 0 differ). So the bugs you fix show up on hardware, and your new rules match it.

Regressions: none found.

  • scripts/run_gpu_tests.py: 175/189 pass, 14 skipped, none failed. This holds natively and with LEONIDA_W64_EMULATE=1 VKC_SUBGROUP_SIZE=32 VKH_SUBGROUP_SIZE=32, and every test has the same result as on main 1628210.

  • tools/crosslane_fuzz.py --programs 100, seeds 1–3: the output is identical to main's, program by program, in both configurations:

    • --wave both: wave32 native and wave64 emulated;
    • --wave 64 --w64-native: wave64 natively on 64 lanes, and emulated.

    The mismatches the fuzzer already reports on main are unchanged.

AI-assisted (Claude Code).

@kacper-serewis kacper-serewis 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.

SHADER-688: independent CPU probe on 99c048f, eb89f82 (#663), and combined #3225 + #3582 tree bb00d21b preserved a cleared EXEC_HI when restoring EXEC_LO from a saved SGPR. Each respective parent incorrectly reactivated the same eight upper lanes seen on RTX. #3225 merges cleanly with #3582; #663 has a #615 dependency and conflicts with #3582. Full combined-tree RTX matrix is pending. AI-assisted, synthetic CPU validation.

@kacper-serewis kacper-serewis 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.

SHADER-688: the clean #3225 (99c048f) + #3582 (d714ee8) tree bb00d21be4ccb6c3319bea5f8712c8064caa3a27 passed 28/28 synthetic probes, spirv-val Vulkan 1.3, 8960/8960 readback words and 1024/1024 SGPR restore lanes. RTX 5070 616.56, Vulkan 1.4.351, subgroup 32, emulated wave64. #3582 alone had 112 wrong restore words; the common parent rejected eight probes and mismatched 1028 words. Native wave64 was outside this run. AI-assisted.

@WHOISJARDAS

Copy link
Copy Markdown
Contributor

Retested at the new head, 99c048f, on the same RX 9070 XT (AMD 26.11.2, Windows 11): pass. 99c048f is rebased onto f847748 and adds the lane_masks.py fix for the RX 9060 XT case and the extra b32 constant folds. I rebuilt the runtime at f847748 for these runs.

  • Semantic programs on the GPU (same wrapper as above): 8/8 tests OK. 117 programs ran (101 natively on 64 lanes, 16 wave32), 15,232 words were compared, and 0 differ from the interpreter. This includes test_wave64_vcc_half_after_whole_vcc_const_write, so LloydNicholson's case (s_mov_b64 vcc, -1/0, then a b32 op on one half) also matches natively on 64 lanes.
  • scripts/run_gpu_tests.py: 185/199 pass, 14 skipped, none failed, both natively and emulated on 32 lanes. Every test gives the same result as on f847748.
  • tools/crosslane_fuzz.py --wave 64 --w64-native --programs 100, seeds 1–3: the output is identical to f847748's, natively and emulated.

The fuzzer's own mismatches (the same on main and here) all come from programs that read a mask as data. With --no-maskdata, f847748 has 0 mismatches over seeds 1–3 in wave32 native, wave64 native and wave64 emulated. So they belong to #293, not to this PR.

AI-assisted (Claude Code).

@Impre-visible

Copy link
Copy Markdown

Tested 99c048f on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): pass, no regression.

Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4.

  • spirv-val on the modules the PR's semantic tests translate (test_salu_b32_lane_masks.py): distinct modules: 38, pass spirv-val: 38, rejected: 0
  • tests/semantic: 7330 OK
  • build.py: 224 shaders, 224 translated, 224 pass spirv-val; check.py: 139 of 174 pass, 35 skipped (wave64 without emulation, mesh fixtures)
  • run_gpu_tests.py: 204 shaders: 204 translated, 204 pass spirv-val; 153/199 passed, 46 skipped, 0 failed
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py: 184/199 passed, 15 skipped, 0 failed

AI-assisted run (Claude). Synthetic programs only, no game files involved.

@ItsNotRexed
ItsNotRexed merged commit 73ce162 into ItsNotRexed:main Nov 23, 2026
3 of 10 checks passed
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