Skip to content

wave64_halves: fix soundness gaps in RMW, SCC, movrel, s_bfm_b64, and EXEC tracking - #1982

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
plentilha:fix/wave64-halves-soundness
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
plentilha:fix/wave64-halves-soundness

Conversation

@plentilha

Copy link
Copy Markdown
Contributor

Claim: #1745 (comment) (carried over to #1902)

Summary

Closes several soundness gaps in tools/wave64_halves.py where partition-dependent wave64 VS/PS/GS programs could be classified as Tier A or B (or lose a genuine wave vote) instead of Tier C, and adds regression tests in a standalone test module tests/semantic/test_wave64_halves_soundness.py (leaving tests/semantic/test_wave64_halves.py and lines ~370–380 of tools/wave64_halves.py untouched so this stays conflict-free with #1875 and #1830):

  1. In-place RMW destinations, s_pack, v_swap_b32, and implicit operands (operands, Analysis.step, Analysis.transfer)

    • Remove s_pack from NO_DEST so s_pack_* destinations are written in dataflow and tracked in stale_uses().
    • Add RMW_DEST (s_addk_i32, s_mulk_i32, s_cmovk_i32, s_cmov_b32/b64, s_bitset*, v_*fmac/mac*, v_dot*c*) so existing taint on the read-modify-write destination is preserved in srcs.
    • Swap taint, tags, uniformity, rank tokens (st["r"]), and base tokens (st["b"]) cleanly for v_swap_b32 under full exec, and conservatively union taint/rank/base (clearing uniformity and marking ELECT if in an elected region) under narrowed/elected exec.
    • Attach implicit "vcc" only via srcs (len(srcs) == 2, plus "vcc" check in srcs for v_div_fmas_*) so 4-operand v_add_co_ci_u32 / v_sub_co_ci_u32 / v_subrev_co_ci_u32 (whose destination a[1] is vcc) still read implicit vcc; also attach implicit "scc" on s_cmovk_i32 and "m0" on MOVREL_OPS.
  2. SCC-preserving ops & non-vote SCC writes (SCC_LESS, Analysis.transfer)

    • Add s_mul, s_pack, s_sext, s_flbit, s_ff0, s_ff1, s_brev, s_bfm, s_bitreplicate, and s_get_waveid to SCC_LESS so they do not clobber a prior wave-vote or tainted scc.
    • Only mark scc as a ("vote", ...) producer on s_bcnt1_i32_b64 (not s_ff1_i32_b64, which does not write SCC); on s_bcnt0_* / s_bcnt1_i32_b32 over a mask, taint T["scc"] and clear any prior vote tag.
  3. s_bfm_b64 and relative register indexing (Analysis.transfer, Analysis.stale_uses)

    • Taint s_bfm_b64 with LANE (matching tools/rdna2spirv.py per-half wave_lane() comparison).
    • Handle s_movrels_b32/b64, v_movrels_b32, and v_movreld_b32/v_movrelsd_b32/v_movrelsd_2_b32/v_swaprel_b32: propagate VGPR/SGPR taint (and MASKD | X when SGPR masks or parked/VGPR lane masks exist in self.masks), taint with ELECT inside elected regions, invalidate affected st["u"] / st["l"] / waterfall tags at/above the base register, and flag stale SGPR reads in stale_uses() for s_movrels_*.
  4. EXEC updates, _wrexec_, s_andn1_*/s_andn2_*, and _b32 mask halves (SAVEEXEC, Analysis.exec_write, Analysis.narrows)

    • Catch (op.startswith("s_") and op.endswith("_b32")) as mask-half in exec_write.
    • Evaluate the SAVEEXEC source before overwriting dests[0], store site (new EXEC) as the saved token for _wrexec_ vs old for _saveexec_, restrict narrowing / positive mask tag propagation on s_and*_saveexec_* / s_and*_wrexec_*, and add s_and_wrexec_b64 / s_andn2_wrexec_b64 to narrows().
    • For general EXEC writes, combine taint across all srcregs, only forward mg when len(srcregs) == 1, require "exec" among srcs for s_or_b64 / s_and_b64, and require len(srcs) == 2 and _regs(srcs[0]) == ["exec"] with mg=None for s_andn2_b64.
  5. Regression tests (tests/semantic/test_wave64_halves_soundness.py)

    • 27 synthetic test methods covering each fixed failure mode (including 1x64-vs-2x32 wave-interpreter divergence checks on integer VS cases) plus clean Tier A/B controls.

Verification

  • python -I tests/semantic/test_wave64_halves_soundness.py -v: Ran 27 tests in 1.472s, OK
  • python -I tests/semantic/test_wave64_halves.py -v: Ran 57 tests in 1.055s, OK
  • python -m compileall -q make_game.py tools tests: exited 0
  • python -m unittest discover -s tests/semantic -v: Ran 1671 tests in 334.490s, OK (skipped=2)
  • Confirmed LF line endings (\r\n count = 0) on both files and git diff --cached --check clean.

Environment & disclosures: Tested on Windows 11, Python 3.11.3, NVIDIA GeForce RTX 3070 (driver 32.0.16.2124) — CPU/semantic tests only (no GPU execution tests or C++ runtime build run; no game data used). AI-assisted.

… EXEC tracking

Closes several soundness gaps where partition-dependent wave64 VS/PS/GS programs could be classified as Tier A or B instead of Tier C:
- RMW destinations & s_pack: track in-place read-modify-write sources (s_addk_i32, s_mulk_i32, s_cmovk_i32, s_cmov_b32/b64, s_bitset*, v_*fmac/mac*, v_dot*c*), remove s_pack from NO_DEST, handle v_swap_b32 rank/base/tag/uniformity swap (conservatively unioning under narrowed/elected exec), and attach implicit vcc (len(srcs) == 2), scc (s_cmovk_i32), and m0 (MOVREL_OPS).
- SCC tracking: add SCC-preserving scalar ops (s_mul, s_pack, s_sext, s_flbit, s_ff0/ff1, s_brev, s_bfm, s_bitreplicate, s_get_waveid) to SCC_LESS, stop s_ff1_i32_b64 from marking SCC as a wave vote, and keep s_bcnt0_*/s_bcnt1_i32_b32 SCC taint without creating a vote.
- s_bfm_b64 & dynamic register indexing: taint s_bfm_b64 with LANE; propagate VGPR/SGPR taint, mask-SGPR/parked-VGPR-mask usage (MASKD | X), elected-lane taint, uniformity/literal invalidation, and stale SGPR checks across v_movrels_b32, s_movrels_b32/b64, v_movreld_b32, v_movrelsd_b32, v_movrelsd_2_b32, and v_swaprel_b32.
- EXEC / mask-half rules: check s_*_saveexec_b32 / s_*_wrexec_b32 via mask-half, handle s_andn1_saveexec_b*/s_andn1_wrexec_b*/s_andn2_wrexec_b64 narrowing and destination tokens accurately, evaluate SAVEEXEC source before overwriting dest, and check EXEC presence/operand order in s_and_b64 / s_or_b64 / s_andn2_b64.
- Add synthetic regression classification and 1x64-vs-2x32 wave-interpreter execution tests in tests/semantic/test_wave64_halves_soundness.py.

Claim: ItsNotRexed#1745 (comment)

@Flyander Flyander 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 found two remaining soundness issues in the wave64 analysis:

  1. Dynamic movrel destination taint is incomplete (tools/wave64_halves.py:617–645). For v_movreld_b32 and related operations, hardware writes to a VGPR selected by dst_base + M0, but this path invalidates the destination range and then propagates wt only to T[("v", dst_base)]. With M0 = 1, a value tainted by lane identity can be written to the next VGPR while that register remains untainted in the analysis. A later export can then avoid the required Tier C classification. Please propagate the taint and RNK/BASE tokens to every possible destination, or conservatively refuse dynamic indexed destinations. The current tests cover the zero-offset case only. RDNA2 ISA §6.6 describes the indexed destination behavior: https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf

  2. ANDN2 is treated as a narrowing EXEC guard (tools/wave64_halves.py:899–901). narrows() accepts s_andn2_wrexec_b64, but its ISA operation is EXEC = S0 & ~EXEC; it can re-enable lanes outside the old EXEC mask. Since vote() relies on narrows() as proof that a VCC branch is safe, this can suppress unsafe-vote-branch and incorrectly classify partition-dependent code. exec_write() already treats ANDN2 as non-narrowing. Please remove both ANDN2 variants from the narrowing proof and recognize the ANDN1 variants whose result is ~S0 & EXEC. The opcode semantics are in RDNA2 ISA §12.3 (same manual linked above).

@Impre-visible

Copy link
Copy Markdown

Tested 69bd312 on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): pass, no regression.

Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4.

  • spirv-val on the modules the PR's semantic tests translate (test_wave64_halves_soundness.py): distinct modules: 38, pass spirv-val: 38, rejected: 0
  • tests/semantic: 1671 OK
  • build.py: 185 shaders, 185 translated, 185 pass spirv-val; check.py: 122 of 157 pass, 35 skipped (wave64 without emulation, mesh fixtures)
  • run_gpu_tests.py: 178 shaders: 178 translated, 178 pass spirv-val; 133/173 passed, 40 skipped, 0 failed
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py: 164/173 passed, 9 skipped (8 fixtures run by the d3d12 tests, 1 needs a 64-wide fragment subgroup), 0 failed
  • build.py, this head against main 6a6704d: all 185 test modules byte-identical, and no wave64_graphics tier changes in index.json (2 A, 4 B, 9 C on both sides). So the stricter classifier does not move any existing fixture.
  • runtime/test/shaders/w64_graphics.py: tier A 2/2 ok, tier B 4/4 ok, tier C 8 SKIP (expected: this GPU can only require 32)

Not covered here: the corpus census (wave64_halves.py --census), where the tier counts could move.

AI-assisted run (Claude). Synthetic programs only, no game files involved.

… from narrows()

Address @Flyander's review comments on PR ItsNotRexed#1982:
- Propagate wt and rank/base tokens (st['r'], st['b']) across every referenced VGPR >= dst_base on v_movreld_b32/v_movrelsd_b32/v_movrelsd_2_b32/v_swaprel_b32 (and read-side VGPRs >= src_base on v_movrels_b32/relsd/swaprel in step()), so non-zero M0 targets (e.g. M0 = 1) preserve downstream taint into sinks.
- Remove s_andn2_saveexec_b64 and s_andn2_wrexec_b64 from narrows(); keep s_andn1_saveexec_b64 and s_andn1_wrexec_b64 excluded as well because ~S0 & EXEC runs all lanes when S0 == 0, which diverges on 1x64 vs 2x32 behind s_cbranch_vccz.
- Add regression and 1x64-vs-2x32 divergence tests for non-zero M0 v_movreld_b32 (both tainted and clean control) and ANDN1/ANDN2 in vote branches.
@plentilha

Copy link
Copy Markdown
Contributor Author

Thanks for the review @Flyander — pushed follow-up commit 1222fd54 addressing both points:

  1. Dynamic movrel destination taint & token propagation (tools/wave64_halves.py)

    • While bits_violation(wt) at the v_movreld_b32 site already produced Tier C when wt != 0, T only tagged ("v", dst_base). We now collect referenced VGPR indices in Analysis.__init__ (self.vgprs) and propagate wt (in transfer()) as well as RNK/BASE tokens (st["r"], st["b"] in step(), including read-side VGPRs >= src_base for v_movrels_b32/v_movrelsd*/v_swaprel_b32) across every referenced VGPR >= dst_base (preserving pre-existing tokens since a dynamic write may leave a register untouched).
    • Added test_v_movreld_b32_nonzero_offset_propagates_taint (vs_vmovreld_src_off1, m0 = 1) checking both 1x64-vs-2x32 divergence and that "exp" receives the downstream violation in w64g["sites"], plus a clean m0 = 1 control (vs_clean_vmovreld_off1).
  2. Removing ANDN2 from narrows(), and why ANDN1 must also stay excluded

    • Removed both s_andn2_saveexec_b64 and s_andn2_wrexec_b64 from narrows(), with regression + wave-interpreter divergence tests in test_s_andn2_exec_in_vote_branch_is_unsafe (vs_vote_saveexec_andn2, vs_vote_wrexec_andn2).
    • Kept s_andn1_saveexec_b64 and s_andn1_wrexec_b64 excluded from narrows(): vote() treats the skipped block as an empty join that s_cbranch_vccz bypasses whenever vcc == 0 in every lane of a half. With s_andn1_* (EXEC = ~S0 & EXEC), lanes where S0 == 0 become active rather than masked out — so in 1x64 (if the other half has vcc != 0) the vcc == 0 half runs the block across all its lanes, whereas in 2x32 it branches around the block via s_cbranch_vccz. test_s_andn1_exec_in_vote_branch_is_unsafe (vs_vote_saveexec_andn1, vs_vote_wrexec_andn1) demonstrates this 1x64-vs-2x32 divergence directly.

Verification (commit 1222fd54)

  • python -I tests/semantic/test_wave64_halves_soundness.py -v: Ran 30 tests in 1.989s, OK
  • python -I tests/semantic/test_wave64_halves.py -v: Ran 57 tests in 1.346s, OK
  • python -m compileall -q make_game.py tools tests: exited 0
  • python -m unittest discover -s tests/semantic -v: Ran 1674 tests in 340.552s, OK (skipped=2)

Windows 11 CPU/semantic tests only (no game files, no GPU run). AI-assisted.

@465d6a0eac

Copy link
Copy Markdown
Contributor

Tested 69bd312 against its base main 6a6704d on the full corpus (26,503 shaders from my own 1.0.2.1 copy, counts only): pass, translation unchanged; how the tiers move is below.

Setup: WSL Ubuntu, Python 3.14.4, llvm-mc 21.1.8, SPIRV-Tools v2026.1; rdna2spirv.py --shard i/12 --no-asm ×12 + --merge, default mode, --wave64-graphics warn. CPU only.

run translated spirv-val valid tier A tier B tier C
main 6a6704d 26,487 26,487 1,919 17 9,469
this PR, 69bd312 26,487 26,487 1,922 17 9,466
  • All 26,487 modules are byte-identical. 2,069 index entries differ, only in wave64_graphics / wave64_graphics_reasons.

  • wave64_halves.py --census: ps A 1,454 → 1,443, C 8,574 → 8,585; vs A 465 → 479, C 700 → 686; gs C 195 on both.

  • 20 A → C (9 VS, 11 PS). Their reasons on this PR: tainted-exec in 16, cross-lane in 6. This is the direction the PR is for.

  • 23 C → A, all VS, whose only reason on main was stale-scalar. I attributed them by translating just those 23 with the PR's wave64_halves.py and one part put back to main's version:

    • s_pack back in NO_DEST: still 23 A;
    • main's SCC_LESS: still 23 A;
    • main's implicit-vcc rule in operands() (len(a) <= 4 and no vcc among the operands → reads vcc): all 23 back to C, stale-scalar.

    The 23 have v_cndmask_b32_e64 whose condition is an SGPR, not vcc (94 sites). main's rule gives those a vcc read the instruction doesn't make. So these look like false positives on main that the len(srcs) == 2 test removes, not a lost check. None of the three ablations changes the 20 A → C.

  • Reason totals over tier C: cross-lane 6,111 → 4,760, tainted-exec 4,959 → 5,189, stale-scalar 3,162 → 3,415, unsafe-vote-branch 8,175 → 8,285, lane-id 277 → 347, tainted-branch 6,582 → 6,550, waterfall-scalar 167 → 161; the rest unchanged. The cross-lane drop comes from 1,407 shaders (56 gain it) that all stay C. In a random sample of 60 of the 1,407, main's implicit-vcc rule alone puts cross-lane back in all 60, and the other two ablations in none. So it has the same cause, the phantom vcc read.

Not run here: GPU tests (w64_graphics.py, the tier checks on 32-wide hardware); no Vulkan SDK on this machine.

AI-assisted (Claude Code).

@zneel

zneel commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 1222fd54 on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM), Python 3.12.10: pass. This adds a GPU run to the CPU-only results so far.

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. No game files.

run result
tests/semantic/test_wave64_halves_soundness.py 30 OK
tests/semantic/test_wave64_halves.py 57 OK
unittest discover -s tests/semantic 1674 OK
run_gpu_tests.py (whole suite, native wave64) 164/173, 0 failed
same with LEONIDA_W64_EMULATE=1 164/173, 0 failed

The GPU run only rules out regressions; it doesn't exercise the new rules. All 192 translated modules, native and emulated, are byte-identical to a run of another PR on the same base 6a6704df that doesn't touch the translator's output. So none of the synthetic shaders hits a site where the soundness fixes change the result, and the semantic tests above are what cover them.

AI-assisted (Claude Code); I checked the results.

@465d6a0eac

Copy link
Copy Markdown
Contributor

Corpus re-run on the new head 1222fd5 (same setup as my run above, full corpus, default mode): 26,487 translated, 26,487 pass spirv-val, and all 26,487 modules byte-identical to main 6a6704d. The merged index.json is identical to 69bd312's, so the movrel/narrows() follow-up moves no corpus shader. The tiers stay A 1,922 / B 17 / C 9,466, with the same 20 A → C and 23 C → A as in my comment above.

AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 7394865 into ItsNotRexed:main Nov 22, 2026
labj1987 added a commit to labj1987/Leonida that referenced this pull request Nov 22, 2026
…us GS are tier A with the GS ring rule

The census in the row predated the GS ring rule (TEST-251) and ItsNotRexed#1982; re-run over the same 1.0.2.1 corpus:
tier A 2,117 (was 1,919), GS 195/0/0 (was 0/0/195). The row's open question (whether the corpus GS were a false
tier C) is answered by it.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
labj1987 added a commit to labj1987/Leonida that referenced this pull request Nov 22, 2026
…us GS are tier A with the GS ring rule

The census in the row predated the GS ring rule (TEST-251) and ItsNotRexed#1982; re-run over the same 1.0.2.1 corpus:
tier A 2,117 (was 1,919), GS 195/0/0 (was 0/0/195). The row's open question (whether the corpus GS were a false
tier C) is answered by it.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
labj1987 added a commit to labj1987/Leonida that referenced this pull request Nov 22, 2026
…us GS are tier A with the GS ring rule

The census in the row predated the GS ring rule (TEST-251) and ItsNotRexed#1982; re-run over the same 1.0.2.1 corpus:
tier A 2,117 (was 1,919), GS 195/0/0 (was 0/0/195). The row's open question (whether the corpus GS were a false
tier C) is answered by it.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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