Skip to content

wave64_halves: count v_cmpx as an EXEC write - #4585

Open
paratustra wants to merge 1 commit into
ItsNotRexed:mainfrom
paratustra:shader-wave64-halves-vcmpx
Open

paratustra wants to merge 1 commit into
ItsNotRexed:mainfrom
paratustra:shader-wave64-halves-vcmpx

Conversation

@paratustra

Copy link
Copy Markdown

Claimed in the coordination issue as: new SHADER item (claim in #4421, no ID yet)

What and why

tools/wave64_halves.py operands() sends every op that starts with a NO_DEST prefix to its first branch (no destinations, no implicit defs), except s_cmp/s_bitcmp. NO_DEST includes v_cmpx, so the later branch, elif op.startswith("v_cmpx"): defs.append("exec"), was unreachable. v_cmpx was therefore never seen as an EXEC write: exec_write()/new_exec() didn't run, and neither did the tainted-exec check.

So a wave64 VS (or PS/GS) that narrows EXEC with a lane-dependent v_cmpx came out tier A, "exact as translated on any subgroup size", when it isn't:

v_mbcnt_lo_u32_b32 v10, -1, 0
v_mbcnt_hi_u32_b32 v10, -1, v10     ; lane id 0..63
v_mov_b32 v6, 1
v_cmpx_gt_u32_e32 32, v10           ; EXEC = lanes 0..31
v_mov_b32 v6, 2                     ; exported

On the wave interpreter (test_wave64_halves.Execution.run_both), one 64-lane wave gives 2 for lanes 0..31 and 0 for 32..63. Two 32-lane waves give 2 everywhere, because the upper half sees lane ids 0..31. The same shader written as v_cmp_gt_u32 vcc, 32, v10 plus s_and_b64 exec, exec, vcc was already tier C tainted-exec.

Change: v_cmpx is excluded from the first branch, so it reaches the existing branch that adds the EXEC def. One word in tools/wave64_halves.py.

None of the open wave64_halves PRs touches this line (#4541, #4423, #4373, #4156, #4138). With #4541's code the listing above is still tier A.

Tested

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

  • New in tests/semantic/test_wave64_halves.py:
    • test_c_vcmpx_on_the_lane_id (e32 and e64 forms): tier C, tainted-exec. It fails on main (tier A) and passes here.
    • test_a_vcmpx_on_a_uniform_value: a v_cmpx on a scalar stays tier A, so the fix doesn't refuse more than it should. It passes on both.
  • test_wave64_halves.py 75 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.
  • None of the 28 ps/vs/gs_w64_* fixtures that tests/test_wave64_halves_fixtures.py pins uses v_cmpx, so their expected tiers don't change. The 8 other fixtures with v_cmpx are wave32 and aren't analysed.

Not tested

  • tests/test_wave64_halves_fixtures.py itself: it needs pytest, llvm-mc and spirv-tools, which this machine doesn't have.
  • Corpus effect: wave64 graphics shaders with a lane-dependent v_cmpx move from tier A to C. That affects the tier counts, any census in the docs, and the --wave64-graphics error refusals. I can't measure it without the game. A run of the wave64 census before and after would show how many move.

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 makes the existing EXEC-write rule apply to v_cmpx.)

AI-assisted (Claude Code).

operands() sends every op with a NO_DEST prefix to its first branch,
except s_cmp and s_bitcmp. NO_DEST includes v_cmpx, so the later
`elif op.startswith("v_cmpx"): defs.append("exec")` was unreachable:
EXEC was taken as unchanged and the tainted-exec check never ran.

A wave64 VS that narrowed EXEC with a lane-dependent v_cmpx and then
wrote an output was tier A, although one 64-lane wave and two 32-lane
waves give different words for lanes 32..63. The same shader written
as v_cmp plus s_and_b64 exec was already tier C, tainted-exec.

v_cmpx now leaves the first branch, so it reaches the branch that adds
the EXEC def. A v_cmpx on a wave-uniform value stays tier A.
@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 50241a19 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): 75/75 OK (including the two new v_cmpx tests for both e32/e64 tainted-exec Tier C and uniform Tier A).

Code review:

  • Good catch: NO_DEST previously caught v_cmpx* on line 142, making the elif op.startswith("v_cmpx"): branch on line 147 (defs.append("exec")) unreachable. Excluding "v_cmpx" on line 142 restores implicit exec definition tracking cleanly without breaking uniform v_cmpx cases.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 50241a1 merged with main d0835dd (already up to date) on Linux (WSL2) Python 3.12.16 and native Windows 11 Python 3.13.1: pass.

  • tests/semantic/test_wave64_halves.py: 75/75 OK on both. Other semantic files that use wave64_halves (test_wave64_halves_soundness, test_salu_xor_saveexec_b64, test_valu_fma_f16, test_valu_fma_f64, test_valu_trig_f32, test_valu_permlane16): OK on Linux.
  • Negative control: tools/wave64_halves.py from main, same test file: 1 failure.
  • Read the diff: with v_cmpx out of the first (no-destination) branch it reaches the existing v_cmpx branch that records exec as written; the new tests cover both encodings (e32, e64) tainted by the lane id (tier C, tainted-exec) and a uniform operand (tier A).

@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 50241a19 on Windows 11: tests/semantic/test_wave64_halves.py -> 75 tests, OK; git diff --check clean.

v_cmpx* now falls through to the destination-extracting branch instead of the NO_DEST list, so an EXEC write through a compare is counted as a def (and its operands as uses). The added 10-line case covers it and the 75-case suite passes. No blocking issue.

@theswiss24

Copy link
Copy Markdown

Tested 50241a19 (contains main d0835dd9) on two NVIDIA GPUs, with test programs built from main d0835dd9 (this PR changes no C++): pass on both. Every test has the same result as main on each device.

GPU driver, OS run_gpu_tests.py (LEONIDA_W64_EMULATE=1)
RTX 2060 SUPER (Turing) 580.178.04, Ubuntu 24.04.5 195/210 passed, 15 skipped, 0 failed
MX150 (Pascal) 580.178.04, Linux Mint 22.3 185/210 passed, 25 skipped, 0 failed (no mesh shaders or fragment barycentrics)

llvm-mc 20.1.2. No game files.

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

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

v_cmpx_* has no VGPR/SGPR destination but writes EXEC, so excluding it from the NO_DEST early return (like s_cmp/s_bitcmp) lets the EXEC taint be recorded. The tests pin both sides: lane-id compare (e32 and e64) goes to tier C tainted-exec, and a uniform-operand compare stays tier A. Code read only, nothing executed. AI-assisted (Claude).

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