perf(shader): promote bitcast-wrapped i32 PHIs to f32 in bitcode opt (#276) #311
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/311"
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 Androtim/MiniVI. Originally posted by RUIZ-Enrique-25002136 on 2026-12-02T08:55:28Z.
Summary
Implements
Module::promote_float_phisincrates/victory-shader/src/emit/bitcode/opt.rs(tracked in #276'svictory-shaderGPU Shader Time backlog): promotes bitcast-wrapped 32-biti32Op::Phiwebs directly tof32Op::Phis duringModule::optimise, allowingmerge()anddrop_dead()to eliminate redundantf32 <-> i32bitcastround-trips across branches and loops.Refs #276
AI-Ready Context & Change Map
For downstream coding/review agents, here is the exact scope, algorithm, and invariant set of this PR:
crates/victory-shader/src/emit/bitcode/opt.rs—Module::promote_float_phis& Pass IntegrationModule::optimise,opt.rs:236-247):self.promote_float_phis()inside thefor _ in 0..4loop wheneverpasses & FOLD != 0, beforeself.merge(passes).merge(passes)ensures that every promotedi32 -> f32PHI immediately turns its downstreamOp::Cast(Cast::Bitcast, V::Inst(phi))users intof32 -> f32identity bitcasts (opt.rs:650:if from == ty { return Some(x); }), whichmerge()deletes fromblk.bodyand rewires in the same iteration, whiledrop_dead()drops all now-unusedf32 -> i32producer bitcasts.opt.rs:705-746):self.reached()[b]is true, matchingmerge()'s dominator-tree traversal andModule::write()'s reachable postorder traversal so no unreachablebitcast(f32 -> f32)can ever be orphaned.opt.rs:753-795):cand[i] = truefor every non-empty 32-bit integerOp::Phiin a reachable block.iif:term_used[i]is true (used directly byTerm::CondBr), ORuinusers[i]is neither ani32 -> f32Cast::Bitcastnor another survivingcandPHI, OR(v, _)ofiis notfloat_bitcast_src(f32_ty, v)(Op::Cast(Cast::Bitcast, x)withtype_of(x) == f32_ty), a survivingcandPHI, or a 32-bitConst::Int/Const::Undef.opt.rs:799-838):candweb to be connected (transitively alongcandPHI-to-PHI edges) to at least one actualf32 -> i32incoming bitcast ori32 -> f32user bitcast.opt.rs:840-866):self.insts[i].ty = f32_tyand rewrites each incoming(v, b)to:(x, b)whenvisbitcast(x: f32 -> i32),(v, b)whenvis another promotedcandPHI,(self.f32(bits as u32), b)forConst::Int(bits),(self.undef(f32_ty), b)forConst::Undef.Const::Int(bits)maps bit-for-bit toConst::Float(bits as u32)and bitcasts preserve exact 32-bit payloads (including-0.0and NaN encodings), semantics are bit-exact.crates/victory-shader/src/emit/bitcode/opt.rs— Unit Testsfloat_phis_promote_across_diamond_and_loop(opt.rs:2418-2504):Const::Int(0)init +bitcast(fadd(bitcast(phi)))back-edge), asserting both PHIs becomef32,0Cast::Bitcastinstructions remain afteroptimise(ALL),Module::write()succeeds, and the interpreterrun()trace matches before/after across branching inputs.mixed_int_and_float_phi_stays_i32(opt.rs:2506-2548):i32PHI mixingbitcast(f32 -> i32)and an integerBin::Addstaysi32and preserves exactrun()behavior.Rig Specs Tested On
400seeds ×8pass configs, and clippy)x86_64-pc-windows-msvcfull workspace clippy and live GPU frame-time benchmarks deferred to GitHub Actions (windows-latest) / maintainer runRun Settings & Profile
victory-shaderbitcode optimizer changes on top oforigin/main(c7d3102)VICTORY_*Overrides: N/AMeasurements & Verification
cargo clippy --workspace --all-targets --locked -- -D warnings(requiresx86_64-pc-windows-msvc; verified on GitHub Actionswindows-latestCI)cargo clippy --workspace --all-targets --all-features --locked -- -D warnings(requiresx86_64-pc-windows-msvc; verified on GitHub Actionswindows-latestCI)cargo clippy --locked -p victory-shader --lib -- -D warnings-> 0 warnings, exit 0cargo test --locked -p victory-shader emit::bitcode::opt::tests-> 13 passed, 0 failed (includingevery_pass_leaves_what_random_programs_store,float_phis_promote_across_diamond_and_loop,mixed_int_and_float_phi_stays_i32)cargo test --locked -p victory-shader -- --test-threads=1-> 254 passed, 0 failed, 2 ignoredcargo test --locked -p xtask-> 8 passed, 0 failedcargo xtask policy->asset policy: 321 files okImported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T09:03:43Z.
Automated triage for the maintainers (not an approval).
Verdict:
needs-gpu-test· CI (check):passingReview Summary
Logic, type-safety invariants, and test coverage look solid, and CI is passing. Because this changes emitted DXIL shader bitcode (
victory-shader), a quick local Windows/D3D12 real-GPU smoke test (cold shader compile + PSO creation + visual check) is warranted before merging.What was verified
crates/victory-shader/src/emit/bitcode/opt.rs,Module::optimise): Runningself.promote_float_phis()inside thefor _ in 0..4loop right beforeself.merge(passes)whenpasses & FOLD != 0ensures newly createdf32 -> f32identity bitcasts are immediately folded byModule::fold(Cast::Bitcastfrom == tybranch) in the same iteration, with dead producer bitcasts cleaned up bydrop_dead().Module::promote_float_phis,Module::float_bitcast_src):self.reached()blocks.i32PHI with non-bitcast/non-candidate users, terminator uses (Term::CondBr), or non-float-bitcast/non-candidate/non-Const::Int|Const::Undefincoming edges.witnessedpropagation overcandPHI-to-PHI edges ensures connected PHI webs are promoted atomically (no mixedi32/f32PHI edges) and only when anchored to at least one actual float bitcast.Const::Int(bits) => self.f32(bits as u32)andConst::Undef => self.undef(f32_ty)preserve exact 32-bit bit patterns (including-0.0and NaN payloads).float_phis_promote_across_diamond_and_loopandmixed_int_and_float_phi_stays_i32cover both positive (diamond + loop-carried PHI with zero remaining bitcasts andModule::write()) and negative (mixed int/float PHI staysi32) cases alongside the existing randomized differential pass tests.Minor note (non-blocking)
promote_float_phisiterate0..self.insts.len(); collecting candidate PHI indices into a smallVec<usize>would avoid scanning non-PHI instructions on repeated fixpoint passes, though the current cost is negligible for typical shader sizes.Next step
Run a local Windows D3D12 smoke test with a cold shader cache to confirm DXIL validation, driver PSO compilation, and rendering output on real GPU hardware.
Generated with Antigravity CLI (
agy).Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-03T11:12:46Z.
Gate 3 Live Windows D3D12 Cold-Cache GPU Verification — PASS
Tested combined PR #311 + PR #314 on Windows 11 + NVIDIA RTX 4060 Ti 8 GB (
1440pDLSS Quality, fresh cold shader cache):14/14victory-shader::emit::bitcode::opt::testspassing (includingfloat_phis_promote_across_diamond_and_loop,mixed_int_and_float_phi_stays_i32,idempotent_saturate_and_minmax_calls_fold, and randomized differential tests).DRAWS 9904,DROPPED 172,0 being made, peak dedicated VRAM5476 / 6613 MiB, crisp lighting/textures/meshes).Ready to merge!