perf(shader): eliminate quadratic scans in unneeded_edges and PrimMaskUses #240

Merged
Sheffy007 merged 1 commit from upstream-pr/240 into main 2026-11-27 03:56:48 +00:00
Sheffy007 commented 2026-11-27 03:14:31 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T03:14:31Z.


Addresses the recent 46–48 KB pixel shader translation slowdown noted on #180 (tracked under #15):

  1. Lower::unneeded_edges (crates/victory-shader/src/emit/lower.rs):
    Once guessed wave32 pixel shaders began reaching Lower::fill_phis (df21edf), a 46–48 KB pixel shader with hundreds of sequential per-lane merge blocks and dozens of live SGPRs (descriptors, root constants, loop counters) starts unneeded_edges with thousands of intermediate Reg::S(_) SSA phis that have no direct instruction reads in their block. Iterating blocks top-down (0..p.blocks.len()) while invalidity propagates backward from the final consumers removed one block layer per outer pass, running O(B^2 * S) bools.meet evaluations and Vec allocations.

    • Memoize dead_relay(b, n, e) per (block, pred, succ) CFG triple so st.bools.meet runs at most once per CFG step rather than once per SGPR phi per iteration.
    • Replace the multi-pass block loop with a single-pass reverse-dependency worklist, preserving the exact greatest-fixpoint result in O(B * S) time.
  2. io::PrimMaskUses (crates/victory-shader/src/io.rs, graphics.rs, lower.rs):
    Since dc5223b, graphics::infer probes every live entry SGPR in start..32 for is_prim_mask_only (in addition to io::ps_prim_mask and Lower::inputs). When entry SGPRs are spilled via v_writelane_b32 into a shared VGPR carried across hundreds of blocks and phis, each popped (vv, lane) scanned every block, phi, and instruction in the program (plus full-program scans for scc and m0 consumers).

    • Index phi and instruction readers once per Program in PrimMaskUses and reuse it across entry SGPR checks in graphics::infer, io::ps_prim_mask, and Lower::inputs.

Tests & Verification

  • Added emit::lower::tests::unneeded_edges_propagates_across_deep_sgpr_phi_chains_without_quadratic_scans (160 sequential per-lane diamonds carrying 32 SGPRs + an unneeded_edges mask).
  • Added io::tests::prim_mask_uses_visits_spills_and_phis_linearly.
  • cargo test -p victory-shader -p victory-gpu -p victory-metal -p victory-hle -p victory -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5520547070 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/240). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T03:14:31Z. --- Addresses the recent 46–48 KB pixel shader translation slowdown noted on #180 (tracked under #15): 1. **`Lower::unneeded_edges` (`crates/victory-shader/src/emit/lower.rs`):** Once guessed wave32 pixel shaders began reaching `Lower::fill_phis` (`df21edf`), a 46–48 KB pixel shader with hundreds of sequential per-lane merge blocks and dozens of live SGPRs (descriptors, root constants, loop counters) starts `unneeded_edges` with thousands of intermediate `Reg::S(_)` SSA phis that have no direct instruction reads in their block. Iterating blocks top-down (`0..p.blocks.len()`) while invalidity propagates backward from the final consumers removed one block layer per outer pass, running `O(B^2 * S)` `bools.meet` evaluations and `Vec` allocations. - Memoize `dead_relay(b, n, e)` per `(block, pred, succ)` CFG triple so `st.bools.meet` runs at most once per CFG step rather than once per SGPR phi per iteration. - Replace the multi-pass block loop with a single-pass reverse-dependency worklist, preserving the exact greatest-fixpoint result in `O(B * S)` time. 2. **`io::PrimMaskUses` (`crates/victory-shader/src/io.rs`, `graphics.rs`, `lower.rs`):** Since `dc5223b`, `graphics::infer` probes every live entry SGPR in `start..32` for `is_prim_mask_only` (in addition to `io::ps_prim_mask` and `Lower::inputs`). When entry SGPRs are spilled via `v_writelane_b32` into a shared VGPR carried across hundreds of blocks and phis, each popped `(vv, lane)` scanned every block, phi, and instruction in the program (plus full-program scans for `scc` and `m0` consumers). - Index phi and instruction readers once per `Program` in `PrimMaskUses` and reuse it across entry SGPR checks in `graphics::infer`, `io::ps_prim_mask`, and `Lower::inputs`. ### Tests & Verification - Added `emit::lower::tests::unneeded_edges_propagates_across_deep_sgpr_phi_chains_without_quadratic_scans` (160 sequential per-lane diamonds carrying 32 SGPRs + an `unneeded_edges` mask). - Added `io::tests::prim_mask_uses_visits_spills_and_phis_linearly`. - `cargo test -p victory-shader -p victory-gpu -p victory-metal -p victory-hle -p victory -p xtask` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy`
LakshanSivakumar commented 2026-11-27 03:23:33 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T03:23:33Z.

Review: CHANGES_REQUESTED


Thanks for the speed-up. It conflicts with main, though: 2fa17f16 (landed just before this) changed the same per-read test in unneeded_edges. It now also accepts s_andn2 / s_orn2 by EXEC (keeps_exec), and it reads EXEC in another block only when neither block is on a cycle (bb == b || !self.on_cycle(b) && !self.on_cycle(bb)). Your version of that closure keeps only inst.read(Reg::S(EXEC)), so a plain merge would drop both rules. It also conflicts in docs/native-host.md.

Could you merge main and rebuild the indexing on top of the new closure, keeping by_exec / keeps_exec and the on_cycle filter as they are? Please also confirm the dumped-corpus translate counts are unchanged before and after (main reports 7,879 pixel shaders and 24 lane-mask join refusals), since this is meant to be a pure speed-up.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6083045604 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/240#pullrequestreview-6083045604). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T03:23:33Z. **Review:** CHANGES_REQUESTED --- Thanks for the speed-up. It conflicts with main, though: 2fa17f16 (landed just before this) changed the same per-read test in `unneeded_edges`. It now also accepts `s_andn2` / `s_orn2` by EXEC (`keeps_exec`), and it reads EXEC in another block only when neither block is on a cycle (`bb == b || !self.on_cycle(b) && !self.on_cycle(bb)`). Your version of that closure keeps only `inst.read(Reg::S(EXEC))`, so a plain merge would drop both rules. It also conflicts in docs/native-host.md. Could you merge main and rebuild the indexing on top of the new closure, keeping `by_exec` / `keeps_exec` and the `on_cycle` filter as they are? Please also confirm the dumped-corpus translate counts are unchanged before and after (main reports 7,879 pixel shaders and 24 lane-mask join refusals), since this is meant to be a pure speed-up.
LakshanSivakumar commented 2026-11-27 03:56:45 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T03:56:45Z.

Review: APPROVED


Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). Thanks!

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6083279479 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/240#pullrequestreview-6083279479). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T03:56:45Z. **Review:** APPROVED --- Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). Thanks!
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
LakshanSivakumar/Native-Victory-macOS!240
No description provided.