Shaders/opt: push Bitcast through Select/Phi and fold constant chains/masks #1398
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1398"
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 fxceless/victory-fork. Originally posted by n-romanovich on 2026-11-26T23:06:51Z.
Related to #1383 (reducing the ~2.4x translated DXIL instruction ratio, where
bitcastandselecteach make up ~12% of emitted instructions).Problem
Because
lower.rsstores vector/scalar registers andOp::Phivalues asi32and lowers predicated writes viai32Op::Select, guarded float VALU ops, float loopPhis,v_cndmask_b32, andraw_dwordsfloat loads systematically emitbitcast(f32 -> i32) -> select/phi(i32) -> bitcast(i32 -> f32)chains thatopt.rsleft unsimplified.Module::simplifyandModule::keyalso missed associative constant folding across buffer/SMEM addressing (Add(Add(x, k1), k2)), redundant bitmask elimination (And(LShr(x, 27), 31)), booleanSelect/Cmp(ZExt(i1))peepholes, and commutedCmpcanonicalization.Changes (
crates/victory-shader/src/emit/bitcode/opt.rs)Cast::BitcastthroughOp::SelectandOp::Phi:Module::simplify, rewriteBitcast(Select(c, t, f))toSelect(c, t', f')when at least one arm strips a matching inverseBitcastand the other converts viarebitcast(bit-exact for 32-bit ints/floats, preserving-0.0and NaN payloads).Module::retype_bitcast_phis(run insideModule::optimiseunderFOLD) to retypeOp::Phinodes whose non-self users are exclusivelyBitcastto the same target typeTwhen all incoming values convert toT, exposing the surroundingBitcasts for elimination on the nextmergepass.assoc_const_binto fold associative constant chains acrossAdd/Sub/And/Or/Xor/Muland boundedShl/LShr/AShr, plusXor(x, x) -> 0andSub(x, x) -> 0.possible_onesbitmask tracking to eliminate redundantAnd(x, m)masks (such as& 31afterLShr(_, 27)orZExt).Select(Xor(c, true), t, f) -> Select(c, f, t),Select(c, false, true) -> Xor(c, true), andCmp(EQ/NE, ZExt(x: i1), 0/1).b < a) with exact predicate flipping inModule::keyforOp::Cmp(preserving IEEE/NaN semantics).Verification
emit::bitcode::opt::tests::pushes_bitcast_through_select_and_phiandemit::bitcode::opt::tests::reassociates_constants_known_bits_and_boolean_peepholes.cargo test --locked -p victory-shader --features host --lib:235 passed; 0 failed; 4 ignored(including GPUdiff_tests,valu_tests,wave64_tests, andevery_pass_leaves_what_random_programs_storeacross 400 random programs).cargo build --release --locked -p victoryImported from fxceless/victory-fork. Originally posted by Melow-code on 2026-11-29T08:17:37Z.
Review: COMMENTED
Fuzzed the new folds on Linux,
5c36f59merged onto main368967f8(no conflict): no objection.I wrote a generator and an evaluator of my own for this, separate from
random_programandrunin your test module: chains of one to four constant operations (every integer operation, constant on either side, masks that match shift widths), float writes kept as bits behind up to two nested selects, joins of those in branches and loops, bits widened and compared with constants, selects of bits and repeated nots, 8-, 16- and 64-bit values, values combined with a register and undone.FOLDalone up toALL): 7,710,402 stored values, the same bit for bit before and after, NaN payloads included. In the reached blocks the operands of every instruction and join still have the types they need, and no bitcast to its own type is left.(x + k1) - k2with its operands swapped (seed 48),zext(bit) != 1folded to the bit (seed 2), twoashrwhose sum passes the width folded to 0 (seed 23), a join retyped although another instruction reads it as an integer (seed 37).cargo test -p victory-formats -p victory-shader --lib: 42 and 209 passed, 0 failed (5 and 2 ignored).Not run: Windows, the D3D12 validator, any card, the title's shaders. The fuzz evaluates one thread, so it cannot see a driver that treats a float
selectorphiof NaN bits or denormals differently from an integer one; a run of the semantics tests on a card would.AI-assisted review (Claude Code).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.