fix(shader): find strongly connected components without recursion #9701

Merged
frankea merged 3 commits from upstream-pr/9701 into main 2026-10-25 22:03:07 +00:00
frankea commented 2026-10-22 08:56:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-22T08:56:51Z.


What

computeStronglyConnectedComponents visited blocks recursively, one stack frame per block on the deepest path. With APS5_FORCE_STRUCTURIZER_FALLBACK=1, the reach-flag fallback turns Ghostrunner's compute shader 0x200c520000 (PPSA03685, linked with #9068 and #8070) into a graph of 32,490 blocks. tarjanVisit then overflowed agc_shader_replay's 2 MiB stack after 4 s, and the replay crashed. The recursion is as deep as the longest depth-first path, so any graph that large overflows a thread's stack the same way.

The visit now keeps its frames in a vector: block, successor list, next successor. It enters successors in the same order, lowers the parent's lowlink when a child finishes, and closes a component when a block's lowlink equals its index. That is the order the recursion used, so the components, their order, their entry blocks and the irreducibility verdict are the same. The component bookkeeping moves unchanged into emitComponent.

Tested

  • Refreshed onto main 0db47b263 (head 2a70b8026; no conflicts), RTX 4090 (NVIDIA 616.92, Windows 11): the 9 ctest entries of agc_structurizer, agc_shader_control_flow, scalar_call and agc_driver_compute_table_calls pass 9/9, each test run three times, and 9/9 under the validation layer (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1, ctest -V), with no VUID- line; the layer reports nothing on this head. main 0db47b263 passes the union of the refreshed sets, 115/115, under the same layer settings with no VUID- line.
  • 0x200c520000 with the forced fallback, the CFG dumped at Structurize and replayed natively on macOS with ulimit -s 2048: main segfaults; with this commit it completes, 4,750 to 32,490 blocks. The rest of the fallback is slow (hasStructuredConstructs tests every block for each header); that is a separate change.
  • The structured graph (GraphToString, which lists every component with its entries) is byte-identical to main's for that shader without the fallback. It is also identical to main's on 40,000 generated graphs in each of default, APS5_NO_PERF_DOM_INTERSECTION, APS5_NO_PERF_ITERATION_SELECTION and APS5_FORCE_STRUCTURIZER_FALLBACK (160,000 comparisons). The generated graphs include irreducible ones, unreachable blocks and exitless loops.
  • 360682431 on main 7e2ba73e4, 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 9/9 with the intersection_off, iteration_selection_off and forced_fallback variants, each run three times (ctest --repeat until-fail:3). agc_structurizer_tests also passes in all four modes built with AppleClang on macOS.

Validation: I ran the same focused tests on this commit (360682431) 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. The log has no was not found but was requested by env var VK_INSTANCE_LAYERS, no VUID- line and no validation message. Before the rebase, main a6dcea927 and this commit on it 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: 050af89f1 merges main 7b32a842f. agc_structurizer, agc_shader_control_flow, agc_driver_compute_table_calls and scalar_call pass 9/9 with the intersection_off, iteration_selection_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 9/9; on main 4d57821d4, five commits past the merged main and none in core/shader, the same tests pass 9/9. Neither log has was not found but was requested by env var VK_INSTANCE_LAYERS, a VUID- line or any other validation message.

Checklist

  • Based on current main; no other open PR implements the same functions. No open PR changes Structurizer.cpp.
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4090, driver 616.92, Windows 11
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4964758617 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9701). Originally posted by [frankea](https://github.com/frankea) on 2026-10-22T08:56:51Z. --- ### What `computeStronglyConnectedComponents` visited blocks recursively, one stack frame per block on the deepest path. With `APS5_FORCE_STRUCTURIZER_FALLBACK=1`, the reach-flag fallback turns Ghostrunner's compute shader 0x200c520000 (PPSA03685, linked with #9068 and #8070) into a graph of 32,490 blocks. `tarjanVisit` then overflowed `agc_shader_replay`'s 2 MiB stack after 4 s, and the replay crashed. The recursion is as deep as the longest depth-first path, so any graph that large overflows a thread's stack the same way. The visit now keeps its frames in a vector: block, successor list, next successor. It enters successors in the same order, lowers the parent's lowlink when a child finishes, and closes a component when a block's lowlink equals its index. That is the order the recursion used, so the components, their order, their entry blocks and the irreducibility verdict are the same. The component bookkeeping moves unchanged into `emitComponent`. ### Tested - Refreshed onto `main` 0db47b263 (head 2a70b8026; no conflicts), RTX 4090 (NVIDIA 616.92, Windows 11): the 9 ctest entries of `agc_structurizer`, `agc_shader_control_flow`, `scalar_call` and `agc_driver_compute_table_calls` pass 9/9, each test run three times, and 9/9 under the validation layer (`VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1`, `ctest -V`), with no `VUID-` line; the layer reports nothing on this head. `main` 0db47b263 passes the union of the refreshed sets, 115/115, under the same layer settings with no `VUID-` line. - 0x200c520000 with the forced fallback, the CFG dumped at `Structurize` and replayed natively on macOS with `ulimit -s 2048`: `main` segfaults; with this commit it completes, 4,750 to 32,490 blocks. The rest of the fallback is slow (`hasStructuredConstructs` tests every block for each header); that is a separate change. - The structured graph (`GraphToString`, which lists every component with its entries) is byte-identical to `main`'s for that shader without the fallback. It is also identical to `main`'s on 40,000 generated graphs in each of default, `APS5_NO_PERF_DOM_INTERSECTION`, `APS5_NO_PERF_ITERATION_SELECTION` and `APS5_FORCE_STRUCTURIZER_FALLBACK` (160,000 comparisons). The generated graphs include irreducible ones, unreachable blocks and exitless loops. - 360682431 on `main` 7e2ba73e4, 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 9/9 with the `intersection_off`, `iteration_selection_off` and `forced_fallback` variants, each run three times (`ctest --repeat until-fail:3`). `agc_structurizer_tests` also passes in all four modes built with AppleClang on macOS. Validation: I ran the same focused tests on this commit (360682431) 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. The log has no `was not found but was requested by env var VK_INSTANCE_LAYERS`, no `VUID-` line and no validation message. Before the rebase, `main` a6dcea927 and this commit on it 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: 050af89f1 merges `main` 7b32a842f. `agc_structurizer`, `agc_shader_control_flow`, `agc_driver_compute_table_calls` and `scalar_call` pass 9/9 with the `intersection_off`, `iteration_selection_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 9/9; on `main` 4d57821d4, five commits past the merged `main` and none in `core/shader`, the same tests pass 9/9. Neither log has `was not found but was requested by env var VK_INSTANCE_LAYERS`, a `VUID-` line or any other validation message. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions. No open PR changes `Structurizer.cpp`. - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4090, driver 616.92, Windows 11 - [x] AI-assisted: yes (Claude Code)
clairtonmichel commented 2026-10-22 09:20:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by clairtonmichel on 2026-10-22T09:20:12Z.


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 (62/62 tests): 100% passed successfully in 9.78s total.
  • Non-recursive strongly connected components fix is stable. Ready to merge!
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337286589 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9701#issuecomment-6337286589). Originally posted by [clairtonmichel](https://github.com/clairtonmichel) on 2026-10-22T09:20:12Z. --- 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 (62/62 tests): **100% passed successfully in 9.78s total**. - Non-recursive strongly connected components fix is stable. Ready to merge!
MarcZX10 commented 2026-10-22 12:17:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T12:17:56Z.

Review: DISMISSED


I reviewed the iterative Tarjan conversion. The explicit frame stack preserves the recursive successor order; the parent low-link update occurs when its DFS child finishes, and component emission/bookkeeping is unchanged. I found no code defect in this diff. The author-reported 160,000 differential graph comparisons, 9/9 repeated focused tests and Vulkan-validation run are strong, but I could not independently rerun the C++ suite because this checkout has no configured product build.\n\nPlease update this branch to current main before approval. It is based on 7e2ba73e4c, 13 commits behind current main (8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768), contrary to the repository rule to keep PRs based on current main. Re-run the focused tests after the update. This is also the declared base dependency for #9702/#9703, so please coordinate the stack and ensure those PRs use the updated head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613990440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9701#pullrequestreview-5613990440). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T12:17:56Z. **Review:** DISMISSED --- I reviewed the iterative Tarjan conversion. The explicit frame stack preserves the recursive successor order; the parent low-link update occurs when its DFS child finishes, and component emission/bookkeeping is unchanged. I found no code defect in this diff. The author-reported 160,000 differential graph comparisons, 9/9 repeated focused tests and Vulkan-validation run are strong, but I could not independently rerun the C++ suite because this checkout has no configured product build.\n\nPlease update this branch to current `main` before approval. It is based on `7e2ba73e4c`, 13 commits behind current `main` (`8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768`), contrary to the repository rule to keep PRs based on current main. Re-run the focused tests after the update. This is also the declared base dependency for #9702/#9703, so please coordinate the stack and ensure those PRs use the updated head.
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
boykopovar/AnyPS5!9701
No description provided.