Repository navigation
fix(shader): structurize a 4,750-block graph before Unreal's render thread watchdog fires - #9702
Conversation
…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.
|
This pull request is labeled |
|
Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5):
|
|
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.
|
MarcZX10
left a comment
There was a problem hiding this comment.
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.
|
@MarcZX10 thank you for the review. Both points are addressed, and the body is updated.
AI-assisted: yes (Claude Code) |
|
@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 AI-assisted: yes (Claude Code) |
|
Test report: Ghostrunner (PPSA03685) on Windows 10 22H2, NVIDIA GeForce RTX 3080 (driver 617.42), MinGW GCC Release build, 2026-10-23, with On main AI-assisted: yes (Claude Code). |
|
Title data point for this PR on current 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 (
So on current 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). |
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.
|
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 |
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.splitSharedMergeBlocksmakes 1,302 splits, and the graph grows to 10,170 blocks.The time was not in the analyses. I dumped the graph at
Structurizeand replayed it throughStructurizer.cppalone with scoped timers. After each split,splitOneSelectionMergeexamines every selection header again: 1.71 million header checks in all. That loop took 144 of 177 s:FindNearestCommonPostDominatortook 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) andnaturalLoopBodytested membership withstd::findin the list they were building, andexternalEntryEdges(14 s) did the same in the sorted region.findInnermostContainingLoop(18 s) scanned loop bodies linearly.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, andselectionRegionreads its sorted result from the bitset.FindNearestCommonPostDominatorskips a candidate whose post-dominator set is too small to hold the other common blocks and checks the rest in one pass.remapIdssorts 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, andFindNearestCommonPostDominatorclimbs the tree when both blocks have one.APS5_NO_PERF_DOMINATOR_TREEturns this commit and the next off. The structurizer and control-flow tests also run with it set, and it has a row indocs/dev/ENVIRONMENT.md.perf(shader): test a selection construct's members by dominance. In some graphssplitSharedMergeBlockcan 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.splitOneSelectionMergechecks 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 uphasStructuredConstructsseparately.BasicBlockgainsimmediatePostDominator.GraphToStringdoesn't print it, and nothing outside the structurizer reads it.Tested
main0db47b2 (head 00ed884, through fix(shader): find strongly connected components without recursion #9701's refreshed head; no conflicts), RTX 4090 (NVIDIA 616.92, Windows 11): the 11 ctest entries ofagc_structurizer,agc_shader_control_flow,scalar_callandagc_driver_compute_table_callspass 11/11, each test run three times, and 11/11 under the validation layer (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1,ctest -V), with noVUID-line; the layer reports nothing on this head.main0db47b2 passes the union of the refreshed sets, 115/115, under the same layer settings with noVUID-line.Ghostrunner 0x200c520000,
agc_shader_replay --prepareon the request the driver dumped when the shader was first used. Both builds aremaind34da5d + fix(shader): link table callees that together exceed s_call_b64 range #9068 + fix(shader): link a table call whose V# is a slice of an s_load_dwordx8 or s_load_dwordx16 #8070 (the shader doesn't link without them) with local diagnostics that dump the input graph and print timestamps; the second adds these commits. The structurizer files are the same on d34da5d and on currentmain. Machine: Ryzen 7 5800X, RTX 4090, Windows 11, WinLibs GCC 15.2, Release, otherwise idle:main+ #9068 + #8070splitSharedMergeBlocks--prepareBoth 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
main9d6adeb + #7048 + #9068 + #8070 with a larger local callee window,agc_shader_replay --prepareof 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 withAPS5_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:
GraphToString, 16 MB) is byte-identical tomain's after each commit.main's and this structurizer on generated graphs and comparedGraphToString, or the exception text when one throws. The graphs:APS5_NO_PERF_DOM_INTERSECTION,APS5_NO_PERF_ITERATION_SELECTION,APS5_FORCE_STRUCTURIZER_FALLBACK, andAPS5_NO_PERF_DOMINATOR_TREEagainstmain'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.agc_shader_control_flow_tests,agc_structurizer_testsandagc_driver_compute_table_calls_testshand toStructurize, plus 7 Ghostrunner shaders, in 4 modes: 460 comparisons, all identical, including the 34 that throw.Tests on the head, 231be5c on
main7e2ba73, Windows 11, RTX 4090, NVIDIA 616.92, WinLibs GCC 15.2:agc_structurizer,agc_shader_control_flow,agc_driver_compute_table_callsandscalar_callpass 11/11 with theintersection_off,iteration_selection_off,dominator_tree_offandforced_fallbackvariants, each run three times (ctest --repeat until-fail:3).agc_structurizer_testsalso 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
main7e2ba73, withVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(Vulkan SDK 1.4.363.0),ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1, usingctest -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 twodominator_tree_offvariants). No log haswas not found but was requested by env var VK_INSTANCE_LAYERS, aVUID-line or a validation message. Before the rebase,maina6dcea9 also passed 9/9 under the layer with no validation messages, andVK_LOADER_DEBUG=layershowed the loader inserting the layer inagc_driver_compute_table_calls.After the
mainmerge, Windows 11, RTX 4090, NVIDIA 616.92, WinLibs GCC 15.2: 969aada mergesmain7b32a84, through #9701's merge.agc_structurizer,agc_shader_control_flow,agc_driver_compute_table_callsandscalar_callpass 11/11 with theintersection_off,iteration_selection_off,dominator_tree_offandforced_fallbackvariants, each run three times (ctest --repeat until-fail:3). UnderVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(Vulkan SDK 1.4.363.0),ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1, withctest -V, they pass 11/11; onmain4d57821, five commits past the mergedmainand none incore/shader, the nine of them that exist there pass 9/9 (the twodominator_tree_offvariants come with #9702). Neither log haswas not found but was requested by env var VK_INSTANCE_LAYERS, aVUID-line or any other validation message.Checklist
main; no other open PR implements the same functions. No open PR changesStructurizer.cpporControlFlowGraph.cpp;tools/pr_overlap.pyreports no overlap. perf(shader): validate value IR dominance with a dominator tree #9198 uses the same dominator algorithm forValidateProgram's IR dominance and touches no file here.NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)