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

Merged
wbkepler merged 1 commit from upstream-pr/2306 into main 2026-11-25 21:56:28 +00:00
wbkepler commented 2026-11-25 10:15:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-25T10:15:11Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6602159178 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2306). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-25T10:15:11Z. --- ## 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)
catminou4 commented 2026-11-25 11:04:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T10:19:05Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 146 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — a loop leaving to a no-output end is no longer treated as discard.

<!-- greyfinch-sync:Marvisak/VIctory:review:6055710860 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2306#pullrequestreview-6055710860). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T10:19:05Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 146 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — a loop leaving to a no-output end is no longer treated as discard.
DenRakEiw commented 2026-11-25 11:04:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T10:22:42Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Tracking the operand code live of the s_andn2 X, X, exec mask in discards and rejecting kill_exit when EXEC is restored from a different saved mask cleanly distinguishes a normal no-color-output pixel exit after a loop from an actual discard exit.
  • cargo test -p victory-shader structure::tests: 13/13 passed on RTX 5090, 13/13 passed on WARP (including a_loop_exit_to_a_null_export_is_no_discard and a_forward_exit_to_a_null_export_is_a_discard).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6055761997 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2306#pullrequestreview-6055761997). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T10:22:42Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Tracking the operand code `live` of the `s_andn2 X, X, exec` mask in `discards` and rejecting `kill_exit` when `EXEC` is restored from a different saved mask cleanly distinguishes a normal no-color-output pixel exit after a loop from an actual discard exit. - `cargo test -p victory-shader structure::tests`: 13/13 passed on RTX 5090, 13/13 passed on WARP (including `a_loop_exit_to_a_null_export_is_no_discard` and `a_forward_exit_to_a_null_export_is_a_discard`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-25 16:09:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:22:17Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166737206 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2306#issuecomment-7166737206). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:22:17Z. --- <!-- 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)_
upstream-sync closed this pull request 2026-11-25 22:03:37 +00:00
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
Marvisak/VIctory!2304
No description provided.