Skip to content

Shaders/IR: pruned SSA via bitset IDF + dominator-tree renaming, linear CFG scan, and early dropped-inst skip - #2957

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/ir-pruned-ssa
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/ir-pruned-ssa

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown

Speed up victory_shader::ir::build across all shaders while producing structurally identical SSA output (crates/victory-shader/src/ir.rs):

  • Pruned SSA via parallel [u64; 7] bitset IDF + dominator-tree DFS renaming: propagate iterated dominance frontiers for all 385 registers simultaneously in bitset words, gate phi insertion by live_in, and rename in a single iterative dominator-tree DFS with an undo trail — eliminating maximal-SSA trivial phi creation, per-block outs Vec allocations, and binary-search lookup() calls.
  • Trim liveness() and fast-return in remove_trivial_phis_steps(): drop unused live_out computation from liveness() and skip extra passes when no phis exist or none are eliminated.
  • Linear CFG block assignment & early is_dropped() skip: assign basic blocks linearly in build_bounded() (dropping next_offsets and per-instruction leaders binary searches) and skip scheduling/waitcnt hints before machine.fields(wave64) via is_dropped().
  • Completely disjoint from Shaders/IR: eliminate per-instruction/CFG Vec allocations and O(1) effects dedup #2664 (Exit::succs and effects() dedup).

Measured (--release, median of 3 runs)

  • bench_ir_build_cfg_and_ssa (102 basic blocks, 40 phis): 613.6 µs → 292.1 µs/run (2.10×, -52.4%)

Verification

  • Official Windows MSVC + WARP SM 6.6 CI (Rust 1.98.1, default + --tests + --features diagnostics clippy, xtask policy, full workspace tests): RVZO6/VIctory CI run #45734072839 (green, combined Wave 7 check on 63bcca4).
  • All 11 ir::tests pass, including pruned_ssa_matches_maximal_ssa_oracle (verifying structural equivalence against the maximal-SSA algorithm) and is_dropped_matches_dropped_names.
  • Not run: the title; any hardware GPU.

AI-assisted: T3 Code (Antigravity, gemini-4-neon).

…ar CFG scan, and early dropped-inst skip

Speed up ir::build across all shaders with structurally identical SSA output:
- Pruned SSA construction via parallel [u64; 7] bitset iterated dominance
  frontiers (propagating all 385 registers simultaneously) and single-pass
  iterative dominator-tree DFS renaming with an undo trail, eliminating
  maximal-SSA trivial phi creation, outs Vec allocations, and binary-search
  lookup() calls.
- Drop live_out from liveness() (returning (live_in, defs)) and fast-return
  in remove_trivial_phis_steps() when no phis exist or none are removed.
- Linear CFG block assignment in build_bounded() (dropping next_offsets Vec
  and per-instruction leaders binary searches) and early is_dropped() opcode
  skip before machine.fields(wave64) for scheduling/waitcnt hints.
- Disjoint from Marvisak#2664's Exit::succs and effects() dedup.

Measured (--release bench_ir_build_cfg_and_ssa, median of 3 runs, 102 blocks / 40 phis):
- 613.6 -> 292.1 us/run (2.10x, -52.4%)

Assisted-by: T3 Code (Antigravity, gemini-4-neon)

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and verified against main (63bcca43) on Windows 11 (RTX 5090 + WARP + dxil.dll) — clean, fast, and ready to merge:

  • build_pruned_ssa (crates/victory-shader/src/ir.rs:236-423) computes idoms/DF after split_irreducible, builds pruned phi shells via [u64; REGS.div_ceil(64)] bitset IDF filtered by live_in[y], and renames in a single explicit-stack dominator-tree pass with trail rollback.
  • Monotonic block scan and early is_dropped(encoding, opcode) skip avoid unnecessary machine.fields(wave64) work, and is_dropped_matches_dropped_names guards against opcode drift.
  • Zero changes to emit/lower.rs.

Local verification:

  • cargo test --locked -p victory-shader --lib: ir:: 11/11, structure:: 13/13, resource:: 35/35, emit::tests 17/17, emit::lower::tests 33/33 (including pruned_ssa_matches_maximal_ssa_oracle and is_dropped_matches_dropped_names)
  • emit::semantics_tests: 34/34 passed on both RTX 5090 and WARP
  • --release bench_ir_build_cfg_and_ssa: 179.3 us/run
  • cargo clippy (default & --all-features) + cargo xtask policy (191 files ok): 0 warnings

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: textbook pruned SSA. The parallel bitset IDF inserts phis only at df nodes where the reg is live-in (defs & live_in & !phi_regs), re-queueing on new bits — monotone so it terminates. Renaming is standard Cytron: phi values bound before reads, successor args filled at parent exit with current[], trail undo on Leave. The NONE-arg invariant holds because a reg live-in at a join is either defined on all reaching paths or lands in live_in[0] as an input. The is_dropped opcode table self-validates against DROPPED names via decode, and the linear leader scan replaces binary_search correctly since both lists are offset-sorted. The canonical oracle test comparing against maximal SSA on identical blocks is exactly the right check.

@steven-seagull27

Copy link
Copy Markdown

@RVZO6 I compared this PR and #2942 individually and together through the public translate_with entry point, in light of the eight translation workers reported on #2936. Both commits start at 63bcca4, affect different compiler stages, and merge cleanly.

Median wall time in ms for one batch of 64 distinct compute containers on a Ryzen 7 9850X3D, Windows x64, Rust 1.98.1 MSVC, release build. Lower is faster.

Synthetic batch Workers Base #2957 dfa6126 #2942 1569277 Both
Indexed loads, dependent VALU and stores 1 299.2 307.5 263.3 256.4
Same 8 75.4 74.8 67.4 63.4
16 live VGPRs across successive control-flow joins 1 173.2 164.3 157.8 142.9
Same 8 42.2 40.8 36.4 35.5

The buffer batch varies program size and puts one dynamic diamond per eight loads in half of its shaders. The join batch has 4–130 diamonds; all 16 VGPRs feed the final store. The largest emits 2,080 DXIL phi instructions. Half the inputs in each batch enable view marks. Every timed call parses its compressed synthetic container, builds the IR, structures it, tracks resources and emits DXIL. Each row is 15 measurements across five processes with run order rotated and a warm-up outside the timer. The worker threads use the in-memory inputs directly; no shader cache, D3D pipeline creation, validator, file I/O or GPU dispatch is in the timer.

Correctness: I compared the actual DXIL bytes and the remaining HostShader binding metadata for all 128 inputs. All four builds matched, and the Windows SDK DXIL validator accepted every output on each build (512 validations). Output sizes were 2,512–99,264 bytes. The paired change reduced these eight-worker medians by 16% in both batches. #2942 showed the more consistent individual benefit. The small eight-worker difference between #2957 alone and base is within the spread of repeated runs.

Reproducible, test-only fixture 29c0e1b applies to the common base or either PR. From PowerShell:

git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2957-translation-corpus
git cherry-pick 29c0e1b
$env:VICTORY_BENCH_WORKERS = '8'
foreach ($family in 'buffers','joins') {
    $env:VICTORY_BENCH_CORPUS = $family
    cargo test --release -p victory-shader --lib translate_public_compute_batch -- --ignored --nocapture
}

Set VICTORY_BENCH_OUTPUT to a separate directory for each build and batch to validate and save the binary outputs and binding metadata before timing. No title files were used; the eight-worker cold-cache run of the actual game remains the check for gameplay startup.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. This replaces the SSA construction in crates/victory-shader/src/ir.rs (build_pruned_ssa: dominance-frontier phi placement gated by live_in, dominator-tree renaming with an undo trail), scans leaders linearly in build_bounded, and skips dropped instructions before decoding fields. Reading it, the renaming and phi-argument filling look right, and pruned_ssa_matches_maximal_ssa_oracle compares against the old algorithm, but it rewrites the core of every shader's translation, so the bar is a measured benefit.

The 2.1x figure is for ir::build alone on one synthetic program. The end-to-end measurement in this thread (steven-seagull27, translate_with over 128 synthetic containers, byte-identical DXIL on all builds) found this PR alone inside run-to-run noise with 8 workers and slower in one 1-worker batch (299.2 -> 307.5 ms), while #2942 gave the more consistent gain. To move this forward: show a whole-title victory translate timing (cold cache, the worker count the loader uses) where this PR measurably helps on its own or on top of #2942, plus a program-by-program comparison showing identical output across the title's shaders. Without that, the risk of a core IR rewrite isn't matched by a demonstrated gain.

CI: passing.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants