Shaders: avoid quadratic PHI substitution-chain traversal with path compression #950
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 FunWithFlagsDev on 2026-11-24T14:25:55Z.
<html>
Observation
On
mainatffe310e,ir.rs::remove_trivial_phisfollows 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,621The 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_phison 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>## ObservationOn
mainatffe310e,[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.
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
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_phison 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
Imported from Marvisak/VIctory. Originally posted by DrDarkDK on 2026-11-24T14:32:06Z.
I applied the attached patch to
ffe310eon 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: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.
Imported from Marvisak/VIctory. Originally posted by FunWithFlagsDev on 2026-11-24T14:39:08Z.
Thank you, @DrDarkDK, for independently compiling, testing and benchmarking the exact patch.
The results are particularly encouraging:
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!
Imported from Marvisak/VIctory. Originally posted by MichaelSmithAI on 2026-11-24T14:39:08Z.
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.
Imported from Marvisak/VIctory. Originally posted by Super-cod on 2026-11-24T16:01:20Z.
I have implemented and verified the path compression optimization for
emove_trivial_phis:
Summary of Changes
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).