Shaders: fold constant bin/cmp/select/cast ops and identity ops at emit time #1645
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1646"
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?
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 everybin/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 / -1stay 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,selectdrops 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 --lib124 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 warningsclean,cargo build --release -p victorybuilds,rustfmt --edition 2024 --checkclean on the changed file. New unit tests pin each fold and the stay-an-instruction cases;written_bytes_unchangedgoldens updated to the folded output (64x64x64: 38916 -> 38920 bytes with a constant where an instruction was).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 warningscargo xtask policy— okClean emit-time constant folding and algebraic identity simplification for
bin/cmp/select/castops with thorough unit tests. Looks good.AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T20:46:43Z.
Verified head
64a7038merged cleanly ontomain(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.
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, andpoison_sensitive_ops_are_not_folded.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).
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
70564aeand compared program by program (victory translate --validate --list,shaders.rpf1.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: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.
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 incrates/victory-shader/src/emit/bitcode.rs). It will be reviewed once it is rebased onto the currentmain. The earlier verdict (needs work) was for an older state ofmain.Generated by Claude Code
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 lostTriage asked for a census and a benefit. I merged head
64a70384ontomain940d32f(clean merge) together with #2115, which counts the instructions of every emitted shader, and compared the two listings program by program (translate --validate --list --countover the title'sshaders.rpf, dxil.dll 10.0.26100.0).mainmain+ this PRbitcast−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.mainand fail here withFlags must match usage.These are the four @zed6000 found on70564ae; they are still there on940d32f. No program is gained.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
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.