Shaders: keep a skip around a loop that may not end with no lane left #71

Open
pr0vId3nce wants to merge 1 commit from upstream-pr/71 into main
pr0vId3nce commented 2026-11-26 14:03:29 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by pr0vId3nce on 2026-11-26T14:03:29Z.


The cause and the shape of this fix come from fxceless/victory-fork#262 by xADEL. That PR found that without_skips lets an empty wave into a loop it cannot leave, in the same particle light-atlas programs. This ports the fix to this tree as its own implementation. The differences, and why, are below.

The problem. On a 32-lane host, the "without skips" forms of a wave64 program (ir::without_skips) drop s_cbranch_execz skips. A region the console only enters with lanes left then also runs with none. For straight-line code that is harmless, but not for a loop whose exit waits for some lane: with no lane left, a lane compare gives an empty VCC, the exit never comes, and every lane walks the loop to the 4,096-iteration limit. 2561af9577c47289 and c06aa62112ffba4c, the particle light-atlas sprite and trail programs, have such a loop. They are the two programs the comment in emit/mod.rs names for losing the card under VICTORY_WAVE_WIDE=1. That form is built on the deep "without skips" base, and it puts dozens of group barriers in that loop nest.

What this does. without_skips keeps a skip, and an early-out to the program's end, when the code it would leave running with no lane left holds a loop that may not end there:

  • Empty masks. With no lane left, EXEC and lane compares are empty. So is what moves, ANDs, AND-NOTs (of an empty first operand), ORs of two empty masks and the saveexec/wrexec forms make of them, round loops as well. A branch on an empty EXEC, VCC or SCC goes one known way. So a waterfall over the lanes of EXEC (v_cmpx and s_cbranch_execnz, or an AND-NOT and s_cbranch_scc1) still ends at once, and its skip is still dropped.
  • Counted loops. A loop whose branches test scalars the lanes do not make counts its way out. Those scalars are constants, values from before the region other than EXEC, and scalar arithmetic on them, joined only where the loop chooses. A count test is an ordered compare, or an equality of counts made from constants alone. A count to a bound from before the region could pass the bound unseen when that bound is 0 with no lane left. If a branch in the loop tests the lanes, no join in it may choose, and every way round must pass a count test that can leave the loop.
  • Everything else can go either way. That includes a skip on EXEC inside the region, because this form or a later one may drop that skip too.

A loop until a lane compare holds, or on a scalar read from a lane, keeps its skip.

How this differs from fxceless/victory-fork#262. fxceless/victory-fork#262 keeps a skip when a mask branch inside the region (vccz/vccnz, execz/execnz, or SCC from an AND with EXEC or VCC) has its zero edge on a cycle. Ported unchanged to this tree, at VICTORY_HOST_MAX_WAVE=32:

main fxceless/victory-fork#262 this
compute programs requiring WaveSize(64) 29 56 29
... with #40 7 9 7
compute programs whose DXIL changes 89 21

fxceless/victory-fork#262 counts every VCC test as empty, including VCC made by scalar code. It also counts an inner execz exit as a way to stay on a loop, and it guards early-outs. This tree has more exact 32-lane forms that depend on dropping those skips, so 27 programs lose their only 32-lane form there, and with default switches the light-atlas programs change too. Here those cases are told apart by which masks are actually empty.

Two more differences:

  • fxceless/victory-fork#262 drops the skip around any scalar loop. Here a loop on a scalar read from a lane (v_readfirstlane) keeps it, and so does a != count to a bound from before the region.
  • fxceless/victory-fork#262 also guards without_early_outs. This PR does not (see Limits).

Runs (RTX 4080, prologue save, DRED on):

  • VICTORY_WAVE_WIDE=1, unchanged main: the device was removed about 105 s in, 7 s after both light-atlas programs were first bound. DRED: the list in flight stopped at an indirect dispatch of c06aa62112ffba4c.
  • VICTORY_WAVE_WIDE=1 with this change, same save and keys: 306 s, no device loss. Both light-atlas programs were first bound at frame 1995 and ran for the remaining 3 minutes, at 255-305 ms per frame.
  • Default switches, #40 with and without an earlier version of this change (the same 21 programs change), 300 s each: no device loss either way, the same picture, 13 of the changed programs bound, including both particle pre-render programs that take an approximate form.
  • Not run: --rt with VICTORY_WAVE_WIDE=1.

Census (shaders.rpf, VICTORY_HOST_MAX_WAVE=32, wave sizes from PSV0, dumps compared):

main main + this #40 #40 + this
compute programs emitted / validated 2,682 / 2,682 2,682 / 2,682 2,682 / 2,682 2,682 / 2,682
compute programs in waves of 32 1,260 1,260 1,282 1,282
compute programs requiring WaveSize(64) 29 29 7 7
compute programs whose DXIL changes 21 35

Every other program (vertex, pixel, mesh) is byte-identical, apart from the validator's error-report files, which change on every run. With default switches the light-atlas programs do not change; they take an approximate local-wave form before and after.

The 21 on main, by the form they take (before → after):

  • 6: "without skips" → "without skips", with the skips around their loops kept;
  • 4: "local waves (APPROXIMATE), without skips" → the same, skips kept;
  • 5: "without skips" → "local waves (idiom)", 3 of them "without early-outs" (two ray-bin offset programs, three water-patch programs);
  • 3: "without skips" → "local waves (APPROXIMATE), without early-outs" (a particle attach-mesh update, the particle sprite and trail pre-render programs);
  • 3: "without skips, scalars followed" → "local waves (APPROXIMATE)", one of them "any mask", all "without early-outs" (two particle attach-mesh updates, a fire resolve).

With #40 the same 21 change the same way, and 14 programs in #40's last-resort form change code, not form.

So 6 programs go from an exact form to an approximate one. Their exact form ran a loop with no lane left that the guard cannot show ends. Only the two pre-render programs were bound in the runs above.

Limits:

  • without_early_outs is unchanged, so a form built on it still drops an early-out in front of such a loop. Guarding it the same way, as fxceless/victory-fork#262 does, costs 11 programs their 32-lane form on main (tree instance culling, shadow masks, a temporal resolve, a decal classification). With #40 those 11 move to the wave-wide last resort instead. That change is ready as a separate commit if wanted.
  • The counted-loop rule is a judgement, not a proof: a count whose step or bound comes from before the region is trusted to move as it does on the console.
  • The stair-stepped cascade shadows on NVIDIA are not fixed here. None of the 21 is a cascade-shadow program.
  • The tests are IR tests; none dispatches on a card (fxceless/victory-fork#262 has one that does).

Tests (ir.rs; each "kept" test fails on main):

  • a_skip_around_a_loop_is_kept: a loop until a lane compare holds;
  • a_skip_around_a_skipped_loop_is_kept: the same loop behind a skip of its own;
  • an_early_out_before_a_loop_is_kept: a skip to the end of the program;
  • a_skip_around_a_loop_on_a_lane_read_is_kept: a loop on v_readfirstlane;
  • a_skip_around_a_loop_whose_count_cannot_leave_it_is_kept: a count test whose branch stays in the loop;
  • a_skip_around_a_count_to_a_bound_it_may_pass_is_kept: while (++i != s0) with s0 from before the skip;
  • a_skip_around_a_loop_that_ends_with_its_lanes_is_left_out: v_cmpx and s_cbranch_execnz;
  • a_skip_around_a_waterfall_over_exec_is_left_out: an AND-NOT of a mask from EXEC and s_cbranch_scc1;
  • a_skip_around_a_counted_loop_is_left_out: a count from 0 to 4;
  • a_skip_around_a_counted_loop_with_a_lane_branch_in_it: the same count with a skip on EXEC inside is left out; a count that goes up on one side of that skip only is kept.

victory-shader --lib on an RTX 4080 and an AMD iGPU: 239 passed before, 249 after, 0 failed. Clippy -D warnings clean; xtask policy ok.

🤖 Generated with Claude Code

<!-- greyfinch-sync:DexNom/VIctory:issue:5511440274 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/71). Originally posted by [pr0vId3nce](https://github.com/pr0vId3nce) on 2026-11-26T14:03:29Z. --- The cause and the shape of this fix come from fxceless/victory-fork#262 by xADEL. That PR found that `without_skips` lets an empty wave into a loop it cannot leave, in the same particle light-atlas programs. This ports the fix to this tree as its own implementation. The differences, and why, are below. **The problem.** On a 32-lane host, the "without skips" forms of a wave64 program (`ir::without_skips`) drop `s_cbranch_execz` skips. A region the console only enters with lanes left then also runs with none. For straight-line code that is harmless, but not for a loop whose exit waits for some lane: with no lane left, a lane compare gives an empty VCC, the exit never comes, and every lane walks the loop to the 4,096-iteration limit. `2561af9577c47289` and `c06aa62112ffba4c`, the particle light-atlas sprite and trail programs, have such a loop. They are the two programs the comment in `emit/mod.rs` names for losing the card under `VICTORY_WAVE_WIDE=1`. That form is built on the deep "without skips" base, and it puts dozens of group barriers in that loop nest. **What this does.** `without_skips` keeps a skip, and an early-out to the program's end, when the code it would leave running with no lane left holds a loop that may not end there: - **Empty masks.** With no lane left, EXEC and lane compares are empty. So is what moves, ANDs, AND-NOTs (of an empty first operand), ORs of two empty masks and the `saveexec`/`wrexec` forms make of them, round loops as well. A branch on an empty EXEC, VCC or SCC goes one known way. So a waterfall over the lanes of EXEC (`v_cmpx` and `s_cbranch_execnz`, or an AND-NOT and `s_cbranch_scc1`) still ends at once, and its skip is still dropped. - **Counted loops.** A loop whose branches test scalars the lanes do not make counts its way out. Those scalars are constants, values from before the region other than EXEC, and scalar arithmetic on them, joined only where the loop chooses. A count test is an ordered compare, or an equality of counts made from constants alone. A count to a bound from before the region could pass the bound unseen when that bound is 0 with no lane left. If a branch in the loop tests the lanes, no join in it may choose, and every way round must pass a count test that can leave the loop. - **Everything else** can go either way. That includes a skip on EXEC inside the region, because this form or a later one may drop that skip too. A loop until a lane compare holds, or on a scalar read from a lane, keeps its skip. **How this differs from fxceless/victory-fork#262.** fxceless/victory-fork#262 keeps a skip when a mask branch inside the region (`vccz`/`vccnz`, `execz`/`execnz`, or SCC from an AND with EXEC or VCC) has its zero edge on a cycle. Ported unchanged to this tree, at `VICTORY_HOST_MAX_WAVE=32`: | | main | fxceless/victory-fork#262 | this | |---|---|---|---| | compute programs requiring `WaveSize(64)` | 29 | 56 | 29 | | ... with #40 | 7 | 9 | 7 | | compute programs whose DXIL changes | | 89 | 21 | fxceless/victory-fork#262 counts every VCC test as empty, including VCC made by scalar code. It also counts an inner `execz` exit as a way to stay on a loop, and it guards early-outs. This tree has more exact 32-lane forms that depend on dropping those skips, so 27 programs lose their only 32-lane form there, and with default switches the light-atlas programs change too. Here those cases are told apart by which masks are actually empty. Two more differences: - fxceless/victory-fork#262 drops the skip around any scalar loop. Here a loop on a scalar read from a lane (`v_readfirstlane`) keeps it, and so does a `!=` count to a bound from before the region. - fxceless/victory-fork#262 also guards `without_early_outs`. This PR does not (see Limits). **Runs (RTX 4080, prologue save, DRED on):** - `VICTORY_WAVE_WIDE=1`, unchanged main: the device was removed about 105 s in, 7 s after both light-atlas programs were first bound. DRED: the list in flight stopped at an indirect dispatch of `c06aa62112ffba4c`. - `VICTORY_WAVE_WIDE=1` with this change, same save and keys: 306 s, no device loss. Both light-atlas programs were first bound at frame 1995 and ran for the remaining 3 minutes, at 255-305 ms per frame. - Default switches, #40 with and without an earlier version of this change (the same 21 programs change), 300 s each: no device loss either way, the same picture, 13 of the changed programs bound, including both particle pre-render programs that take an approximate form. - Not run: `--rt` with `VICTORY_WAVE_WIDE=1`. **Census** (`shaders.rpf`, `VICTORY_HOST_MAX_WAVE=32`, wave sizes from `PSV0`, dumps compared): | | main | main + this | #40 | #40 + this | |---|---|---|---|---| | compute programs emitted / validated | 2,682 / 2,682 | 2,682 / 2,682 | 2,682 / 2,682 | 2,682 / 2,682 | | compute programs in waves of 32 | 1,260 | 1,260 | 1,282 | 1,282 | | compute programs requiring `WaveSize(64)` | 29 | 29 | 7 | 7 | | compute programs whose DXIL changes | | 21 | | 35 | Every other program (vertex, pixel, mesh) is byte-identical, apart from the validator's error-report files, which change on every run. With default switches the light-atlas programs do not change; they take an approximate local-wave form before and after. The 21 on main, by the form they take (before → after): - 6: "without skips" → "without skips", with the skips around their loops kept; - 4: "local waves (APPROXIMATE), without skips" → the same, skips kept; - 5: "without skips" → "local waves (idiom)", 3 of them "without early-outs" (two ray-bin offset programs, three water-patch programs); - 3: "without skips" → "local waves (APPROXIMATE), without early-outs" (a particle attach-mesh update, the particle sprite and trail pre-render programs); - 3: "without skips, scalars followed" → "local waves (APPROXIMATE)", one of them "any mask", all "without early-outs" (two particle attach-mesh updates, a fire resolve). With #40 the same 21 change the same way, and 14 programs in #40's last-resort form change code, not form. So 6 programs go from an exact form to an approximate one. Their exact form ran a loop with no lane left that the guard cannot show ends. Only the two pre-render programs were bound in the runs above. **Limits:** - `without_early_outs` is unchanged, so a form built on it still drops an early-out in front of such a loop. Guarding it the same way, as fxceless/victory-fork#262 does, costs 11 programs their 32-lane form on main (tree instance culling, shadow masks, a temporal resolve, a decal classification). With #40 those 11 move to the wave-wide last resort instead. That change is ready as a separate commit if wanted. - The counted-loop rule is a judgement, not a proof: a count whose step or bound comes from before the region is trusted to move as it does on the console. - The stair-stepped cascade shadows on NVIDIA are not fixed here. None of the 21 is a cascade-shadow program. - The tests are IR tests; none dispatches on a card (fxceless/victory-fork#262 has one that does). **Tests** (`ir.rs`; each "kept" test fails on main): - `a_skip_around_a_loop_is_kept`: a loop until a lane compare holds; - `a_skip_around_a_skipped_loop_is_kept`: the same loop behind a skip of its own; - `an_early_out_before_a_loop_is_kept`: a skip to the end of the program; - `a_skip_around_a_loop_on_a_lane_read_is_kept`: a loop on `v_readfirstlane`; - `a_skip_around_a_loop_whose_count_cannot_leave_it_is_kept`: a count test whose branch stays in the loop; - `a_skip_around_a_count_to_a_bound_it_may_pass_is_kept`: `while (++i != s0)` with `s0` from before the skip; - `a_skip_around_a_loop_that_ends_with_its_lanes_is_left_out`: `v_cmpx` and `s_cbranch_execnz`; - `a_skip_around_a_waterfall_over_exec_is_left_out`: an AND-NOT of a mask from EXEC and `s_cbranch_scc1`; - `a_skip_around_a_counted_loop_is_left_out`: a count from 0 to 4; - `a_skip_around_a_counted_loop_with_a_lane_branch_in_it`: the same count with a skip on EXEC inside is left out; a count that goes up on one side of that skip only is kept. `victory-shader --lib` on an RTX 4080 and an AMD iGPU: 239 passed before, 249 after, 0 failed. Clippy `-D warnings` clean; `xtask policy` ok. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
andrewDoing commented 2026-11-26 23:47:05 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by andrewDoing on 2026-11-26T23:47:05Z.


I found a wrapping-induction case not protected by the new ordered-comparison guard at b279216. Using the existing skip() fixture with body [0xBE95_0385, 0x4A02_0281, 0x8015_C215, 0xBF08_0015, 0xBF85_FFFC] gives s21=5, a vector operation, s21 += -2, s_cmp_gt_u32 s21,s0, and a loop while true.

For s0=0, the outer unsigned s0 > v0 condition is false for every lane, so the original guard skips the body. After removing the skip, the unsigned counter stays odd (3, 1, 0xffffffff, ...) and is always greater than zero, so the loop cannot terminate naturally. At s0=3 it can terminate on the first iteration for an active wave. The fixture had one original skip and zero after without_skips(&looped, true); an assertion that the guard remains failed.

An ordered comparison alone does not establish that a wrapping induction crosses its limit. Conservatively retaining the guard unless an exit or range/step proof excludes wrap may be safer. This is missing coverage in the PR's protection, not a claim that the PR introduced the underlying behavior versus base; no retail shader or GPU hang was tested.

Verification was AI-assisted with GPT-6.2 Astra.

<!-- greyfinch-sync:DexNom/VIctory:comment:7205855338 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/71#issuecomment-7205855338). Originally posted by [andrewDoing](https://github.com/andrewDoing) on 2026-11-26T23:47:05Z. --- I found a wrapping-induction case not protected by the new ordered-comparison guard at `b279216`. Using the existing `skip()` fixture with body `[0xBE95_0385, 0x4A02_0281, 0x8015_C215, 0xBF08_0015, 0xBF85_FFFC]` gives `s21=5`, a vector operation, `s21 += -2`, `s_cmp_gt_u32 s21,s0`, and a loop while true. For `s0=0`, the outer unsigned `s0 > v0` condition is false for every lane, so the original guard skips the body. After removing the skip, the unsigned counter stays odd (`3, 1, 0xffffffff, ...`) and is always greater than zero, so the loop cannot terminate naturally. At `s0=3` it can terminate on the first iteration for an active wave. The fixture had one original skip and zero after `without_skips(&looped, true)`; an assertion that the guard remains failed. An ordered comparison alone does not establish that a wrapping induction crosses its limit. Conservatively retaining the guard unless an exit or range/step proof excludes wrap may be safer. This is missing coverage in the PR's protection, not a claim that the PR introduced the underlying behavior versus base; no retail shader or GPU hang was tested. Verification was AI-assisted with GPT-6.2 Astra.
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/71:upstream-pr/71'
git switch 'upstream-pr/71'

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/71'
git switch 'upstream-pr/71'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/71'
git switch 'upstream-pr/71'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/71'
git switch 'main'
git merge --squash 'upstream-pr/71'
git switch 'main'
git merge --ff-only 'upstream-pr/71'
git switch 'main'
git merge 'upstream-pr/71'
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
DexNom/VIctory!71
No description provided.