fix(shader): validate and optimize SPIR-V on a host thread when the stack is short #9495
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9495"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
ValidateAndOptimizeSpirvnow 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 sinceef022d53b(StackHoldsImport): on Windows the bounds come fromGetCurrentThreadStackLimits, which reads the TIB bounds libSceFiber sets to the fiber context; elsewhere frompthread_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.cppgets_WIN32_WINNT=0x0602on Windows, asLifecycle.cppdoes.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.main(d6eafa54d) and on this branch (rebased onto it) withANYPS5_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; 0VUID-lines in either log, and neither log containswas not found but was requested by env var VK_INSTANCE_LAYERS. Both full builds completed with no failed target.r_Wants4K=WantsPerf), runs made before the rebase:main50efb5c40, warm shader cache: 3 of 3 runs died with0xc0000005on aBPE JobWorkerThreadin 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 holdingSSARewriter::AddPhiOperands+0x1ebandSSARewriter::GetReachingDefreturn addresses (libSceAgcDriver.prx) where the function had stored its own constants.main2457319ab+ #8310 + #7844 + #8552: 1 crash in the cold run (same function), 0 in two warm runs.Checklist
main; no other open PR implements the same functionsNotImplemented_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)🤖 Generated with Claude Code
https://claude.ai/code/session_01SnKKQN6LzjYxS24ViHmw8j
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
StackHoldsImportapproach; 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.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.
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 -Vvalidation 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 sharedlibSceAgcDriver/CMakeLists.txttest registration with #7464; please preserve both registrations through the rebase. The current branch and Vulkan comparison are based ona6dcea9279, while upstream main isab347d144d. 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.Imported from boykopovar/AnyPS5. Originally posted by Neppolo on 2026-10-22T08:34:17Z.
Rebased onto
maind6eafa54d(newer thanab347d144; none of the 108 commits sincea6dcea927touch the changed files or the submodules). New head:32a7a8a35, same diff.mainhas since moved two commits to7e2ba73e4; 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):mainand of this branch: both complete with no failed target (thealignasfailure 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:main6/6 passed, this branch 7/7 passed; 0VUID-lines in either log; neither containswas 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 mainwith the title and body: all passed.tools/pr_overlap.py --branch: still only #7464, at the test-registration insertion at the end ofcore/libs/prx/libSceAgcDriver/CMakeLists.txt. #7464 is open, somainhas nothing of it to keep yet; whichever lands second keeps bothadd_testblocks.The PR body's Tested section now names
d6eafa54d. The Demon's Souls runs in it are still the ones made before the rebase.