Shaders: eliminate heap allocations during CFG traversal #369

Open
pedrofort wants to merge 1 commit from upstream-pr/369 into main
pedrofort commented 2026-11-24 02:41:01 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573398881 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/369). Originally posted by [pedrofort](https://github.com/pedrofort) on 2026-11-24T02:41:01Z. --- <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>
jeremygold02 commented 2026-11-24 06:55:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:04:09Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121677019 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/369#issuecomment-7121677019). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:04:09Z. --- 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.
mercuryVM commented 2026-11-24 13:01:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:56:21Z.

Review: APPROVED


Tested 3417821 on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 71 passed, including cfg_successors_preserve_order and cfg_postorder_matches_reference; the microbenchmark is #[ignore] as intended. cargo clippy -p victory-shader --all-targets is clean. victory translate --validate --pipelines on the local title executable still reports 12 of 12 emitted, validated and built into pipelines.

  • successor_at and successor_iter keep the old successors() order (taken, then not-taken), which postorder and the dominator walk depend on. cfg_postorder_matches_reference checks that against the allocating version.
  • The forward and backward walks now share one visit closure instead of cloning preds, so nothing changes semantically.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039569681 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/369#pullrequestreview-6039569681). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:56:21Z. **Review:** APPROVED --- Tested 3417821 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-shader -- --test-threads=1` 71 passed, including `cfg_successors_preserve_order` and `cfg_postorder_matches_reference`; the microbenchmark is `#[ignore]` as intended. `cargo clippy -p victory-shader --all-targets` is clean. `victory translate --validate --pipelines` on the local title executable still reports 12 of 12 emitted, validated and built into pipelines. - `successor_at` and `successor_iter` keep the old `successors()` order (taken, then not-taken), which postorder and the dominator walk depend on. `cfg_postorder_matches_reference` checks that against the allocating version. - The `forward` and backward walks now share one `visit` closure instead of cloning `preds`, so nothing changes semantically. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 01:07:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:28:00Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148538719 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/369#issuecomment-7148538719). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:28:00Z. --- <!-- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/369:upstream-pr/369'
git switch 'upstream-pr/369'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/369'
git switch 'upstream-pr/369'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/369'
git switch 'upstream-pr/369'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/369'
git switch 'main'
git merge --squash 'upstream-pr/369'
git switch 'main'
git merge --ff-only 'upstream-pr/369'
git switch 'main'
git merge 'upstream-pr/369'
git push origin 'main'
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
Marvisak/VIctory!369
No description provided.