Shaders: a pixel discard in a loop kills its lanes, as the console does #2043

Closed
github-bot wants to merge 2 commits from gh-pr/2043/head into gh-pr/2043/base
Owner

@⁠Deilach on GitHub · 2026-11-25 04:01 UTC · original
Deilach:linux-discard-loop → main · +144 −10 · 1 files · 2 commits · closed without merging

What

If a pixel program discards inside a loop, it is now lowered in the killing form (structure::killing) first, falling back to the helper form if the killing form does not lower. Programs without a discard in a loop are lowered exactly as before.

Why

A world light-volume pixel shader leaves a loop by killing its lanes: s_andn2 takes them out of the live mask, and the loop ends once no live lane is left. In the helper form, discarded lanes "run on as live lanes", so the only exit left for them is the loop limit, and each one runs the loop 2^20 times.

When the host's discard terminates lanes (the Windows AMD driver, apparently), this goes unnoticed. When it demotes them to helper lanes, as D3D12's discard allows and vkd3d-proton does (OpDemoteToHelperInvocation), every discarded lane spins to the limit. On Linux each draw of that box (36 indices) took 380–460 ms, nearly all of a world frame's GPU time.

Testing

On Linux (RX 9070 XT, RADV, Wine 11.22 + vkd3d-proton master), with VICTORY_GPU_TIMING:

before after
slow host lists per run 38 1
slowest command 460 ms (that draw) 25 ms
GPU time of a world frame 600–720 ms 150–310 ms
  • The fixed loop now leaves at its counter's end, checked by reading the DXIL through dxil-spirv.
  • translate --cache of the title: 25,985 containers, 307 refused, the same as before.
  • cargo clippy --workspace --all-targets [--all-features] -- -D warnings is clean.
  • victory-shader tests under Wine: identical to main. The 75 tests that need WARP or dxil.dll fail in Wine on main too.

Trade-off, the one structure::killing already documents: derivatives taken after a discard inside a loop read killed lanes as the console does, rather than as helpers. I haven't tested on Windows. Programs without a discard in a loop are unaffected.

🤖 Generated with Claude Code

**@⁠Deilach** on GitHub · 2026-11-25 04:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2043) `Deilach:linux-discard-loop` → `main` · +144 −10 · 1 files · 2 commits · closed without merging ## What If a pixel program discards inside a loop, it is now lowered in the killing form (`structure::killing`) first, falling back to the helper form if the killing form does not lower. Programs without a discard in a loop are lowered exactly as before. ## Why A world light-volume pixel shader leaves a loop by killing its lanes: `s_andn2` takes them out of the live mask, and the loop ends once no live lane is left. In the helper form, discarded lanes "run on as live lanes", so the only exit left for them is the loop limit, and each one runs the loop 2^20 times. When the host's `discard` terminates lanes (the Windows AMD driver, apparently), this goes unnoticed. When it demotes them to helper lanes, as D3D12's discard allows and vkd3d-proton does (`OpDemoteToHelperInvocation`), every discarded lane spins to the limit. On Linux each draw of that box (36 indices) took 380–460 ms, nearly all of a world frame's GPU time. ## Testing On Linux (RX 9070 XT, RADV, Wine 11.22 + vkd3d-proton master), with `VICTORY_GPU_TIMING`: | | before | after | |---|---|---| | slow host lists per run | 38 | 1 | | slowest command | 460 ms (that draw) | 25 ms | | GPU time of a world frame | 600–720 ms | 150–310 ms | - The fixed loop now leaves at its counter's end, checked by reading the DXIL through dxil-spirv. - `translate --cache` of the title: 25,985 containers, 307 refused, the same as before. - `cargo clippy --workspace --all-targets [--all-features] -- -D warnings` is clean. - `victory-shader` tests under Wine: identical to `main`. The 75 tests that need WARP or `dxil.dll` fail in Wine on `main` too. Trade-off, the one `structure::killing` already documents: derivatives taken after a discard inside a loop read killed lanes as the console does, rather than as helpers. I haven't tested on Windows. Programs without a discard in a loop are unaffected. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A light-volume pixel shader of the world leaves a loop by killing its
lanes: `s_andn2` takes them out of the live mask, and the loop ends when
no live lane is left. In the helper form discarded lanes run on as live
lanes, so the only exit left for them is the loop limit: each ran the
loop 2^20 times. Where the host's `discard` terminates the lanes this
goes unseen; where it demotes them to helpers, as D3D12 allows and
vkd3d-proton does (OpDemoteToHelperInvocation under Wine/Proton), each
draw of that box (36 indices) took 380-460 ms, about all of a world
frame's GPU time.

A program with a discard in a loop is now lowered in the killing form
first (`structure::killing`), the helper form if that does not lower.
That shader's loop now leaves at its counter's end like its first loop
does. With VICTORY_GPU_TIMING on vkd3d-proton (RX 9070 XT): slow lists
per run 38 -> 1, the slowest command 460 ms -> 25 ms, GPU time of a
world frame 600-720 ms -> 150-310 ms. Translating the title's shaders:
25985 containers, 307 refused, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of the previous commit: it chose the killing form for any discard
in a block on a cycle, whether or not the loop also has an exit of its
own, and an error of `structure::killing` or `resource::track` returned
from `emit_with` before the helper form was tried.

The killing form is now chosen where a discard is in a loop that no lane
can leave in the helper form: no edge out of the loop through the
discard's block is one a lane may take (a branch whose condition is
constant counts only on its open side). Each step of the killing
attempt can fail and falls back to the helper form.

New test: a loop left only by killing (killing form) and a loop around
the same discard with an exit of its own and a quad read after it
(helper form kept). In the title's shaders 6015 discard; of them only
the light-volume shader discards in a loop, and it is trapped, its DXIL
unchanged from the previous commit. 307 refused, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:39 +00:00
github-bot closed this pull request 2026-11-26 04:52:39 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 04:03 UTC · original

Triage verdict — Keep the fix, preserve the full helper fallback (2a95408)

Evidence

  • @⁠Deilach's Linux before/after identifies a case where demoted helper lanes can reach the loop limit. The new policy runs the killing path first for any discard in a cyclic block. The predicate does not check whether the loop also has an ordinary exit.
  • The fallback covers failure from lower::lower. Both structure::killing and the subsequent resource::track use ? before that fallback, so a refusal there returns from emit_with even if the original helper path could succeed. Fallback selection · Cycle predicate

Checks and limits

  • This is static path review. The Linux speedup and unchanged corpus refusal count are author-reported. I have not reproduced a setup refusal for a specific shader.

Next step

  • Treat the complete killing attempt as fallible, including structuring and resource tracking, and try the original helper path if it fails. Add a synthetic loop-discard regression for the kill-only exit and a bounded-loop control with a derivative after discard to check how broadly the new policy should apply.

This verification was AI-assisted.

**@⁠jeremygold02** on GitHub · 2026-11-25 04:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7152487548) **Triage verdict — Keep the fix, preserve the full helper fallback** (`2a95408`) **Evidence** - @⁠Deilach's Linux before/after identifies a case where demoted helper lanes can reach the loop limit. The new policy runs the killing path first for any discard in a cyclic block. The predicate does not check whether the loop also has an ordinary exit. - The fallback covers failure from `lower::lower`. Both `structure::killing` and the subsequent `resource::track` use `?` before that fallback, so a refusal there returns from `emit_with` even if the original helper path could succeed. [Fallback selection](https://github.com/Marvisak/VIctory/blob/2a95408333af52447cf8ece04c46751b47256966/crates/victory-shader/src/emit/mod.rs#L485-L514) · [Cycle predicate](https://github.com/Marvisak/VIctory/blob/2a95408333af52447cf8ece04c46751b47256966/crates/victory-shader/src/emit/mod.rs#L547-L558) **Checks and limits** - This is static path review. The Linux speedup and unchanged corpus refusal count are author-reported. I have not reproduced a setup refusal for a specific shader. **Next step** - Treat the complete killing attempt as fallible, including structuring and resource tracking, and try the original helper path if it fails. Add a synthetic loop-discard regression for the kill-only exit and a bounded-loop control with a derivative after discard to check how broadly the new policy should apply. This verification was AI-assisted.
Author
Owner

@⁠Deilach on GitHub · 2026-11-25 04:20 UTC · original

Thanks for the review. Both points are addressed in 7686b6f:

Predicate. The killing form is now chosen only when the discard sits in a loop that no lane can leave in the helper form, meaning no edge out of the loop through the discard's block is one a lane may take. A branch with a constant condition counts only on its open side. A loop with an exit of its own keeps the helper form.

Fallback. structure::killing, resource::track and lower::lower are each fallible in the killing attempt. Any failure falls back to the original helper path.

Tests. New emit::tests::a_loop_left_only_by_killing_is_made_in_the_killing_form, with two programs:

  • A loop left only by killing (s_andn2 live, live, exec → "no live lane left" exit). It is trapped and made in the killing form.
  • The same discard in a loop with its own scalar exit (4 passes), plus a DPP quad_perm read after it as the derivative. It keeps the helper form. The previous predicate would have switched this one to the killing form.

Both programs also go through emit_pixel.

How broadly it applies, measured on the title. I translated all 25,985 containers with a temporary counter. 6,015 programs discard, and exactly one of them discards inside a loop: the light-volume shader. It is trapped under the new predicate, and its DXIL is byte-identical to the previous commit's. Refusals are unchanged at 307. So on this title the narrowing changes nothing; it only guards other titles and future shaders.

cargo clippy --workspace --all-targets [--all-features] -- -D warnings is clean. victory-shader and victory-gpu tests under Wine match main plus the new test. The 75 tests that need WARP or dxil.dll fail under Wine on main too.

**@⁠Deilach** on GitHub · 2026-11-25 04:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7152752333) Thanks for the review. Both points are addressed in 7686b6f: **Predicate.** The killing form is now chosen only when the discard sits in a loop that no lane can leave in the helper form, meaning no edge out of the loop through the discard's block is one a lane may take. A branch with a constant condition counts only on its open side. A loop with an exit of its own keeps the helper form. **Fallback.** `structure::killing`, `resource::track` and `lower::lower` are each fallible in the killing attempt. Any failure falls back to the original helper path. **Tests.** New `emit::tests::a_loop_left_only_by_killing_is_made_in_the_killing_form`, with two programs: - A loop left only by killing (`s_andn2 live, live, exec` → "no live lane left" exit). It is trapped and made in the killing form. - The same discard in a loop with its own scalar exit (4 passes), plus a DPP `quad_perm` read after it as the derivative. It keeps the helper form. The previous predicate would have switched this one to the killing form. Both programs also go through `emit_pixel`. **How broadly it applies, measured on the title.** I translated all 25,985 containers with a temporary counter. 6,015 programs discard, and exactly one of them discards inside a loop: the light-volume shader. It is trapped under the new predicate, and its DXIL is byte-identical to the previous commit's. Refusals are unchanged at 307. So on this title the narrowing changes nothing; it only guards other titles and future shaders. `cargo clippy --workspace --all-targets [--all-features] -- -D warnings` is clean. `victory-shader` and `victory-gpu` tests under Wine match `main` plus the new test. The 75 tests that need WARP or `dxil.dll` fail under Wine on `main` too.
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 04:41 UTC · original

Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 7686b6f. victory-shader: 245 passed, 3 failed, 3 ignored; the new a_loop_left_only_by_killing_is_made_in_the_killing_form passes. The 3 failures are #1744's NVIDIA failures on main. Same as main on this hardware. Not run: a title.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 04:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7153078478) Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at `7686b6f`. `victory-shader`: 245 passed, 3 failed, 3 ignored; the new `a_loop_left_only_by_killing_is_made_in_the_killing_form` passes. The 3 failures are #1744's NVIDIA failures on `main`. Same as `main` on this hardware. Not run: a title. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 09:18 UTC · original

Automated triage for the maintainers.

Merge candidate. In crates/victory-shader/src/emit/mod.rs (emit_with), a pixel program is now lowered in the killing form that main already has (structure::killing) when traps_discarded finds a discard in a loop that no lane can leave in the helper form. On main, discarded lanes stay live in that loop until they hit the loop limit. Every step of the killing attempt can now fail and fall back to the original helper path, which addresses the earlier review. Other programs are lowered exactly as before.

The evidence is the title's own shader: the author's before/after GPU timings on vkd3d-proton, and a corpus run in which the light-volume shader is the only program affected and the refusal count is unchanged. The new test covers both a trapped loop and a bounded loop with a derivative after the discard, which keeps the helper form.

Recommending a look, not approving.

CI: not run. No check runs exist for this head, so the build has to be confirmed before a merge.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 09:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7158165591) <!-- victory-triage v1 head=7686b6ff106caf5d2676dfb7ebbd440c3bc2f8cc verdict=merge-candidate at=2026-11-25T09:18:50Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-shader/src/emit/mod.rs` (`emit_with`), a pixel program is now lowered in the killing form that `main` already has (`structure::killing`) when `traps_discarded` finds a discard in a loop that no lane can leave in the helper form. On `main`, discarded lanes stay live in that loop until they hit the loop limit. Every step of the killing attempt can now fail and fall back to the original helper path, which addresses the earlier review. Other programs are lowered exactly as before. The evidence is the title's own shader: the author's before/after GPU timings on vkd3d-proton, and a corpus run in which the light-volume shader is the only program affected and the refusal count is unchanged. The new test covers both a trapped loop and a bounded loop with a derivative after the discard, which keeps the helper form. Recommending a look, not approving. CI: not run. No check runs exist for this head, so the build has to be confirmed before a merge. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 09:33 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head 7686b6ff onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 09:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7158495306) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 7686b6ff onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

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

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing in favour of #2306, which fixes the cause. The one title program with a "discard" inside a loop is a misread loop exit: it leaves on s_andn2 X, X, exec; s_cbranch_scc and restores EXEC from a mask saved before the loop ahead of its null export, so on the console those lanes end as valid pixels. structure.rs kill_exit takes that for a kill; #2306 corrects it, the back edge becomes per-lane and the cost goes away. This PR keeps the misreading and kills those lanes, which would discard pixels the console keeps.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 22:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7175916044) Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing in favour of #2306, which fixes the cause. The one title program with a "discard" inside a loop is a misread loop exit: it leaves on `s_andn2 X, X, exec; s_cbranch_scc` and restores EXEC from a mask saved before the loop ahead of its null export, so on the console those lanes end as valid pixels. `structure.rs` `kill_exit` takes that for a kill; #2306 corrects it, the back edge becomes per-lane and the cost goes away. This PR keeps the misreading and kills those lanes, which would discard pixels the console keeps. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

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!2043
No description provided.