Skip to content

rdna2spirv: a wave64 VALU lane mask printed in wave32 syntax is the SGPR pair - #3910

Open
Dom-in wants to merge 1 commit into
ItsNotRexed:mainfrom
Dom-in:w64-mask-pairs
Open

Dom-in wants to merge 1 commit into
ItsNotRexed:mainfrom
Dom-in:w64-mask-pairs

Conversation

@Dom-in

@Dom-in Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claim: #3574 (comment)

The bug

tools/shader_disasm.py disassembles every program in llvm-mc's default wave32 mode. In a wave64 program, a VOP3 lane mask that is the SGPR pair s[6:7] therefore prints as s6; the encoding is the same in both modes, only the text differs. RDNA2's wave64 VOP3 SSRC2 and VOPC/VOP3b SDST are 64-bit pairs. Gen.mask_src only widens an operand written as s[6:7] (n == 2), so for the wave32-printed form, lanes 32-63 read the low SGPR's bits instead of the high one's.

docs/design.md counts 23,841 of the corpus's 26,503 shaders as wave64, and they all reach the translator as wave32-printed listings. Every one that keeps a VALU mask in an SGPR pair, rather than VCC, was translated wrong in its upper half.

How it showed up

XboxBvhBuilder's PrefixSum256 to PrefixSum4096 (TEST-594, #3618) scan in place. Their upper half adds the lower half's total through v_cndmask_b32_e64 …, s6 after s6 = 0, s7 = -1. Translated as before and run 64 wide on an RX 9070, the scan restarted at 0 at element 128 or 256. With this PR, all seven prefix sums match their exclusive scan byte for byte.

The fix

For a wave64 program, widen_wave64_lane_masks runs once after the wave size is known. It reads an even SGPR as the pair at the 64-bit mask positions:

  • v_cndmask_b32_e64's condition;
  • v_cmp_*_e64's destination (v_cmpx writes EXEC);
  • the carry out of v_add/sub/subrev_co_u32, v_mad_u64_u32/v_mad_i64_i32 and v_div_scale_*;
  • both carries of the *_co_ci_u32 forms.

VOP2/VOPC take VCC implicitly (vcc_lo), which is already handled. A listing already written with s[6:7] is unchanged, as is a wave32 program.

Tests

  • tests/semantic/test_wave64_mask_pair_text.py: each wave64 program translates to identical SPIR-V whether written with pairs or with the single SGPRs shader_disasm prints. It covers the v_cndmask condition, a v_cmp_e64 destination read back, both v_add_co_ci carries, and the v_mad_u64_u32 carry. Unit checks cover the positions widened and the ones left alone: data operands, v_cmpx, VCC, odd SGPRs, existing pairs. A wave32 program isn't widened. Without the fix, 6 of 7 fail.
  • GPU fixture runtime/test/shaders/cs_w64_mask_pair_w32text.asm, wired into check.py and run_gpu_tests.py: 0 0 1 1 1 1 0 0 on an RX 9070 at subgroup 64; before the fix, 0 0 0 0 1 1 0 0. The write-then-read half already passed, because the translator keeps a mask a v_cmp writes as each lane's own bit, and it stays as a guard.
  • check.py: 173 of 176 pass, 3 skipped (the mesh fixtures build.py doesn't build), on the RX 9070. run_gpu_tests.py --filter cs_w64_mask: 3/3.
  • The CPU suites (tests/semantic, test_rdna2spirv*, test_wave64*, test_lane_masks*, test_translate*): 8,009 passed, 5 skipped, 1 xfailed, 0 failed. After rebasing onto main aa2988c, the wave64, lane-mask and runner subsets give 408 passed.
  • docs/design.md's Wave64 row records the rule and the checks.

Not measured: how many of the corpus's wave64 shaders change. That needs the corpus disassembled, which I haven't run here. The rule only changes programs with a single-SGPR operand at those positions.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…GPR pair

tools/shader_disasm.py disassembles every program in llvm-mc's default wave32
mode, so in a wave64 program a VOP3 lane mask that is s[6:7] prints as s6 (the
encoding is the same in both modes). Gen.mask_src widened only s[6:7], so lanes
32-63 read s6's bits. For wave64 programs, widen_wave64_lane_masks now reads an
even SGPR as the pair at the 64-bit mask positions: v_cndmask_b32_e64's
condition, v_cmp_*_e64's destination, the carry of v_add/sub/subrev_co_u32,
v_mad_u64_u32/v_mad_i64_i32 and v_div_scale_*, and both carries of *_co_ci_u32.

Found through XboxBvhBuilder's PrefixSum256..4096 (TEST-594), whose upper half
adds the lower half's total through v_cndmask_b32_e64 ..., s6 with s6=0, s7=-1.
GPU fixture cs_w64_mask_pair_w32text (0 0 1 1 1 1 0 0; 0 0 0 0 1 1 0 0 before);
tests/semantic/test_wave64_mask_pair_text.py requires the wave32-printed and
pair-printed forms to translate to identical SPIR-V.

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

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 39f62b5 on Windows, CPython 3.14.7, CPU only (no GPU, no game files): pass.

  • python -m pytest -q tests/semantic/test_wave64_mask_pair_text.py tests/semantic/test_wave64_scmp_masks.py tests/semantic/test_wave64_vcc_half_write.py (venv with the pinned tests/requirements.txt): 29 passed, 140 subtests passed.
  • Negative control: this head's new test file with the merge-base translator (aa2988c): 6 failed, 1 passed.
  • Extra hand-built wave64 case with s22 = 0 and s23 = -1: v_cndmask_b32_e64 v1, 7, 2, s22 and the explicit s[22:23] spelling emit identical SPIR-V.

The GPU fixture was not run. No defect found. AI-assisted.

Dom-in added a commit to Dom-in/Leonida that referenced this pull request Nov 22, 2026
…fix sums

The runner now sets VKC_SUBGROUP_SIZE from index.json's wave64 flag, as
check.py does: a wave64 program left to the device's default may run 32 wide.
PrefixSum64..PrefixSum4096 are checked against their exclusive scan and total.
On main the five wave64 ones fail at element 128 or 256 (a VOP3 lane mask
printed as s6 for s[6:7], fixed by ItsNotRexed#3910); with ItsNotRexed#3910 all twelve checked
programs match.

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

Copy link
Copy Markdown
Contributor

Automated test: 39f62b5b merged onto main aa2988cc on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

  • scripts/run_gpu_tests.py: 193/210 passed, 15 skipped (plain main: 192/209 passed, 15 skipped).
    • new test cs_w64_mask_pair_w32text: pass (0 0 1 1 1 1 0 0)
  • Python tests this PR touches: 7 passed.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@justr4mzi

Copy link
Copy Markdown
Contributor

Tested 39f62b5b on AMD Radeon RX 6700 XT (gfx1031, Windows 11 25H2 build 26200.9974, AMD driver 26.11.2, Vulkan 1.4.315, llvm-mc 21.1.8, SPIRV-Tools v2026.4, built harness from main f847748c):

  • Semantic & AMD RDNA2 GPU tests — PASS:
    • python -I -m unittest discover -s tests/semantic -p "test_wave64_mask_pair_text.py": 7/7 OK.
    • python -I -m unittest discover -s tests/semantic -p "*mask*.py": 245/245 OK.
    • python -I scripts/run_gpu_tests.py --build E:\leonida\runtime\build --filter cs_w64 --require-pass (native wave64, subgroup 64): 22/22 translated, 22/22 pass spirv-val, 22/22 passed (cs_w64_mask_pair_w32text -> 0 0 1 1 1 1 0 0).
    • With LEONIDA_W64_EMULATE=1: 22/22 translated, 22/22 pass spirv-val, 22/22 passed.
    • Negative control on RX 6700 XT (translating cs_w64_mask_pair_w32text.asm with main f847748c's tools/rdna2spirv.py via VKC_SUBGROUP_SIZE=64 vk_compute_test): 0 0 0 0 1 1 0 0 (lanes 32–63 read s6 instead of s7).
  • Docs check — 1 failure (review note):
    • python -I tests/test_docs_cli_flags.py: FAIL (2/3 passed, 1 failed): docs/design.md:327 mentions --checked in `tools/bvh_gpu_smoke.py --checked`, which comes from still-open #3618 and does not exist on main (nor does tools/bvh_gpu_smoke.py).
    • test_docs_design_rows.py (2/2), test_docs_links.py (6/6), test_docs_list_items.py (2/2), test_docs_repo_paths.py (2/2), test_docs_table_rows.py (2/2): passed.

AI-assisted (Gemini, Antigravity).

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 39f62b5 on Windows 11, AMD Radeon iGPU gfx1036 (RDNA2, driver 26.10.1, subgroup 32..64, native wave64) and RTX 5080 (driver 616.92, subgroup 32), runtime built from this head (MSVC, RelWithDebInfo), LEONIDA_VK_DEVICE set per run with the device line confirmed, VK_LOADER_LAYERS_DISABLE=~implicit~ (no validation layer installed):

  • tests/semantic/test_wave64_mask_pair_text.py: 7/7 OK.
  • run_gpu_tests.py --filter cs_w64_mask: Radeon 3/3 (cs_w64_mask_pair_w32text 0 0 1 1 1 1 0 0); RTX 5080 3 skipped (no 64-wide compute subgroup).
  • Negative control on the Radeon: with main's tools/rdna2spirv.py swapped in, FAIL cs_w64_mask_pair_w32text 0 0 0 0 1 1 0 0. That matches the "before" words in the description, now on real RDNA2.
  • Full run_gpu_tests.py: Radeon 196/211 passed, 15 skipped; RTX 5080 164/211 passed, 47 skipped; 0 failed on either.

AI-assisted.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Correction to my earlier comment (Tested 39f62b5): on this machine vk_texture_test, vk_sampler_test and vk_bindless_test ignore LEONIDA_VK_DEVICE and pick the RTX 5080. So in that "Radeon" run_gpu_tests.py run, the texture, sampler and bindless cases actually ran on the 5080. Rerun on the same head with the Radeon forced by VK_LOADER_DRIVERS_SELECT=*amd* (device line AMD Radeon(TM) Graphics): full run_gpu_tests.py 196/211 passed, 15 skipped, 0 failed, so the result stands.

AI-assisted.

@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Following up on @justr4mzi's test_docs_cli_flags.py failure. I reproduced it, and it goes away once #3618 is in. Linux, CPython 3.12.16:

So adding Depends on: #3618 to the description, as CONTRIBUTING asks, covers it, as long as this lands after #3618. Otherwise the design.md row would have to drop the --checked reference until #3618 merges.

AI-assisted.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 39f62b5 merged into main aa2988cc (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New test passes natively: cs_w64_mask_pair_w32text (0 0 1 1 1 1 0 0).
  • Whole run_gpu_tests.py: 197/211 native, 197/211 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 47/47.
  • tests/semantic/test_wave64_mask_pair_text.py: 7 passed.

No game files. AI-assisted (Claude Code).

@itsonlysimbo

Copy link
Copy Markdown
Contributor

Tested 39f62b5b on RTX 4070, NVIDIA 621.14, Windows 11 (10.0.26300), Vulkan SDK 1.4.363, llvm-mc 23.1.6, test programs from main aa2988cc: pass.

  • tests/semantic: 7463 tests (7 more than main), with the same 7 failures and 2 errors main aa2988cc has on this machine (test_loop_ballot_barrier, test_mutations, test_cmp_class_f64); none new.
  • run_gpu_tests.py: 164/211 passed, 47 skipped, 0 failed. The new cs_w64_mask_pair_w32text skips here (no 64-wide compute subgroup on this card); with LEONIDA_W64_EMULATE=1 it passes (0 0 1 1 1 1 0 0).
  • No suite shader's SPIR-V changes against main.

AI-assisted (Claude Code).

@jeremygold02 jeremygold02 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.

I compared aa2988cc with 39f62b5b on an independently indexed, 14-meshlet input. I passed the same default LLVM gfx1013 listing to both translators with --wave64-emulate --mesh-compute. The shader header selects wave64. I checked each emitted triangle's three indices and cull flag against the source index buffer.

Diagnostic readback Base PR head
Correct triangle records 498 / 809 809 / 809
Valid triangles incorrectly marked culled 311 0
Exact vertex positions and UV pairs 1,663 / 1,663 1,663 / 1,663

Every lost record on base came from lanes 32–63 of a logical wave. As a separate control, I disassembled the identical instruction bytes with LLVM's explicit wave64 setting. Translating that listing on an earlier revision produced the same complete output buffer as the PR head using the default listing.

I drew the head's captured positions, UVs and triangle indices with a diagnostic camera and three fragment checks (UVs, a uniform diffuse texture and a nonuniform texture). Each 320 × 96 RGBA32F frame matched a separate draw from the source geometry byte for byte, including all 8,062 covered pixels.

Both revisions used the same diagnostic change to forward the shader's own allocation count through a storage buffer. With the stock allocation path, the head still emitted a zero-count header for the first meshlet. The comparison isolates mask handling. End-to-end allocation and the original surface pixel shader remain separate.

The seven tests in test_wave64_mask_pair_text.py passed. Native Windows Vulkan on NVIDIA GeForce RTX 5070, driver 621.01, subgroup 32. Intel Core Ultra 7 265K, Windows 11 Home 25H2 build 26200.9974, LLVM 21.1.8. Only aggregate results are included here.

AI-assisted by Codex.

@byPreaZy

Copy link
Copy Markdown

Corpus count + full-corpus SPIR-V diff + RX 9060 XT validation for the wave32-printed SGPR-pair mask fix.

Corpus count (the "not measured" item)

I ran the corpus disassembly through translate() up to the widen_wave64_lane_masks call on all 23,841 wave64 shaders (FSSM wave size, 90% of the 26,503-shader corpus), counting operand positions the transform rewrites:

  • 19,436 wave64 shaders (81.5%) have at least one widen site
  • 765,010 widen sites total
  • 18 failed before reaching widen (unsupported earlier in translate(), identical without the fix), 0 errors

The wave32-printed pair form is the common case for wave64 VALU mask operands in this corpus, not a rarity.

Full-corpus SPIR-V diff, aa2988c vs 39f62b5

Translated the whole corpus on both heads, same FSSM sidecars and options:

  • 26,400 translated and spirv-val-valid on both, identical index entry sets, identical wave64 flags (23,760)
  • 19,410 shaders (73.5% of translated) produce different .spv; 6,990 are byte-identical
  • The 26 widened shaders that stayed identical have their sites in unreachable code
  • The 103 shaders that fail on both are a pre-existing aa2988cc issue (fmt-table constants registered after the consts pass), unrelated to this PR; reported separately

So this changes the emitted SPIR-V of about three quarters of the corpus's translated shaders.

GPU validation, AMD Radeon RX 9060 XT (RDNA4)

Windows 11, driver 32.0.32015.7029, native wave64 (subgroup 32..64, native64 via tools/vk_subgroup_info.py), runtime built from 39f62b5b (MSVC, RelWithDebInfo):

  • New fixture cs_w64_mask_pair_w32text: PASS, 0 0 1 1 1 1 0 0
  • cs_w64_mask_detect, cs_w64_mask_imm: PASS
  • Full scripts/run_gpu_tests.py: 197/211 passed, 14 skipped (d3d12_ milestone tests), 0 failed*; 215 shaders translated, 215 pass spirv-val
  • The v_cmp_*_e64 family the fix touches (ps_vcmp_f16_e64, ps_vcmpx_f16_e64, ps_vcmp_64) all PASS natively at subgroup 64

First RDNA4 result for this PR. AI-assisted (Devin).

@nschlaepfer

Copy link
Copy Markdown
Contributor

Independent cross-GPU check of @Dom-in’s wave64 mask fix at 39f62b5b. I applied the translator change from that revision to a clean 03b09a08 build. The disassembly input stayed in LLVM’s normal wave32-printed spelling. The selected mesh shader’s own header indicates wave64.

For one group, the CPU reference predicted 128 vertices and 125 triangles. On an M3 Max through Leonida’s mesh-to-compute path, the baseline returned the right vertex and primitive counts but spuriously culled 61 triangles. Only 64 of the 125 index triples matched. With this PR’s change, all 125 matched, all 128 positions agreed within 2.04e-7, and the range checks recorded zero faults. The native RTX 5090 mesh path covered 180 pixels on the baseline and 261 after the change, matching the 261-pixel CPU reference. Explicit wave64 disassembly provided a separate positive control.

These are controlled geometry checks with supplied draw constants, not a measurement of title frame selection. The separate RTX mesh-to-compute diagnostic returned an empty frame in this setup, so the RTX comparison above is for native mesh execution only.

AI-assisted verification. Only aggregate results are included.

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.