Shaders: eliminate heap allocations during CFG traversal #369

Open
github-bot wants to merge 1 commit from github-bot/gh-369 into main AGit
Owner

@⁠pedrofort on GitHub · 2026-11-24 02:41 UTC · original
pedrofort:optimize-shader-cfg → main · +197 −12 · 1 files · 1 commits · open

<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 d2a4bae.

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.482x

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 --nocapture

All 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.

**@⁠pedrofort** on GitHub · 2026-11-24 02:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/369) `pedrofort:optimize-shader-cfg` → `main` · +197 −12 · 1 files · 1 commits · open <head></head><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Motivation</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><code><span>Exit::successors()</span></code><span><span class="Apple-converted-space"> </span>allocates a temporary vector whenever a control-flow block's successors are queried, even though an exit has at most two successors.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>These allocations occur repeatedly during reverse postorder traversal, predecessor linking, irreducible control-flow analysis and register liveness analysis.</span></p><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Implementation</span></h2><ul style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><li><p><span>Introduce allocation-free successor access and iteration.</span></p></li><li><p><span>Avoid cloning predecessor vectors during backward traversal.</span></p></li><li><p><span>Preserve successor order, duplicate successors and the existing public API.</span></p></li><li><p><span>No new dependencies or unsafe code.</span></p></li></ul><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Performance</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Synthetic release microbenchmarks on Apple M5, macOS arm64, Rust 1.98.1, base commit<span class="Apple-converted-space"> </span></span><code><span>d2a4bae</span></code><span>.</span></p> 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.482x <p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Each measurement performs 128 reverse postorder traversals. Each session includes warm-up and five measurements with alternating execution order.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Baseline control measurements comparing the original implementation against itself produced speedups between 0.987x and 1.009x.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>These measurements apply specifically to CFG traversal, not end-to-end shader compilation or game performance.</span></p><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Correctness</span></h2><ul style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><li><p><span>Successor ordering matches the original implementation.</span></p></li><li><p><span>Reverse postorder matches the original implementation across synthetic graphs containing branches, loops and unreachable blocks.</span></p></li><li><p><span>Full shader suite: 58 passed, 0 failed, 1 ignored.</span></p></li><li><p><span>Library Clippy with<span class="Apple-converted-space"> </span></span><code><span>-D warnings</span></code><span>: clean.</span></p></li><li><p><span>Asset policy: 151 files OK.</span></p></li><li><p><code><span>git diff --check</span></code><span>: clean.</span></p></li></ul><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Reproduction</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><code><span>cargo test --locked --release -p victory-shader --lib cfg_postorder_benchmark -- --ignored --nocapture</span></code></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>All benchmark graphs are synthetic. No game assets were used.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>AI-assisted implementation and test preparation (ChatGPT), with tests and benchmarks executed locally on Apple Silicon.</span></p>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:01:14 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:04 UTC · original

Thanks @⁠pedrofort for this optimization. I tested head 341782191c557d278810a613d4dbf8cf47421094 with:

cargo test --locked -p victory-shader --lib cfg_ -- --test-threads=1

Result: 2 passed, 0 failed, 1 ignored (69 filtered out). cfg_successors_preserve_order and cfg_postorder_matches_reference passed, 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.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/369#issuecomment-7121677019) Thanks @⁠pedrofort for this optimization. I tested head `341782191c557d278810a613d4dbf8cf47421094` with: ``` cargo test --locked -p victory-shader --lib cfg_ -- --test-threads=1 ``` Result: 2 passed, 0 failed, 1 ignored (69 filtered out). `cfg_successors_preserve_order` and `cfg_postorder_matches_reference` passed, 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:28 UTC · original

Automated triage for the maintainers.

Needs work (small). Exit::successor_at and successor_iter (crates/victory-shader/src/ir.rs) keep the order of Exit::successors(), including a Branch whose 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: run cargo fmt (doubled blank line in mod tests); drop the ~70-line ignored cfg_postorder_benchmark and keep cfg_successors_preserve_order and cfg_postorder_matches_reference; and show the end-to-end effect (e.g. victory translate wall 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

**@⁠Marvisak** on GitHub · 2026-11-25 00:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/369#issuecomment-7148538719) <!-- victory-triage v1 head=341782191c557d278810a613d4dbf8cf47421094 verdict=needs-work at=2026-11-25T00:22:20Z --> Automated triage for the maintainers. **Needs work (small).** `Exit::successor_at` and `successor_iter` (`crates/victory-shader/src/ir.rs`) keep the order of `Exit::successors()`, including a `Branch` whose 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: run `cargo fmt` (doubled blank line in `mod tests`); drop the ~70-line ignored `cfg_postorder_benchmark` and keep `cfg_successors_preserve_order` and `cfg_postorder_matches_reference`; and show the end-to-end effect (e.g. `victory translate` wall 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](https://claude.ai/code)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/369/head:github-bot/gh-369'
git switch 'github-bot/gh-369'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-369'
git switch 'github-bot/gh-369'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-369'
git switch 'github-bot/gh-369'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-369'
git switch 'main'
git merge --squash 'github-bot/gh-369'
git switch 'main'
git merge --ff-only 'github-bot/gh-369'
git switch 'main'
git merge 'github-bot/gh-369'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!369
No description provided.