Skip to content

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

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

paratustra wants to merge 1 commit into
ItsNotRexed:mainfrom
paratustra:shader-wave64-halves-sgpr-mask-half

Conversation

@paratustra

Copy link
Copy Markdown

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: wave64 graphics shaders that read a pair with one half overwritten move from tier A to C. I can't count them without the game. A wave64 census before and after would show it.

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

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

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

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.

6 participants