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

Manually merged
github merged 1 commit from github-bot/gh-240 into main 2026-11-27 04:00:46 +00:00 AGit
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 03:14 UTC · original
Sheffy007:perf/shader-pixel-translation-blowups → main · +275 −117 · 4 files · 1 commits · merged 2026-11-27 03:56 UTC

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
**@⁠Sheffy007** on GitHub · 2026-11-27 03:14 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/240) `Sheffy007:perf/shader-pixel-translation-blowups` → `main` · +275 −117 · 4 files · 1 commits · merged 2026-11-27 03:56 UTC 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`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:17:22 +00:00
github-bot force-pushed github-bot/gh-240 from e89f17dc9b to 88ec6f4b22 2026-11-27 03:27:54 +00:00 Compare
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 03:23 UTC · original · requested changes

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** on GitHub · 2026-11-27 03:23 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/240#pullrequestreview-6083045604) · **requested changes** 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.
github manually merged commit bc67a6edbe into main 2026-11-27 04:00:46 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 03:56 UTC · original · approved these changes

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

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 03:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/240#pullrequestreview-6083279479) · **approved these changes** Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.