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

Closed
Deilach wants to merge 2 commits from upstream-pr/2043 into main
Deilach commented 2026-11-25 04:01:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Deilach on 2026-11-25T04:01:39Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6596919621 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043). Originally posted by [Deilach](https://github.com/Deilach) on 2026-11-25T04:01:39Z. --- ## 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)
jeremygold02 commented 2026-11-25 04:07:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T04:03:23Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152487548 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7152487548). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T04:03:23Z. --- **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.
DenRakEiw commented 2026-11-25 05:05:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:06:49Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib emit::tests:: — 16 passed, 0 failed
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean loop-cycle detection (in_cycle) so pixel discards inside loops kill their lanes as on the console. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051715155 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#pullrequestreview-6051715155). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:06:49Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-shader --lib emit::tests::` — 16 passed, 0 failed - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean loop-cycle detection (`in_cycle`) so pixel discards inside loops kill their lanes as on the console. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 05:05:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T04:26:01Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. Pixel discard in a loop kills its lanes, as the console does — discard/kill lane semantics in loops; matches console behavior.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051847671 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#pullrequestreview-6051847671). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T04:26:01Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 144 passed / 0 failed / 2 ignored. Pixel discard in a loop kills its lanes, as the console does — discard/kill lane semantics in loops; matches console behavior.
Deilach commented 2026-11-25 05:06:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Deilach on 2026-11-25T04:20:22Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152752333 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7152752333). Originally posted by [Deilach](https://github.com/Deilach) on 2026-11-25T04:20:22Z. --- 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.
upipibi commented 2026-11-25 05:06:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T04:41:38Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7153078478 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7153078478). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-25T04:41:38Z. --- 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).
Marvisak commented 2026-11-25 10:07:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:18:57Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158165591 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7158165591). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:18:57Z. --- <!-- 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)_
sinan-cmyk commented 2026-11-25 10:07:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T09:33:39Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158495306 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7158495306). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T09:33:39Z. --- ### 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).
upstream-sync closed this pull request 2026-11-25 23:03:37 +00:00
Marvisak commented 2026-11-25 23:05:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:05:10Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175916044 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2043#issuecomment-7175916044). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:05:10Z. --- 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

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