Shaders: a loop that leaves to a no-output end is no discard #2306

Manually merged
github merged 1 commit from gh-pr/2306/head into gh-pr/2306/base 2026-11-26 05:01:45 +00:00
Owner

@⁠wbkepler on GitHub · 2026-11-25 10:15 UTC · original
wbkepler:shader/loop-exit-not-discard → main · +100 −21 · 1 files · 1 commits · merged 2026-11-25 21:56 UTC

What

Structurisation took a loop's ordinary exit test for a pixel discard. A pixel program with no color output (it writes only through atomics) ends with exp null, off, off, off, off done vm. When its last loop leaves through the usual test on its own exit mask, s_andn2 X, X, exec followed by s_cbranch_scc1 <loop>, that has the same shape as a discard's "no live lane is left" exit (discards + kill_exit). The translation then:

  • always took the back edge (w = Bool::TRUE for a kill),
  • turned the lanes that were done into a discard instead of letting them leave,
  • so every lane ran the loop to the loop limit (2^20 iterations) and left through the abort block.

In the DXIL this showed up as discard(i+1 > n); br i1 true, label %loop, ....

kill_exit now takes the discard's live-mask register. A path that first gives EXEC a saved mask other than that one (s_mov exec, s[saved]) marks the lanes it exports as valid, so it is a normal program end and not a kill exit. A real discard's exit is unchanged: plain exp null ... done, or s_mov exec, 0/the emptied live mask first.

Measured (Windows 11, NVIDIA RTX 5090)

On a world scene of the title, with VICTORY_GPU_TIMING and every command timestamped:

  • A small box draw (36 indices, a few instances) took 203.0 ms on the GPU every time it hit this loop (25 to 49 instances, always 203 to 235 ms). It was the most expensive single item over a 200 s run: 6.2 s of the 79 s of GPU time.
  • With a loop limit of 4096 for that program only, the cost went away. That confirms it was the loop limit.
  • With this change it is gone from the timings: 0 slow occurrences (22 before), and the DXIL loop exits on its counter (br i1 (i+1 > n), %exit, %loop).
  • Whole-run GPU time went from 79.0 s to 77.0 s. The steady-state frame (about 340 to 380 ms GPU, 1.3 to 1.5 fps) is still dominated by data-dependent ray-tracing and BVH-build compute programs. Their cost varies by more than this gain from run to run, so the per-frame number did not measurably change.

victory translate --cache of the title: 25,985 containers, 25,851 translated, 134 refused (the same as before).

Relation to #2043

#2043 addresses the same draw by lowering programs with a "discard in a loop" in the killing form. This change argues that the program has no discard at all: the "kill" is a misread loop exit. With the exit read correctly the helper/killing question does not arise, derivatives keep the helper form, and other programs whose loop leaves to a no-output end are fixed too. The two do not conflict. If this lands, #2043's traps_discarded should no longer match this program.

Tests

  • structure::tests::a_loop_exit_to_a_null_export_is_no_discard: such a loop has no discards, and its back edge is a per-lane condition, not "always".
  • structure::tests::a_forward_exit_to_a_null_export_is_a_discard: a forward discard to a plain null export is still one.
  • cargo clippy -p victory-shader --all-targets -- -D warnings and cargo clippy -p victory --features diagnostics --all-targets -- -D warnings are clean, and cargo xtask policy passes.
  • cargo test -p victory-shader: 246 passed. 3 hardware tests fail identically on main on this host (stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware).

Host: Windows 11, NVIDIA RTX 5090.

🤖 Generated with Claude Code

**@⁠wbkepler** on GitHub · 2026-11-25 10:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/2306) `wbkepler:shader/loop-exit-not-discard` → `main` · +100 −21 · 1 files · 1 commits · merged 2026-11-25 21:56 UTC ## What Structurisation took a loop's ordinary exit test for a pixel discard. A pixel program with no color output (it writes only through atomics) ends with `exp null, off, off, off, off done vm`. When its last loop leaves through the usual test on its own exit mask, `s_andn2 X, X, exec` followed by `s_cbranch_scc1 <loop>`, that has the same shape as a discard's "no live lane is left" exit (`discards` + `kill_exit`). The translation then: - always took the back edge (`w = Bool::TRUE` for a kill), - turned the lanes that were done into a `discard` instead of letting them leave, - so every lane ran the loop to the loop limit (2^20 iterations) and left through the abort block. In the DXIL this showed up as `discard(i+1 > n); br i1 true, label %loop, ...`. `kill_exit` now takes the discard's live-mask register. A path that first gives EXEC a saved mask other than that one (`s_mov exec, s[saved]`) marks the lanes it exports as valid, so it is a normal program end and not a kill exit. A real discard's exit is unchanged: plain `exp null ... done`, or `s_mov exec, 0`/the emptied live mask first. ## Measured (Windows 11, NVIDIA RTX 5090) On a world scene of the title, with `VICTORY_GPU_TIMING` and every command timestamped: - A small box draw (36 indices, a few instances) took **203.0 ms on the GPU every time** it hit this loop (25 to 49 instances, always 203 to 235 ms). It was the most expensive single item over a 200 s run: 6.2 s of the 79 s of GPU time. - With a loop limit of 4096 for that program only, the cost went away. That confirms it was the loop limit. - With this change it is gone from the timings: 0 slow occurrences (22 before), and the DXIL loop exits on its counter (`br i1 (i+1 > n), %exit, %loop`). - Whole-run GPU time went from 79.0 s to 77.0 s. The steady-state frame (about 340 to 380 ms GPU, 1.3 to 1.5 fps) is still dominated by data-dependent ray-tracing and BVH-build compute programs. Their cost varies by more than this gain from run to run, so the per-frame number did not measurably change. `victory translate --cache` of the title: 25,985 containers, 25,851 translated, 134 refused (the same as before). ## Relation to #2043 #2043 addresses the same draw by lowering programs with a "discard in a loop" in the killing form. This change argues that the program has no discard at all: the "kill" is a misread loop exit. With the exit read correctly the helper/killing question does not arise, derivatives keep the helper form, and other programs whose loop leaves to a no-output end are fixed too. The two do not conflict. If this lands, #2043's `traps_discarded` should no longer match this program. ## Tests - `structure::tests::a_loop_exit_to_a_null_export_is_no_discard`: such a loop has no discards, and its back edge is a per-lane condition, not "always". - `structure::tests::a_forward_exit_to_a_null_export_is_a_discard`: a forward discard to a plain null export is still one. - `cargo clippy -p victory-shader --all-targets -- -D warnings` and `cargo clippy -p victory --features diagnostics --all-targets -- -D warnings` are clean, and `cargo xtask policy` passes. - `cargo test -p victory-shader`: 246 passed. 3 hardware tests fail identically on `main` on this host (`stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros`, `synthetic_tests::gathers_at_a_level_match_the_hardware`). Host: Windows 11, NVIDIA RTX 5090. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A pixel program with no color output ends with `exp null done vm`. When its
last loop leaves through the usual exit test on its own exit mask
(`s_andn2 X, X, exec`, then the SCC branch), structurisation took that test
for a discard's "no live lane is left" exit: the back edge became always
taken, the lanes that were done were discarded instead of leaving, and every
lane ran the loop to the loop limit (2^20 iterations). On an RTX 5090 each
such draw (a small box, 36 indices) took 203 ms.

A discard's exit ends the program with no lane live. An end that first gives
EXEC a saved mask other than the discard's live mask exports those lanes as
valid: a normal end. Such a block is no longer a kill exit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:01:42 +00:00
github manually merged commit a4057f2580 into gh-pr/2306/base 2026-11-26 05:01:45 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:22 UTC · original

Automated triage for the maintainers.

Merge candidate (worth a look). In crates/victory-shader/src/structure.rs, discards now returns the live-mask register and kill_exit takes it, so an exit path that first restores EXEC from a different saved SGPR is read as a normal end of the program, not as a discard. Before, a loop that leaves on s_andn2 X, X, exec / s_cbranch_scc to an exp null done end was taken for a kill, its back edge was always taken, and every lane ran to the loop limit (about 200 ms per draw by the author's timings). I checked that the second use, in the lane-exit lowering, behaves as before (kills is only filled where discards matched), that a real discard exit is still a kill (s_mov exec, 0 is an inline constant, so the new arm does not catch it), and that the test encodings and branch offsets decode as commented. Evidence: the title's own program shape, DXIL before and after, and RTX 5090 timings. It may change what #2043 needs to match. CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2306#issuecomment-7166737206) <!-- victory-triage v1 head=a4057f2580a5c87281e1a09720db54cc72ab7f00 verdict=merge-candidate at=2026-11-25T15:30:00Z --> Automated triage for the maintainers. **Merge candidate (worth a look).** In `crates/victory-shader/src/structure.rs`, `discards` now returns the live-mask register and `kill_exit` takes it, so an exit path that first restores EXEC from a different saved SGPR is read as a normal end of the program, not as a discard. Before, a loop that leaves on `s_andn2 X, X, exec` / `s_cbranch_scc` to an `exp null done` end was taken for a kill, its back edge was always taken, and every lane ran to the loop limit (about 200 ms per draw by the author's timings). I checked that the second use, in the lane-exit lowering, behaves as before (`kills` is only filled where `discards` matched), that a real discard exit is still a kill (`s_mov exec, 0` is an inline constant, so the new arm does not catch it), and that the test encodings and branch offsets decode as commented. Evidence: the title's own program shape, DXIL before and after, and RTX 5090 timings. It may change what #2043 needs to match. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2306
No description provided.