shader: fold shift-mask, masked bitwise, associative mul, and complement ops in DXIL opt #2357

Merged
nidea1 merged 1 commit from upstream-pr/2357 into main 2026-11-30 14:12:03 +00:00
nidea1 commented 2026-11-29 12:21:19 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nidea1 on 2026-11-29T12:21:19Z.


What it changes

Extends the integer peephole and strength-reduction pass in crates/victory-shader/src/emit/bitcode/opt.rs with sound algebraic folds common in lowered RDNA2 compute and graphics shaders:

  • Shift-then-mask / mask-then-shift identity and zero folds ((x << s) & m, (x >> s) & m, (x & m) << s, (x & m) >> s)
  • Masked bitwise simplifications ((x & m1) & m2, (x | k) & m, (x ^ k) & m, redundant mask elimination)
  • Associative constant multiplication folding ((x * c1) * c2 -> x * (c1.wrapping_mul(c2)))
  • Bitwise complement and absorption folds (~(~x), x & ~x, x | ~x, x ^ ~x, x & (x | y), x | (x & y))

All folds preserve exact 32-bit two's-complement DXIL semantics and run within the existing fixpoint loop without adding extra passes.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): RTX 3060 Ti 8 GB, Series S profile, 1440p, FSR 3 2x FG, RT off (verified in live gameplay with zero visual defects or TDRs; hardware bit-exact parity test run directly on RTX 3060 Ti)
  • Before: redundant shift/mask/mul/complement instruction chains remained in emitted DXIL bitcode
  • After: bitcode::opt folds all targeted patterns; 27/27 tests in victory-shader bitcode::opt:: pass, including shift_mask_mul_complement_and_absorption_fold, dxil_opt_hw_gpu_bit_exact_parity_and_validation, and randomized differential testing (every_pass_leaves_what_random_programs_store)
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): 32 lanes (RTX 3060 Ti hardware parity test) and wave-independent IR/DXIL tests
  • cargo test passed for the crates it touches: yes (cargo test -p victory-shader --lib --locked bitcode::opt:: — 27 passed, 0 failed, 1 ignored GPU benchmark)

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: none
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5550650343 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2357). Originally posted by [nidea1](https://github.com/nidea1) on 2026-11-29T12:21:19Z. --- ## What it changes Extends the integer peephole and strength-reduction pass in `crates/victory-shader/src/emit/bitcode/opt.rs` with sound algebraic folds common in lowered RDNA2 compute and graphics shaders: - Shift-then-mask / mask-then-shift identity and zero folds (`(x << s) & m`, `(x >> s) & m`, `(x & m) << s`, `(x & m) >> s`) - Masked bitwise simplifications (`(x & m1) & m2`, `(x | k) & m`, `(x ^ k) & m`, redundant mask elimination) - Associative constant multiplication folding (`(x * c1) * c2 -> x * (c1.wrapping_mul(c2))`) - Bitwise complement and absorption folds (`~(~x)`, `x & ~x`, `x | ~x`, `x ^ ~x`, `x & (x | y)`, `x | (x & y)`) All folds preserve exact 32-bit two's-complement DXIL semantics and run within the existing fixpoint loop without adding extra passes. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): RTX 3060 Ti 8 GB, Series S profile, 1440p, FSR 3 2x FG, RT off (verified in live gameplay with zero visual defects or TDRs; hardware bit-exact parity test run directly on RTX 3060 Ti) - Before: redundant shift/mask/mul/complement instruction chains remained in emitted DXIL bitcode - After: `bitcode::opt` folds all targeted patterns; 27/27 tests in `victory-shader` `bitcode::opt::` pass, including `shift_mask_mul_complement_and_absorption_fold`, `dxil_opt_hw_gpu_bit_exact_parity_and_validation`, and randomized differential testing (`every_pass_leaves_what_random_programs_store`) - For shader changes, the wave sizes tested (32 and/or 64 lanes): 32 lanes (RTX 3060 Ti hardware parity test) and wave-independent IR/DXIL tests - `cargo test` passed for the crates it touches: yes (`cargo test -p victory-shader --lib --locked bitcode::opt::` — 27 passed, 0 failed, 1 ignored GPU benchmark) ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: none
Ondra97 commented 2026-11-29 14:44:10 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Ondra97 on 2026-11-29T14:44:10Z.


Independent check of exact head 482dce1a applied as the only source delta to a local Windows diagnostic control. Locked offline build succeeded, and bitcode::opt:: passed 27 tests with one ignored. GPU is an RTX 4070 Ti SUPER on driver 621.40.

I isolated one compute recipe that reproduces a long native create. It contains about 29,000 decoded console instructions and uses the exact Wave64 folding path on this host. Control produced 1,808,720 bytes of DXIL and took 67.956 s in the native create. This patch produced 1,808,700 bytes and took 61.256 s in one isolated observation. The two DXIL payloads were distinct and the existing driver cache was left in place.

A separate seven-minute start with empty emulator caches, RT off and FG off reused the same diagnostic binary. This same candidate PSO took 69.311 s in the game. REA sampled its exact calling thread inside the NVIDIA compiler after about 67 s. This test still had a sizeable unresolved pipeline queue at the end and did not verify a playable cold start. The result is specific to this large folded shader and does not measure the broader runtime benefit of the peepholes.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7257535357 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2357#issuecomment-7257535357). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-11-29T14:44:10Z. --- Independent check of exact head 482dce1a applied as the only source delta to a local Windows diagnostic control. Locked offline build succeeded, and bitcode::opt:: passed 27 tests with one ignored. GPU is an RTX 4070 Ti SUPER on driver 621.40. I isolated one compute recipe that reproduces a long native create. It contains about 29,000 decoded console instructions and uses the exact Wave64 folding path on this host. Control produced 1,808,720 bytes of DXIL and took 67.956 s in the native create. This patch produced 1,808,700 bytes and took 61.256 s in one isolated observation. The two DXIL payloads were distinct and the existing driver cache was left in place. A separate seven-minute start with empty emulator caches, RT off and FG off reused the same diagnostic binary. This same candidate PSO took 69.311 s in the game. REA sampled its exact calling thread inside the NVIDIA compiler after about 67 s. This test still had a sizeable unresolved pipeline queue at the end and did not verify a playable cold start. The result is specific to this large folded shader and does not measure the broader runtime benefit of the peepholes.
nidea1 commented 2026-11-29 16:18:13 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nidea1 on 2026-11-29T16:18:13Z.


Thanks @Ondra97 for the independent test run and profiling datapoint! That 1,808,720-byte Wave64-folded compute shader breakdown pointed straight to the real driver compile-time bottlenecks (which scalar shift/mul folding alone in this PR barely touches).

Profiling that exact DXIL output under VICTORY_SAMPLED_MARKS=1 showed two dominant bloat sources:

  1. Redundant SLOT_SAMPLED marking diamonds (1,547 atomics / +6,184 basic blocks / +232 KB DXIL in that shader alone) emitted on every repeated texture sample.
  2. guard_select + bitcast + nested i1 select blowup (71,537 selects including 36,153 guard_select, and 62,533 bitcasts) across Wave64-folded per-half chains and narrowed and(g, inner) guards.

We split the fixes into focused follow-up PRs (all clean against main and orthogonal to this PR):

  • #2702 — deduplicate mark_sampled along dominated IR paths
  • #2703 — backward guarded-demand select unwrapping (conjunction-aware), bitcast-through-select, and known-And/Or nested select collapsing
  • #2623 — CFG single-pred block coalescing & dead diamond folding

Measured on RTX 3060 Ti (same-session idle back-to-back uncached CreateComputePipelineState A/B vs main 55d06914 on that 1,808,720 B Wave64-folded shader):

  • DXIL size: 1,808,720 B -> 1,316,688 B (-27.2% / -492 KB)
  • Basic blocks: 8,548 -> 2,822 (-67.0%)
  • Instructions: 251,709 -> 184,474 (-26.7%; bitcasts -45.4%, guard_select -41.6%, SLOT_SAMPLED atomics -89.9%)
  • Cold CreateComputePipelineState: 29,080 ms -> 19,078 ms (-34.4% / 1.52x faster; top-3 Wave64 total 66.1 s -> 48.1 s, -27.2% / 1.37x faster).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7258778736 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2357#issuecomment-7258778736). Originally posted by [nidea1](https://github.com/nidea1) on 2026-11-29T16:18:13Z. --- Thanks @Ondra97 for the independent test run and profiling datapoint! That 1,808,720-byte Wave64-folded compute shader breakdown pointed straight to the real driver compile-time bottlenecks (which scalar shift/mul folding alone in this PR barely touches). Profiling that exact DXIL output under `VICTORY_SAMPLED_MARKS=1` showed two dominant bloat sources: 1. **Redundant `SLOT_SAMPLED` marking diamonds** (`1,547` atomics / `+6,184` basic blocks / `+232 KB` DXIL in that shader alone) emitted on every repeated texture sample. 2. **`guard_select` + `bitcast` + nested `i1 select` blowup** (`71,537` selects including `36,153` `guard_select`, and `62,533` bitcasts) across Wave64-folded per-half chains and narrowed `and(g, inner)` guards. We split the fixes into focused follow-up PRs (all clean against `main` and orthogonal to this PR): - #2702 — deduplicate `mark_sampled` along dominated IR paths - #2703 — backward guarded-demand select unwrapping (conjunction-aware), bitcast-through-select, and `known`-And/Or nested select collapsing - #2623 — CFG single-pred block coalescing & dead diamond folding Measured on RTX 3060 Ti (same-session idle back-to-back uncached `CreateComputePipelineState` A/B vs `main` `55d06914` on that 1,808,720 B Wave64-folded shader): - **DXIL size:** `1,808,720 B` -> **`1,316,688 B`** (`-27.2%` / `-492 KB`) - **Basic blocks:** `8,548` -> **`2,822`** (`-67.0%`) - **Instructions:** `251,709` -> **`184,474`** (`-26.7%`; bitcasts `-45.4%`, `guard_select` `-41.6%`, `SLOT_SAMPLED` atomics `-89.9%`) - **Cold `CreateComputePipelineState`:** `29,080 ms` -> **`19,078 ms`** (**`-34.4%` / `1.52x` faster**; top-3 Wave64 total `66.1 s` -> **`48.1 s`**, **`-27.2%` / `1.37x` faster**).
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
trustno1-coder/VIctory-NVIDIA!2357
No description provided.