Skip to content

wave64_halves: an SGPR-pair mask with one half overwritten is mask-half - #4624

Open
paratustra wants to merge 2 commits into
ItsNotRexed:mainfrom
paratustra:shader-wave64-halves-sgpr-mask-half
Open

paratustra wants to merge 2 commits into
ItsNotRexed:mainfrom
paratustra:shader-wave64-halves-sgpr-mask-half

Conversation

@paratustra

@paratustra paratustra commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new SHADER item (claim, point 2, in #4571; point 1 was released to SHADER-819)

What and why

lane_masks keeps a 64-bit lane mask in an SGPR pair as MASK_LO/MASK_HI, one per register. A 32-bit write to one of those registers leaves the pair holding part of a mask: s_mov_b32 s25, 0 after v_cmp_eq_u32_e64 s[24:25], …, or the low register overwritten, or the halves swapped. wave64_halves raises mask-half only for halves of EXEC and VCC (_half), so a v_cndmask or saveexec on such a pair came out tier A, although it isn't exact on 32-wide subgroups:

v_cmp_eq_u32_e64 s[24:25], 40, v0
s_mov_b32 s25, 0                      ; lanes 32..63 of the mask cleared
v_cndmask_b32_e64 v7, 0, 7, s[24:25]  ; exported

On the wave interpreter (test_wave64_halves.Execution.run_both), lane 40 gives 0 with one 64-lane wave and 7 with two 32-lane waves. In wave64 the high register is lanes 32..63, and a 32-wide half has no such word.

Found with a differential search: about 18,000 random synthetic wave64 listings, each run as 64 lanes and as 2×32 lanes, keeping those classified tier A/B whose outputs differ. Then reproduced by hand.

Change (tools/wave64_halves.py, 10 lines): a source SGPR pair that holds a mask kind in either register, but not MASK_LO and MASK_HI in place, raises mask-half. The check is on the read, not the write. So a mask register reused as data afterwards (s_mov_b32 s25, 9 with no later mask read of the pair) stays tier A, and ordinary register reuse is not refused.

Related, not overlapping:

Tested

macOS, Python 3.15.2, CPU only, synthetic listings, no game files.

  • New test_c_half_of_an_sgpr_pair_mask in tests/semantic/test_wave64_halves.py: high half cleared, low half set, s_and_saveexec_b64 on the clobbered pair (all tier C mask-half), and an intact pair whose register is then reused as data (tier A). It fails on main and passes here.

  • Interpreter check of the same shapes, one 64-lane wave vs two 32-lane waves:

    listing main this PR outputs equal on 2×32
    high half cleared A C mask-half no
    low half set A C mask-half no
    saveexec on clobbered pair A C mask-half no
    intact pair A A yes
    intact, then s25 reused as data A A yes
    s_mov_b64 copy of the pair A A yes
  • test_wave64_halves.py OK, test_wave64_halves_soundness.py OK.

  • Full semantic suite, per test, against main (d0835dd): the same 39 failures/errors on both sides (the known test_alu_vop3_mods_hw, test_alu_sdwa_hw, test_cmp_class_f64, test_mutations, test_loop_ballot_barrier, test_vcmp16_opsel ones). Nothing new.

  • The *_w64_* fixtures: I ran the 23 that this machine can analyse without llvm-mc through meta_of, and all get identical tiers on main and on this branch. The other 6 (3 compute, 2 geometry, 1 more) error out the same way on both.

Not tested

  • tests/test_wave64_halves_fixtures.py itself: it needs pytest, llvm-mc and spirv-tools.
  • Corpus effect: none. 465d6a0eac's census of e9387802 over 13,405 wave64 VS/PS/GS of their own copy: A/B/C and every shader's reasons unchanged, all .spv byte-identical (comment). So no shader in the game reads a pair with one half overwritten; the rule guards the classifier, it moves nothing today.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No new rule: this extends the existing mask-half refusal from EXEC/VCC halves to SGPR-pair masks.)

AI-assisted (Claude Code).

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 1c194524 on Linux (x86_64, Python 3.13.5, SPIRV-Tools on PATH): pass (code read + unit tests).

What I ran:

  • git diff --check d0835dd9...HEAD: clean.
  • PYTHONPATH=tests/semantic python3 -m unittest -v tests/semantic/test_wave64_halves.py (with LEONIDA_REQUIRE_SPIRV_TOOLS=1): 74/74 OK (including the new test_c_half_of_an_sgpr_pair_mask covering overwritten high/low halves, s_and_saveexec_b64, and intact pair / later data reuse staying Tier A).

Code review:

  • Checking any(k is not None for k in ks) and ks != (lane_masks.MASK_LO, lane_masks.MASK_HI) on 2-SGPR sources cleanly flags partial-half overwrites and swapped halves as mask-half (Tier C), while leaving intact (MASK_LO, MASK_HI) pairs and plain (None, None) data pairs untouched.

@theswiss24

Copy link
Copy Markdown

Tested 1c194524 (contains main d0835dd9) on two GPUs in a Dell Latitude 5501 (Linux Mint 22.3), test programs built from main d0835dd9 (this PR changes no C++), llvm-mc 20.1.2, LEONIDA_W64_EMULATE=1: pass. Every test has the same result as main on each device:

GPU driver run_gpu_tests.py
NVIDIA MX150 (Pascal) 580.178.04 185/210 passed, 25 skipped, 0 failed
Intel UHD 630 (Gen9.5, subgroup 8..32) Mesa ANV 25.2.8 181/210 passed, 27 skipped, 2 failed, the same 2 as main (cs_fmt_int_narrow, my finding in #4571, and cs_signed_zero_nan, the known Mesa 25.2 NIR issue)

No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

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

Ran this at 1c194524 on Windows 11: tests/semantic/test_wave64_halves.py -> 74 tests, OK; tests/semantic/test_wave64_halves_soundness.py -> 30 tests, OK; git diff --check clean.

A two-register SGPR source whose halves are both present but not (MASK_LO, MASK_HI) — one half overwritten by s_mov_b32, or the halves swapped — now raises mask-half and tier C. The new cases cover hi, lo, saveexec and the intact pair that must stay tier A. Merge check: git merge-tree with #4619 and with #4620 is clean (exit 0) for both, so this can land in any order with those two.

No blocking issue.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 1c19452 merged with main d0835dd on WSL2 Ubuntu (Python 3.12.16) and native Windows 11 (Python 3.13.1): pass.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested head 1c194524 — Windows, CPython 3.15.2, CPU only, synthetic fixtures, no game files: pass.

  • python -m unittest discover -s tests/semantic -p "test_wave64_halves.py" -> 74/74 OK.
  • Control (main's tools/wave64_halves.py + this test file): 1 failure — the half-overwritten SGPR-pair mask case is not caught on main, as intended.
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

@465d6a0eac

Copy link
Copy Markdown
Contributor

Census (TEST-1006) of 1c194524: 84 shaders move to tier C (70 A→C, 14 B→C), and 7,092 tier C shaders gain mask-half. Every flagged site I looked at reads a pair whose low register the lane-mask state holds as MASK, not MASK_LO. That looks like a false positive.

Setup. Each PR is merged onto main 0db7fed5 (clean merge) and compared with 0db7fed5 itself, from my own 1.0.2.1 copy, counts only. Native mode: halves_analysis returns early under --wave64-emulate. The run covers the 13,405 wave64 VS/PS/GS, picked by the FSSM wave-size flag and the carve-index stage; no other shader reaches wave64_halves. WSL Ubuntu, Python 3.14.4, llvm-mc 21.1.8, SPIRV-Tools v2026.1, CPU only. Both sides translate all 13,405 and pass spirv-val on all 13,405. The .spv files are byte-identical on both sides, so only the tier metadata changes. On 0db7fed5, wave64_halves.py --census gives A 5,054 / B 106 / C 8,245 (VS 1,455 / 79 / 702, PS 3,404 / 27 / 7,543, GS 195 A).

0db7fed5 + #4624
A / B / C 5,054 / 106 / 8,245 4,984 / 92 / 8,329
VS A / B / C 1,455 / 79 / 702 1,426 / 73 / 737
PS A / B / C 3,404 / 27 / 7,543 3,363 / 19 / 7,592
tier C with mask-half 759 7,935
  • Moves. 70 A→C (29 VS, 41 PS) and 14 B→C (6 VS, 8 PS). The only new reason in each is mask-half.
    7,092 shaders that were already tier C gain mask-half too. No reason is lost anywhere.

What triggers it. I recorded (held[r], held[r+1]) at each violation("mask-half"), using a local copy of the
tool that is not committed. This covers all 84 moved shaders, plus the first 300 (by name) of the 7,092 that were
already tier C.

sites 84 moved shaders 300 already tier C
(MASK, None) 340 23,666
(MASK, MASK_HI) 184 7,393
anything else 0 0

The ops are s_or_b64, s_and_b64, s_and_saveexec_b64, s_xor_b64 and s_andn2_b64 on a 64-bit mask pair.
Not one site has the shape the PR targets: a low half left as MASK_LO with the high register overwritten, or
swapped halves (MASK_HI, MASK_LO). The low register always holds MASK. lane_masks.put() never writes MASK into
one register of a pair: it splits it into MASK_LO / MASK_HI. So I think the MASK comes from the join in
lane_masks.analyse(). Two paths that disagree on a register's kind merge to MASK, and a register that only one
path has is dropped by the key intersection.

That means an intact 64-bit mask reaching a join looks "not in place" to the new check. I haven't proved this for
every site. Two possible fixes: accept MASK in the low register, or use a dedicated "unknown half" marker at the
join instead of MASK. Re-running the census then would show how many real half-overwrites the corpus has.

AI-assisted (Claude Code). Part of TEST-1006 on the coordination issue.

@paratustra

Copy link
Copy Markdown
Contributor Author

Thanks @465d6a0eac. That census is exactly the check this needed, and your reading is right. lane_masks.analyse() joins two different kinds of one register to a whole MASK, so (MASK, None) and (MASK, MASK_HI) mean that two paths met there, not that a half was overwritten. The first version flagged them.

e938780 now flags only a half that is out of place: a MASK_LO/MASK_HI that isn't in its own register, with no MASK anywhere in the pair. So:

(held[r], held[r+1]) before now
(MASK, None), (MASK, MASK_HI) (your census sites) mask-half not flagged
(MASK_LO, None): high register overwritten mask-half mask-half
(None, MASK_HI): low register overwritten mask-half mask-half
(MASK_HI, MASK_LO): swapped mask-half mask-half
(MASK_LO, MASK_HI): intact not flagged not flagged

The PR's listings keep their tiers: the three half-overwrite shapes are still tier C and the outputs differ on two 32-lane waves, while the intact pair, the pair reused as data and the s_mov_b64 copy are still tier A. test_wave64_halves.py and test_wave64_halves_soundness.py are OK.

Going by your table, I'd expect the 84 moves and the 7,092 extra mask-half reasons to drop to about zero. If you can re-run the census at this head, it would show whether the corpus has any real half-overwrites at all. If it has none, this PR only closes a hole that the game doesn't use, and it should say that.

AI-assisted (Claude Code).

@DenRakEiw

Copy link
Copy Markdown

Tested head e9387802a5e03246910b1de6066e696dcf2a6bfd on Windows 11 / Python 3.12.10.

  • Trial merge: Merged cleanly onto origin/main (d974c88a), git diff --cached --check passed (tools/wave64_halves.py +12, tests/semantic/test_wave64_halves.py +13).
  • Tests:
    • python -m unittest discover -s tests/semantic -p test_wave64_halves.py: 102/102 passed
  • Code read: Analysis.transfer flags mask-half whenever a 2-SGPR source holds MASK_LO or MASK_HI out of place (not (MASK_LO, MASK_HI) and excluding joined MASK), catching single-half clobbers and swapped halves without breaking intact pair reads or subsequent register reuse.

AI-assisted review (Gemini 4 Neon).

@AwkGrep256

Copy link
Copy Markdown
Contributor

Tested e938780 on Linux (CachyOS, kernel 7.3.5, Mesa 26.3.1-arch3.5 RADV): RX 9070 XT (gfx1201) and the Ryzen 7 9800X3D iGPU (gfx1036). Pass, and the divergence is real on hardware.

  • I wrote your example as a pixel fixture (ps_w64_c_mask_half, below). Every pixel sets its bit, s_mov_b32 s25, 0 clears lanes 32..63, and v_cndmask_b32_e64 v6, 0, 7, s[24:25] follows.
  • Classifier: main (ec34b54f) calls it tier A; this PR calls it tier C mask-half.
  • GPU: w64_graphics.py draws it with the fragment subgroup size required to 64 and to 32. On both GPUs r0 differs between the two sizes, i.e. tier C.
  • tests/semantic/test_wave64_halves.py: 74 passed.

Fixture body (header and interpolation as in ps_w64_c_vote_stale; the s_mov_b64 marks a synthetic listing wave64), registered as "ps_w64_c_mask_half": ("C", lambda i: (None, 0, 0, 0), {}) in w64_graphics.py:

s_mov_b64 s[10:11], exec
v_or_b32_e32 v4, v2, v3
v_cmp_le_u32_e64 s[24:25], 0, v4
s_mov_b32 s25, 0
v_cndmask_b32_e64 v6, 0, 7, s[24:25]
v_mov_b32_e32 v8, 0
exp mrt0 v6, v8, v8, v8 done vm
s_endpgm

AI-assisted (Claude Code).

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Census of e9387802 (the new head, after @465d6a0eac's TEST-1006 census of 1c194524). Counts only, from my own 1.0.2.1 copy.

Setup: I merged e9387802 onto main 98fa503f (clean merge, 801beaec) and translated the whole corpus there in native mode (--no-asm, 8 shards, merged). I compared it with a main 98fa503f index from earlier today, then ran wave64_halves.py --census on both.

main 98fa503 this PR merged
translated / spirv-val valid 26,503 / 26,503 26,503 / 26,503
wave64 VS/PS/GS tier A / B / C 5,054 / 106 / 8,245 5,054 / 106 / 8,245
mask-half reasons 759 759
shaders whose index entry differs 0
  • The false positives are gone: the 84 tier moves (70 A→C, 14 B→C) and the 7,092 extra mask-half reasons from 1c194524 no longer appear.
  • This corpus has no out-of-place SGPR-pair half: no shader moves tier, so the rule changes nothing in 1.0.2.1. It still fixes the classification of the synthetic shape in the PR description.

Tests: python -m unittest discover -s tests/semantic -p "test_wave64_halves*.py" on the merged tree passes. Linux (Arch, Python 3.14), CPU only. GPU-suite results for this head will follow from a separate run.

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

@KkelebekK

Copy link
Copy Markdown

Reviewed and tested e938780 on Linux (CachyOS, kernel 7.3.4), Python 3.14.10, CPU only, synthetic listings, no game files.

  • Merges into current main (af70fc7) without conflicts. test_wave64_halves: 74 OK on the branch, 102 OK merged (101 on main). test_wave64_halves_soundness 30 OK, test_wave64_vcc_half_write 7 OK, and the other modules that use wave64_halves (salu_xor_saveexec_b64, valu_permlane16, valu_fma_f64/f16, valu_trig_f32) are OK. Full tests/semantic: main 6245 tests, merged 6246, both with the same 3 failures and 2 errors, all in test_loop_ballot_barrier (fixed by tests/semantic: run the structured wave64 emulation in test_loop_ballot_barrier #4808). Nothing new.
  • The new test alone on main's code fails ('A' != 'C'), as stated.
  • Your 6-row table reproduces on the wave interpreter: same tiers, same 64 vs 2×32 results. Also C now, and differing on the interpreter: restoring EXEC from a saveexec pair whose s13 was cleared (31 lanes), and s_cmp_lg_u64 on a clobbered pair (64 lanes). Still A/B, and equal: a full rewrite by a later v_cmp, both halves reused as data, a clobbered pair used only as a saveexec destination, an in-place v_writelane/v_readlane spill.

False negative: the same clobber behind a uniform branch, or across a loop back edge, stays tier A:

v_cmp_eq_u32_e64 s[24:25], 40, v0
s_cmp_eq_u32 s4, 0
s_cbranch_scc1 1
s_mov_b32 s25, 0
v_cndmask_b32_e64 v7, 0, 7, s[24:25]
v_mov_b32_e32 v6, 1

Tier A, and with s4 = 1, lane 40 is 0 in one 64-lane wave vs 7 in two 32-lane waves. s_mov_b32 s24, -1 in that spot is also A and differs in 31 lanes. A loop that reads the pair, then does s_mov_b32 s25, 0 and branches back, is also A (lane 40 is 7 vs 14 after 2 iterations). The cause looks like lane_masks.analyse joining MASK_HI with EITHER into MASK_HI, so the read sees an intact pair. The translator's split-pair handling relies on EITHER (include_either), so the fix probably belongs in wave64_halves' own state (say, a flag for a 32-bit write to a mask half's partner, joined by union and checked at the read) rather than in lane_masks.

Two smaller notes:

  • Swapped halves through parked VGPR lanes are now C (B on main). The interpreter can't confirm this either way, because the 64- and 32-lane runs both give the unswapped result. By the same join, (LO,HI) on one path and (HI,LO) on the other become (MASK,MASK) and pass the MASK not in ks exception; I have no execution evidence for that one. The commit message cites a TEST-1006 census for the exception, which the description doesn't mention.
  • Unrelated and pre-existing: s_and_b64 s[24:25], s[24:25], 0xffffffff followed by v_cndmask is A on main and here, but differs at lane 40. The translator zero-extends the literal, while wave64_halves treats 0xFFFFFFFF as an all-ones mask constant.

AI-assisted.

@abilliontokens

Copy link
Copy Markdown
Contributor

Tested e9387802 on AMD Radeon RX 9060 XT (RDNA4, AMDVLK 32.0.32029.2010, subgroup 32..64, Windows 11): pass (CPU-only change; no GPU path).

  • python -I -m unittest discover -s tests/semantic -p "test_wave64_halves.py": 74 tests OK.
  • python -I -m compileall -q tools tests: clean.
  • No GPU test needed: the diff is tools/wave64_halves.py classification + its semantic test only; the interpreter's own wave64-vs-2×32 check runs inside the unittest.

@465d6a0eac

Copy link
Copy Markdown
Contributor

Census (TEST-1006) of e9387802, the fix for the whole-MASK case: no change on the corpus now.

Same setup as my earlier comment: merged onto main (d974c88a now; tools/ differs from 0db7fed5 only in exe_checklist.py, gdk_runtime_calls.py and saves.py) and compared with my 0db7fed5 run, over the 13,405 wave64 VS/PS/GS of my own copy, counts only.

0db7fed5 + #4624 at 1c194524 + #4624 at e9387802
A / B / C 5,054 / 106 / 8,245 4,984 / 92 / 8,329 5,054 / 106 / 8,245
tier C with mask-half 759 7,935 759

No shader's reason set changes, and all 13,405 .spv files are byte-identical. So all 84 moves and all 7,092 new mask-half reasons came from the join's MASK. With that excluded, no wave64 VS/PS/GS in the game reads a pair with one half overwritten or with swapped halves. The rule changes the result only for the synthetic listings, where it is right (and @AwkGrep256 showed the divergence on hardware).

AI-assisted (Claude Code).

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested e938780 on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 196/210 passed, 14 skipped, 0 failed on both GPUs, and every test has the same result as at the merge base d0835dd. This PR changes no C++, so this mostly confirms that the base is sound.
  • python -m unittest discover -s tests/semantic: 7457 run, 7 failures and 2 errors, the same 9 tests by name as at the base d0835dd (test_loop_ballot_barrier, test_mutations and test_cmp_class_f64). Nothing new.
  • Note: the merge base is 922 commits behind main. The census in my earlier comment covers this head merged onto main 98fa503.

No game files. AI-assisted.

@memorypressure

Copy link
Copy Markdown

Merged with main 3faac55; test_wave64_halves runs 102/102 under unittest discover (re-run) and the four sibling suites sharing the classifier (soundness, permlane16, fma_f64, trig_f32) stay green. The pair-mask distinction is exactly right — half-clobbered pair versus swapped pair versus a register two paths joined as a whole MASK — and excluding the whole-mask join from the new tier is what keeps the tier-A controls passing. Note: the branch reports no CI checks at all — worth a push. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

lane_masks keeps a 64-bit mask in an SGPR pair as MASK_LO / MASK_HI
per register. A 32-bit write to one of them (s_mov_b32 s25, 0 after
v_cmp_eq_u32_e64 s[24:25], ...) leaves the pair holding part of a mask,
and wave64_halves raised mask-half only for halves of EXEC and VCC. A
v_cndmask or saveexec on that pair was tier A, although one 64-lane
wave and two 32-lane waves give different words: in wave64 the high
register is lanes 32..63, and a 32-wide half has no such word.

A source SGPR pair that holds a mask kind in either register, but not
MASK_LO and MASK_HI in place, now raises mask-half. The check is on
the read, so reusing a mask register as data afterwards stays tier A.
The census on the game's 13,405 wave64 VS/PS/GS (TEST-1006) found every
mask-half site of the first version reading (MASK, None) or
(MASK, MASK_HI): a register lane_masks merged to MASK where two paths
met with different kinds, not a half overwritten. Only a half out of
place (MASK_LO / MASK_HI not in their own registers, and no MASK in the
pair) is flagged now.
@paratustra
paratustra force-pushed the shader-wave64-halves-sgpr-mask-half branch from e938780 to 8aab345 Compare November 23, 2026 14:29
@paratustra

Copy link
Copy Markdown
Contributor Author

Rebased onto main 1cc6e3c1 (head 8aab345d; the conflict was only #4585's v_cmpx tests landing next to these). test_wave64_halves.py 126/126 and the soundness, permlane16, fma_f64 and trig_f32 suites pass. The description now records the census result: no change on the corpus.

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.