fix(shader): end a pixel shader's failed BDA reads through one exit #7377
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7377"
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 EagleBearerYS on 2026-10-18T03:38:47Z.
What
On AMD's Windows driver, two titles crash the shader compiler inside
vkCreateGraphicsPipelines:main143f13055with #6379, #6488, #6417 and #6504. The crash happens at boot: an access violation atamdvlk64.dll+0x9e251fon a driver thread, on pixel shaders with up to 112 BDA-fault exits. Cleverdee got past it with poonasor's commit below. Demon's Souls also needed #6379 for a separate meshgl_Layerlink crash on AMD.379d8b4eplus their commits). It's the same access violation atamdvlk64.dll+0x9e251finvkCreateGraphicsPipelines, after START. One main-menu fragment shader is 662,679 SPIR-V words with 216OpTerminateInvocation/OpKillexits. poonasor reproduced the crash offline with a stand-alone pipeline-creation tool, and the module passes the validation layer.StopBdaInvocationIf(SpirvBdaFault.cpp) ends a pixel invocation at every guest-memory read that faults. That's anOpTerminateInvocationwhenSPV_KHR_terminate_invocationis supported (#5804), and anOpKillotherwise. A large shader therefore carries one exit per read.This is poonasor's fix from poonasor/AnyPS5@eb2e2309, ported to current
mainwith them as co-author:Two changes from eb2e2309:
SPV_KHR_terminate_invocation. Otherwise every unordered pixel shader would declare the extension.agc_driver_layered_volume_drawvalidates its mesh pipeline withoutshaderTerminateInvocationenabled, and rejects such a shader with "SPV_KHR_terminate_invocation requires enabled shaderTerminateInvocation in a fragment shader".Tests:
orderedPixelShaderTests(agc_driver_graphics) now expects these exits:SPV_KHR_terminate_invocationOpTerminateInvocationin the module, in the entry pointOpKillin the entry point, plus the shader's ownValidateShaderswithoutshaderTerminateInvocation#6416 also edits
StopBdaInvocationIf. Its write masking applies to every stage except pixel shaders, and this PR changes only pixel shaders, so the two compose. They merge without conflicts.Tested
main's code:agc_driver_graphicsfails with "AGC graphics: a pixel shader with BDA reads and SPV_KHR_terminate_invocation did not end its BDA faults through one OpTerminateInvocation in its entry point (5 in the module)".agc_driver_graphics,agc_driver_layered_volume_drawandagc_driver_gpu_buffer_formatamong them, pass 10 of 10 runs on both runners. Two of them skip on the Windows runner, which has no Vulkan device.precise_waittiming test failed in this run. It also failed there onmain's code in an earlier run of the same tree, and it passes in the Linux mirror below.check_skipspasses.ANYPS5_ENABLE_SPIRV_TOOLS=ON, run as an unprivileged user):maintoo, and no test changes status againstmain195d0d08a8.guest_sce_net, whichmainshows too.e8b3198337onmain6efb7261bwith 13 other open PRs, built once as one tree (comment):agc_driver_gpu_buffer_format,agc_driver_graphicsandagc_driver_layered_volume_drawpass 3 of 3 runs;agc_*tests, 713 of 716 pass. LeandroLP23 reports that the 3 that fail (agc_driver_host_import_lifetime,agc_driver_quad_pixel_maskandagc_driver_layered_volume_vertex_no_gpl) fail the same way on unchangedmainthere.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)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T03:59:38Z.
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.
core/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/libs/prx/libSceAgcDriver/tests/Graphics.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvBdaFault.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppImported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T16:33:22Z.
AMD on Windows: the tests this PR names pass on an RDNA 3.5 GPU.
Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release.
main6efb7261bwith this PR (e8b319833) merged, together with #7296, #7446, #7445, #6708, #6748, #6785, #6706, #6613, #7449, #7391, #7489, #7484, #7514. They were built once, so the result is for the combination; each merge was clean.agc_driver_gpu_buffer_format,agc_driver_graphics,agc_driver_layered_volume_draw: pass, 3 runs of 3.agc_*test on the combined build: 713 of 716 pass. The 3 that fail,agc_driver_host_import_lifetime,agc_driver_quad_pixel_maskandagc_driver_layered_volume_vertex_no_gpl, fail the same way on unchangedmainhere, so none comes from these PRs.AI-assisted
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-18T16:39:22Z.
@LeandroLP23 thanks for running them on the Radeon 860M. Your run is now in the Tested section, with your note that the three
agc_*failures fail the same way on unchangedmainthere.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-18T20:38:27Z.
A reproduction of the crash on a discrete RDNA 2 card, red on
mainand green with this PR. RX 6700 XT (gfx1031, poonasor's model), AMD Windows driver 32.0.21045.5002 (the 21045 branch of Cleverdee's 32.0.21045.11001), Windows 11 26200, WinLibs GCC 15.2.0 r7, Release.The test,
agc_driver_pixel_bda_stops: a pixel shader that does N chainedglobal_load_dwordreads of mapped guest memory and exports the result, drawn once over mapped memory (all pixels must come out white) and once with the reads pointed past it (the draw must report the failed read and leave the target unchanged). It also counts the module's invocation exits. Each run below is a fresh renamed copy of the test executable, so AMD's per-executable pipeline cache starts empty, withANYPS5_NO_SHADER_CACHE=1. Two runs per row:mainc4c2b3aa7main+ this PR (e8b319833)FATAL: unhandled exception 0xc0000005,rip amdvlk64.dll+0x9e251fOn
mainthe process dies in the first draw, atamdvlk64.dll+0x9e251f, the offset in both reports above. With this PR the module ends the invocation in 2 places, the one BDA exit and the valid-mask kill, and every draw is correct.With this PR, the three tests the description names and this one pass 3 of 3 runs each under ctest on this GPU:
agc_driver_graphics,agc_driver_gpu_buffer_format,agc_driver_layered_volume_drawandagc_driver_pixel_bda_stops. The first, cold run of the 160-read pipeline took 167 s here, while parallel builds kept the CPU busy; the later runs, served from the driver cache, took about 5 s. I haven't timed it on an idle machine.The test is on my fork as one commit on top of this PR's head:
thuanquang/AnyPS5@d95a7903a(tests/execution/PixelBdaStops.cpp, 160 reads). Feel free to take it into this PR if you want an execution test of the crash. On a device that compiles the per-read form, it still fails onmain, at the exit count.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T00:13:16Z.
AMD hardware result, which the PR lists as not tested. Tested at
e8b319833on Windows 11 (GCC 15.2.0, Release), merged ontomain9f25d0bf5, on an AMD Radeon iGPU, RDNA2 (1002:13c0, 32.0.21042.62,ANYPS5_GPU=AMD) and an RTX 5070 Ti (617.42).agc_driver_graphics,agc_driver_layered_volume_drawandagc_driver_gpu_buffer_formatpass on both.agc_|shader|recompilerset, no test changes status againstmainon either GPU.main,agc_driver_graphicsfails on both GPUs with... did not end its BDA faults through one OpTerminateInvocation in its entry point (5 in the module).I didn't reproduce the amdvlk crash itself; that needs the titles' large shaders.
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T02:19:07Z.
Review: COMMENTED
Apple GPU / MoltenVK run. One regression on Metal:
agc_driver_stencil_ref_export_unusedfails with this PR and passes without it.Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Head
e8b319833, PR diff applied cleanly on top of the tree below. Drivers: MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704.Tested: the tree is
maina373e85e5+ mugurc's macOS series (ci/macos-series-2, includes ImportWatched) + the PRs needed to build on macOS (#6667, #6668, #7524, #6969, and two files of #7332), not plainmain(it doesn't build on macOS yet). Base on that tree: 40^agcfailures with MoltenVK 1.4.2, the same known failures in every run below unless noted.agc_driver_graphics,agc_driver_gpu_buffer_format,agc_driver_layered_volume_drawpass (the mesh case skips: noVK_EXT_mesh_shader).PixelBdaStops.cpp(d95a7903a) passes:160 chained reads, 2 invocation exits. Without the PR (exit-count check disabled) Metal also compiles the 161-exit module and both draws are correct, so Apple doesn't have the amdvlk crash.^agcsuite: 40 of 728 fail, as on the base, but one of them is new:agc_driver_stencil_ref_export_unusedfails 5 of 5 runs with the PR on MoltenVK 1.4.2 and also on MoltenVK main + #2704, and passes on the base tree. (The count matches only becauseagc_driver_cross_workgroup_flag, intermittent on Apple, passed in this run.)MoltenVK rejects the fragment shader:
The guest code is now its own function (
%22), andgl_FragStencilRefARB(BuiltIn FragStencilRefEXT,OpVariable %_ptr_Output_int) is stored there. In this test the pipeline doesn't export stencil, so SPIRV-Cross makes the builtin a localint gl_FragStencilRefARB;inmain0. It still declares the helper's parameter with the builtin's MSL type,thread uint&, so the call doesn't compile. Before this PR the store stayed in the entry point and no parameter was needed.agc_driver_stencil_ref_export, where the stencil output is live, passes.That's a SPIRV-Cross issue, but this PR is what exposes it. Possible ways around it: declare the stencil reference output as a 32-bit uint (FragStencilRefEXT takes a scalar 32-bit integer), or keep the per-site form for pixel shaders that write
FragStencilRefEXT, as for primitive-ordered ones. I haven't tried either. The MSL and SPIR-V dumps are available if useful.GPUs tested: Apple M2 Max (MoltenVK 1.4.2; MoltenVK main + SPIRV-Cross #2704 for the stencil failure)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.