Skip to content

Recompiler: a mask a per-lane loop wrote is unknown after the loop (#3440) - #3451

Draft
neongatzu wants to merge 1 commit into
xcl-dev:mainfrom
neongatzu:fix-3440-loop-mask-guard
Draft

neongatzu wants to merge 1 commit into
xcl-dev:mainfrom
neongatzu:fix-3440-loop-mask-guard

Conversation

@neongatzu

Copy link
Copy Markdown

Fixes #3440.

lane_masks follows each lane on its own: a lane that leaves a loop through a per-lane branch stops running the loop. On the console the wave runs on until every lane has left, and every later round writes the loop's masks again (a VALU compare writes 0 for the lanes no longer in EXEC). After the loop such a mask holds the last round's bits, not each lane's own last value, so taking it as the lane's bit dropped guards that were needed (the issue's program: exec = vcc after the loop, the v_mov at pc 28 left unguarded).

What is in it

  • Lanes::exit_state: on an edge that leaves one or more loops through a per-lane branch (cond kind not Uniform), every SGPR mask and the SCC whose function depends on an atom born inside those loops becomes a new atom, Atom::Exit(block, successor, slot). The SCC's kind becomes SccKind::Unknown, so a later read of it is refused as elsewhere.
  • EXEC is kept: the wave leaves when it is empty, which is also each lane's own bit as it leaves. Masks set before the loop and not written in it keep their value. Wave-uniform loops and wave_branches are untouched (their branches are Uniform).
  • Bdd::depends: whether a function depends on an atom a predicate names.
  • Tests in lane_masks.rs: a_mask_from_a_per_lane_loop_still_guards_what_follows (the issue's program, unchanged; failed on main 0fe06eb0 with left: [], now [28]) and a_mask_saved_before_a_per_lane_loop_is_still_known_after_it (a control: exec = s[0:1] & exec after the same loop adds no guard).

Tested

  • hardware: NVIDIA GeForce RTX 3060 Ti, driver 621.24 (32.0.16.2124), AMD Ryzen 5 5600; Windows 11; Rust 1.98.1 msvc; debug.
  • cargo test -p xcl --lib -- lane_masks: 15 passed, 0 failed (13 before + 2 new).
  • cargo test -p xcl --lib -- gpu::recomp: 501 passed, 0 failed, 21 ignored.
  • cargo clippy -p xcl --lib --tests: no finding in lane_masks.rs (the crate's existing clippy errors elsewhere are unchanged).
  • Not run: the full cargo test --workspace.

Regression run
Not run: I have no title to run scripts/smoke.ps1 on. The change only affects the per-lane analysis, which is used with XCL_PER_LANE=1 (off by default), so the default path should not move; a smoke run from someone with a title is still needed before merge, which is why this is a draft.

main: not run

PR: not run

Not tested

  • A GPU run of the per-lane form with this program.
  • The other shape the issue mentions (a mask written on one side of an if and read after the join) is not covered by this change.

AI use

  • Claude Code (Claude Opus 5.5): wrote the fix and the control test, ran the commands above and wrote this description. The first test is the issue's, unchanged.

🤖 Generated with Claude Code

…cl-dev#3440)

lane_masks follows each lane on its own: a lane that leaves a loop
through a per-lane branch stops running it. On the console the wave runs
on until every lane has left, and every later round writes the loop's
masks again (a VALU compare writes 0 for lanes no longer in EXEC). After
the loop such a mask holds the last round's bits, not each lane's own
last value, so taking it as the lane's bit dropped needed guards.

On an edge that leaves loops through a per-lane branch, every SGPR mask
and the SCC whose function depends on an atom born inside those loops
becomes a new atom (Atom::Exit); the SCC's kind becomes Unknown. EXEC is
kept: the wave leaves when it is empty, which is each lane's own bit as
it leaves. Masks set before the loop keep their value.

Tests: the issue's program (failed with `left: []`, now [28]) and a
control where a mask saved before the loop needs no guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 29, 2026
@andrewDoing

Copy link
Copy Markdown
Contributor

I tested a terminating wave32 version of the #3440 loop on RTX 5070 12 GB (616.92). I changed the add to increment the compared register so lanes could exit on four different iterations. The saved EXEC, loop-carried VCC and post-loop v_mov at pc 28 kept the reported shape. I ran the same fixture on main 0fe06eb0 and this head e1273c28.

  • The analysis summary kept no guard on main and kept pc 28 on the PR. The whole-wave HLSL reference returned write mask 0x11111111 for staggered exits and 0xffffffff when all lanes exited together. Native per-lane kernel lowering refused the shape on both revisions with “a value written while some lanes were elsewhere, read after they met again.”
  • I also passed the equivalent compute shader through the runtime's compute_with entry. With 31 threads, the per-lane route refused on both revisions and the per-instruction fallback wrote exactly the eight expected lanes, preserving the other lanes and output canaries. With 32 threads, the whole-wave branch route compiled and its native readback agreed with the fallback and the expected eight writes on both revisions.

cargo test --offline --locked -p xcl --lib -- post_loop_compare_mask_reaches_rtx_readback lane_masks --test-threads=1 --nocapture: main 14 passed, head 16 passed, zero failures, ignored tests or skips on either. Windows 11 26300.10075, Ryzen 7 5700X3D, Rust 1.100.0, debug. This validates the guard summary and the safe fallback for the bounded wave32 form. The retail workload and wave64 GPU execution remain unmeasured. Codex with GPT-6.2 Sol adapted and ran the fixture.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head e1273c2, diff read, not a merge-check; draft, PR description red, from reading, nothing run):

  • The analysis now asks for the guard, but the guard may not change what runs. In the per-lane lowering the guard is select(read_bit(EXEC), new, old) with the lane's own EXEC bit (lower.rs ~564-567). After the issue's s_and_saveexec_b64 s[2:3], vcc, each lane's EXEC bit is its own last VCC bit, and every lane left the loop with that bit set. So the guard on pc 28 is true in every lane, and the v_mov still runs where the console skips it. The analysis can't give the lowering the last round's wave-wide value. The issue's other option fits better: refuse the program when such a mask is read after the loop, so it stays on the HLSL path. The GPU run of both forms (listed under Not tested) would settle it.
  • Masks become unknown too eagerly (reasoned, not run). exit_state (lane_masks.rs ~769-775) makes every mask that depends on an atom born in the loop an Exit atom, even when it is already right per lane. The usual compiler loop collects the leaving lanes with s_or_b64 s[0:1], vcc, s[0:1]; s_andn2_b64 exec, exec, s[0:1]; s_cbranch_execnz; s_or_b64 exec, exec, s[0:1]. There each lane's bit of s[0:1] matches the console, and on main the restored EXEC is every lane. With the PR, s[0:1] is an atom, so everything after such a loop gets a guard it doesn't need. No test covers this form.
  • The control test (a_mask_saved_before_... ~1143) sets s[0:1] to a constant, so it passes on main too. It doesn't check that a mask the loop writes correctly stays known.
  • An SCC that is the same in every lane (an s_cmp on a loop counter) also becomes SccKind::Unknown, so a later read is refused with a message about a per-lane mask, which is misleading.

Fine: exits come from the existing dominator-checked loop bodies, so breaks and nested loops are handled. Exit atoms are keyed per edge and slot, so the fixpoint stays stable. Keeping EXEC is right, and uniform branches and wave_branches aren't touched. The issue's test fails on main ([]) and passes here ([28]). Only XCL_PER_LANE=1 reaches this.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head e1273c2

No open or merged PR makes the same change for #3440. #1912 and #2117 also edit lane_masks.rs, but in other places (WREXEC empty-loop exits and null SGPR 125 handling), not the per-lane loop exit state.
No problems found in the diff: Lanes::exit_state only turns masks into Atom::Exit on non-uniform exit edges and when they depend on atoms born inside the loops being left, EXEC excluded, and the issue's test plus a control are included. The failing check is the PR description (no regression run and no full workspace run yet).
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Per-lane: a mask last written in a per-lane loop leaves the code behind it unguarded (lane_masks)

3 participants