Skip to content

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

Open
adamdotpy wants to merge 1 commit into
Marvisak:mainfrom
adamdotpy:shader/emit-const-fold
Open

adamdotpy wants to merge 1 commit into
Marvisak:mainfrom
adamdotpy:shader/emit-const-fold

Conversation

@adamdotpy

Copy link
Copy Markdown

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

…it time

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 <vibe@mistral.ai>

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Author

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.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zed6000 commented Nov 25, 2026

Copy link
Copy Markdown

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 Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@r4tedx

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants