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

Open
adamdotpy wants to merge 1 commit from upstream-pr/1646 into main
adamdotpy commented 2026-11-24 20:22:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T20:22:44Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6590677262 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T20:22:44Z. --- 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).
DenRakEiw commented 2026-11-24 21:02:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T20:26:14Z.

Review: APPROVED


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

  • cargo test -p victory-shader --lib emit::bitcode:: — 9 passed, 0 failed (1 ignored)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok

Clean emit-time constant folding and algebraic identity simplification for bin/cmp/select/cast ops with thorough unit tests. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6046815852 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646#pullrequestreview-6046815852). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T20:26:14Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-shader --lib emit::bitcode::` — 9 passed, 0 failed (1 ignored) - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok Clean emit-time constant folding and algebraic identity simplification for `bin`/`cmp`/`select`/`cast` ops with thorough unit tests. Looks good. AI-assisted (Gemini 4 Neon).
geiyiliyabumo commented 2026-11-24 21:03:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T20:46:43Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144043165 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7144043165). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T20:46:43Z. --- 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.*
adamdotpy commented 2026-11-24 21:03:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T20:53:28Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144193640 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7144193640). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T20:53:28Z. --- 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`.
catminou4 commented 2026-11-24 22:01:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T21:42:47Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 100 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows.

Emit-time folding of constant bin/cmp/select/cast ops and identity ops: bits_of/sext_bits/fold_bin/fold_bin_identity with careful bit-width handling. +4 macOS tests (100 total).

<!-- greyfinch-sync:Marvisak/VIctory:review:6047881725 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646#pullrequestreview-6047881725). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T21:42:47Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 100 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows. Emit-time folding of constant bin/cmp/select/cast ops and identity ops: bits_of/sext_bits/fold_bin/fold_bin_identity with careful bit-width handling. +4 macOS tests (100 total).
zed6000 commented 2026-11-25 02:06:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by zed6000 on 2026-11-25T01:57:21Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150152174 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7150152174). Originally posted by [zed6000](https://github.com/zed6000) on 2026-11-25T01:57:21Z. --- 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.
Marvisak commented 2026-11-25 04:06:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T03:32:21Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152008084 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7152008084). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T03:32:21Z. --- <!-- 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)_
r4tedx commented 2026-11-25 08:06:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T07:31:37Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155957827 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1646#issuecomment-7155957827). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T07:31:37Z. --- **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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1646:upstream-pr/1646'
git switch 'upstream-pr/1646'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1646'
git switch 'upstream-pr/1646'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1646'
git switch 'upstream-pr/1646'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1646'
git switch 'main'
git merge --squash 'upstream-pr/1646'
git switch 'main'
git merge --ff-only 'upstream-pr/1646'
git switch 'main'
git merge 'upstream-pr/1646'
git push origin 'main'
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!1645
No description provided.