Shaders: what a loop computes the same way on every pass is computed once before it (opt 128, off by default) #963

Open
github-bot wants to merge 1 commit from github-bot/gh-963 into main AGit
Owner

@⁠jptorres96 on GitHub · 2026-11-28 10:14 UTC · original
jptorres96:shader-licm → main · +480 −9 · 2 files · 1 commits · open

Port of Belobug/VIctory-Belobug-AMD#633: a loop-invariant code motion pass in victory-shader's bitcode::opt (shader optimization, the "optimize shaders" item).

What it does. Inside a loop, a value whose operands are all defined outside it (or are themselves hoisted) is computed once in the loop's preheader instead of on every pass. A preheader block is created where the loop is entered by a conditional branch.

What stays.

  • Integer division and remainder never move (a hoisted division by a value the loop would have guarded could fault).
  • Calls move only if they are pure and their block dominates every exit of the loop, so nothing runs that the loop would not have run.
  • Loads, stores, wave operations and anything with an effect stay where they are.

Opt-in only. New bit 128 in VICTORY_SHADER_OPT. DEFAULT is unchanged (63), so the shader cache key and every existing translation stay the same. 191 adds it to the usual passes, 255 is every pass.

Tests. Unit tests for the hoist, preheader creation and the two-loops case, plus the existing randomized check (every_pass_leaves_what_random_programs_store) now runs with the pass included.

Full cargo test -p victory-shader --lib on Windows with the DXIL validator (DXC 1.9.2609.5), RTX 4080 SUPER host:

VICTORY_SHADER_OPT result
unset (63) 298 passed, 0 failed, 5 ignored
191 298 passed, 0 failed, 5 ignored
255 298 passed, 0 failed, 5 ignored

Also 212/212 on Linux (no validator) in all three modes.

Not measured: frame time in the game (I don't have it). The pass is off by default so it can be tried with VICTORY_SHADER_OPT=191 by anyone who does.

🤖 Generated with Claude Code

https://claude.ai/code/session_01H4Yjw2JbmwkmbLrWRuREWT

**@⁠jptorres96** on GitHub · 2026-11-28 10:14 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/963) `jptorres96:shader-licm` → `main` · +480 −9 · 2 files · 1 commits · open Port of Belobug/VIctory-Belobug-AMD#633: a loop-invariant code motion pass in `victory-shader`'s `bitcode::opt` (shader optimization, the "optimize shaders" item). **What it does.** Inside a loop, a value whose operands are all defined outside it (or are themselves hoisted) is computed once in the loop's preheader instead of on every pass. A preheader block is created where the loop is entered by a conditional branch. **What stays.** - Integer division and remainder never move (a hoisted division by a value the loop would have guarded could fault). - Calls move only if they are pure and their block dominates every exit of the loop, so nothing runs that the loop would not have run. - Loads, stores, wave operations and anything with an effect stay where they are. **Opt-in only.** New bit `128` in `VICTORY_SHADER_OPT`. `DEFAULT` is unchanged (63), so the shader cache key and every existing translation stay the same. `191` adds it to the usual passes, `255` is every pass. **Tests.** Unit tests for the hoist, preheader creation and the two-loops case, plus the existing randomized check (`every_pass_leaves_what_random_programs_store`) now runs with the pass included. Full `cargo test -p victory-shader --lib` on Windows with the DXIL validator (DXC 1.9.2609.5), RTX 4080 SUPER host: | `VICTORY_SHADER_OPT` | result | |---|---| | unset (63) | 298 passed, 0 failed, 5 ignored | | 191 | 298 passed, 0 failed, 5 ignored | | 255 | 298 passed, 0 failed, 5 ignored | Also 212/212 on Linux (no validator) in all three modes. Not measured: frame time in the game (I don't have it). The pass is off by default so it can be tried with `VICTORY_SHADER_OPT=191` by anyone who does. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01H4Yjw2JbmwkmbLrWRuREWT
Port of Belobug/VIctory-Belobug-AMD#633. A loop-invariant code motion pass in bitcode::opt: pure arithmetic, and calls that dominate every exit, move to a preheader made where needed; integer division never moves. Off unless asked (VICTORY_SHADER_OPT=191 adds it to the usual passes, 255 is every pass); DEFAULT is unchanged, so the shader cache key is too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Claude-Session: https://claude.ai/code/session_01H4Yjw2JbmwkmbLrWRuREWT
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 14:30:54 +00:00
Author
Owner

@⁠gut0to on GitHub · 2026-12-01 14:52 UTC · original · reviewed

AI-assisted review: I checked this LICM finding against the DXIL operation definitions and the changed code. The review was prepared with AI assistance, and I verified the issue against the PR source.

**@⁠gut0to** on GitHub · 2026-12-01 14:52 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/963#pullrequestreview-6133726909) · **reviewed** AI-assisted review: I checked this LICM finding against the DXIL operation definitions and the changed code. The review was prepared with AI assistance, and I verified the issue against the PR source.
@ -1216,0 +1424,4 @@
Op::Bin(..) | Op::Cast(..) | Op::Cmp(..) | Op::Select(..) | Op::Extract(..) => true,
Op::Gep(..) => true,
Op::Call(f, _) => every_pass && effects[*f as usize] == Effect::Pure,
_ => false,
Author
Owner

@⁠gut0to on GitHub · 2026-12-01 14:52 UTC · original

Could derivative operations be classified as lane-sensitive here? DXIL represents derivFineX/Y and derivCoarseX/Y through dx.op.unary.f32, so effect() currently sees their ReadNone attribute and returns Pure; unlike ordinary pure arithmetic, derivative results can depend on the active 2x2 quad lanes. Hoisting one out of divergent loop control flow may therefore change its value. Please identify derivative opcodes from the operation argument and keep them in place, with a divergent-loop regression test.

**@⁠gut0to** on GitHub · 2026-12-01 14:52 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/963#discussion_r4755986892) Could derivative operations be classified as lane-sensitive here? DXIL represents `derivFineX/Y` and `derivCoarseX/Y` through `dx.op.unary.f32`, so `effect()` currently sees their `ReadNone` attribute and returns `Pure`; unlike ordinary pure arithmetic, derivative results can depend on the active 2x2 quad lanes. Hoisting one out of divergent loop control flow may therefore change its value. Please identify derivative opcodes from the operation argument and keep them in place, with a divergent-loop regression test.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/bitcode/opt.rs
  • crates/victory-shader/src/emit/mod.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/963/head:github-bot/gh-963'
git switch 'github-bot/gh-963'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-963'
git switch 'github-bot/gh-963'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-963'
git switch 'github-bot/gh-963'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-963'
git switch 'main'
git merge --squash 'github-bot/gh-963'
git switch 'main'
git merge --ff-only 'github-bot/gh-963'
git switch 'main'
git merge 'github-bot/gh-963'
git push origin 'main'
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/trustno1-coder--VIctory-NVIDIA!963
No description provided.