shader: fold guarded-demand selects, bitcast-through-select, and known-And/Or nested selects #2703

Manually merged
github merged 1 commit from github-bot/gh-2703 into gh-pr/2703/base 2026-11-30 14:17:06 +00:00 AGit
Owner

@⁠nidea1 on GitHub · 2026-11-29 16:17 UTC · original
nidea1:perf/dxil-opt-guarded-demand-and-bitcast-select → main · +869 −10 · 1 files · 1 commits · merged 2026-11-30 14:12 UTC

Summary

Eliminates redundant guard_select, bitcast, and nested select bloat in DXIL optimization (crates/victory-shader/src/emit/bitcode/opt.rs), targeting the primary instruction blowup in Wave64-folded compute shaders.

In Wave64-folded shaders, every guarded vector instruction emits bitcast -> op -> bitcast -> select(g, _, old). Previously, simplify() only folded immediately adjacent same-condition selects, leaving tens of thousands of intermediate guard_selects and paired i32 <-> f32 bitcasts alive across ALU ops, v_cndmask selects, and narrowed nested guards (and(g, inner)).

Changes

  1. Backward guarded-demand simplification (Module::simplify_guarded_uses, under FOLD):
    • Runs a bounded-height worklist dataflow (Unused < UnderTrue(Conj) / UnderFalse < Any) over reachable instructions to determine when a value is only observed under a conjunction of i1 guards (including asymmetric i1 Bin::And propagation and strictly dominating Phi propagation).
    • For pure, non-trapping, per-lane instructions (Bin excluding integer UDiv/SDiv/URem, Cmp, Cast, Extract, Select, speculatable pure math Calls, and strictly dominated Phis), unwraps matching or logically implied select(c, t, f) operands directly to the active arm and folds implied i1 operands to constants.
    • Rewrites Phis first and then walks body instructions in reverse RPO / reverse instruction order so consumers always inspect definitions before mutation.
    • Strictly preserves trapping integer divides, Effect::Lanes wave/quad ops, Effect::Reads memory/texture loads, non-dominated loop-header phis, and narrowed-guard false arms.
  2. Bitcast-through-Select folding (simplify Cast::Bitcast):
    • Folds bitcast(select(c, a, b)) into select(c, a', b') when both arms reinterpret to the target type for free (inverse Cast::Bitcast, 32-bit Const::Int <-> Const::Float, or Const::Undef).
  3. Nested Select collapsing via existing known And/Or (merge under FOLD | REUSE):
    • Collapses select(c1, select(c2, t2, f), f) -> select(both, t2, f) and select(c1, t, select(c2, t, f2)) -> select(either, t, f2) when And(c1, c2) / Or(c1, c2) is already available in the dominating known CSE map (zero new instructions).

Measured Impact (RTX 3060 Ti, VICTORY_SAMPLED_MARKS=1, cold CreateComputePipelineState)

Measured across the 10 largest Wave64-folded compute shaders (main 829188e9 vs this PR alone):

  • Largest shader (#1, ~29K console insts):
    • DXIL size: 1,808,720 B -> 1,524,836 B (-283.9 KB / -15.7%)
    • Total DXIL insts: 251,709 -> 201,042 (-50,667 / -20.1%)
    • guard_select: 36,153 -> 21,115 (-41.6%); bitcast: 62,533 -> 35,544 (-43.2%); select: 71,537 -> 53,041 (-25.9%)
    • Cold PSO compile: 36,963 ms -> 28,580 ms (-22.7% / -8.4 s)
  • Top 3 cold PSO compile total: 87,039 ms -> 67,426 ms (-22.5% / -19.6 s)
  • Top 10 Wave64 totals: DXIL 8.45 MB -> 7.10 MB (-16.0%), insts 1,152,029 -> 913,335 (-20.7%), translation time 4,697 ms -> 4,542 ms (-3.3%).

Combined with #2702 + #2623 + #2357 (same-session idle back-to-back uncached A/B vs main 55d06914):

  • #1: DXIL 1,808,720 B -> 1,316,688 B (-27.2%), blocks 8,548 -> 2,822 (-67.0%), insts 251,709 -> 184,474 (-26.7%), cold PSO 29,080 ms -> 19,078 ms (-34.4% / 1.52x faster)
  • #2: DXIL 1,508,464 B -> 1,135,060 B (-24.8%), blocks 6,702 -> 2,708 (-59.6%), cold PSO 24,376 ms -> 19,287 ms (-20.9% / 1.26x faster)
  • #3: DXIL 1,021,396 B -> 803,656 B (-21.3%), blocks 2,341 -> 1,444 (-38.3%), cold PSO 12,599 ms -> 9,722 ms (-22.8% / 1.30x faster)
  • Top-3 cold PSO total: 66,055 ms -> 48,087 ms (-27.2% / -18.0 s / 1.37x faster); Top-10 DXIL 8.45 MB -> 6.36 MB (-24.7%).

Verification

  • cargo test -p victory-shader -j 4 --lib bitcode::opt — 29 passed, 0 failed (including randomized differential interpreter every_pass_leaves_what_random_programs_store, hardware RTX 3060 Ti + WARP bit-exact parity dxil_opt_hw_gpu_bit_exact_parity_and_validation, and 3 new positive + negative-control tests).
  • cargo test -p victory-shader -j 4 --lib folded — 19 passed, 0 failed (hardware + WARP Wave64-folded tests).
  • Combined stack merged onto main (55d06914): cargo test -p victory-shader --locked (470 passed, 0 failed), full pipeline preload (2,720/2,720 active pipelines compiled cleanly), and live runtime validation (0 shader errors / device loss).
  • cargo clippy -p victory-shader -j 4 --tests — 0 new warnings.
  • cargo xtask policy --staged — clean.
**@⁠nidea1** on GitHub · 2026-11-29 16:17 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2703) `nidea1:perf/dxil-opt-guarded-demand-and-bitcast-select` → `main` · +869 −10 · 1 files · 1 commits · merged 2026-11-30 14:12 UTC ## Summary Eliminates redundant `guard_select`, `bitcast`, and nested `select` bloat in DXIL optimization (`crates/victory-shader/src/emit/bitcode/opt.rs`), targeting the primary instruction blowup in Wave64-folded compute shaders. In Wave64-folded shaders, every guarded vector instruction emits `bitcast -> op -> bitcast -> select(g, _, old)`. Previously, `simplify()` only folded immediately adjacent same-condition selects, leaving tens of thousands of intermediate `guard_select`s and paired `i32 <-> f32` bitcasts alive across ALU ops, `v_cndmask` selects, and narrowed nested guards (`and(g, inner)`). ### Changes 1. **Backward guarded-demand simplification (`Module::simplify_guarded_uses`, under `FOLD`):** - Runs a bounded-height worklist dataflow (`Unused < UnderTrue(Conj) / UnderFalse < Any`) over reachable instructions to determine when a value is only observed under a conjunction of `i1` guards (including asymmetric `i1 Bin::And` propagation and strictly dominating `Phi` propagation). - For pure, non-trapping, per-lane instructions (`Bin` excluding integer `UDiv`/`SDiv`/`URem`, `Cmp`, `Cast`, `Extract`, `Select`, speculatable pure math `Call`s, and strictly dominated `Phi`s), unwraps matching or logically implied `select(c, t, f)` operands directly to the active arm and folds implied `i1` operands to constants. - Rewrites `Phi`s first and then walks `body` instructions in reverse RPO / reverse instruction order so consumers always inspect definitions before mutation. - Strictly preserves trapping integer divides, `Effect::Lanes` wave/quad ops, `Effect::Reads` memory/texture loads, non-dominated loop-header phis, and narrowed-guard false arms. 2. **Bitcast-through-`Select` folding (`simplify` `Cast::Bitcast`):** - Folds `bitcast(select(c, a, b))` into `select(c, a', b')` when both arms reinterpret to the target type for free (inverse `Cast::Bitcast`, 32-bit `Const::Int` <-> `Const::Float`, or `Const::Undef`). 3. **Nested `Select` collapsing via existing `known` `And`/`Or` (`merge` under `FOLD | REUSE`):** - Collapses `select(c1, select(c2, t2, f), f)` -> `select(both, t2, f)` and `select(c1, t, select(c2, t, f2))` -> `select(either, t, f2)` when `And(c1, c2)` / `Or(c1, c2)` is already available in the dominating `known` CSE map (zero new instructions). ### Measured Impact (RTX 3060 Ti, `VICTORY_SAMPLED_MARKS=1`, cold `CreateComputePipelineState`) Measured across the 10 largest Wave64-folded compute shaders (`main` `829188e9` vs this PR alone): - **Largest shader (#1, ~29K console insts):** - DXIL size: `1,808,720 B` -> `1,524,836 B` (`-283.9 KB` / `-15.7%`) - Total DXIL insts: `251,709` -> `201,042` (`-50,667` / `-20.1%`) - `guard_select`: `36,153` -> `21,115` (`-41.6%`); `bitcast`: `62,533` -> `35,544` (`-43.2%`); `select`: `71,537` -> `53,041` (`-25.9%`) - Cold PSO compile: `36,963 ms` -> `28,580 ms` (**`-22.7%` / `-8.4 s`**) - **Top 3 cold PSO compile total:** `87,039 ms` -> `67,426 ms` (**`-22.5%` / `-19.6 s`**) - **Top 10 Wave64 totals:** DXIL `8.45 MB` -> `7.10 MB` (`-16.0%`), insts `1,152,029` -> `913,335` (`-20.7%`), translation time `4,697 ms` -> `4,542 ms` (`-3.3%`). **Combined with #2702 + #2623 + #2357 (same-session idle back-to-back uncached A/B vs `main` `55d06914`):** - `#1`: DXIL `1,808,720 B` -> `1,316,688 B` (`-27.2%`), blocks `8,548` -> `2,822` (`-67.0%`), insts `251,709` -> `184,474` (`-26.7%`), cold PSO `29,080 ms` -> **`19,078 ms`** (**`-34.4%` / `1.52x` faster**) - `#2`: DXIL `1,508,464 B` -> `1,135,060 B` (`-24.8%`), blocks `6,702` -> `2,708` (`-59.6%`), cold PSO `24,376 ms` -> **`19,287 ms`** (**`-20.9%` / `1.26x` faster**) - `#3`: DXIL `1,021,396 B` -> `803,656 B` (`-21.3%`), blocks `2,341` -> `1,444` (`-38.3%`), cold PSO `12,599 ms` -> **`9,722 ms`** (**`-22.8%` / `1.30x` faster**) - **Top-3 cold PSO total:** `66,055 ms` -> **`48,087 ms`** (**`-27.2%` / `-18.0 s` / `1.37x` faster**); Top-10 DXIL `8.45 MB` -> `6.36 MB` (`-24.7%`). ### Verification - `cargo test -p victory-shader -j 4 --lib bitcode::opt` — **29 passed, 0 failed** (including randomized differential interpreter `every_pass_leaves_what_random_programs_store`, hardware RTX 3060 Ti + WARP bit-exact parity `dxil_opt_hw_gpu_bit_exact_parity_and_validation`, and 3 new positive + negative-control tests). - `cargo test -p victory-shader -j 4 --lib folded` — **19 passed, 0 failed** (hardware + WARP Wave64-folded tests). - Combined stack merged onto `main` (`55d06914`): `cargo test -p victory-shader --locked` (**470 passed, 0 failed**), full pipeline preload (**2,720/2,720** active pipelines compiled cleanly), and live runtime validation (**0** shader errors / device loss). - `cargo clippy -p victory-shader -j 4 --tests` — 0 new warnings. - `cargo xtask policy --staged` — clean.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 16:21:14 +00:00
github-bot changed target branch from main to gh-pr/2703/base 2026-11-30 14:16:48 +00:00
github manually merged commit 991c6fa8d0 into gh-pr/2703/base 2026-11-30 14:16:49 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!2703
No description provided.