fix(shader): validate and optimize SPIR-V on a host thread when the stack is short #9495

Merged
Neppolo merged 1 commit from upstream-pr/9495 into main 2026-10-24 18:15:00 +00:00
Neppolo commented 2026-10-21 23:22:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Neppolo on 2026-10-21T23:22:14Z.


What

SPIR-V validation and optimization ran on the stack of whatever thread asked for the shader. Guest fibers run on contexts the title allocates in guest memory, without a guard page, and libSceFiber keeps a context of 64 KiB or more as the fiber's stack. SPIRV-Tools passes recurse (12,352 bytes of stack for a 410-word compute shader; the recompiler's shaders reach hundreds of thousands of words), so an optimization on a fiber can run past its context into the next one.

ValidateAndOptimizeSpirv now runs inline only when the frame lies inside the thread's stack bounds with at least 256 KiB below it, and otherwise on a host thread that it joins, rethrowing that thread's exception. This is the same check the GPU-map import uses since ef022d53b (StackHoldsImport): on Windows the bounds come from GetCurrentThreadStackLimits, which reads the TIB bounds libSceFiber sets to the fiber context; elsewhere from pthread_getattr_np, whose bounds a fiber's stack lies outside of. The optimizer takes no lock, so the joined thread cannot wait on its caller. SpirvOptimizer.cpp gets _WIN32_WINNT=0x0602 on Windows, as Lifecycle.cpp does.

New test agc_driver_optimize_on_fiber: optimizes a compute shader from a fiber running on a 64 KiB context, requires that it use at most 8 KiB of the context and start one host thread, that an ordinary thread start none, and that both return the same module.

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0, Release, ANYPS5_ENABLE_SPIRV_TOOLS=ON, RTX 5060 Ti 8 GB (driver 617.14).

  • agc_driver_optimize_on_fiber: 3,040 bytes of the context used with this change; with the check forced to run inline the same test fails with 12,352 bytes.
  • Focused tests on main (d6eafa54d) and on this branch (rebased onto it) with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest -R '^(agc_driver_optimize_on_fiber|agc_driver_gpu_map_on_fiber|agc_shader_memory|agc_spirv_module|spirv_validator_exit|agc_driver_graphics|agc_driver_bda_device)$' -V: main 6/6 passed, this branch 7/7 passed; 0 VUID- lines in either log, and neither log contains was not found but was requested by env var VK_INSTANCE_LAYERS. Both full builds completed with no failed target.
  • Demon's Souls PPSA01341 01.007.000 (Windows, same machine, r_Wants4K=WantsPerf), runs made before the rebase:
    • main 50efb5c40, warm shader cache: 3 of 3 runs died with 0xc0000005 on a BPE JobWorkerThread in the title's job-steal loop after 535, 55 and 48 s, still on the boot loading screen. The crash report's stack dump shows the faulting fiber's frame holding SSARewriter::AddPhiOperands+0x1eb and SSARewriter::GetReachingDef return addresses (libSceAgcDriver.prx) where the function had stored its own constants.
    • main 2457319ab + #8310 + #7844 + #8552: 1 crash in the cold run (same function), 0 in two warm runs.
    • The same + this change: no crash in a cold run (6,397 shaders compiled, 1,200 s cap) and a warm run (1,200 s cap); the first-boot language menu is presented at ~24 fps.

Checklist

  • Based on current main; no other open PR implements the same functions
  • 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: PPSA01341 #1283
  • GPUs tested: NVIDIA GeForce RTX 5060 Ti 8 GB, driver 617.14, Windows 11 Pro 10.0.26200
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01SnKKQN6LzjYxS24ViHmw8j

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4959685157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9495). Originally posted by [Neppolo](https://github.com/Neppolo) on 2026-10-21T23:22:14Z. --- ### What SPIR-V validation and optimization ran on the stack of whatever thread asked for the shader. Guest fibers run on contexts the title allocates in guest memory, without a guard page, and libSceFiber keeps a context of 64 KiB or more as the fiber's stack. SPIRV-Tools passes recurse (12,352 bytes of stack for a 410-word compute shader; the recompiler's shaders reach hundreds of thousands of words), so an optimization on a fiber can run past its context into the next one. `ValidateAndOptimizeSpirv` now runs inline only when the frame lies inside the thread's stack bounds with at least 256 KiB below it, and otherwise on a host thread that it joins, rethrowing that thread's exception. This is the same check the GPU-map import uses since ef022d53b (`StackHoldsImport`): on Windows the bounds come from `GetCurrentThreadStackLimits`, which reads the TIB bounds libSceFiber sets to the fiber context; elsewhere from `pthread_getattr_np`, whose bounds a fiber's stack lies outside of. The optimizer takes no lock, so the joined thread cannot wait on its caller. `SpirvOptimizer.cpp` gets `_WIN32_WINNT=0x0602` on Windows, as `Lifecycle.cpp` does. New test `agc_driver_optimize_on_fiber`: optimizes a compute shader from a fiber running on a 64 KiB context, requires that it use at most 8 KiB of the context and start one host thread, that an ordinary thread start none, and that both return the same module. ### Tested Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0, Release, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, RTX 5060 Ti 8 GB (driver 617.14). - `agc_driver_optimize_on_fiber`: 3,040 bytes of the context used with this change; with the check forced to run inline the same test fails with 12,352 bytes. - Focused tests on `main` (d6eafa54d) and on this branch (rebased onto it) with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ctest -R '^(agc_driver_optimize_on_fiber|agc_driver_gpu_map_on_fiber|agc_shader_memory|agc_spirv_module|spirv_validator_exit|agc_driver_graphics|agc_driver_bda_device)$' -V`: main 6/6 passed, this branch 7/7 passed; 0 `VUID-` lines in either log, and neither log contains `was not found but was requested by env var VK_INSTANCE_LAYERS`. Both full builds completed with no failed target. - Demon's Souls PPSA01341 01.007.000 (Windows, same machine, `r_Wants4K=WantsPerf`), runs made before the rebase: - `main` 50efb5c40, warm shader cache: 3 of 3 runs died with `0xc0000005` on a `BPE JobWorkerThread` in the title's job-steal loop after 535, 55 and 48 s, still on the boot loading screen. The crash report's stack dump shows the faulting fiber's frame holding `SSARewriter::AddPhiOperands+0x1eb` and `SSARewriter::GetReachingDef` return addresses (libSceAgcDriver.prx) where the function had stored its own constants. - `main` 2457319ab + #8310 + #7844 + #8552: 1 crash in the cold run (same function), 0 in two warm runs. - The same + this change: no crash in a cold run (6,397 shaders compiled, 1,200 s cap) and a warm run (1,200 s cap); the first-boot language menu is presented at ~24 fps. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [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: PPSA01341 #1283 - [x] GPUs tested: NVIDIA GeForce RTX 5060 Ti 8 GB, driver 617.14, Windows 11 Pro 10.0.26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01SnKKQN6LzjYxS24ViHmw8j
MarcZX10 commented 2026-10-21 23:27:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:27:15Z.

Review: APPROVED


Reviewed the stack-bound check, fallback thread execution and focused fiber test. The Windows and POSIX paths match the existing StackHoldsImport approach; the test verifies the optimizer runs off the 64 KiB guest fiber context, stays inline on a normal thread, preserves output, and propagates failures. The reported main/change Vulkan validation runs include the required environment, matching focused test set, zero new VUIDs and actual layer-loading checks. The Windows test/build evidence is detailed; hosted product build jobs are not present in this PR checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607466306 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9495#pullrequestreview-5607466306). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T23:27:15Z. **Review:** APPROVED --- Reviewed the stack-bound check, fallback thread execution and focused fiber test. The Windows and POSIX paths match the existing `StackHoldsImport` approach; the test verifies the optimizer runs off the 64 KiB guest fiber context, stays inline on a normal thread, preserves output, and propagates failures. The reported main/change Vulkan validation runs include the required environment, matching focused test set, zero new VUIDs and actual layer-loading checks. The Windows test/build evidence is detailed; hosted product build jobs are not present in this PR checks.
MarcZX10 commented 2026-10-22 02:10:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:10:08Z.

Review: APPROVED


Re-reviewed the current head and its overlap report. The reported title unblock matches Demon's Souls (PPSA01341), discussion #1283. The fiber fallback preserves the inline path when stack headroom is sufficient, runs the optimizer synchronously on a joined host thread otherwise, and propagates exceptions. The focused test covers fiber stack use, host-thread selection and output equivalence. The author reports the required main/change validation-layer comparison on the same GPU with the layer confirmed loaded and no VUID lines. Hosted PR checks currently cover conventions, intake and labels; full product builds were not run here. The overlap report identifies only a shared AGC test registration insertion with #7464; retain that registration when integrating. I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5608475796 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9495#pullrequestreview-5608475796). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T02:10:08Z. **Review:** APPROVED --- Re-reviewed the current head and its overlap report. The reported title unblock matches Demon's Souls (PPSA01341), discussion #1283. The fiber fallback preserves the inline path when stack headroom is sufficient, runs the optimizer synchronously on a joined host thread otherwise, and propagates exceptions. The focused test covers fiber stack use, host-thread selection and output equivalence. The author reports the required main/change validation-layer comparison on the same GPU with the layer confirmed loaded and no VUID lines. Hosted PR checks currently cover conventions, intake and labels; full product builds were not run here. The overlap report identifies only a shared AGC test registration insertion with #7464; retain that registration when integrating. I did not run local tests.
MarcZX10 commented 2026-10-22 04:26:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:26:36Z.

Review: DISMISSED


The stack-bound fallback has strong focused coverage: the fiber test checks bounded stack use, host-thread selection and output equivalence, and the author reports matching main/change ctest -V validation runs with all required environment variables, confirmed layer loading and no VUID lines. The Demon's Souls PPSA01341 report and #1283 reference support the title-specific issue claim. The overlap report identifies a shared libSceAgcDriver/CMakeLists.txt test registration with #7464; please preserve both registrations through the rebase. The current branch and Vulkan comparison are based on a6dcea9279, while upstream main is ab347d144d. Please rebase and rerun the focused fiber/optimizer tests plus the required main/change validation comparison on current main, and refresh the overlap scan including the reported shared CMake insertion. Hosted full product jobs are skipped; I did not run local tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609256466 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9495#pullrequestreview-5609256466). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:26:36Z. **Review:** DISMISSED --- The stack-bound fallback has strong focused coverage: the fiber test checks bounded stack use, host-thread selection and output equivalence, and the author reports matching main/change `ctest -V` validation runs with all required environment variables, confirmed layer loading and no VUID lines. The Demon's Souls PPSA01341 report and #1283 reference support the title-specific issue claim. The overlap report identifies a shared `libSceAgcDriver/CMakeLists.txt` test registration with #7464; please preserve both registrations through the rebase. The current branch and Vulkan comparison are based on a6dcea9279, while upstream main is ab347d144d88fae054d9770cf0db8d3565735c02. Please rebase and rerun the focused fiber/optimizer tests plus the required main/change validation comparison on current main, and refresh the overlap scan including the reported shared CMake insertion. Hosted full product jobs are skipped; I did not run local tests.
Neppolo commented 2026-10-22 08:34:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Neppolo on 2026-10-22T08:34:17Z.


Rebased onto main d6eafa54d (newer than ab347d144; none of the 108 commits since a6dcea927 touch the changed files or the submodules). New head: 32a7a8a35, same diff. main has since moved two commits to 7e2ba73e4; neither touches these files and the branch merges into it cleanly.

Redone on the new base, same machine (RTX 5060 Ti 8 GB, driver 617.14, Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0, Release, ANYPS5_ENABLE_SPIRV_TOOLS=ON):

  • Full builds of main and of this branch: both complete with no failed target (the alignas failure noted before is gone since #8310 merged).
  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest -R '^(spirv_validator_exit|agc_shader_memory|agc_spirv_module|agc_driver_graphics|agc_driver_bda_device|agc_driver_gpu_map_on_fiber|agc_driver_optimize_on_fiber)$' -V: main 6/6 passed, this branch 7/7 passed; 0 VUID- lines in either log; neither contains was not found but was requested by env var VK_INSTANCE_LAYERS.
  • agc_driver_optimize_on_fiber: "SPIR-V optimization on a fiber used 3040 bytes of its context", as before.
  • tools/check_conventions.py --base main with the title and body: all passed.
  • tools/pr_overlap.py --branch: still only #7464, at the test-registration insertion at the end of core/libs/prx/libSceAgcDriver/CMakeLists.txt. #7464 is open, so main has nothing of it to keep yet; whichever lands second keeps both add_test blocks.

The PR body's Tested section now names d6eafa54d. The Demon's Souls runs in it are still the ones made before the rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6336422843 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9495#issuecomment-6336422843). Originally posted by [Neppolo](https://github.com/Neppolo) on 2026-10-22T08:34:17Z. --- Rebased onto `main` d6eafa54d (newer than ab347d144; none of the 108 commits since a6dcea927 touch the changed files or the submodules). New head: 32a7a8a35, same diff. `main` has since moved two commits to 7e2ba73e4; neither touches these files and the branch merges into it cleanly. Redone on the new base, same machine (RTX 5060 Ti 8 GB, driver 617.14, Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0, Release, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`): - Full builds of `main` and of this branch: both complete with no failed target (the `alignas` failure noted before is gone since #8310 merged). - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest -R '^(spirv_validator_exit|agc_shader_memory|agc_spirv_module|agc_driver_graphics|agc_driver_bda_device|agc_driver_gpu_map_on_fiber|agc_driver_optimize_on_fiber)$' -V`: `main` 6/6 passed, this branch 7/7 passed; 0 `VUID-` lines in either log; neither contains `was not found but was requested by env var VK_INSTANCE_LAYERS`. - `agc_driver_optimize_on_fiber`: "SPIR-V optimization on a fiber used 3040 bytes of its context", as before. - `tools/check_conventions.py --base main` with the title and body: all passed. - `tools/pr_overlap.py --branch`: still only #7464, at the test-registration insertion at the end of `core/libs/prx/libSceAgcDriver/CMakeLists.txt`. #7464 is open, so `main` has nothing of it to keep yet; whichever lands second keeps both `add_test` blocks. The PR body's Tested section now names d6eafa54d. The Demon's Souls runs in it are still the ones made before the rebase.
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!9495
No description provided.