Shaders: avoid quadratic PHI substitution-chain traversal with path compression #950

Closed
opened 2026-11-26 04:14:46 +00:00 by github-bot · 4 comments
Owner

@⁠FunWithFlagsDev on GitHub · 2026-11-24 14:25 UTC · original

<html>

Observation

On main at ffe310e, ir.rs::remove_trivial_phis follows replacement chains without compressing them.

PHIs are simplified in physical block order, whereas SSA numbering follows reverse postorder. Reverse-code-order chains of diamonds can therefore produce long replacement chains, even when all PHIs are trivial.

Proposal

Flatten replacement paths during lookup, retaining the exact semantic representative and existing scan order.

This is a small, iterative change requiring no additional allocation or dependency in the lookup. Compression changes only non-root nodes to their current root, preserving subsequent substitutions, removal decisions and rewritten operands.

Offline evidence

I reconstructed the relevant CFG, register liveness and SSA construction steps in Python for a restricted synthetic GFX10 scalar fixture, including SCC and both live SGPRs.

For 4,096 reverse-code-order diamonds, the model produces 8,191 trivial PHIs and 12,288 instruction operand reads.

Replacement-array operations | Original | Path compression -- | -- | -- Reads | 16,805,886 | 53,241 Writes | 8,191 | 16,380 Total | 16,814,077 | 69,621

The forward-code-order control has identical operation counts before and after the optimization.

All 16 CPU model tests passed, including exhaustive and randomized equivalence tests, synthetic CFG reconstruction and operation-count verification.

Limitations

  • These are logical operation counts, not measured Rust compiler performance.

  • Small cases may incur extra operations.

  • The attached patch includes native Rust regression tests, but they have not been compiled or executed.

  • No representative real-shader workload profile is available, so the practical impact remains unknown.

Related work

#369 removes CFG traversal allocations; #104 and #697 address DXIL emitter liveness and dead-code elimination; #126 optimizes BDD traversal; #139 caches SSA address sums.

I found no equivalent change in the 30 retrieved PR diffs and eight targeted fork branches examined. This is not an exhaustive duplicate search.

Next steps

Independent native testing and profiling of remove_trivial_phis on representative shader workloads would be appreciated.

A separate dependency-worklist optimization was also investigated, but I would keep it outside this patch until profiling justifies its additional complexity.

AI-assisted. Synthetic inputs only; no game files. Experimental patch, not a verified native fix.

</html>## Observation

On main at ffe310e, [ir.rs::remove_trivial_phis](https://github.com/Marvisak/VIctory/blob/ffe310eb2098e52169f4c44db5ffdabf43a72622/crates/victory-shader/src/ir.rs#L592-L637) follows replacement chains without compressing them.

PHIs are simplified in physical block order, whereas SSA numbering follows reverse postorder. Reverse-code-order chains of diamonds can therefore produce long replacement chains, even when all PHIs are trivial.

Proposal

Flatten replacement paths during lookup, retaining the exact semantic representative and existing scan order.

This is a small, iterative change requiring no additional allocation or dependency in the lookup. Compression changes only non-root nodes to their current root, preserving subsequent substitutions, removal decisions and rewritten operands.

Offline evidence

I reconstructed the relevant CFG, register liveness and SSA construction steps in Python for a restricted synthetic GFX10 scalar fixture, including SCC and both live SGPRs.

For 4,096 reverse-code-order diamonds, the model produces 8,191 trivial PHIs and 12,288 instruction operand reads.

Replacement-array operations Original Path compression
Reads 16,805,886 53,241
Writes 8,191 16,380
Total 16,814,077 69,621

The forward-code-order control has identical operation counts before and after the optimization.

All 16 CPU model tests passed, including exhaustive and randomized equivalence tests, synthetic CFG reconstruction and operation-count verification.

Limitations

  • These are logical operation counts, not measured Rust compiler performance.
  • Small cases may incur extra operations.
  • The attached patch includes native Rust regression tests, but they have not been compiled or executed.
  • No representative real-shader workload profile is available, so the practical impact remains unknown.

#369 removes CFG traversal allocations; #104 and #697 address DXIL emitter liveness and dead-code elimination; #126 optimizes BDD traversal; #139 caches SSA address sums.

I found no equivalent change in the 30 retrieved PR diffs and eight targeted fork branches examined. This is not an exhaustive duplicate search.

Next steps

Independent native testing and profiling of remove_trivial_phis on representative shader workloads would be appreciated.

A separate dependency-worklist optimization was also investigated, but I would keep it outside this patch until profiling justifies its additional complexity.

AI-assisted. Synthetic inputs only; no game files. Experimental patch, not a verified native fix.

victory_phi_final_review.zip

**@⁠FunWithFlagsDev** on GitHub · 2026-11-24 14:25 UTC · [original](https://github.com/Marvisak/VIctory/issues/950) <html> <body> <!--StartFragment--><h2><span>Observation</span></h2><p><span>On </span><code><span>main</span></code><span> at </span><code><span>ffe310e</span></code><span>, </span><a href="https://github.com/Marvisak/VIctory/blob/ffe310eb2098e52169f4c44db5ffdabf43a72622/crates/victory-shader/src/ir.rs#L592-L637"><code><span>ir.rs::remove_trivial_phis</span></code></a><span> follows replacement chains without compressing them.</span></p><p><span>PHIs are simplified in physical block order, whereas SSA numbering follows reverse postorder. Reverse-code-order chains of diamonds can therefore produce long replacement chains, even when all PHIs are trivial.</span></p><h2><span>Proposal</span></h2><p><span>Flatten replacement paths during lookup, retaining the exact semantic representative and existing scan order.</span></p><p><span>This is a small, iterative change requiring no additional allocation or dependency in the lookup. Compression changes only non-root nodes to their current root, preserving subsequent substitutions, removal decisions and rewritten operands.</span></p><h2><span>Offline evidence</span></h2><p><span>I reconstructed the relevant CFG, register liveness and SSA construction steps in Python for a restricted synthetic GFX10 scalar fixture, including SCC and both live SGPRs.</span></p><p><span>For 4,096 reverse-code-order diamonds, the model produces 8,191 trivial PHIs and 12,288 instruction operand reads.</span></p> Replacement-array operations | Original | Path compression -- | -- | -- Reads | 16,805,886 | 53,241 Writes | 8,191 | 16,380 Total | 16,814,077 | 69,621 <p><span>The forward-code-order control has identical operation counts before and after the optimization.</span></p><p><span>All 16 CPU model tests passed, including exhaustive and randomized equivalence tests, synthetic CFG reconstruction and operation-count verification.</span></p><h2><span>Limitations</span></h2><ul><li><p><span>These are logical operation counts, not measured Rust compiler performance.</span></p></li><li><p><span>Small cases may incur extra operations.</span></p></li><li><p><span>The attached patch includes native Rust regression tests, but they have not been compiled or executed.</span></p></li><li><p><span>No representative real-shader workload profile is available, so the practical impact remains unknown.</span></p></li></ul><h2><span>Related work</span></h2><p><span>#369 removes CFG traversal allocations; #104 and #697 address DXIL emitter liveness and dead-code elimination; #126 optimizes BDD traversal; #139 caches SSA address sums.</span></p><p><span>I found no equivalent change in the 30 retrieved PR diffs and eight targeted fork branches examined. This is not an exhaustive duplicate search.</span></p><h2><span>Next steps</span></h2><p><span>Independent native testing and profiling of </span><code><span>remove_trivial_phis</span></code><span> on representative shader workloads would be appreciated.</span></p><p><span>A separate dependency-worklist optimization was also investigated, but I would keep it outside this patch until profiling justifies its additional complexity.</span></p><p><span>AI-assisted. Synthetic inputs only; no game files. Experimental patch, not a verified native fix.</span></p><!--EndFragment--> </body> </html>## Observation On `main` at `ffe310e`, [`[ir.rs::remove_trivial_phis](https://github.com/Marvisak/VIctory/blob/ffe310eb2098e52169f4c44db5ffdabf43a72622/crates/victory-shader/src/ir.rs#L592-L637)`](https://github.com/Marvisak/VIctory/blob/ffe310eb2098e52169f4c44db5ffdabf43a72622/crates/victory-shader/src/ir.rs#L592-L637) follows replacement chains without compressing them. PHIs are simplified in physical block order, whereas SSA numbering follows reverse postorder. Reverse-code-order chains of diamonds can therefore produce long replacement chains, even when all PHIs are trivial. ## Proposal Flatten replacement paths during lookup, retaining the exact semantic representative and existing scan order. This is a small, iterative change requiring no additional allocation or dependency in the lookup. Compression changes only non-root nodes to their current root, preserving subsequent substitutions, removal decisions and rewritten operands. ## Offline evidence I reconstructed the relevant CFG, register liveness and SSA construction steps in Python for a restricted synthetic GFX10 scalar fixture, including SCC and both live SGPRs. For 4,096 reverse-code-order diamonds, the model produces 8,191 trivial PHIs and 12,288 instruction operand reads. | Replacement-array operations | Original | Path compression | |---|---:|---:| | Reads | 16,805,886 | 53,241 | | Writes | 8,191 | 16,380 | | Total | 16,814,077 | 69,621 | The forward-code-order control has identical operation counts before and after the optimization. All 16 CPU model tests passed, including exhaustive and randomized equivalence tests, synthetic CFG reconstruction and operation-count verification. ## Limitations - These are logical operation counts, not measured Rust compiler performance. - Small cases may incur extra operations. - The attached patch includes native Rust regression tests, but they have not been compiled or executed. - No representative real-shader workload profile is available, so the practical impact remains unknown. ## Related work #369 removes CFG traversal allocations; #104 and #697 address DXIL emitter liveness and dead-code elimination; #126 optimizes BDD traversal; #139 caches SSA address sums. I found no equivalent change in the 30 retrieved PR diffs and eight targeted fork branches examined. This is not an exhaustive duplicate search. ## Next steps Independent native testing and profiling of `remove_trivial_phis` on representative shader workloads would be appreciated. A separate dependency-worklist optimization was also investigated, but I would keep it outside this patch until profiling justifies its additional complexity. AI-assisted. Synthetic inputs only; no game files. Experimental patch, not a verified native fix. [victory_phi_final_review.zip](https://github.com/user-attachments/files/35336068/victory_phi_final_review.zip)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:46 +00:00
Author
Owner

@⁠DrDarkDK on GitHub · 2026-11-24 14:32 UTC · original

I applied the attached patch to ffe310e on native Windows/MSVC (Rust 1.98.1, Ryzen 9 7900X). All 8 IR tests passed, including both tests from the patch.

I timed the PHI pass and the full IR build in --release, with identical timing hooks on both revisions. These are medians of 11 runs after 3 warmups:

Synthetic fixture PHI pass, base → patch Full IR build, base → patch
1,024 reverse-order diamonds 1.087 → 0.046 ms 6.32 → 5.26 ms
4,096 reverse-order diamonds 16.79 → 0.24 ms 38.62 → 21.33 ms
4,096 forward-order diamonds 0.31 → 0.23 ms 23.24 → 22.48 ms

The emitted IR fingerprints matched for all six inputs (64, 1,024 and 4,096 diamonds in both orders). The forward-order run showed no clear end-to-end change.

AI-assisted. Synthetic shader inputs only. No retail shader or gameplay timings.

**@⁠DrDarkDK** on GitHub · 2026-11-24 14:32 UTC · [original](https://github.com/Marvisak/VIctory/issues/950#issuecomment-7134534004) I applied the attached patch to `ffe310e` on native Windows/MSVC (Rust 1.98.1, Ryzen 9 7900X). All 8 IR tests passed, including both tests from the patch. I timed the PHI pass and the full IR build in `--release`, with identical timing hooks on both revisions. These are medians of 11 runs after 3 warmups: | Synthetic fixture | PHI pass, base → patch | Full IR build, base → patch | | --- | ---: | ---: | | 1,024 reverse-order diamonds | 1.087 → 0.046 ms | 6.32 → 5.26 ms | | 4,096 reverse-order diamonds | 16.79 → 0.24 ms | 38.62 → 21.33 ms | | 4,096 forward-order diamonds | 0.31 → 0.23 ms | 23.24 → 22.48 ms | The emitted IR fingerprints matched for all six inputs (64, 1,024 and 4,096 diamonds in both orders). The forward-order run showed no clear end-to-end change. AI-assisted. Synthetic shader inputs only. No retail shader or gameplay timings.
Author
Owner

@⁠FunWithFlagsDev on GitHub · 2026-11-24 14:39 UTC · original

Thank you, @⁠DrDarkDK, for independently compiling, testing and benchmarking the exact patch.

The results are particularly encouraging:

  • Approximately 70× faster PHI simplification for 4,096 reverse-order diamonds.
  • A 44.77% reduction in full IR construction time for the same fixture.
  • Matching IR fingerprints across all six tested inputs.
  • All eight native IR tests passing.

The scaling results are also consistent with the original quadratic traversal hypothesis.

I agree that the forward-order control does not establish a meaningful end-to-end improvement, and that representative shader profiling remains necessary to quantify the practical impact.

Given the localized nature of the change and the independent correctness and performance results, I think this patch is now a reasonable candidate for a dedicated pull request.

Thank you for the rigorous validation!

**@⁠FunWithFlagsDev** on GitHub · 2026-11-24 14:39 UTC · [original](https://github.com/Marvisak/VIctory/issues/950#issuecomment-7134725074) Thank you, @⁠DrDarkDK, for independently compiling, testing and benchmarking the exact patch. The results are particularly encouraging: - Approximately 70× faster PHI simplification for 4,096 reverse-order diamonds. - A 44.77% reduction in full IR construction time for the same fixture. - Matching IR fingerprints across all six tested inputs. - All eight native IR tests passing. The scaling results are also consistent with the original quadratic traversal hypothesis. I agree that the forward-order control does not establish a meaningful end-to-end improvement, and that representative shader profiling remains necessary to quantify the practical impact. Given the localized nature of the change and the independent correctness and performance results, I think this patch is now a reasonable candidate for a dedicated pull request. Thank you for the rigorous validation!
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 14:39 UTC · original

On Linux the attached path-compression patch passed 98 shader tests (one ignored). Applied to our #385/#309 integration, it passed 121 (one ignored), including the regression that checks a spilled pixel mask arriving through a PHI produces the same export as the direct mask.

In a separate release-mode check, both binaries resolved the expected terminal value for the reverse-order 256-diamond fixture. Four interleaved samples per binary, each timing 64 full IR builds, gave medians of 120 ms before and 85 ms after. The ranges overlapped (73–140 ms and 68–118 ms) on the shared machine. These are synthetic full-build timings. The Windows measurements above time the PHI pass separately. Retail shader translation time remains unmeasured.

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 14:39 UTC · [original](https://github.com/Marvisak/VIctory/issues/950#issuecomment-7134725120) On Linux the attached path-compression patch passed 98 shader tests (one ignored). Applied to our #385/#309 integration, it passed 121 (one ignored), including the regression that checks a spilled pixel mask arriving through a PHI produces the same export as the direct mask. In a separate release-mode check, both binaries resolved the expected terminal value for the reverse-order 256-diamond fixture. Four interleaved samples per binary, each timing 64 full IR builds, gave medians of 120 ms before and 85 ms after. The ranges overlapped (73–140 ms and 68–118 ms) on the shared machine. These are synthetic full-build timings. The Windows measurements above time the PHI pass separately. Retail shader translation time remains unmeasured.
Author
Owner

@⁠Super-cod on GitHub · 2026-11-24 16:01 UTC · original

I have implemented and verified the path compression optimization for
emove_trivial_phis:

Summary of Changes

  1. In \crates/victory-shader/src/ir.rs, updated \ ind\ in
    emove_trivial_phis\ to apply two-pass path compression (identifying the root representative and then flattening all visited elements on the path to point directly to
    oot).
  2. Added unit test \ rivial_phi_chains_compress_to_root\ verifying that multi-hop trivial PHI chains compress directly to the root value and rewrite operand reads accurately.
  3. Full verification passed: all 121 \ ictory-shader\ tests pass, Clippy produces 0 warnings, \cargo run -p xtask -- policy\ reports 154 files ok, and rustfmt passes without diff.
**@⁠Super-cod** on GitHub · 2026-11-24 16:01 UTC · [original](https://github.com/Marvisak/VIctory/issues/950#issuecomment-7136993039) I have implemented and verified the path compression optimization for \ emove_trivial_phis\: - **Commit**: 1c4d3a5 - **Pull Request**: https://github.com/Super-cod/VIctory/pull/4 - **Compare with main**: https://github.com/Marvisak/VIctory/compare/main...Super-cod:VIctory:fix/phi-path-compression?expand=1 ### Summary of Changes 1. In \crates/victory-shader/src/ir.rs\, updated \ ind\ in \ emove_trivial_phis\ to apply two-pass path compression (identifying the root representative and then flattening all visited elements on the path to point directly to \ oot\). 2. Added unit test \ rivial_phi_chains_compress_to_root\ verifying that multi-hop trivial PHI chains compress directly to the root value and rewrite operand reads accurately. 3. Full verification passed: all 121 \ ictory-shader\ tests pass, Clippy produces 0 warnings, \cargo run -p xtask -- policy\ reports 154 files ok, and rustfmt passes without diff.
This discussion has been locked. Commenting is limited to contributors.
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#950
No description provided.