perf(shader): add SimplifyCFG with guard coalescing and LICM (Wave 11A M2+M3) #429

Merged
Androtim merged 3 commits from upstream-pr/429 into main 2026-12-04 10:31:13 +00:00
Androtim commented 2026-12-04 09:43:36 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-04T09:43:36Z.


Summary

Implements Wave 11A Shader Optimizer Milestone 2 (SimplifyCFG + Guard Coalescing) and Milestone 3 (LICM) as 100% semantically exact, bit-identical DXIL module optimization passes, split cleanly into submodules (opt/cfg.rs and opt/licm.rs) to keep every source file compact and far below the policy size limit.

Milestone 2: SIMPLIFY_CFG = 256 (crates/victory-shader/src/emit/bitcode/opt/cfg.rs)

  • Same-Target CondBr Collapse: CondBr(c, T, T) -> Br(T) with (v, b) deduplication in T's PHIs.
  • Branch Inversion: CondBr(xor(c, true), t, f) -> CondBr(c, f, t).
  • Single-Pred / Single-Succ Merge (A -> B): Merges linear chains when A != B, B != 0, A.term == Br(B), and reachable preds[B] == [A], substituting 1-edge PHIs and rewriting B -> A in successor PHIs in a single O(N) sweep.
  • Empty Guard Diamond -> Select: Folds P: CondBr(c, B, J) (both polarities and double-empty-arm diamonds) into Select(c, vt, vf) in P + Br(J). Hard invariant: skips whenever any PHI type in J is dx.types.Handle or a non-scalar aggregate.
  • Safe Empty Forwarding Block Bypass: Bypasses empty P -> B -> T only when P does not already edge to T (if T has PHIs) and never creates a critical edge into a loop header or out of a loop (preserving dedicated preheaders/exits and itcode.rs AMD empty-latch folding).
  • Adjacent Same-Guard & Complementary-Guard Diamond Coalescing:
    • Same guard (g == g2): merges adjacent egin_guarded diamonds (T1 -> T2), eliminating intermediate J1 and migrating live PHIs when safe.
    • Complementary guard (g2 == !g, including dynamic-heap SMEM loads where J1 computes cmp(EQ, stride, 0)): collapses 4-block double diamonds into a single if (g) { T1 } else { T2 } diamond while guarding against dx.types.Handle, dx.op.barrier/dx.op.discard, and cross-lane Writes vs Reads/Writes reordering.

Milestone 3: LICM = 512 (crates/victory-shader/src/emit/bitcode/opt/licm.rs)

  • Detects natural loops from dominator_tree() back-edges (latch -> H), enforces reducibility (all reachable preds of  in L \ {H} lie in L), and processes loops innermost-first.
  • Reuses an existing dedicated Br(H) outside predecessor as preheader PH, or lazily synthesizes PH (splitting H's PHIs into outside/inside) only when at least one instruction hoists.
  • Exact Hoisting Soundness Rules:
    • Speculative + must_exec: pure non-trapping Bin (Add/Sub/Mul/And/Or/Xor), Cast, Cmp, Select, Extract, Gep, Attr::ReadNone math / clamps, createHandleFromBinding + nnotateHandle(fromBinding), and constant-global Op::Load (space == 0, init.is_some()).
    • must_exec ONLY (linear prefix from H stopping before any dx.op.discard): createHandleFromHeap + nnotateHandle (wave-uniformity invariant), cbufferLoadLegacy, getDimensions, pure_loads (SLOT_MEMORY, SLOT_STRIDES), Shl/LShr/AShr (const RHS < bits), and UDiv/SDiv/URem (non-zero const divisor, != -1 for SDiv).
    • NEVER HOIST: non-pure
      awBufferLoad SRVs, extureLoad, sample*, LDS/TGSM Op::Load (space == 3) (preserves cross-wave spin-polling loops), Effect::Lanes (wave*/quad*/derivatives), Op::Phi, and Effect::Writes.

VICTORY_SHADER_OPT Integration (opt.rs, emit/mod.rs)

  • Included in MODULE / ALL / DEFAULT (2047; opt-out via VICTORY_SHADER_OPT=1279 to omit both, 1535 for SIMPLIFY_CFG only, 1791 for LICM only).
  • Integrated into Module::optimise fixpoint loop: promote_float_webs -> merge -> prune -> dce_insts -> simplify_cfg -> licm.

Verification

  • cargo test -p victory-shader emit::bitcode::opt::: 41 passed, 0 failed (including 11 new cfg + licm tests covering every transform, every hard invariant, dxil.dll validation, and the 400-seed differential fuzzer across SIMPLIFY_CFG, LICM, SIMPLIFY_CFG | LICM, SIMPLIFY_CFG | LICM | DEAD, and ALL).
  • cargo test -p victory-shader: 486 passed (3 pre-existing local dxil.dll PSV0 failures on geometry/barycentric shaders that also fail identically at VICTORY_SHADER_OPT=0).
  • cargo clippy --workspace --all-targets -- -D warnings: 0 warnings, 0 errors.
  • cargo xtask policy & cargo xtask policy --staged: ok.
<!-- greyfinch-sync:Androtim/MiniVI:issue:5644109754 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/429). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-04T09:43:36Z. --- ## Summary Implements Wave 11A Shader Optimizer Milestone 2 (SimplifyCFG + Guard Coalescing) and Milestone 3 (LICM) as 100% semantically exact, bit-identical DXIL module optimization passes, split cleanly into submodules (opt/cfg.rs and opt/licm.rs) to keep every source file compact and far below the policy size limit. ### Milestone 2: SIMPLIFY_CFG = 256 (crates/victory-shader/src/emit/bitcode/opt/cfg.rs) - **Same-Target CondBr Collapse:** CondBr(c, T, T) -> Br(T) with (v, b) deduplication in T's PHIs. - **Branch Inversion:** CondBr(xor(c, true), t, f) -> CondBr(c, f, t). - **Single-Pred / Single-Succ Merge (A -> B):** Merges linear chains when A != B, B != 0, A.term == Br(B), and reachable preds[B] == [A], substituting 1-edge PHIs and rewriting B -> A in successor PHIs in a single O(N) sweep. - **Empty Guard Diamond -> Select:** Folds P: CondBr(c, B, J) (both polarities and double-empty-arm diamonds) into Select(c, vt, vf) in P + Br(J). **Hard invariant:** skips whenever any PHI type in J is dx.types.Handle or a non-scalar aggregate. - **Safe Empty Forwarding Block Bypass:** Bypasses empty P -> B -> T only when P does not already edge to T (if T has PHIs) and **never creates a critical edge into a loop header or out of a loop** (preserving dedicated preheaders/exits and itcode.rs AMD empty-latch folding). - **Adjacent Same-Guard & Complementary-Guard Diamond Coalescing:** - Same guard (g == g2): merges adjacent egin_guarded diamonds (T1 -> T2), eliminating intermediate J1 and migrating live PHIs when safe. - Complementary guard (g2 == !g, including dynamic-heap SMEM loads where J1 computes cmp(EQ, stride, 0)): collapses 4-block double diamonds into a single if (g) { T1 } else { T2 } diamond while guarding against dx.types.Handle, dx.op.barrier/dx.op.discard, and cross-lane Writes vs Reads/Writes reordering. ### Milestone 3: LICM = 512 (crates/victory-shader/src/emit/bitcode/opt/licm.rs) - Detects natural loops from dominator_tree() back-edges (latch -> H), enforces reducibility (all reachable preds of  in L \ {H} lie in L), and processes loops **innermost-first**. - Reuses an existing dedicated Br(H) outside predecessor as preheader PH, or lazily synthesizes PH (splitting H's PHIs into outside/inside) only when at least one instruction hoists. - **Exact Hoisting Soundness Rules:** - Speculative + must_exec: pure non-trapping Bin (Add/Sub/Mul/And/Or/Xor), Cast, Cmp, Select, Extract, Gep, Attr::ReadNone math / clamps, createHandleFromBinding + nnotateHandle(fromBinding), and constant-global Op::Load (space == 0, init.is_some()). - must_exec ONLY (linear prefix from H stopping before any dx.op.discard): createHandleFromHeap + nnotateHandle (wave-uniformity invariant), cbufferLoadLegacy, getDimensions, pure_loads (SLOT_MEMORY, SLOT_STRIDES), Shl/LShr/AShr (const RHS < bits), and UDiv/SDiv/URem (non-zero const divisor, != -1 for SDiv). - **NEVER HOIST:** non-pure awBufferLoad SRVs, extureLoad, sample*, LDS/TGSM Op::Load (space == 3) (preserves cross-wave spin-polling loops), Effect::Lanes (wave*/quad*/derivatives), Op::Phi, and Effect::Writes. ### VICTORY_SHADER_OPT Integration (opt.rs, emit/mod.rs) - Included in MODULE / ALL / DEFAULT (2047; opt-out via VICTORY_SHADER_OPT=1279 to omit both, 1535 for SIMPLIFY_CFG only, 1791 for LICM only). - Integrated into Module::optimise fixpoint loop: promote_float_webs -> merge -> prune -> dce_insts -> simplify_cfg -> licm. ## Verification - cargo test -p victory-shader emit::bitcode::opt::: **41 passed, 0 failed** (including 11 new cfg + licm tests covering every transform, every hard invariant, dxil.dll validation, and the 400-seed differential fuzzer across SIMPLIFY_CFG, LICM, SIMPLIFY_CFG | LICM, SIMPLIFY_CFG | LICM | DEAD, and ALL). - cargo test -p victory-shader: **486 passed** (3 pre-existing local dxil.dll PSV0 failures on geometry/barycentric shaders that also fail identically at VICTORY_SHADER_OPT=0). - cargo clippy --workspace --all-targets -- -D warnings: **0 warnings, 0 errors**. - cargo xtask policy & cargo xtask policy --staged: **ok**.
Androtim commented 2026-12-04 10:01:41 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-04T10:01:41Z.


Triage (2e44fc99, CI check PASS, MERGEABLE):

  • Clean modular Wave 11A M2+M3 structure (+2,617/-29 split into opt/cfg.rs + opt/licm.rs, zero opt-in toggles), however Gate 2 Adversarial Audit (#282 comment 7408048958) flagged 1 [P0] + 1 [P1] merge blockers (cross-lane wave*/quad* & derivative op handling).
  • Marked triage: needs work pending fixes + Gate 2 clean re-audit + Gate 3 plain---release & --features diagnostics visual verification.
<!-- greyfinch-sync:Androtim/MiniVI:comment:7408141677 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/429#issuecomment-7408141677). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-04T10:01:41Z. --- Triage (`2e44fc99`, CI `check` PASS, `MERGEABLE`): - Clean modular Wave 11A M2+M3 structure (`+2,617/-29` split into `opt/cfg.rs` + `opt/licm.rs`, zero opt-in toggles), **however Gate 2 Adversarial Audit (`#282` comment `7408048958`) flagged `1 [P0]` + `1 [P1]` merge blockers** (cross-lane `wave*`/`quad*` & derivative op handling). - Marked `triage: needs work` pending fixes + Gate 2 clean re-audit + Gate 3 plain-`--release` & `--features diagnostics` visual verification.
Androtim commented 2026-12-04 10:31:30 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-04T10:31:30Z.


Updated triage (dd6b2f0f, CI check PASS, MERGEABLE):

  • Gate 2 Pure-Reasoning Re-Audit (#282 comment 7408448066): VERIFIED CLEAN (0 P0, 0 P1) — prior cross-lane/derivative and P1 findings resolved.
  • Swapped label triage: needs work -> triage: needs gpu test — ready for Gate 3 plain---release A/B & --features diagnostics visual verification.
<!-- greyfinch-sync:Androtim/MiniVI:comment:7408835707 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/429#issuecomment-7408835707). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-04T10:31:30Z. --- Updated triage (`dd6b2f0f`, **CI `check` PASS**, `MERGEABLE`): - **Gate 2 Pure-Reasoning Re-Audit (`#282` comment `7408448066`):** `VERIFIED CLEAN (0 P0, 0 P1)` — prior cross-lane/derivative and P1 findings resolved. - Swapped label `triage: needs work` -> `triage: needs gpu test` — ready for Gate 3 plain-`--release` A/B & `--features diagnostics` visual verification.
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!429
No description provided.