Shaders: eliminate heap allocations during CFG traversal #369
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/369"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by pedrofort on 2026-11-24T02:41:01Z.
<head></head>
Motivation
Exit::successors()allocates a temporary vector whenever a control-flow block's successors are queried, even though an exit has at most two successors.These allocations occur repeatedly during reverse postorder traversal, predecessor linking, irreducible control-flow analysis and register liveness analysis.
Implementation
Introduce allocation-free successor access and iteration.
Avoid cloning predecessor vectors during backward traversal.
Preserve successor order, duplicate successors and the existing public API.
No new dependencies or unsafe code.
Performance
Synthetic release microbenchmarks on Apple M5, macOS arm64, Rust 1.98.1, base commit
CFG blocks | Session 1 | Session 2 | Session 3 | Median speedup -- | -- | -- | -- | -- 770 | 4.536x | 4.814x | 4.683x | 4.683x 6,146 | 5.075x | 5.181x | 5.082x | 5.082x 24,578 | 4.438x | 4.482x | 4.972x | 4.482xd2a4bae.Each measurement performs 128 reverse postorder traversals. Each session includes warm-up and five measurements with alternating execution order.
Baseline control measurements comparing the original implementation against itself produced speedups between 0.987x and 1.009x.
These measurements apply specifically to CFG traversal, not end-to-end shader compilation or game performance.
Correctness
Successor ordering matches the original implementation.
Reverse postorder matches the original implementation across synthetic graphs containing branches, loops and unreachable blocks.
Full shader suite: 58 passed, 0 failed, 1 ignored.
Library Clippy with
-D warnings: clean.Asset policy: 151 files OK.
git diff --check: clean.Reproduction
cargo test --locked --release -p victory-shader --lib cfg_postorder_benchmark -- --ignored --nocaptureAll benchmark graphs are synthetic. No game assets were used.
AI-assisted implementation and test preparation (ChatGPT), with tests and benchmarks executed locally on Apple Silicon.
Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:04:09Z.
Thanks @pedrofort for this optimization. I tested head
341782191c557d278810a613d4dbf8cf47421094with:Result: 2 passed, 0 failed, 1 ignored (69 filtered out).
cfg_successors_preserve_orderandcfg_postorder_matches_referencepassed, covering successor order and reverse-postorder equivalence on the synthetic graphs. The ignored benchmark was not run, so this result does not measure the allocation or speedup claim.Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:56:21Z.
Review: APPROVED
Tested
3417821on Windows 11, RTX 5070, MSVC:cargo test -p victory-shader -- --test-threads=171 passed, includingcfg_successors_preserve_orderandcfg_postorder_matches_reference; the microbenchmark is#[ignore]as intended.cargo clippy -p victory-shader --all-targetsis clean.victory translate --validate --pipelineson the local title executable still reports 12 of 12 emitted, validated and built into pipelines.successor_atandsuccessor_iterkeep the oldsuccessors()order (taken, then not-taken), which postorder and the dominator walk depend on.cfg_postorder_matches_referencechecks that against the allocating version.forwardand backward walks now share onevisitclosure instead of cloningpreds, so nothing changes semantically.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:28:00Z.
Automated triage for the maintainers.
Needs work (small).
Exit::successor_atandsuccessor_iter(crates/victory-shader/src/ir.rs) keep the order ofExit::successors(), including aBranchwhose two targets are the same block, and the call-site changes (reverse_postorder,link,irreducible_edge,split_irreducible,multi_entry_loop,liveness) keep the old behaviour. To merge: runcargo fmt(doubled blank line inmod tests); drop the ~70-line ignoredcfg_postorder_benchmarkand keepcfg_successors_preserve_orderandcfg_postorder_matches_reference; and show the end-to-end effect (e.g.victory translatewall time before/after), since a 4.5x speed-up on reverse postorder alone doesn't say what it saves per shader.CI: no check runs on the head commit.
Generated by Claude Code
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.