refactor(shader): walk each construct when checking the structured constructs #9703

Merged
frankea merged 11 commits from upstream-pr/9703 into main 2026-10-26 12:48:49 +00:00
frankea commented 2026-10-22 08:57:10 +00:00 (Migrated from github.com)

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


What

hasStructuredConstructs runs after every structurize attempt. For each merge header it tested every block of the graph, and for each block it searched every block for its innermost loop, with dominator binary searches each time. The work grows with the number of headers times the number of blocks. With APS5_FORCE_STRUCTURIZER_FALLBACK=1, Ghostrunner's compute shader 0x200c520000 (PPSA03685, linked with #9068 and #8070) becomes a 32,490-block reach-flag graph with 6,647 merge headers, so the check runs about 216 million member tests.

  • The innermost loop is now searched among the loop headers only. Every other block failed the loop-header test first, so the candidates and their order are unchanged.
  • When constructsFollowDominance (from #9702) holds for the structural graph, the member checks visit only the blocks dominatedBlocks finds from the header, stopping at its merge, and for a loop also from its continue block, in block order. Only a block in the header's construct or in the loop's continue construct can fail a member check. A construct block is dominated by the header and not by the merge, so some path from the entry reaches it without the merge; from the last header on that path every block is dominated by the header, so the walk reaches it. A continue-construct block is dominated by the continue block and is reached from it the same way. The blocks the walk skips passed every check, so the result is unchanged.
  • Otherwise every block is checked as before, and APS5_NO_PERF_DOMINATOR_TREE keeps the old loop. Its row in docs/dev/ENVIRONMENT.md says so.

Tested

  • Refreshed onto main 0db47b263 (head 98c6aef28, through #9702's refreshed head; no conflicts), RTX 4090 (NVIDIA 616.92, Windows 11): the 11 ctest entries of agc_structurizer, agc_shader_control_flow, scalar_call and agc_driver_compute_table_calls pass 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 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.
    This PR makes no speed claim. The repository's speed evidence is a capture of a scene on main and on the change, and no scene runs this check on a large graph: the only one known is 0x200c520000 under the forced fallback, and its scene ends before its next present (#9702 explains why).

Same result:

  • A differential fuzzer compared #9702's structurizer with this one on generated graphs (nested selections and loops with breaks, continues, early returns and cross edges; DAGs with back edges; small random graphs with irreducible parts, unreachable blocks and exitless loops). It ran 40,000 graphs of up to 83 blocks and 10,000 of up to 601 blocks in each of default, APS5_NO_PERF_DOM_INTERSECTION, APS5_NO_PERF_ITERATION_SELECTION, APS5_FORCE_STRUCTURIZER_FALLBACK and APS5_NO_PERF_DOMINATOR_TREE: 250,000 comparisons of GraphToString (or the exception text), no differences.
  • A build that runs the old and the new check side by side compared them on all 256,744 checks those runs made, and on 923,416 altered graphs (structured graphs with edges or constructs changed, of which 68% fail the check). They never disagreed.
  • 0x200c520000's structured graph is byte-identical in all five modes, and the side-by-side check agrees on it.

Tests on the head, 6ba72890a on main 7e2ba73e4 (on top of #9702), 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).

Validation: I ran the same focused tests on this head 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 11/11. The log has no was not found but was requested by env var VK_INSTANCE_LAYERS, no VUID- line and no validation message.

After the main merge, Windows 11, RTX 4090, NVIDIA 616.92, WinLibs GCC 15.2: 958f3f960 merges main 7b32a842f, through #9702'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 4d57821d4, 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

  • 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: #9702
  • 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:4964762188 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9703). Originally posted by [frankea](https://github.com/frankea) on 2026-10-22T08:57:10Z. --- ### What `hasStructuredConstructs` runs after every structurize attempt. For each merge header it tested every block of the graph, and for each block it searched every block for its innermost loop, with dominator binary searches each time. The work grows with the number of headers times the number of blocks. With `APS5_FORCE_STRUCTURIZER_FALLBACK=1`, Ghostrunner's compute shader 0x200c520000 (PPSA03685, linked with #9068 and #8070) becomes a 32,490-block reach-flag graph with 6,647 merge headers, so the check runs about 216 million member tests. - The innermost loop is now searched among the loop headers only. Every other block failed the loop-header test first, so the candidates and their order are unchanged. - When `constructsFollowDominance` (from #9702) holds for the structural graph, the member checks visit only the blocks `dominatedBlocks` finds from the header, stopping at its merge, and for a loop also from its continue block, in block order. Only a block in the header's construct or in the loop's continue construct can fail a member check. A construct block is dominated by the header and not by the merge, so some path from the entry reaches it without the merge; from the last header on that path every block is dominated by the header, so the walk reaches it. A continue-construct block is dominated by the continue block and is reached from it the same way. The blocks the walk skips passed every check, so the result is unchanged. - Otherwise every block is checked as before, and `APS5_NO_PERF_DOMINATOR_TREE` keeps the old loop. Its row in `docs/dev/ENVIRONMENT.md` says so. ### Tested - Refreshed onto `main` 0db47b263 (head 98c6aef28, through #9702's refreshed head; no conflicts), RTX 4090 (NVIDIA 616.92, Windows 11): the 11 ctest entries of `agc_structurizer`, `agc_shader_control_flow`, `scalar_call` and `agc_driver_compute_table_calls` pass 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 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. This PR makes no speed claim. The repository's speed evidence is a capture of a scene on `main` and on the change, and no scene runs this check on a large graph: the only one known is 0x200c520000 under the forced fallback, and its scene ends before its next present (#9702 explains why). Same result: - A differential fuzzer compared #9702's structurizer with this one on generated graphs (nested selections and loops with breaks, continues, early returns and cross edges; DAGs with back edges; small random graphs with irreducible parts, unreachable blocks and exitless loops). It ran 40,000 graphs of up to 83 blocks and 10,000 of up to 601 blocks in each of default, `APS5_NO_PERF_DOM_INTERSECTION`, `APS5_NO_PERF_ITERATION_SELECTION`, `APS5_FORCE_STRUCTURIZER_FALLBACK` and `APS5_NO_PERF_DOMINATOR_TREE`: 250,000 comparisons of `GraphToString` (or the exception text), no differences. - A build that runs the old and the new check side by side compared them on all 256,744 checks those runs made, and on 923,416 altered graphs (structured graphs with edges or constructs changed, of which 68% fail the check). They never disagreed. - 0x200c520000's structured graph is byte-identical in all five modes, and the side-by-side check agrees on it. Tests on the head, 6ba72890a on `main` 7e2ba73e4 (on top of #9702), 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`). Validation: I ran the same focused tests on this head 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 11/11. The log has no `was not found but was requested by env var VK_INSTANCE_LAYERS`, no `VUID-` line and no validation message. After the `main` merge, Windows 11, RTX 4090, NVIDIA 616.92, WinLibs GCC 15.2: 958f3f960 merges `main` 7b32a842f, through #9702'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` 4d57821d4, 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 - [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: #9702 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4090, driver 616.92, Windows 11 - [x] AI-assisted: yes (Claude Code)
github-actions[bot] commented 2026-10-22 08:57:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-22T08:57:24Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6336846524 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9703#issuecomment-6336846524). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-22T08:57:24Z. --- <!-- pr-labels-perf --> 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 commented 2026-10-22 09:16:15 +00:00 (Migrated from github.com)

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


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 0.09s / 9.89s total.
  • Shader construct traversal optimization is stable. Ready to merge!
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337205943 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9703#issuecomment-6337205943). Originally posted by [clairtonmichel](https://github.com/clairtonmichel) on 2026-10-22T09:16:15Z. --- 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 0.09s / 9.89s total**. - Shader construct traversal optimization is stable. Ready to merge!
MarcZX10 commented 2026-10-22 12:17:35 +00:00 (Migrated from github.com)

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

Review: DISMISSED


I reviewed the construct-walk optimization against the previous hasStructuredConstructs logic, including the structural graph analysis, the loop/continue member sets, and the fallback path. The optimized walk is guarded by constructsFollowDominance, which checks reachability and predecessor/successor consistency; the original full scan remains as fallback. The reported differential comparisons and repeated focused/Vulkan-validation runs are meaningful evidence. I found no independent code defect in the reviewed diff. I could not run the C++ suite locally because this checkout has no configured product build.

Before approval, please satisfy the repository performance-report rule: this PR is labeled perf, but the description does not include matching aps5-summary.jsonl lines for main and the change from the same title, scene, and machine, the two trace attachments, or tools/trace_digest.py output. The workflow has marked perf numbers missing. Please add the capture artifacts, or remove the performance claim/title if the required capture is unavailable.

This branch is based on 7e2ba73e4c, 13 commits behind current main (8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768), and depends on #9702, which has the same base age and needs the same performance-report artifacts. Please keep the declared dependency, coordinate the stack after #9702 is updated, and rebase/retest this head on current main before requesting another review.

Local checks I ran against the head: git diff --check passed; tools/check_conventions.py passed with 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613985392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9703#pullrequestreview-5613985392). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T12:17:35Z. **Review:** DISMISSED --- I reviewed the construct-walk optimization against the previous `hasStructuredConstructs` logic, including the structural graph analysis, the loop/continue member sets, and the fallback path. The optimized walk is guarded by `constructsFollowDominance`, which checks reachability and predecessor/successor consistency; the original full scan remains as fallback. The reported differential comparisons and repeated focused/Vulkan-validation runs are meaningful evidence. I found no independent code defect in the reviewed diff. I could not run the C++ suite locally because this checkout has no configured product build. Before approval, please satisfy the repository performance-report rule: this PR is labeled `perf`, but the description does not include matching `aps5-summary.jsonl` lines for `main` and the change from the same title, scene, and machine, the two trace attachments, or `tools/trace_digest.py` output. The workflow has marked `perf numbers missing`. Please add the capture artifacts, or remove the performance claim/title if the required capture is unavailable. This branch is based on `7e2ba73e4c`, 13 commits behind current `main` (`8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768`), and depends on #9702, which has the same base age and needs the same performance-report artifacts. Please keep the declared dependency, coordinate the stack after #9702 is updated, and rebase/retest this head on current `main` before requesting another review. Local checks I ran against the head: `git diff --check` passed; `tools/check_conventions.py` passed with 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.
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!9703
No description provided.