perf(agc): release unused registered shader resource plans #6178
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6178"
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 MarcZX10 on 2026-10-15T00:33:06Z.
What
Fixes the resource-plan retention reported in #6139. After registered shader preparation completes, release its cached plan when it has not been captured for runtime use and disk-plan caching is enabled. The plan is stored under the existing request cache key, so the first runtime use can reload it. Runtime capture marks the plan as used, and each capture retains its own shared plan so it remains valid across a reload.
When a released plan misses the disk cache, the recompiler must rebuild the resource plan. If the source already has a compiled variant for the exact binding layout, this rebuild no longer leaves a full IR program resident. If no matching variant exists, the IR remains available for compiling that layout.
Tested
mainata08de6827f217115078660b9f47201edae59668b.BUILD_TESTING=ON.agc_shader_disk_cache_testsand ranctest --test-dir build --output-on-failure -R '^agc_shader_disk_cache$': passed (1/1).a plan reload retained full IR; with the guard it passes.python3 tools/check_conventions.py --base upstream/main --head HEADandgit diff --check upstream/main...HEADpass.templatedisambiguator in an unrelated existing source file, then removed it; it is not part of this pull request. No GPU, Windows, or console test was run.Checklist
main; no other open PR implements the same functionsImported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-15T01:26:26Z.
@MarcZX100 I had a local fix for the same holder from #6139 before I saw this PR, so I won't open a second one. It takes a different route, and the numbers may help you decide.
Mine never loads the plan at registration when the variant comes from the disk cache. The first capture loads or builds it (
ensurePlanunder the entry's lock), and a cold run builds it at registration as main does today. On Demon's Souls (PPSA01341), warm cache, 240 s on mainab9a47684, plan loads went from 3971 to 141 and peak RSS from 15.3 GB to 13.5 GB. Cold was 9.7 GB vs 9.6 GB. The branch is fix/agc-lazy-resource-plans on my fork (b77092a42), with a red/green mode inagc_shader_disk_cache.One thing I noticed reading this PR:
StorePlanonly enqueues the write, andLoadPlanreads the file alone. A plan released before its write lands can't be reloaded, so the first capture rebuilds the front end and stores it again. The test callsShaderDiskCache::Flush()before the reload, so it wouldn't see this. I'm running Demon's Souls cold and warm on this PR now and will add the plan counters and RSS here.Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-15T07:47:55Z.
Confirmed a retained-full-IR consequence of the fallback @fma-rc identified, on head
9f00bfa3d8945d128a711f0b0df0240f4e549c74against its exact base.Using a small synthetic buffer shader and unmodified production recompiler, I ran
PrepareShader -> ReleaseUndrawnResourcePlan -> prepared Capture -> Materialize -> PrepareShader -> Flush. When the released plan's file is missing, corrupt, or unwritable,getSourcerebuilds bothsource->planandsource->program. The existing compiled artifact satisfies materialization and the repeated prepare, so nothing consumes or resets the rebuilt full IR.The retained fixture has two blocks/seven instructions alongside its eight-node plan, surviving every phase above. All sixteen paired materializations preserve output and prepared artifact identity. Cache-off, disk-plans-off, active-capture and failed-capture controls pass; the existing
--resource-plan-codecsuite also passes.The fallback needs to release the rebuilt full IR when an existing artifact serves the request. These are CPU-only lifetime measurements; no whole-title memory or FPS effect is claimed.
AI-assisted: yes (Codex)
Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-15T08:13:17Z.
Prepared a small follow-up for this head: f1b42dae46, available to cherry-pick. It avoids keeping newly rebuilt full IR when an existing artifact serves the request, including compatible relocated push layouts. It preserves IR waiting for a new layout and validates the prepared source handle before suppressing retention.
All eight lifetime scenarios now pass, including missing/corrupt/unwritable plan storage; their materialized outputs and source/artifact identities match the base. The new CPU regression fails on this PR's original head and passes with the fix; the existing resource-plan codec suite passes. Selective controls also catch dropping pending new-layout IR, omitting the relocated-layout hint, and skipping its validation.
Test host: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100. This validation is CPU-only, with no GPU/title test or whole-title RAM/FPS claim. No duplicate PR opened.
AI-assisted: yes
Co-Authored-By: Codex [email protected]
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T09:56:23Z.
Thanks for identifying and validating the cache-miss retention case. I rebased #6178 onto current
mainand pushed a fix inbb4a178f. When rebuilding a missing plan, it now drops the rebuilt IR if a compiled variant already covers that exact binding layout; it still retains IR when the requested layout needs compiling.I added a red/green regression to
agc_shader_disk_cache_tests: it fails on the rebased PR without the guard and passes with it. The updated PR description records the Linux test run and the temporary GCC 12 compatibility workaround used only in an unrelated source file. No GPU or title result is claimed.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:16:09Z.
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/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/Recompiler.cppcore/shader/recompiler/Recompiler.cppcore/shader/recompiler/Recompiler.cppcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/Optimization/include/Optimization/ResourceProgram.hppcore/shader/recompiler/Recompiler.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/Recompiler.cppcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/Optimization/include/Optimization/ResourceProgram.hppcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/Recompiler.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/Recompiler.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/Optimization/include/Optimization/ResourceProgram.hppcore/shader/recompiler/Recompiler.cppImported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-15T13:32:17Z.
@MarcZX100 Thanks,
c00b0dfb9fixes the exact-layout case: the eight lifetime scenarios from my earlier comment all pass on it when the draw layout equals the registration layout. One case is still open, a prepared capture with a relocated push layout.MatchesPreparedShaderlets a prepared artifact serve a request whose push offset and size differ from the artifact's.materializeDrawStagebuilds{0, 0, pushOffset, 128 - pushOffset}, while registration uses{0, 0, 0, 128}, so without graphics pipeline libraries a fragment stage after a vertex stage with push data gets a relocated layout. If that capture reloads a released plan whose entry is missing, corrupt or unwritable,getSourcefinds no variant withsameLayoutand keeps the rebuiltsource->program. The prepared artifact then serves the draw, so nothing consumes or resets that IR.Results on
c00b0dfb9(CPU-only, synthetic compute fixture; retention probed withResourcePrograms()when preparing a new layout afterwards):PreparedShaderInvocation::CaptureCaptureResources(request, runtime, handle)Minimal red test, placed after your regression in
verifyPlanRoundTrip. Onc00b0dfb9,--resource-plan-codecstops witha plan reload retained full IR after the prepared artifact served a relocated push layout.Test diff
A fix: pass the prepared handle from both capture paths through
acquirePlanintogetSource, and skip keeping the rebuilt IR when that handle's artifact serves the request. The handle is checked against the source and request before the entry changes. On top of your guard this gives exactly theRecompiler.cppfrom f1b42dae4. Both commits are on fdfawef324:claude/pr6178-relocated-layout (aaaebc3d5 test, a376d0155 fix), ready to cherry-pick. With the fix, all 24 runs above,--resource-plan-codec, the fullagc_shader_disk_cache_testsrun and the conventions check pass.There was no GPU or title run. The cost is memory held by the retained IR; output doesn't change.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-16T02:52:21Z.
A title measurement, since the description has none: Crimson Desert (PPSA25286, 01.000.568). It compiles about 12,000 to 13,000 shaders in its first five minutes and was one of the titles growing host memory in #6139.
Setup: Linux, RTX 4070 Ti, NVIDIA 595.99. Both builds are
main5d9dcc42d+ #6612 + #6613, which Crimson Desert needs to get past its first stops; the second addsc00b0dfb9. Cold shader cache, 300 s with scripted pad input, a 14 GiB memory cap, and/proc/<pid>/statussampled every second. One run each.RssAnonat 120 sRssAnonat 240 sRssAnonat 298 sRssAnonper compiled shader at 298 sVmHWMVmRSSandVmHWMinclude the mmapped shader cache files andmemfdguest memory, so they sit near the cap either way.RssAnonis the part that grew without bound in my earlier runs, which ended at the 14 and 16 GiB caps. With the PR it grows about a third slower per compiled shader, and the 116 plan hits look like the reloads after release. Neither run drew a frame within the 300 s (the title is still compiling), so this shows the memory only, not rendering.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T15:32:34Z.
I reviewed the proposed relocated-layout regression and fix, then carried the two commits onto this PR while preserving fdfawef324 as the author. Locally,
agc_shader_disk_cache_testsbuilds and passes on Linux/GCC 12.2, including the new reload path. The build needed a temporary GCC 12 compatibility edit in unrelatedSrtDescriptorEvaluation.cpp; I reverted it before pushing. No GPU/title test was run.