perf(shader): Wave 10B M1 — InstCombine, float Select+PHI web promotion, intra-loop DCE, and slot_word mark_pure #428

Merged
Androtim merged 2 commits from upstream-pr/428 into main 2026-12-04 09:03:32 +00:00
Androtim commented 2026-12-04 08:25:11 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-04T08:25:11Z.


Summary

Implements Wave 10B: Shader Optimizer Milestone 1 — 100% semantically exact, bit-identical DXIL optimizer passes targeting BITCAST + VSELECT bloat, redundant index/bitfield/clamp logic, and cross-block CSE gaps:

  1. Intra-loop lightweight DCE (dce_insts): Splits drop_dead so dce_insts(&mut self) -> bool runs before and inside the fixpoint loop (leaving drop_unused_functions/drop_unused_tables once at the end), removing dead non-bitcast users that previously blocked float-web promotion.
  2. slot_word mark_pure (lower.rs): Marks SLOT_STRIDES/SLOT_TABLE_STRIDES table loads pure (Effect::Pure) so REUSE can CSE them past stores and barriers.
  3. Canonical Key::Cmp + extended FACTS + 8-iteration fixpoint cap:
    • Swaps  < a operand order and predicate direction via swap_pred (never producing pred::ONE (6)).
    • Records integer Cmp(EQ/NE, x, const_k) value equalities in Facts (excluding float equalities to preserve -0.0).
    • Allows FACTS on reducible loop headers with a single non-back-edge forward predecessor (safely excluding irreducible multi-entry loops).
    • Raises the fixpoint iteration cap from 4 to 8.
  4. Float Select + PHI web promotion (promote_float_webs):
    • Extends promote_float_phis greatest-fixpoint candidate set to include i32 Op::Select alongside Op::Phi, preserving exact bits including -0.0 and NaN payloads.
    • Adds the itcast(select(c, bitcast(f1)|k1, bitcast(f2)|k2)) -> select(c, f1|f32(k1), f2|f32(k2)) peephole for selects that also have integer users.
  5. Exact InstCombine (INST_COMBINE = 1024, included in DEFAULT/ALL = 1279, opt-out via VICTORY_SHADER_OPT=255):
    • Core integer folds: Sub(a,a)->0, Xor(a,a)->0, power-of-2 Mul->Shl, UDiv->LShr, URem->And, URem(x,1)->0; constant-fold, idempotence, nested-clamp collapse, and known-bits bounds for dx.op.binary.i32 (IMax/IMin/UMax/UMin) and dx.op.unaryBits.i32 (Countbits/FirstbitLo/FirstbitHi/FirstbitSHi).
    • Boolean/Select/Compare combines: Select(xor(c,1), t, f) -> Select(c, f, t), Xor(Xor(x,k1),k2) -> Xor(x,k1^k2), Xor(Cmp(p,a,b), 1) -> Cmp(opposite(p), a, b) (hard-guarded against p == pred::UEQ (9) so pred::ONE (6) is never emitted), i1 Select to Or/And/!c, Cmp(p, Select(c,k1,k2), k), and Cmp(EQ/NE, ZExt/SExt(i1), k).
    • Shift/mask/fe/fi & depth-4 known-bits: constant reassociation (Add/Sub/And/Or/Xor/Shl/LShr/AShr with strict < width shift-overflow guard), Shl(And(x,m), s) -> Shl(x,s), Or(And(m,a), And(~m,a)) -> a, and depth-4 (known_zero, known_one) analysis collapsing cbuffer_dword 3-Cmp/3-Select trees to a single Extract.
    • Preserves all hard invariants: never folds float Bin arithmetic, never rewrites integer NaN bitmask checks to cmp uno, never folds ract select into FMin, never emits pred::ONE (6), and never rewrites Select on dx.types.Handle.

Verification

  • cargo test -p victory-shader --lib bitcode::opt — 29 passed, 0 failed (including 400-seed random differential fuzzer across INST_COMBINE, INST_COMBINE | DEAD, and ALL, plus dedicated unit tests for every new fold and every hard guard).
  • cargo test -p victory-shader --lib updated tests ( he_clean_up_switch_parses_or_runs_every_pass, depth_only_alpha_test_inverted_compare_discards_only_killed_lanes, cost_checks under both OPT=1279 and OPT=255) — passed. (Note: 3 unrelated pre-existing PSV0 validator failures on
    ect/arycentric reproduce identically under VICTORY_SHADER_OPT=0.)
  • cargo clippy --workspace --all-targets -- -D warnings — 0 warnings.
  • cargo xtask policy — asset policy: 365 files ok (opt.rs = 212 KB, well under the 640 KB cap).
<!-- greyfinch-sync:Androtim/MiniVI:issue:5643022070 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/428). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-04T08:25:11Z. --- ## Summary Implements **Wave 10B: Shader Optimizer Milestone 1** — 100% semantically exact, bit-identical DXIL optimizer passes targeting BITCAST + VSELECT bloat, redundant index/bitfield/clamp logic, and cross-block CSE gaps: 1. **Intra-loop lightweight DCE (dce_insts)**: Splits drop_dead so dce_insts(&mut self) -> bool runs before and inside the fixpoint loop (leaving drop_unused_functions/drop_unused_tables once at the end), removing dead non-bitcast users that previously blocked float-web promotion. 2. **slot_word mark_pure (lower.rs)**: Marks SLOT_STRIDES/SLOT_TABLE_STRIDES table loads pure (Effect::Pure) so REUSE can CSE them past stores and barriers. 3. **Canonical Key::Cmp + extended FACTS + 8-iteration fixpoint cap**: - Swaps  < a operand order and predicate direction via swap_pred (never producing pred::ONE (6)). - Records integer Cmp(EQ/NE, x, const_k) value equalities in Facts (excluding float equalities to preserve -0.0). - Allows FACTS on reducible loop headers with a single non-back-edge forward predecessor (safely excluding irreducible multi-entry loops). - Raises the fixpoint iteration cap from 4 to 8. 4. **Float Select + PHI web promotion (promote_float_webs)**: - Extends promote_float_phis greatest-fixpoint candidate set to include i32 Op::Select alongside Op::Phi, preserving exact bits including -0.0 and NaN payloads. - Adds the itcast(select(c, bitcast(f1)|k1, bitcast(f2)|k2)) -> select(c, f1|f32(k1), f2|f32(k2)) peephole for selects that also have integer users. 5. **Exact InstCombine (INST_COMBINE = 1024, included in DEFAULT/ALL = 1279, opt-out via VICTORY_SHADER_OPT=255)**: - Core integer folds: Sub(a,a)->0, Xor(a,a)->0, power-of-2 Mul->Shl, UDiv->LShr, URem->And, URem(x,1)->0; constant-fold, idempotence, nested-clamp collapse, and known-bits bounds for dx.op.binary.i32 (IMax/IMin/UMax/UMin) and dx.op.unaryBits.i32 (Countbits/FirstbitLo/FirstbitHi/FirstbitSHi). - Boolean/Select/Compare combines: Select(xor(c,1), t, f) -> Select(c, f, t), Xor(Xor(x,k1),k2) -> Xor(x,k1^k2), Xor(Cmp(p,a,b), 1) -> Cmp(opposite(p), a, b) (hard-guarded against p == pred::UEQ (9) so pred::ONE (6) is never emitted), i1 Select to Or/And/!c, Cmp(p, Select(c,k1,k2), k), and Cmp(EQ/NE, ZExt/SExt(i1), k). - Shift/mask/fe/fi & depth-4 known-bits: constant reassociation (Add/Sub/And/Or/Xor/Shl/LShr/AShr with strict < width shift-overflow guard), Shl(And(x,m), s) -> Shl(x,s), Or(And(m,a), And(~m,a)) -> a, and depth-4 (known_zero, known_one) analysis collapsing cbuffer_dword 3-Cmp/3-Select trees to a single Extract. - Preserves all hard invariants: never folds float Bin arithmetic, never rewrites integer NaN bitmask checks to cmp uno, never folds ract select into FMin, never emits pred::ONE (6), and never rewrites Select on dx.types.Handle. ## Verification - cargo test -p victory-shader --lib bitcode::opt — **29 passed, 0 failed** (including 400-seed random differential fuzzer across INST_COMBINE, INST_COMBINE | DEAD, and ALL, plus dedicated unit tests for every new fold and every hard guard). - cargo test -p victory-shader --lib updated tests ( he_clean_up_switch_parses_or_runs_every_pass, depth_only_alpha_test_inverted_compare_discards_only_killed_lanes, cost_checks under both OPT=1279 and OPT=255) — passed. *(Note: 3 unrelated pre-existing PSV0 validator failures on ect/arycentric reproduce identically under VICTORY_SHADER_OPT=0.)* - cargo clippy --workspace --all-targets -- -D warnings — **0 warnings**. - cargo xtask policy — **asset policy: 365 files ok** (opt.rs = 212 KB, well under the 640 KB cap).
Androtim commented 2026-12-04 08:32:04 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-04T08:32:04Z.


Triage (905e8e7d, CI IN_PROGRESS, MERGEABLE):

  • Clean, 100% title-neutral Wave 10B M1 DXIL optimizer (+1,944/-118 across 4 files, zero opt-in toggles — INST_COMBINE = 1024, float Select+PHI web promotion, intra-loop dce_insts, slot_word mark_pure, canonical Key::Cmp, and 8-iteration fixpoint are all default-on in VICTORY_SHADER_OPT = 1279).
  • Marked triage: needs gpu test — awaiting CI check PASS + Gate 2 Audit (#282) + Gate 3 plain---release A/B & --features diagnostics visual verification (confirming untranslated does not increase and cold-cache shaders validate cleanly).
<!-- greyfinch-sync:Androtim/MiniVI:comment:7406061222 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/428#issuecomment-7406061222). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-04T08:32:04Z. --- Triage (`905e8e7d`, CI `IN_PROGRESS`, `MERGEABLE`): - Clean, 100% title-neutral Wave 10B M1 DXIL optimizer (`+1,944/-118` across 4 files, **zero opt-in toggles** — `INST_COMBINE = 1024`, float `Select`+`PHI` web promotion, intra-loop `dce_insts`, `slot_word` `mark_pure`, canonical `Key::Cmp`, and 8-iteration fixpoint are all default-on in `VICTORY_SHADER_OPT = 1279`). - Marked `triage: needs gpu test` — awaiting CI `check` PASS + Gate 2 Audit (`#282`) + Gate 3 plain-`--release` A/B & `--features diagnostics` visual verification (confirming `untranslated` does not increase and cold-cache shaders validate cleanly).
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
Androtim/MiniVI!428
No description provided.