Shaders: fold constant bin/cmp/select/cast ops and identity ops at emit time #1646

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

@⁠adamdotpy on GitHub · 2026-11-24 20:22 UTC · original
adamdotpy:shader/emit-const-fold → main · +465 −4 · 1 files · 1 commits · open

The DXIL module builder (emit/bitcode.rs) pushed an instruction for every bin/cmp/select/cast, even when both operands were already constants or one was the identity (x & -1, x | 0, x + 0, x * 1, x << 0, x / 1). The driver folds those at PSO compile time anyway; folding at emit time keeps the DXIL — and with it dxil.dll validation, the on-disk shader cache (#1094) and the driver's compile — smaller.

Folding is conservative where LLVM would not fold: shifts at/past the type width stay instructions (poison), division by zero and i64::MIN / -1 stay instructions, NaN float operands keep the instruction (payload could differ). Int constants fold per-width with two's-complement wrap (i8 included), compares fold with signed/unsigned semantics, floats fold Add/Sub/Mul/Div/Rem on non-NaN operands, select drops to an arm on a constant condition or equal arms, and casts of constants fold for width changes and i32<->f32 bitcasts.

Verified on Windows 11 / Rust 1.98.1: cargo test -p victory-shader --lib 124 passed / 0 failed / 1 ignored (GPU + dxil.dll validation included, so emitted DXIL is still valid and executes identically), cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings clean, cargo build --release -p victory builds, rustfmt --edition 2024 --check clean on the changed file. New unit tests pin each fold and the stay-an-instruction cases; written_bytes_unchanged goldens updated to the folded output (64x64x64: 38916 -> 38920 bytes with a constant where an instruction was).

**@⁠adamdotpy** on GitHub · 2026-11-24 20:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/1646) `adamdotpy:shader/emit-const-fold` → `main` · +465 −4 · 1 files · 1 commits · open The DXIL module builder (`emit/bitcode.rs`) pushed an instruction for every `bin`/`cmp`/`select`/`cast`, even when both operands were already constants or one was the identity (`x & -1`, `x | 0`, `x + 0`, `x * 1`, `x << 0`, `x / 1`). The driver folds those at PSO compile time anyway; folding at emit time keeps the DXIL — and with it dxil.dll validation, the on-disk shader cache (#1094) and the driver's compile — smaller. Folding is conservative where LLVM would not fold: shifts at/past the type width stay instructions (poison), division by zero and `i64::MIN / -1` stay instructions, NaN float operands keep the instruction (payload could differ). Int constants fold per-width with two's-complement wrap (i8 included), compares fold with signed/unsigned semantics, floats fold Add/Sub/Mul/Div/Rem on non-NaN operands, `select` drops to an arm on a constant condition or equal arms, and casts of constants fold for width changes and i32<->f32 bitcasts. Verified on Windows 11 / Rust 1.98.1: `cargo test -p victory-shader --lib` 124 passed / 0 failed / 1 ignored (GPU + dxil.dll validation included, so emitted DXIL is still valid and executes identically), `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` clean, `cargo build --release -p victory` builds, `rustfmt --edition 2024 --check` clean on the changed file. New unit tests pin each fold and the stay-an-instruction cases; `written_bytes_unchanged` goldens updated to the folded output (64x64x64: 38916 -> 38920 bytes with a constant where an instruction was).
The DXIL module builder pushed an instruction for every bin/cmp/select/
cast even when both operands were already constants, or one operand was
the identity (x & -1, x | 0, x + 0, x * 1, x << 0, x / 1). The driver
folds those at PSO compile time anyway; folding at emit time keeps the
DXIL — and so dxil.dll validation, the on-disk shader cache and the
driver's compile — smaller.

Folding is conservative where LLVM would not fold: shifts at or past
the type width stay instructions (poison), division by zero and the
i64::MIN / -1 overflow stay instructions, NaN float operands keep the
instruction (the payload could differ), and constants fold per-width
with two's-complement wrap (i8 included), sign-extended compares, and
full fcmp predicate semantics. select drops to an arm on a constant
condition or equal arms; casts of constants fold for width changes and
i32<->f32 bitcasts.

Tests pin the folds (including the stay-an-instruction cases) and that
only the two un-foldable ops in the test module remain instructions;
written_bytes_unchanged goldens updated to the folded output.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:39:42 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 20:46 UTC · original

Verified head 64a7038 merged cleanly onto main (ffe310e) on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, gfx1200) + WARP / Rust 1.98.1 MSVC:

  • cargo test -p victory-shader --lib: 124 passed, 0 failed, 1 ignored (all 4 new constant-folding tests + full GPU/WARP shader suite pass).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 20:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7144043165) Verified head `64a7038` merged cleanly onto `main` (`ffe310e`) on **Windows 11 / AMD Radeon RX 9060 XT (RDNA4, `gfx1200`) + WARP / Rust 1.98.1 MSVC**: - `cargo test -p victory-shader --lib`: **124 passed, 0 failed, 1 ignored** (all 4 new constant-folding tests + full GPU/WARP shader suite pass). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 20:53 UTC · original

Thanks for the independent verification on RDNA4/WARP — 124 passed / 0 failed matches what I measured here (Windows, RX 7900 XT path) when I opened it. The four new tests are const_const_bin_ops_fold_at_emit, const_const_cmp_and_select_fold_at_emit, identity_ops_fold_at_emit, and poison_sensitive_ops_are_not_folded.

**@⁠adamdotpy** on GitHub · 2026-11-24 20:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7144193640) Thanks for the independent verification on RDNA4/WARP — 124 passed / 0 failed matches what I measured here (Windows, RX 7900 XT path) when I opened it. The four new tests are `const_const_bin_ops_fold_at_emit`, `const_const_cmp_and_select_fold_at_emit`, `identity_ops_fold_at_emit`, and `poison_sensitive_ops_are_not_folded`.
Author
Owner

@⁠zed6000 on GitHub · 2026-11-25 01:57 UTC · original

Measured offline on the title's shaders (as in #1247): merged onto main 70564ae and compared program by program (victory translate --validate --list, shaders.rpf 1.0.2.1, dxil.dll 1.8.2502). This PR gains nothing there and loses 4 programs that validate on main (2 pixel, 2 compute). With the PR, all 4 fail validation with:

validator: error: Flags must match usage.

This looks like folding removing the last use of an operation, while the shader's feature flags (the ones the validator recomputes from the module) still declare it. Recomputing or trimming the flags after folding might cover it. Offline counts only; no program names or data.

**@⁠zed6000** on GitHub · 2026-11-25 01:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7150152174) Measured offline on the title's shaders (as in #1247): merged onto main `70564ae` and compared program by program (`victory translate --validate --list`, `shaders.rpf` 1.0.2.1, dxil.dll 1.8.2502). This PR gains nothing there and **loses 4 programs** that validate on main (2 pixel, 2 compute). With the PR, all 4 fail validation with: ``` validator: error: Flags must match usage. ``` This looks like folding removing the last use of an operation, while the shader's feature flags (the ones the validator recomputes from the module) still declare it. Recomputing or trimming the flags after folding might cover it. Offline counts only; no program names or data.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:32 UTC · original · edited 2026-11-25 15:54 UTC

Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-shader/src/emit/bitcode.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs work) was for an older state of main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7152008084) · edited 2026-11-25 15:54 UTC <!-- victory-triage v1 head=64a7038444081124fe58710ddd69ee0913a1632e verdict=needs-rebase at=2026-11-25T15:55:00Z --> Automated triage for the maintainers. This pull request no longer merges cleanly into `main` (conflicts in `crates/victory-shader/src/emit/bitcode.rs`). It will be reviewed once it is rebased onto the current `main`. The earlier verdict (needs work) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠r4tedx on GitHub · 2026-11-25 07:31 UTC · original

Measured on current main (940d32f): 9.4 % fewer instructions, and the same 4 programs lost

Triage asked for a census and a benefit. I merged head 64a70384 onto main 940d32f (clean merge) together with #2115, which counts the instructions of every emitted shader, and compared the two listings program by program (translate --validate --list --count over the title's shaders.rpf, dxil.dll 10.0.26100.0).

main main + this PR
instructions in emitted programs 190,954,569 172,956,100 −9.43 %
basic blocks 3,213,914 3,213,914 unchanged
programs validated 23,918 23,914 −4
  • 25,544 of the 25,591 emitted programs get smaller, 47 stay the same, none grows.
  • Where it comes from: bitcast −6,394,458, add −3,727,355, and −2,411,957, or −1,187,721, fcmp −878,937, select −856,200, icmp −744,859, lshr −682,021.
  • The 4 lost programs validate on main and fail here with Flags must match usage. These are the four @⁠zed6000 found on 70564ae; they are still there on 940d32f. No program is gained.
  • 878,937 float compares are folded. That is the code triage found wrong for NaN operands (UGT/UGE/ULT/ULE, UNE, ONE), so the predicate fix applies to real programs, not only to the unit tests.

What this does not show: GPU time. A driver's compiler folds constants itself, so the likelier gain is pipeline compile time and cache size. I did not measure either.

Local title data, offline, CPU only; counts and validator reasons only. The title was not run.

AI-assisted

**@⁠r4tedx** on GitHub · 2026-11-25 07:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7155957827) **Measured on current `main` (`940d32f`): 9.4 % fewer instructions, and the same 4 programs lost** Triage asked for a census and a benefit. I merged head `64a70384` onto `main` `940d32f` (clean merge) together with #2115, which counts the instructions of every emitted shader, and compared the two listings program by program (`translate --validate --list --count` over the title's `shaders.rpf`, dxil.dll 10.0.26100.0). | | `main` | `main` + this PR | | | --- | ---: | ---: | ---: | | instructions in emitted programs | 190,954,569 | 172,956,100 | −9.43 % | | basic blocks | 3,213,914 | 3,213,914 | unchanged | | programs validated | 23,918 | 23,914 | −4 | - 25,544 of the 25,591 emitted programs get smaller, 47 stay the same, none grows. - Where it comes from: `bitcast` −6,394,458, `add` −3,727,355, `and` −2,411,957, `or` −1,187,721, `fcmp` −878,937, `select` −856,200, `icmp` −744,859, `lshr` −682,021. - The 4 lost programs validate on `main` and fail here with `Flags must match usage.` These are the four @⁠zed6000 found on `70564ae`; they are still there on `940d32f`. No program is gained. - 878,937 float compares are folded. That is the code triage found wrong for NaN operands (`UGT/UGE/ULT/ULE`, `UNE`, `ONE`), so the predicate fix applies to real programs, not only to the unit tests. What this does not show: GPU time. A driver's compiler folds constants itself, so the likelier gain is pipeline compile time and cache size. I did not measure either. Local title data, offline, CPU only; counts and validator reasons only. The title was not run. AI-assisted
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/bitcode.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/1646/head:github-bot/gh-1646'
git switch 'github-bot/gh-1646'

Merge

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