Skip to content

wave64_halves: a lane mask accumulated by lane-local defs is not stale (SHADER-821) - #4656

Open
sperm wants to merge 1 commit into
ItsNotRexed:mainfrom
sperm:shader-821-break-loop-lane-mask
Open

sperm wants to merge 1 commit into
ItsNotRexed:mainfrom
sperm:shader-821-break-loop-lane-mask

Conversation

@sperm

@sperm sperm commented Nov 23, 2026

Copy link
Copy Markdown

Claim: SHADER-821 (#4571 (comment)), from @shdann's finding.

What and why

stale_uses() treats a 64-bit lane mask in an SGPR pair as a scalar. In a compiler's break loop, the pair collects the lanes that left:

s_mov_b64 s[16:17], 0
loop:
  v_add_nc_u32_e32 v9, 1, v9
  v_cmp_ge_u32_e32 vcc, v9, v10        ; v10 differs per lane
  s_or_b64 s[16:17], vcc, s[16:17]
  s_andn2_b64 exec, exec, s[16:17]
  s_cbranch_execnz loop
s_or_b64 exec, exec, s[16:17]

The pair's in-loop def is control dependent on the loop's EXEC vote, so its reads were stale and the two EXEC writes tainted-exec: tier C. Each bit belongs to one lane, though. A v_cmp writes 0 for the lanes that are off, so OR-ing it in can't change an off lane's bit. A half that leaves the loop early therefore has the same bits for its lanes as the wave.

The rule (narrow on purpose)

  • Lane-local def (Analysis.lane_local_defs): s_or_b64 D, M, D (either order) or s_andn2_b64 D, D, M, where D is an SGPR pair and M is VCC or a pair that a v_cmp (not v_cmpx) wrote earlier in the same block, with no EXEC write in between.
  • Not stale: in stale_uses(), a read of D is not stale when every vote-dependent def that reaches it is lane-local and the read only uses D as a lane mask, either as an EXEC write or as the next lane-local def.
  • Unchanged: scalar uses of the same pair (s_bcnt1, s_cmp, data) are still stale. So is an OR with anything that isn't a fresh v_cmp result.

Tests (tests/semantic/test_wave64_halves.py)

  • test_a_break_loop_lane_mask: the loop above as a PS is tier A (tier C tainted-exec on main).
  • test_a_break_loop_lane_mask_runs_the_same: as a VS through the CPU wave interpreter, one 64-lane wave and two 32-lane waves write the same outputs (trip counts 1 to 4 per lane), and the tier is A.
  • Near misses that stay tier C:
    • test_c_break_loop_mask_read_as_scalar: s_bcnt1_i32_b64 of the pair is exported.
    • test_c_break_loop_or_with_a_non_compare_mask: OR-ing a copy of EXEC instead of a v_cmp result.

Corpus census (asked for by the item)

On my own 1.0.2.1 copy I translated the whole corpus at main (d0835dd) and with this change, then ran wave64_halves.py --census. Counts only.

main this PR
A / B / C (13,401 wave64 VS/PS/GS) 4,033 / 85 / 9,283 4,033 / 85 / 9,283
tainted-exec / stale-scalar 5,188 / 3,414 5,188 / 3,414

No shader changes tier. The shape doesn't occur in this corpus: none of the 25,734 listings narrows EXEC with s_andn2_b64 exec, …. The game's compiler narrows with s_and_b64 exec, exec, s[pair] (15,971 times) and loops with s_andn2_wrexec_b64 waterfalls. So this PR fixes the classifier for the LLVM-style loop in the finding, but it has no effect on 1.0.2.1's tiers. I'm reporting that plainly so the maintainer can decide whether the rule is worth keeping. If not, the census still answers the item's open question, and I'm happy to close this.

Tested

Touches tools/wave64_halves.py alongside #4619/#4620 (SHADER-819/820), but in stale_uses() and a new helper, not in put()/narrows.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…e (SHADER-821)

A compiler's break loop ORs a v_cmp result into an SGPR pair and narrows EXEC
with it. stale_uses() treated the pair as a scalar, so the loop was tier C
tainted-exec although each bit belongs to one lane. A def that ORs (or
AND-NOTs) a same-block v_cmp result into the pair leaves the off lanes' bits
alone; a read that only uses the pair as a lane mask (an EXEC write, or the next
such def) is no longer stale. Scalar uses (s_bcnt1, s_cmp) still are.

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.

1 participant