Skip to content

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

Open
pr0vId3nce wants to merge 1 commit into
DexNom:mainfrom
pr0vId3nce:skip-loop
Open

pr0vId3nce wants to merge 1 commit into
DexNom:mainfrom
pr0vId3nce:skip-loop

Conversation

@pr0vId3nce

Copy link
Copy Markdown

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:

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, Shaders: take the wave-wide form as a last resort on 32-lane hosts #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:

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

The "without skips" forms drop `s_cbranch_execz` skips, so a region the
console only enters with lanes left also runs with none. A loop whose exit
waits for some lane (a lane compare's VCC, empty then) never ends there, and
every lane walks it to the iteration limit: in the particle light-atlas
sprite and trail programs, with the group barriers of the wave-wide form in
the loop nest. The cause was found first in fxceless/victory-fork#262.

Such a skip, or an early-out to the program's end, is now kept when the code
left running with no lane holds a loop that may not end there:

- With no lane left, EXEC, lane compares and the masks moves, ANDs,
  AND-NOTs, ORs and the saveexec/wrexec forms make of them are empty, and a
  branch on an empty EXEC, VCC or SCC goes one known way: a waterfall over
  EXEC's lanes ends at once.
- A loop that counts its way out (its branches test scalars the lanes do not
  make: constants, values from before the region, scalar arithmetic on them,
  joined only where the loop chooses) goes round as on the console. Its
  tests are ordered compares, or equalities of counts made from constants
  alone: a count to a bound from before the region may pass it unseen when
  that bound is 0 with no lane left. If a branch in it tests the lanes, no
  join in it may choose, and every way round must pass a test of a count
  that can leave the loop.
- Any other branch, and a skip inside the region (this or a later form may
  drop it too), can go either way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pr0vId3nce added a commit to pr0vId3nce/VIctory-NVIDIA that referenced this pull request Nov 26, 2026
…ith no lane left

Port of DexNom/VIctory#71 (b27921645, pr0vId3nce) and its follow-up
b60997d11 onto this tree; the cause was found first in
fxceless/victory-fork#262.

The "without skips" and "without early-outs" forms drop `s_cbranch_execz`
skips and early-outs, so a region the console only enters with lanes left
also runs with none. A loop whose exit waits for some lane (a lane
compare's VCC, empty then) never ends there, and every lane walks it to the
iteration limit: in the particle light-atlas sprite and trail programs,
with the group barriers of the wave-wide form in the loop nest (a GPU hang
on 32-lane cards).

Such a skip, or an early-out to the program's end, is now kept when the
code left running with no lane holds a loop that may not end there
(`goes_round`). Here the early-out test is `early_out_continuation`, which
both forms use, so the far-jump early-out (`s_cbranch_execnz` past a far
jump to the end) is guarded too.

(cherry picked from commit b27921645)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andrewDoing

Copy link
Copy Markdown

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.

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.

2 participants