Skip to content

rdna2spirv: fix s_cmp_eq_u64/s_cmp_lg_u64 on split, swapped, and mixed SGPR mask pairs - #3007

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Kosmabb8:fix/scmp-u64-mask-pairs
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Kosmabb8:fix/scmp-u64-mask-pairs

Conversation

@Kosmabb8

Copy link
Copy Markdown
Contributor

Claim

Problem

In tools/rdna2spirv.py, s_cmp_eq_u64 / s_cmp_lg_u64 miscomputed SCC for SGPR pairs whose two 32-bit halves did not both hold the same full-wave mask representation:

  1. Split / half-built wave64 mask pairs such as (vcc_lo, 0), (0, vcc_hi), (vcc_lo, -1), or (vcc_lo, exec_hi) satisfied holds_lane_mask(...), so comparing against 0 or another mask took the use_vote branch. In halves(), SGPR pairs skipped mask_src() and loaded both raw registers (sgpr[r], sgpr[r+1]) directly. Because each lane's sgpr[r] holds that lane's own bit (e.g. lanes 32..63 hold their own vcc bits in a MASK_LO register, and lanes 0..31 hold their own exec bits in a MASK_HI register), comparing both words in every lane produced wrong SCC values.
  2. Partial-wave (MASK_LO, -1) pairs: on a 40-thread wave64, a per-lane vote over launched lanes cannot distinguish unlaunched lane bits (0 in vcc_hi = 0x000000FF vs 1 in -1 = 0xFFFFFFFF), so pairs with EITHER (0/-1) halves must not take the vote path.
  3. Swapped or duplicated halves ((vcc_hi, vcc_lo), (vcc_lo, vcc_lo)) and mixed mask + plain data pairs ((vcc_lo, s7), (s6, vcc_hi) on wave64 or wave32): holds_lane_mask() is False for mixed pairs, causing raw per-lane 0xFFFFFFFF/0 bits to be compared against scalar words; for swapped/duplicated pairs wave_mask(pair) read the wrong half per lane.

Fix

  • In tools/rdna2spirv.py (s_cmp_eq_u64 / s_cmp_lg_u64):
    • Restrict the per-lane wave_equal vote fast path to canonical full-wave masks (exec, mask vcc, or a wave64 (MASK_LO, MASK_HI) SGPR pair) compared against another canonical full-wave mask or 0, and read each lane's bit via self.mask_src(...) (which selects sgpr[r] for lanes 0..31 and sgpr[r+1] for lanes 32..63).
    • On the non-vote path, use a single self.wave_mask(s) for canonical full-wave masks, and materialize non-canonical SGPR pairs word-by-word: each mask word uses wave_mask(reg) (hi for wave64 MASK_HI, lo otherwise) and each plain-data/EITHER word uses load_reg("sgpr", r). Also handle null (0, 0) and data-mode vcc (%vcc, %vcc_hi).
  • In tests/semantic/test_scmp_u64_mask_pairs.py (new file, disjoint from test_wave64_scmp_masks: drop the ctypes int patch, GFX10-less u64 compares and a self-check (follow-up to #2280) #2929 and wave64_halves: s_cmp_eq_u64 and s_mov exec no longer pass as votes the wrong way (false tier A) #2675):
    • Added regression tests covering wave64 split pairs ((vcc_lo, 0), (0, vcc_hi), (vcc_lo, -1), (vcc_lo, exec_hi)), swapped/duplicated halves ((vcc_hi, vcc_lo), (vcc_lo, vcc_lo)), mixed mask+data pairs ((vcc_lo, s7), (s6, vcc_hi)), 40-thread partial wave64 (vcc_lo, -1) vs vcc, wave32 mask pairs, --wave64-emulate assembly check (spirv_asm.assemble), fast-path vote preservation, and plain-data zero-subgroup-op check.

Validation

Environment: Windows 11, portable Python 3.12.10 (stdlib only, python -I), synthetic compute shaders only.

  • Positive control (new tests against unmodified tools/rdna2spirv.py at 89c2d9a):
    • python -I tests/semantic/test_scmp_u64_mask_pairs.py -v -> failed on wave64 split/swapped/mixed pairs (6/8 subtests failed), 40-thread partial wave64 (vcc_lo, -1) failed, and wave32 mixed pair failed.
  • After fix (commit on base 89c2d9a):
    • python -I tests/semantic/test_scmp_u64_mask_pairs.py -v -> 6 tests OK
    • python -I tests/semantic/test_wave64_scmp_masks.py -> 16 tests OK
    • python -I tests/test_resource_hygiene.py -> 2 tests OK
    • python -I tests/test_text_write_line_endings.py -> 2 tests OK
    • python -m compileall -q make_game.py tools tests -> exit 0

Limitations

  • Executed via the CPU SPIR-V mini-interpreter (tests/semantic/spirv_mini_interp.py) and assembled with tools/spirv_asm.py on synthetic shaders only; spirv-val and GPU execution were not run in this environment (the --wave64-emulate case checks translation, absence of OpGroupNonUniform*, and binary SPIR-V assembly because spirv_mini_interp.py does not implement OpControlBarrier).
  • No game binaries, shaders, or extracted assets were used.

AI Assistance

  • AI-assisted contribution (Antigravity). Code changes and tests were reviewed and verified locally as listed above.

@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-551 independent Windows RTX 5070 readback, NVIDIA 616.56. Comparing one result word per invocation against an independent CPU lane-mask calculation, parent 89c2d9a missed 64/97 in the 97-thread dispatch and 104/104 in the 104-thread dispatch. Head 6ab975e matched all 201 words. Both comparisons were exercised with split, swapped and mixed pairs under Wave64 emulation on fixed 32-lane subgroups. All four synthetic modules passed spirv-val and created Vulkan pipelines. These are compute-stage results. GPU report. AI-assisted.

@Doffi4

Doffi4 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Independent GPU check of 6ab975ee on Windows 11 (26200). Ran the PR's hand-written SGPR-mask fixtures on Radeon 780M (26.11.2) and RTX 5060 Laptop (621.01): 4 wave32 and 9 wave64 dispatches per GPU. Wave64 was native on the Radeon and translated with wave64 emulation on the RTX. This includes the 40-thread partial wave64 case.

For the GPU adaptation, assigned a separate output region per lane to avoid concurrent writes to the same address. Verified that every active lane matched the literal expected comparison results. All six generated compute modules passed spirv-val, and the harness confirmed the GPU and requested native subgroup size. Runtime harness sources match 16282101. Synthetic inputs only. AI-assisted testing and reporting.

@kacper-serewis

Copy link
Copy Markdown
Contributor

AI-assisted retail census for SHADER-551. All 26,503 indexed listings were present. The parser recognized 1,196 s_cmp_eq_u64 and 7 s_cmp_lg_u64 sites across 221 listings. Of 1,473 listings with invalid-encoding diagnostics, one contained a recognized compare. Native-byte re-anchoring and reachability pruning removed its single s_cmp_lg_u64 site. That leaves 1,202 reachable compares in 220 listings.

The translator’s control-flow mask analysis found zero split, swapped, duplicated, or mixed mask pairs at those sites. Both SGPR words at every reachable compare had scalar memory loads as their last writers in the same basic block. At 948 sites a compared register held a mask elsewhere in the shader, which explains why register-number matching alone would overcount. No reachable site required an ambiguous predecessor to determine the last writer. Undecoded words are outside the recognized-instruction count.

Using the normal translator and assembler on all 221 candidate listings with local native blobs and stage metadata, parent 89c2d9a and head 6ab975e each translated and passed Vulkan 1.2 spirv-val on 221/221. All 221 SPIR-V binaries were byte-identical between revisions. This static census found no retail instance exercising the fix in this index. The earlier GPU readback exercised synthetic inputs. No retail GPU execution was performed.

@Kam471 Kam471 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 on the PR head: tests/semantic/test_scmp_u64_mask_pairs.py -> 6 tests OK; current main fails 8 (6/8 split/swapped subcases, the 40-thread (vcc_lo,-1), and wave32 mixed). An independent 780-case differential against a Python u64 reference (40 random full-wave64 cases x 15 compare/mask ops, plus 18 partial-wave64 40-thread cases x 10 ops) gives 0 mismatches; split (vcc_lo,0), (0,vcc_hi), swapped, mixed and canonical pairs all agree, and the SCC is a single uniform value per case. Mutation of the expected slot-9 halves gives 5 subtest failures. Regressions (wave64_scmp_masks 16, scalar_bitcmp 21, scalar_alu_misc 21, wave64_halves 66, scselect_b64_data 12, export_valid_mask 16, cf_ref 25, hygiene, compileall) all pass. No game data or absolute paths. Approve.

Minor: the PR adds no docs/design.md contract row (CONTRIBUTING asks for one on a new translator rule) and is 2 commits behind main, though the touched file blob is identical.

@Kosmabb8

Copy link
Copy Markdown
Contributor Author

Added the required SHADER-551 contract entry in docs/design.md in 81cbf33. It distinguishes canonical whole-mask vote comparisons from exact reconstructed SGPR words, covers partial waves/all-ones comparisons and separates CPU tests from the independent Vulkan readback already reported here. Six focused mask-pair tests plus sixteen wave64 scalar-comparison regressions passed on Windows CPython 3.12.10. No native RDNA2 oracle run locally. AI-assisted.

dev-ewann pushed a commit to dev-ewann/Leonida that referenced this pull request Nov 22, 2026
@ItsNotRexed
ItsNotRexed merged commit 0c07183 into ItsNotRexed:main Nov 23, 2026
2 of 4 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.

5 participants