Skip to content

rdna2spirv: fix wave64 s_cmp_eq_u64 / s_cmp_lg_u64 on split half-mask SGPR pairs - #3305

Closed
SweatierKey wants to merge 1 commit into
ItsNotRexed:mainfrom
SweatierKey:fix/w64-scmp-u64-split-mask
Closed

SweatierKey wants to merge 1 commit into
ItsNotRexed:mainfrom
SweatierKey:fix/w64-scmp-u64-split-mask

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Fixes wave64 s_cmp_eq_u64 and s_cmp_lg_u64 when comparing a split lane-mask SGPR pair s[r:r+1] (whose low/high halves were written independently, e.g. s_mov_b64 s[12:13], vcc; s_mov_b32 s12, 0) against 0, exec, vcc, or another mask SGPR pair.

Claimed on #3232 (#3232 (comment)).

Root cause

On main (16282101), s_cmp_eq_u64 / s_cmp_lg_u64's use_vote fast path bypassed mask_src and loaded both raw dwords (s[r], s[r+1]) in every lane, comparing both against (0, 0) or (exec, exec). When a wave64 pair's low and high dwords differ across halves, lanes 0..31 compared the stale high dword and lanes 32..63 compared the stale low dword, yielding SCC = 0 even when the 64-bit masks were equal.

Fix

  • tools/rdna2spirv.py: on the use_vote path of s_cmp_eq_u64 / s_cmp_lg_u64, read each mask operand via self.mask_src(s) (which selects s[r] for lanes 0..31 and s[r+1] for lanes 32..63 in wave64) and compare each lane's single bit once before wave_equal. Keeps the fast OpGroupNonUniformAll path (no OpGroupNonUniformBitwiseOr) and leaves non-mask 64-bit pointer/data comparisons unchanged.
  • tests/semantic/test_wave64_scmp_masks.py: added Wave64SplitPairU64CompareTests covering split pairs vs 0, vs exec, and split-pair vs split-pair, plus spirv-val --target-env vulkan1.2 (native and --wave64-emulate).
  • docs/design.md: updated the Wave64 mask ballot row.

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

  • tests/semantic/test_wave64_scmp_masks.py: 18/18 OK (incl. spirv-val native & --wave64-emulate)
  • test_loop_ballot_barrier (15/15), test_wave64_vcc_half_write (2/2), test_salu64_bitwise_data (11/11), test_text_write_line_endings (2/2), test_docs_design_rows (2/2), test_docs_repo_paths (2/2) — all PASS
  • tools/cf_diff.py --programs 50 --wave both --spirv-val: 100 pass, 0 mismatch, 0 skipped, 0 harness errors

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md).
  • Translator contract updated in docs/design.md with verification status (CPU-checked, not yet GPU-verified).

@LloydNicholson

Copy link
Copy Markdown
Contributor

GPU check of 55da541 on an RX 9060 XT (RDNA4, GFX1200), RADV Mesa 27.0.0-devel, Nobara Linux (Fedora 44): pass

The description says the change is CPU-checked only, so I wrote a small wave64 compute probe and ran it through runtime/test/shaders/build.py and vk_compute_test. It isn't in the repo. Setup: llvm-mc 22.1.8 and SPIRV-Tools 2026.1. The probe passes spirv-val with both translators. v0 is the lane, and vcc = lanes 0-47 from v_cmp_gt_u32_e32 vcc, 48, v0. The split pairs, built as in the description:

  • s[12:13] = vcc, then s_mov_b32 s12, 0: lanes 32-47.
  • s[16:17]: the same steps, the same mask.
  • s[18:19] = vcc, then s18 = 0, s19 = 0xff: lanes 32-39.
  • exec = s_and_b64 exec, vcc, s[14:15] with s[14:15] = lanes 32-63 from v_cmp_le_u32_e64: lanes 32-47.

Each s_cmp_*_u64 result goes through s_cselect_b32 sN, 1, 0, and lane 0 stores it.

case expected this PR base 1628210
s_cmp_lg_u64 s[12:13], 0 1 1 1
s_cmp_eq_u64 s[12:13], 0 0 0 0
s_cmp_eq_u64 s[12:13], exec 1 1 0
s_cmp_eq_u64 s[12:13], s[16:17] 1 1 1
s_cmp_lg_u64 s[12:13], s[18:19] 1 1 1
s_cmp_eq_u64 s[12:13], s[18:19] 0 0 0

The PR's words are the same natively (subgroup 64, --require-wave64) and translated with --wave64-emulate at subgroup 32. The base gives the same wrong word in both modes. On hardware, the split pair against exec is the case the base gets wrong and the PR fixes. The comparisons against 0 and between two split pairs were already right on the base in this probe, so they confirm the PR doesn't break them, not a fix of its own.

Also run:

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

AI-assisted (Claude Code).

@TheMrBeez

TheMrBeez commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested 55da5410 (base 16282101) against native RDNA2, with wave64 emulation on NVIDIA. Pass: the split-pair compares match the hardware, and the base doesn't. AI-assisted run, synthetic shaders only, no game files.

I took the PR's own test_split_pair_vs_zero_exec_and_pair listing (WAVE64_BUILD_VCC_AND_SGPR from s4/s5, then the eight s_cmp_eq/lg_u64 checks, including the exec &= s[14:15] pair) and folded the eight SCC results into bits 0–7 of one word per lane. It ran natively through tools/hw_oracle on an AMD Radeon iGPU (Ryzen 9 9950X3D, gfx1036, driver 25.10.30.02, wave64), and translated through vk_compute_test with s4/s5 from VKC_SGPRS on that iGPU (subgroup 64) and on an RTX 5080 (621.01, --wave64-emulate). This complements @LloydNicholson's RDNA4 probe with a hardware reference and the emulated path.

s4, s5 hardware base, both GPUs this PR, both GPUs
0x0000ffff, 0xffff0000 106 170 ✗ 106 ✓
0x1, 0x80000000 106 170 ✗ 106 ✓
0xffffffff, 0x0 105 170 ✗ 105 ✓
0x0, 0xffffffff 102 170 ✗ 102 ✓
0x12345678, 0x9abcdef0 106 170 ✗ 106 ✓
0x0, 0x0 85 85 85 ✓

All values are uniform across the 64 lanes.

Also: test_wave64_scmp_masks.py OK (1 skipped); scripts/run_gpu_tests.py with this translator: RTX 5080 143/189 native (46 skipped) and 174/189 under LEONIDA_W64_EMULATE=1 (15 skipped), iGPU 174/189 (15 skipped), no failures.

One side note: a simpler version that built the pair with s_mov_b64 s[12:13], vcc instead of s_and_b64 s[12:13], vcc, exec already gave the hardware result on the base (a different set of six compares), so the s_and_b64 … exec form of your listing is what triggered it in my runs.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested 55da541 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 -I tests/semantic/test_wave64_scmp_masks.py — 18/18 OK
  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build — 175/189 passed, 14 skipped, 0 failed (expected D3D12-owned skips + ps_w64x_readlane)

AI-assisted (OpenCode).

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested 55da541 on Windows 11, AMD Radeon iGPU gfx1036 (driver 26.10.1, subgroup 32..64) and RTX 5080 (driver 616.92, subgroup 32): translator checks plus the GPU suite against a main build (LEONIDA_VK_DEVICE set per run, device line confirmed in the runner output, spirv-as/spirv-val and llvm-mc available).

  • tests/semantic/test_wave64_scmp_masks.py: 18/18 OK; tests/test_shader_checks.py: 69/69 OK; regen_fixtures.py --check: clean.
  • scripts/run_gpu_tests.py full suite, no failures: Radeon gfx1036 174/189 passed (15 skipped); RTX 5080 143/189 passed (46 skipped, tests the device cannot run).
  • RTX 5080 with LEONIDA_W64_EMULATE=1 --filter cs_: 90/94 passed, 4 skipped, no failures.
    Limit: the PR adds no GPU test for the split-pair s_cmp_eq_u64 / s_cmp_lg_u64 case, so this shows no regression on either GPU but does not run the fixed case on hardware. cf_diff.py was not run.

AI-assisted.

@boraswim

Copy link
Copy Markdown
Contributor

Tested 55da5410 merged into main f847748c (clean merge, local only) on Windows 11, MSVC 2022, Vulkan SDK 1.4.363, llvm-mc with AMDGPU: pass, and the fix confirmed on two GPUs.

The fixed case on hardware. No GPU fixture in the PR covers a split pair, so I wrote a local one (not pushed) from your semantic test's case: one full wave64, vcc = lanes 0..39, then A = s[12:13] = vcc with s12 = 0, B = s[14:15] = vcc with s15 = 0, C = s[18:19] = vcc with s18 = 0. Lane 0 stores SCC of s_cmp_eq/lg_u64 for A vs 0, B vs 0, A vs B, A vs C, and, with exec narrowed to B, B vs exec and A vs exec. Expected, by hand: 0 1 0 1 0 1 1 0 1 0 0 1.

main this PR
AMD Radeon iGPU (Ryzen 5 7600X, gfx1036), driver 32.0.21045.29003, native wave64 (compute subgroup size: 64) 0 1 0 1 0 1 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 0 1
RTX 4070 SUPER, driver 32.0.16.2124, LEONIDA_W64_EMULATE=1 0 1 0 1 0 1 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 0 1

On main, s_cmp_eq_u64 B, exec gives 0 and s_cmp_lg_u64 gives 1 although B equals the narrowed exec; with the PR all 12 words match. The other ten were already right on main.

Regressions: scripts/run_gpu_tests.py on the AMD iGPU (native wave64): main 184/199 passed, 15 skipped, 0 failed; with the PR the same 184/199, and every test has the same result.

CPU: tests/semantic/test_wave64_scmp_masks.py 18/18 OK (with spirv-val); python -m unittest discover -s tests/semantic OK (5 skipped, 1 expected failure).

AI-assisted.

@towade

towade commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

The corpus side, which the hardware runs above don't cover: 55da541 against its base 1628210 with my own 1.0.2.1 translation (counts only). Windows 11 Pro 10.0.26300, SPIRV-Tools v2026.4.

  • Every listing that uses s_cmp_eq_u64 or s_cmp_lg_u64: 221 of the 26,503, all wave64. Translated with the base and with this head, natively and with --wave64-emulate: 221/221 translated and pass spirv-val in all four runs.
  • All 221 modules are byte-identical between base and PR, in both modes. So the split half-mask pairs never reach these compares in the game's shaders, and merging this changes nothing in their translation. The fix only matters for code like the fixtures above.
  • On an AMD RX 9070 XT (RDNA4, native wave64, AMD 32.0.32029.2010), run_gpu_tests.py from this head: 175/189 passed, 14 skipped, 0 failed. tests/semantic/test_wave64_scmp_masks.py: OK.

AI-assisted (Claude Code); no game data in this comment.

@bvtterbroot

Copy link
Copy Markdown

Tested 55da541 on AMD Radeon RX 7900 XTX (RDNA3, Navi 31; native wave32 and wave64), AMD driver 26.11.2, Vulkan 1.4.359, Windows 11: pass.

  • run_gpu_tests.py: 175/189 passed, 14 skipped.
  • python -I -m unittest discover -s tests/semantic -p test_wave64_scmp_masks.py: 18 tests OK.

Tools: llvm-mc 21.1.8 (WSL), Vulkan SDK 1.4.363.0, MSVC 14.44.

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Closing: now covered on main by #3007 (6ab975ee, @Kosmabb8, merged in Round 42), which fixes s_cmp_eq_u64/s_cmp_lg_u64 on split, swapped, and mixed SGPR mask pairs. Thank you @LloydNicholson, @TheMrBeez, @javdc, @Vonsek, @boraswim, @towade, and @bvtterbroot for all the independent GPU and corpus verifications!

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