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

Open
neongatzu wants to merge 1 commit from upstream-pr/3451 into main
neongatzu commented 2026-11-29 18:41:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by neongatzu on 2026-11-29T18:41:25Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:5553863087 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3451). Originally posted by [neongatzu](https://github.com/neongatzu) on 2026-11-29T18:41:25Z. --- 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](https://claude.com/claude-code)
andrewDoing commented 2026-11-29 19:19:51 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by andrewDoing on 2026-11-29T19:19:51Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7261193577 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3451#issuecomment-7261193577). Originally posted by [andrewDoing](https://github.com/andrewDoing) on 2026-11-29T19:19:51Z. --- 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 commented 2026-12-01 08:46:16 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:46:16Z.


Review (head e1273c28, 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.

<!-- greyfinch-sync:ivall/xcl:comment:7305761780 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3451#issuecomment-7305761780). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T08:46:16Z. --- Review (head e1273c28, 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 commented 2026-12-01 09:56:07 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T09:56:07Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7307423121 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3451#issuecomment-7307423121). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T09:56:07Z. --- **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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3451:upstream-pr/3451'
git switch 'upstream-pr/3451'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3451'
git switch 'upstream-pr/3451'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3451'
git switch 'upstream-pr/3451'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3451'
git switch 'main'
git merge --squash 'upstream-pr/3451'
git switch 'main'
git merge --ff-only 'upstream-pr/3451'
git switch 'main'
git merge 'upstream-pr/3451'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3451
No description provided.