Skip to content

fix(shader): structurize a 4,750-block graph before Unreal's render thread watchdog fires - #9702

Merged
adriadam10 merged 8 commits into
boykopovar:mainfrom
frankea:perf/structurizer-split-search
Oct 26, 2026
Merged

adriadam10 merged 8 commits into
boykopovar:mainfrom
frankea:perf/structurizer-split-search

Conversation

@frankea

@frankea frankea commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What

Ghostrunner (PPSA03685) dispatches a compute shader, 0x200c520000, that links into 104,136 dwords once #9068 and #8070 resolve its table calls. Its structurization took 173 s on the AGC queue-0 worker, so Unreal aborted after 120 s with GameThread timed out waiting for RenderThread (5 of 5 runs, Game bring-up #7320). The graph has 4,750 blocks. splitSharedMergeBlocks makes 1,302 splits, and the graph grows to 10,170 blocks.

The time was not in the analyses. I dumped the graph at Structurize and replayed it through Structurizer.cpp alone with scoped timers. After each split, splitOneSelectionMerge examines every selection header again: 1.71 million header checks in all. That loop took 144 of 177 s:

  • FindNearestCommonPostDominator took 62 s. It intersected the two post-dominator sets, then binary-searched the other common blocks in each candidate's set until one candidate held them all.
  • selectionRegion (34 s) and naturalLoopBody tested membership with std::find in the list they were building, and externalEntryEdges (14 s) did the same in the sorted region. findInnermostContainingLoop (18 s) scanned loop bodies linearly.
  • The whole-graph analyses took 40 s: natural loops 17 s, post-dominators 10 s, dominators 7 s, SCCs 5 s.

The dominator sets themselves are small: at the end they average 138 entries over 10,217 blocks.

Three commits on top of #9701, each with the reasoning for why its output is unchanged:

  • perf(shader): look up the structurizer's sorted block lists instead of scanning them. Loop bodies, construct blocks, regions and SCC members are sorted, so membership is a binary search. The traversals that tested the list they were building test a bitset, and selectionRegion reads its sorted result from the bitset. FindNearestCommonPostDominator skips a candidate whose post-dominator set is too small to hold the other common blocks and checks the rest in one pass. remapIds sorts only when the map broke the order.
  • perf(shader): build the structurizer's dominator trees instead of iterating dominator sets. Dominators and post-dominators are built with the Cooper-Harvey-Kennedy iteration ("A Simple, Fast Dominance Algorithm", 2001) over a virtual root above the entry, or above every exit for post-dominators. Each block's sorted set is written as its parent's set plus itself. When every block is reachable from the virtual root, the greatest fixpoint of the old equations is the dominance relation of that graph, so the sets are the same. When a block is unreachable from the entry, or cannot reach an exit, the old fixpoint runs. Each block keeps its immediate post-dominator, and FindNearestCommonPostDominator climbs the tree when both blocks have one. APS5_NO_PERF_DOMINATOR_TREE turns this commit and the next off. The structurizer and control-flow tests also run with it set, and it has a row in docs/dev/ENVIRONMENT.md.
  • perf(shader): test a selection construct's members by dominance. In some graphs splitSharedMergeBlock can learn which predecessors of the merge M lie in the header H's construct without walking the construct (dominatedBlocks). The conditions: every block is reachable from the entry, the predecessor lists match the successor lists, and M neither is H nor dominates H. Then the construct is exactly the set of blocks that H dominates and M does not, other than M. The commit message has the argument. splitOneSelectionMerge checks the conditions once per pass and walks as before when they fail.

hasStructuredConstructs, the split order and the clone and junction decisions are untouched; #9703 speeds up hasStructuredConstructs separately. BasicBlock gains immediatePostDominator. GraphToString doesn't print it, and nothing outside the structurizer reads it.

Tested

main + #9068 + #8070 + this PR (2 runs)
splitSharedMergeBlocks 172.7 s 18.1 s, 18.0 s
whole --prepare 183.9 s 27.6 s, 27.4 s

Both builds then stop on the same #7459 refusal, GetImageResource dword 0 is not a valid runtime value. In the title with these commits on the same base, the structurization takes 19.3 s (process 192.9 s to 212.2 s) and no watchdog fires. The title then aborts on that refusal, which this PR doesn't touch.

Persona 3 Reload (PPSA10872), @zaidhawk's run on an RTX 5090 (NVIDIA 617.42, Windows 11; #9068 comment): on main 9d6adeb + #7048 + #9068 + #8070 with a larger local callee window, agc_shader_replay --prepare of compute shader 0x2014960000 (119,922 words) took 355.0 s; with #9703's branch 958f3f9 added (this PR with #9701 under it and #9703 on top) it took 32.6 s, and 75.7 s with APS5_NO_PERF_DOMINATOR_TREE=1, all with the same output and the same #7459 refusal. In the title, the base hits the RenderThread watchdog at the Iwatodai Station strip mall; with the stack no watchdog fires and the title goes on to that refusal.

No trace summary lines: the only scene that prepares this shader ends before its next present on both builds. On the base the watchdog kills the title during the preparation; with this PR the #7459 refusal aborts it right after the preparation. A trace window that is open when the title exits writes nothing. With warm caches, scenes before that point don't run the structurizer.

Same structured output:

  • The structured graph of 0x200c520000 (its GraphToString, 16 MB) is byte-identical to main's after each commit.
  • A differential fuzzer ran main's and this structurizer on generated graphs and compared GraphToString, or the exception text when one throws. The graphs:
    • nested selections and loops with breaks, continues, early returns and cross edges;
    • DAGs with back edges;
    • small random graphs, including irreducible ones, unreachable blocks and exitless loops.
  • It ran 40,000 graphs per mode in default, APS5_NO_PERF_DOM_INTERSECTION, APS5_NO_PERF_ITERATION_SELECTION, APS5_FORCE_STRUCTURIZER_FALLBACK, and APS5_NO_PERF_DOMINATOR_TREE against main's default: 200,000 comparisons. A second batch of 80,000 graphs ran up to 601 blocks (4,176 of 200 blocks or more). No differences.
  • 115 distinct graphs that agc_shader_control_flow_tests, agc_structurizer_tests and agc_driver_compute_table_calls_tests hand to Structurize, plus 7 Ghostrunner shaders, in 4 modes: 460 comparisons, all identical, including the 34 that throw.

Tests on the head, 231be5c on main 7e2ba73, Windows 11, RTX 4090, NVIDIA 616.92, WinLibs GCC 15.2: agc_structurizer, agc_shader_control_flow, agc_driver_compute_table_calls and scalar_call pass 11/11 with the intersection_off, iteration_selection_off, dominator_tree_off and forced_fallback variants, each run three times (ctest --repeat until-fail:3). agc_structurizer_tests also passes in all five modes built with AppleClang on macOS.

Validation: I ran the same focused tests on #9701 (3606824) and on this head (231be5c), both on main 7e2ba73, with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Vulkan SDK 1.4.363.0), ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1, using ctest -R "^(agc_structurizer|agc_shader_control_flow|scalar_call|agc_driver_compute_table_calls)" -V. They pass 9/9 and 11/11 (#9701 lacks the two dominator_tree_off variants). No log has was not found but was requested by env var VK_INSTANCE_LAYERS, a VUID- line or a validation message. Before the rebase, main a6dcea9 also passed 9/9 under the layer with no validation messages, and VK_LOADER_DEBUG=layer showed the loader inserting the layer in agc_driver_compute_table_calls.

After the main merge, Windows 11, RTX 4090, NVIDIA 616.92, WinLibs GCC 15.2: 969aada merges main 7b32a84, through #9701's merge. agc_structurizer, agc_shader_control_flow, agc_driver_compute_table_calls and scalar_call pass 11/11 with the intersection_off, iteration_selection_off, dominator_tree_off and forced_fallback variants, each run three times (ctest --repeat until-fail:3). Under VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Vulkan SDK 1.4.363.0), ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1, with ctest -V, they pass 11/11; on main 4d57821, five commits past the merged main and none in core/shader, the nine of them that exist there pass 9/9 (the two dominator_tree_off variants come with #9702). Neither log has was not found but was requested by env var VK_INSTANCE_LAYERS, a VUID- line or any other validation message.

Checklist

…f scanning them

Merge splitting re-examines every selection header after each split, so the lookups behind each examination run millions of times on a large graph. On Ghostrunner's compute shader 0x200c520000 (4,750 blocks growing to 10,170 over 1,302 splits) agc_shader_replay spent 173 s in splitSharedMergeBlocks. A profile put most of it in linear membership tests and in FindNearestCommonPostDominator, not in the analyses.

Loop bodies, construct blocks, selection regions and strongly connected components are sorted, so membership in them is now a binary search. The traversals that tested membership in the list they were building (selectionRegion, reachableWithin, naturalLoopBody) and externalEntryEdges test a bitset instead, and selectionRegion reads its sorted result from the bitset. dominatedBlocks tests the header once and pushes a successor only before it is visited. FindNearestCommonPostDominator skips a candidate whose post-dominator set is smaller than the other common post-dominators and checks the rest in one pass over the set. remapIds sorts only when the map broke the order.

Each change keeps the same sets in the same order and the same first candidate, so the structured graph is unchanged. On an M4 Pro the shader's structurization takes 37 s instead of 176 s.
…rating dominator sets

recomputeAnalyses runs after every merge split, and its dominator and post-dominator fixpoints intersected a bitset of every block for every block until nothing changed. They now build the tree with the Cooper-Harvey-Kennedy iteration over a reverse post-order from a virtual root above the entry block, or above every exit for post-dominators, and write each block's sorted set as its parent's set plus the block itself.

The sets are the same. When every block is reachable from the virtual root, the greatest fixpoint of the old equations is the dominance relation of that rooted graph, which the tree describes. When a block is unreachable from the entry, or cannot reach an exit, the old fixpoint runs as before.

The post-dominator tree also keeps each block's immediate post-dominator, and FindNearestCommonPostDominator climbs it when both blocks have one: the sets are the tree's paths, so the deepest common block is the one the set intersection found. Blocks added or cloned after the analysis have no parent and take the set path, as before.

APS5_NO_PERF_DOMINATOR_TREE turns both off, and the structurizer and control-flow tests also run with it set. On an M4 Pro the structurization of Ghostrunner's compute shader 0x200c520000 takes 18 s instead of 37 s.
computeStronglyConnectedComponents visited blocks recursively, one stack frame per block on the deepest path. The reach-flag fallback turns Ghostrunner's compute shader 0x200c520000 into a graph of 32,490 blocks, and with APS5_FORCE_STRUCTURIZER_FALLBACK the visit overflowed agc_shader_replay's 2 MiB stack after 4 s.

The visit now keeps its frames in a vector. It enters successors in the same order and closes components in the same order, so the components, their entry blocks and the irreducibility verdict are unchanged.
After each split, every selection header that needs no work still walks its construct (dominatedBlocks) to learn which predecessors of its merge lie inside. On Ghostrunner's compute shader 0x200c520000 that is 1.7 million walks.

When every block is reachable from the entry and the predecessor lists match the successor lists, and the merge M neither is the header H nor dominates it, the walk finds exactly the blocks that H dominates and M does not, other than M. A block the walk reaches is dominated by H and reached from the entry on a path that avoids M, because some path to H avoids M. A block that H dominates and M does not lies on a path from the entry that avoids M, and from the last H on that path every block is dominated by H, so the walk reaches it. splitOneSelectionMerge checks those conditions once per pass and then asks the dominator sets directly; otherwise it walks as before. Only membership in the construct is used, so the split and its order are unchanged.

On an M4 Pro the shader's structurization takes 12 to 15 s instead of 18 s, and 176 s before this series.
@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 22, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@clairtonmichel

Copy link
Copy Markdown

Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5):

  • Built cleanly with zero errors.
  • Ran the shader test suite (63/63 tests): 100% passed successfully in 9.84s total.
  • Structurizer cost-cutting optimization on large graphs is stable. Ready to merge!

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1368 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 1395 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 1896 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 2434 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5021 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5371 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5925 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6187 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6188 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6192 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6209 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6280 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6300 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6412 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6481 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6642 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6808 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6924 docs/dev/ENVIRONMENT.md
PR 7048 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7201 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7275 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7449 docs/dev/ENVIRONMENT.md
PR 7457 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 7484 docs/dev/ENVIRONMENT.md
PR 7604 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 7642 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 8004 docs/dev/ENVIRONMENT.md
PR 8014 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 8077 docs/dev/ENVIRONMENT.md
PR 8161 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 8246 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 8484 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 8542 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 8592 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 9176 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 9248 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 9287 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 9289 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 9345 docs/dev/ENVIRONMENT.md
PR 9356 docs/dev/ENVIRONMENT.md
PR 9387 docs/dev/ENVIRONMENT.md
PR 9402 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 9508 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 9676 docs/dev/ENVIRONMENT.md
PR 9748 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 9852 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 10026 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 10193 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 10442 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/ENVIRONMENT.md
PR 10547 docs/dev/ENVIRONMENT.md
PR 11157 core/libs/prx/libSceAgcDriver/CMakeLists.txt

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation and validation report are unusually thorough: I reviewed the sorted membership changes, the tree/fixpoint fallback conditions, the incremental post-dominator lookup, and the selection-construct dominance fast path. The reported differential comparisons and repeated focused/Vulkan-validation runs are useful evidence, though I could not independently run the C++ suite in this Linux checkout because it has no configured product build.

I’m requesting the repository-required performance artifacts before approval. This PR is labeled perf, but the description does not include the aps5-summary.jsonl line from main and the matching line from this change for the same title, scene, and machine, the two trace attachments, or tools/trace_digest.py output. The workflow has also marked perf numbers missing. Please add those artifacts, or remove the performance claim/title if the required capture is unavailable, as the performance-report rule specifies.

The branch is based on 7e2ba73e4c, 13 commits behind current main (8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768). Please rebase/update and rerun the focused checks on the resulting head before requesting another review. The PR is listed as a dependency of #9703, so please preserve that stack and coordinate the resulting heads there.

Local checks I ran against the current head: git diff --check passed; tools/check_conventions.py passed when given the actual PR body. GitHub conventions, eligibility, intake, labels, and report checks pass; product build jobs are skipped by the repository gates. I did not independently reproduce the reported timings, differential fuzzing, or platform/GPU test results.

@github-actions github-actions Bot added build shader core/shader docs agc libSceAgc* labels Oct 22, 2026
@frankea frankea changed the title perf(shader): cut the structurizer's cost per merge split on large graphs fix(shader): structurize a 4,750-block graph before Unreal's render thread watchdog fires Oct 23, 2026
@github-actions github-actions Bot removed perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 23, 2026
@frankea

frankea commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thank you for the review. Both points are addressed, and the body is updated.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 23, 2026
@frankea

frankea commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 since your review this PR is retitled as a fix with the speed claim removed (the perf labels came off), and it now merges #9701's refreshed head on main 0db47b2 (head 00ed884); the tests and the paired validation-layer comparison are rerun on it (first bullet under Tested).

AI-assisted: yes (Claude Code)

@TheSovietWaffle

Copy link
Copy Markdown
Contributor

Test report: Ghostrunner (PPSA03685) on Windows 10 22H2, NVIDIA GeForce RTX 3080 (driver 617.42), MinGW GCC Release build, 2026-10-23, with APS5_PLAYGO_UNLISTED_CHUNKS=absent.

On main 0db47b263 + #9068 (daef563d8) + #8070 (73c9c2a76) + #9703 (98c6aef28, which includes this PR and #9701), compute shader 0x200c520000 is prepared at first use before Unreal's render thread watchdog fires: the log has no timed out waiting. The game then stops at about 148 s on the next, unrelated gap, #7459 (GetImageResource dword 0 is not a valid runtime value, image V# loaded at pc 0x517c0 from a pointer read from a constant buffer at pc 0x15ac).

AI-assisted: yes (Claude Code).

@xXPBSXx

xXPBSXx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Title data point for this PR on current main (data only).

Title: PPSA03685, runs of up to 600 s through a runner script, no input sent, Windows 10 Pro 10.0.19045, WinLibs GCC 15.2.0, Release, relinker and all libraries built from each tree, NVIDIA GeForce RTX 4080 SUPER (ANYPS5_GPU=4080, the Selected GPU line at line 78 of each .out). No other variable was set. main db6af9e0fd already contains #9068 and #8070.

State Run GameThread timed out waiting for RenderThread Stop
main db6af9e0fd run_040006 yes (line 460) Unreal's abort through guest abort() (lines 446-460)
main db6af9e0fd run_074725 yes (line 453) same (lines 439-453)
db6af9e0fd + this PR (00ed884987, local merge 6790b535ae, not pushed) run_075403 no (0 matches) line 442: compute shader 0x200c520000: ... GetImageResource dword 0 is not a valid runtime value, then SIGABRT
db6af9e0fd + this PR run_075712 no (0 matches) line 439: the same message, then SIGABRT

So on current main, without stacking other pull requests, the abort is gone in 2 of 2 runs with this PR and present in 2 of 2 runs without it. The next stop is the one tracked in #7459; it matches the stop @TheSovietWaffle reported above.

Not run: AMD, Linux, other titles. I did not look at the picture and did not run the PR's tests for this comment.

AI-assisted (Claude Code).

@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 26, 2026 09:40

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@adriadam10
adriadam10 merged commit 97934af into boykopovar:main Oct 26, 2026
16 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the interdiff over #9701: the sorted-list lookups, the dominator/post-dominator trees and their fallback to the fixpoint when a block is unreachable or can't reach an exit, the nearest-common-post-dominator climb, and the dominance test for selection construct members (H dominates X and M doesn't equals the old dominatedBlocks walk when M doesn't dominate H and the edge lists agree). immediatePostDominator is remapped and reset wherever blocks are renumbered or cloned. The new *_dominator_tree_off tests are registered, and the env var has its row in ENVIRONMENT.md. Confirmed by @TheSovietWaffle (Ghostrunner) and @zaidhawk (Persona 3 Reload); merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs shader core/shader unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants