shader: counted-loop counter elision (wrap-safe) #531
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!531
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-531"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@cerealfordinner on GitHub · 2026-12-04 23:06 UTC · original
cerealfordinner:upstream-pr-counted-loop-elision→main· +138 −1 · 3 files · 2 commits · openAdds counted-loop counter elision for the shader translator (TRANSLATOR_VERSION 20). Small, self-contained fix.
@Kal0s-design on GitHub · 2026-12-05 02:06 UTC · original
Checked
740c704on native Windows (one NVIDIA desktop card plus WARP, didn't run the game).It builds, and
cargo test -p victory-shader --libmatches main with 419 passed and 1 failed. The failure is the WARP one that already fails on main (size_tests::a_reused_descriptor_register_read_only_behind_its_flag). Clippy on the lib gives the same warnings as main, with and without--features diagnostics, and none of them are in shadow_mask.rs.The matcher is hooked in at lower.rs:3429 and runs on every shader. It isn't limited to the shadow mask container and there's no switch to turn it off. I think the wrap math is right. The add has to read the header phi directly (shadow_mask.rs:127), so other uses or writes of the counter can't fool it, and a negative step fails the wrap checks.
I wrote a throwaway compute test with a scalar counted loop and ran 13 cases on main and the PR. They covered lt and ge forms, s_add_i32, steps above 1, k <= k0, a k0 that wraps, k near 2^32, and trip counts on both sides of the 4096 compute limit. The PR matched exactly the cases it should and left the wrapping and over-limit ones alone. DXIL only changed for the matched cases. Everything passed dxil.dll, and trip counts and final counters came out the same as main on the card and on WARP, with the debug layer on. None of the existing lib tests hit the new path, so right now nothing in the suite covers it.
Two things I think need fixing.
TRANSLATOR_VERSION is still 20 (lib.rs:30), even though the output changes for every shader that matches. The shader cache key is built from it (shader_cache.rs:87), so a warm cache keeps serving the old translations, and going back to an older build would serve the new ones.
e5243e4bumped 19 to 20 for this kind of change.With VICTORY_KILL_CONTINUE=1 it can produce a loop that never exits.
live_ok(lower.rs:18750) treats "has a counter" as meaning "is a loop header". So when a pixel counted loop's latch falls straight into a kill-only block, the exit now becomes discard plus a branch back to the header. My repro iss_mov_b32 s12, 0, thens_add_u32 s12, s12, 1,s_cmp_lt_u32 s12, 10ands_cbranch_scc1back, thenexp null doneands_endpgm. With the switch on, main validates and draws on WARP. The PR fails dxil.dll with "Loop must have break", but the NVIDIA driver still creates the PSO (I didn't draw with it). Translated shaders aren't validated at runtime, so this would reach the driver as an endless loop.live_okprobably needs a header check that doesn't rely on the counter, for example a set of the elided headers. Skipping the elision when kill_continue is on would also work. I haven't tried either.Small stuff. The doc comment ends with "Not compiled.", which looks like a leftover. The PR has no tests, and a couple of the cases above would do as a cheap regression test.
AI-assisted: yes
@cerealfordinner on GitHub · 2026-12-05 03:12 UTC · original
Addressed the review items:
TRANSLATOR_VERSION bumped to 21 — the counted loop elision changes shader output, so the cache key needs updating (lib.rs:30).
VICTORY_KILL_CONTINUE=1 endless loop fixed — skipped the counted loop elision when is enabled (lower.rs:3429). The elision removes the iteration counter, which relied on to identify loop headers; without it a kill-only latch creates an infinite discard + branch back to the header.
"Not compiled." doc leftover removed — from doc comment (shadow_mask.rs:38).
Build verification will run in CI.
@cerealfordinner on GitHub · 2026-12-05 03:12 UTC · original
Addressed the review items:
TRANSLATOR_VERSION bumped to 21 — the counted loop elision changes shader output, so the cache key needs updating (lib.rs:30).
VICTORY_KILL_CONTINUE=1 endless loop fixed — skipped the counted loop elision when
kill_continue()is enabled (lower.rs:3429). The elision removes the iteration counter, whichlive_okrelied on to identify loop headers; without it a kill-only latch creates an infinite discard + branch back to the header."Not compiled." doc leftover removed — from
counted_loopdoc comment (shadow_mask.rs:38).Build verification will run in CI.
@Kal0s-design on GitHub · 2026-12-05 04:30 UTC · original
Re-checked
1ec2d81on native Windows (one NVIDIA desktop card plus WARP).The version bump and the doc fix are fine. TRANSLATOR_VERSION is 21 now and "Not compiled." is gone.
cargo test -p victory-shader --libmatches main with 419 passed and 1 failed, the known WARP one (size_tests::a_reused_descriptor_register_read_only_behind_its_flag). Clippy shows the same 11 warnings as before and none of them are in the changed lines. The suite has no test that covers the elision or a loop under kill-continue, so this result doesn't say anything about the fix.The kill_continue change is inverted, I think.
&& !super::kill_continue()sits in the condition that adds the counter (lower.rs:3430), so with the switch on no loop header gets a counter at all. The intent was to skip only the elision. As a resultlive_okis true for every header, and any pixel loop whose latch falls into a kill-only block becomes discard plus a branch back with no exit. Loops the matcher never matched now get it too. Also from reading, there's no stage check on that condition, so with the switch on every loop in every stage loses its iteration limit, not only loops that reach a kill. I re-ran my earlier repro with the switch turned on through the test override, which goes through the samekill_continue()path as the env var. I also ran a second version where the bound is a register (s_cmp_lt_u32 s12, s13), whichcounted_loopdoesn't match. With the switch on, both fail dxil.dll with "Loop must have break" on1ec2d81, while main validates both. With it off, both validate. From reading only, the shadow mask path also indexesself.counters[&header](count_up and around lower.rs:18798), so with the switch on and that container it would hit an empty map and panic. I didn't reproduce that one.Writing it as
&& (super::kill_continue() || !shadow_mask::counted_loop(p, b, self.iteration_limit()))worked when I tried it locally. With the switch on, both cases then produce byte-identical DXIL to main and validate. With it off, the elision still applies to the constant case. A small test along these lines with the override set would keep this from coming back.On the cache,
VICTORY_KILL_CONTINUEis already part of the shader cache key on main (shader_cache.rs:109), so turning the switch on or off doesn't reuse a translation made with the other setting. That's not new here. Also heads up that #429, #452 and #473 bump TRANSLATOR_VERSION from 20 to 21 too. Whichever lands second has to go to 22 or translations cached by the first get reused.AI-assisted: yes
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.