Shaders: fold constant bin/cmp/select/cast ops and identity ops at emit time #1646
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1646
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@adamdotpy on GitHub · 2026-11-24 20:22 UTC · original
adamdotpy:shader/emit-const-fold→main· +465 −4 · 1 files · 1 commits · openThe 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).@geiyiliyabumo on GitHub · 2026-11-24 20:46 UTC · original
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.
@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, andpoison_sensitive_ops_are_not_folded.@zed6000 on GitHub · 2026-11-25 01:57 UTC · original
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.
@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 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
@r4tedx on GitHub · 2026-11-25 07:31 UTC · original
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
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.Merge
Merge the changes and update on Forgejo.