shader: fold guarded-demand selects, bitcast-through-select, and known-And/Or nested selects #2703
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2703"
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 trustno1-coder/VIctory-NVIDIA. Originally posted by nidea1 on 2026-11-29T16:17:45Z.
Summary
Eliminates redundant
guard_select,bitcast, and nestedselectbloat 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 intermediateguard_selects and pairedi32 <-> f32bitcasts alive across ALU ops,v_cndmaskselects, and narrowed nested guards (and(g, inner)).Changes
Module::simplify_guarded_uses, underFOLD):Unused < UnderTrue(Conj) / UnderFalse < Any) over reachable instructions to determine when a value is only observed under a conjunction ofi1guards (including asymmetrici1 Bin::Andpropagation and strictly dominatingPhipropagation).Binexcluding integerUDiv/SDiv/URem,Cmp,Cast,Extract,Select, speculatable pure mathCalls, and strictly dominatedPhis), unwraps matching or logically impliedselect(c, t, f)operands directly to the active arm and folds impliedi1operands to constants.Phis first and then walksbodyinstructions in reverse RPO / reverse instruction order so consumers always inspect definitions before mutation.Effect::Laneswave/quad ops,Effect::Readsmemory/texture loads, non-dominated loop-header phis, and narrowed-guard false arms.Selectfolding (simplifyCast::Bitcast):bitcast(select(c, a, b))intoselect(c, a', b')when both arms reinterpret to the target type for free (inverseCast::Bitcast, 32-bitConst::Int<->Const::Float, orConst::Undef).Selectcollapsing via existingknownAnd/Or(mergeunderFOLD | REUSE):select(c1, select(c2, t2, f), f)->select(both, t2, f)andselect(c1, t, select(c2, t, f2))->select(either, t, f2)whenAnd(c1, c2)/Or(c1, c2)is already available in the dominatingknownCSE map (zero new instructions).Measured Impact (RTX 3060 Ti,
VICTORY_SAMPLED_MARKS=1, coldCreateComputePipelineState)Measured across the 10 largest Wave64-folded compute shaders (
main829188e9vs this PR alone):1,808,720 B->1,524,836 B(-283.9 KB/-15.7%)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%)36,963 ms->28,580 ms(-22.7%/-8.4 s)87,039 ms->67,426 ms(-22.5%/-19.6 s)8.45 MB->7.10 MB(-16.0%), insts1,152,029->913,335(-20.7%), translation time4,697 ms->4,542 ms(-3.3%).Combined with #2702 + #2623 + #2357 (same-session idle back-to-back uncached A/B vs
main55d06914):#1: DXIL1,808,720 B->1,316,688 B(-27.2%), blocks8,548->2,822(-67.0%), insts251,709->184,474(-26.7%), cold PSO29,080 ms->19,078 ms(-34.4%/1.52xfaster)#2: DXIL1,508,464 B->1,135,060 B(-24.8%), blocks6,702->2,708(-59.6%), cold PSO24,376 ms->19,287 ms(-20.9%/1.26xfaster)#3: DXIL1,021,396 B->803,656 B(-21.3%), blocks2,341->1,444(-38.3%), cold PSO12,599 ms->9,722 ms(-22.8%/1.30xfaster)66,055 ms->48,087 ms(-27.2%/-18.0 s/1.37xfaster); Top-10 DXIL8.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 interpreterevery_pass_leaves_what_random_programs_store, hardware RTX 3060 Ti + WARP bit-exact paritydxil_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).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.