perf(agc): resolve static shader ABIs on the background pool #2589
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2589"
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 gjonn on 2026-10-10T07:12:59Z.
What
Rebased onto current
mainas one commit. #2583 has landed, so its commit is gone from this branch.Driver::ResolveShaderAbifor compute and pixel shaders (types 0 and 1) compiles each new ABI variant on the guest thread that calls it. Titles resolve hundreds of these in a row from a loader thread throughsceAgcCreateInterpolantMapping, so that thread spends its time compiling. Since #3159, the compile no longer runs under the preparation writer lock. It still runs on the calling thread, one variant after another.With this change,
ResolveShaderAbidoes the following instead:PlanRegisteredon the calling thread, so register and validation errors are still reported by the call. It then records the key inpendingAbis, queues the compile on the registration preparation pool as anAbiPreparation, and returns.Waiting for ABI jobs:
SourceHandleFor,InvocationFor) and transactionRead/Editwait for the snapshot's unfinished ABI jobs (abiPreparations).Read/Editfinish these jobs before they take the preparation writer lock, so no thread holds that lock while it waits on the pool.A failed ABI job does not reach the guest:
failedAbis, so later resolutions of that key return at once.APS5_SYNC_SHADER_PREPARE=1keeps the synchronous path.ResolveGraphicsStagesAbiand the rectangle-list path are unchanged.docs/dev/ARCHITECTURE.mddescribes the background ABI resolution and the job list.Tested
Unit tests. New cases in
agc_driver_prepared_shaders:--background-abi, run asagc_driver_prepared_shaders_background_abi(default) and_background_abi_sync. It registers a pixel shader whose code fails only once it is built for its ABI.ResolveShaderAbireturns, and repeated calls for 500 ms never throw.main, the default case fails:ResolveShaderAbithrowsbranch at program counter 0x00000000 targets invalid program counter 0x00000008into the caller. This was checked by building the same test againstmainc13dc7e6bon Linux.QueuedAbiPreparation, in the main run:SourceHandleFor/InvocationFor, and its artifact is reused.failedAbis. It leaves the snapshot readable, and the dispatch prepares at use instead of rethrowing.Results:
^agc_CTest suite, 452/452 passed (1 skipped).agc_driver_prepared_shadersin all modes passed, including both new ones.Synthetic before/after on Linux. Same machine and build flags;
mainc13dc7e6bagainst this PR, alternating runs, 5 each. Ubuntu in WSL2, GCC 15.2, Release,ANYPS5_ENABLE_SPIRV_TOOLS=OFF, Mesa lavapipe, 24 threads. The harness source is in the review reply below.The harness does the following:
0x1b6= 1), so registration does not compile them.mainloader (s)maintotal (s)Every PR run finished before every
mainrun.The earlier Linux slowdown (PPSA11386). It was measured on the previous version of this commit. That version had two places where waits got longer:
Read/Edittook the global preparation writer lock first, then waited for the snapshot's in-flight jobs. An ABI job queued behind a registration burst kept every other registering thread blocked until the pool reached it.This version waits before it takes the lock and runs a job that is still queued on the waiting thread. I have not re-run that title. @SleglJan, if you can repeat your series on this head, that would confirm it.
Not tested here: a performance trace of a title scene in the form the template asks for.
agc_drivercrash in default mode. It does not reproduce on currentmainor on this head:ctest -R '^agc_driver$': 0/30 on this PR and 0/30 onmainc13dc7e6bon Linux (WSL, lavapipe). 0/30 on this PR on Windows (RTX 5090).mainalone, also 0/480 in parallel runs and 0/96 under ASan.The likely cause was the one #2976 (
2dd453dae) fixed:Driver::stopreset the device without waiting for the registration pool.SpirvTargetextension names at exit.#2976 merged after the first review's runs, and this branch is based on it.
CloseRegistrationPreparationdrains ABI jobs as well, because they run on the same pool.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)sceAgcCreateInterpolantMapping, but I have not run that title.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T08:33:15Z.
Review: CHANGES_REQUESTED
Thanks. I rebased only this PR's commit onto current
main(#2583 landed with a different commit, so it applies cleanly) and ran the libSceAgcDriver tests. Findings:agc_driversegfaults intermittently in the default (background) mode: 10/30 runs on this PR, 0/30 withAPS5_SYNC_SHADER_PREPARE=1. Plainmain(#2583) does the same, 8/30 default and 0/30 sync, so this PR didn't introduce it. But it adds more concurrent preparation (pendingAbis,abiFailures, count of in-flight jobs) on top of a crash we can't yet explain, so we can't merge it until the default mode is stable. Could you find the cause (a backtrace ofagc_driver_testsin default mode would be enough to start)? A loop likefor i in $(seq 30); do ctest -R '^agc_driver$'; donereproduces it for us.PreparedShaders.cpponly compile against the new members, so they don't show the behaviour differs frommain. Please add one that fails onmainfor observable behaviour (e.g.ResolveShaderAbireturning before the failure is raised).Please also add a short paragraph to
docs/dev/ARCHITECTURE.md(shader section) covering the background ABI resolution and the in-flight job count.Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-10T08:53:38Z.
A data point on the failure handling here: some titles call
ResolveShaderAbifrom game code, and re-raising a stored failure there ends the title.On LEGO Horizon Adventures (PPSA14632), the call comes from
sceAgcCreateInterpolantMappingon a game thread (CreateInterpolantMapping→Driver::ResolveShaderAbi). Onmain6cc33583, one pixel shader there fails the recompile with #2548'spacked pixel ancillary input has an unsupported live use (used by SelectU32 ? ?). The exception goes up through libSceAgc into the game andterminateruns, about 5 s after boot (stack in #2548).With this PR, if I read it right, the first call queues the job and returns, but a later
ResolveShaderAbicall for the same key rethrows the stored error, which reaches the same caller. Would it make sense for a failed ABI job to behave like a deferred registration instead (logged, then prepared or refused at the draw that uses it)? Locally, marking the snapshotdeferredand recording the key on failure gets LEGO past that shader.Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-10T10:04:48Z.
Additional platform validation on standalone PR head
4d8dd9dbbc9f6dc5c66b026cf1a8c2614dba2fe6:Linux: passed — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja.
Release /
BUILD_TESTING=ON: built the selected test targets; 6 CTest cases, 0 failures, 0 CTest skips.Tests:
agc_driver_prepared_shaders,agc_driver_prepared_shaders_indirect,agc_driver_prepared_shaders_uncached,agc_driver_prepared_shaders_debug,agc_driver_prepared_shaders_debug_failure,agc_driver_prepared_shaders_deferred.Reproduce after building the test targets:
ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^agc_driver_prepared_shaders".Vulkan: Mesa lavapipe 26.2.4 software Vulkan; no native Linux GPU/retail-title validation.
Integration limitation: a local merge against main
948a526c2has source conflicts; these results validate the standalone PR head, not a resolved merge.Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T10:43:17Z.
Data from 007 First Light (PPSA11386 v01.018.000) on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ryzen 9 7950X3D, RTX 3080 with NVIDIA 615.71.09), Release with
ANYPS5_ENABLE_SPIRV_TOOLS=OFF. This measures something different from the description (PPSA01317 menu load, warm caches): 007 presents no frame yet, so it covers only start-up to its first flip.This head does not merge with current
main, so I measured its last commit, 4d8dd9db, cherry-picked ontomain3d1a7d90, which has #2583 as merged. Onlytests/execution/PreparedShaders.cppconflicted; it stays atmain's version.ShaderRegistryhas not changed onmainsince. Both builds also carry the changes listed in #2523 that get 007 to its first flip, including that issue's local experiment raisingShaderInfo::MaxImagesfrom 64 to 128 (row 13).007 currently stops while presenting that flip, on a bindless image table limit. The measure is therefore the process lifetime from exec to that stop. It covers boot, the shader registration burst and the first draws (40 to 46 traced per run). Runs alternated between the two builds.
main+ stackmain+ stack* This run overlapped a git checkout and a CMake configure on the same machine; without it the median is 26.1.
main17d69d17, #2583 at 7fdcc34e and both of that issue's local experiments.MaxImagesexperiment, 32 of 007's shaders fail at registration withshader image heap capacity exceeded.could not be prepared after registrationwithout this commit,could not be prepared in the backgroundwith it. Both builds continue to the flip.APS5_SYNC_SHADER_PREPARE=1(two runs with this commit), 007 stops on the first of them, at the 733rd shader-cache miss.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T09:42:14Z.
Review: CHANGES_REQUESTED
Thanks for the work here. Since there was no push after our last review, we looked at the new data in the thread. Still
changes:main(007 First Light, PPSA11386, RTX 3080, alternating runs): median 27.5 s → 29.8 s cold cache and 24.6 s → 26.5 s warm; every run with the commit is slower than every run without it. The Windows/RTX 5090 numbers in the description come from a local build that is not this branch. Please add a before/after that shows a gain on Linux, or explain where the extra time goes.ResolveShaderAbicall for the same key re-raises the background failure into game code (sceAgcCreateInterpolantMapping), which ends the title wheremainwould not have reached the call yet. A failed ABI job should behave like a deferred registration (logged, then prepared or refused at the draw that uses it).agc_driversegfault in default mode is still unexplained (10/30 here, 8/30 onmain, 0/30 withAPS5_SYNC_SHADER_PREPARE=1). A backtrace ofagc_driver_testsin default mode would be a good start; this PR adds more concurrent preparation on top of it.PreparedShaders.cpponly compile against the new members; add one that fails onmainfor observable behaviour.docs/dev/ARCHITECTURE.md(shader section) needs a paragraph on the background ABI resolution and the in-flight job count.main(ShaderRegistry.cpp,PreparedShaders.cpp): #2583 landed with a different commit, so please rebase onto it and drop its commit from this branch.Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-11T12:56:13Z.
Hi! Since #2583 merged, this PR's stacked base is gone from its diff and it now conflicts with
main, so I rebased the new commit (perf(agc): resolve static shader ABIs on the background pool) onto currentmainas a single commit — #2583's original commit dropped, sincemaintook it as6db5c1369.The rebase needed care because
mainevolvedShaderRegistry.cppsubstantially after #2583 (ReportPreparedAtUse,SubmitRegistrationPreparation, fused hull headers, bit-word ABI walks). The merged result keeps main's newer structure and adds this PR's behavior on top:abiFailuresrethrow inSourceHandleFor/InvocationForis kept, guarded to the original reachability (a settled, non-deferred snapshot with no matching entry) — main's newer prepare-at-use path handles everything else;PrepareRegisteredInBackgroundkeeps main'sSubmitRegistrationPreparationhelper with this PR's addedregistrationparameter and the pending/abi bookkeeping;pendingAbis/abiFailuresfields, the duplicate-entry suppression and the asyncResolveShaderAbipath applied as in the PR.It builds;
agc_driver_prepared_shaders_testspasses (includingBackgroundAbiResolution); full suite 832/832 on Linux (AMD RX 9070 XT / RADV);tools/check_conventions.py --base origin/mainpasses.The rebased branch is available to pull:
Feel free to ignore this if you'd rather rebase yourself.
Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-13T03:38:48Z.
Thanks for the reviews and the data. I pushed a reworked version,
2f9697bb5, as one commit onmainc13dc7e6b. Per point:Rebase. Done. #2583's commit is dropped. @blawesom, thanks for the rebase.
mainhad moved about 1,300 commits past it (including #3159, which already moves the ABI compile out of the writer lock), so I rebuilt on currentmain.Stored failure reaching the game (@alfalfa-star's report). A failed ABI job is no longer rethrown from
ResolveShaderAbi. It is logged and its key is marked failed, so later resolutions of that key return at once. The draw or dispatch that needs that state prepares it at use, throughmain's existing prepare-at-use path, and fails there if the shader cannot be prepared. This is the same handling as a deferred registration.Slower on Linux (@SleglJan's series). The old version had two waits that could run long:
Read/Edittook the global writer lock, then waited for in-flight jobs that were queued behind a registration burst. That blocked every other registering thread.Now
Read/Editfinish the snapshot's ABI jobs before taking the lock, and a waiting thread runs a job the pool has not started. A synthetic before/after on Linux is in the description (maintotal 4.39 s → 1.89 s median, every PR run faster than everymainrun). @SleglJan, if you get a chance to repeat your PPSA11386 series on this head, I'd like to know whether the gap is gone.agc_driversegfault. It does not reproduce on currentmainor on this head:mainalone, also 0/480 in parallel runs and 0/96 under ASan, plus 0/90 on9a1f6f44a, themainat your second review.The likely cause is the one #2976 (
2dd453dae) fixed: the device was reset at shutdown while registration-pool jobs still read itsSpirvTarget. That fix merged after your first measurement. ABI jobs run on the same pool, soCloseRegistrationPreparationdrains them too. I couldn't verify on RADV. If it still shows up there, a backtrace would point me at it.A test that fails on
main.agc_driver_prepared_shaders_background_abiregisters a pixel shader that fails only once it is built for its ABI, and expectsResolveShaderAbito return. Onmainit throwstargets invalid program counterinto the caller; I checked this by building the test againstmainon Linux._background_abi_synckeeps the old throwing behaviour underAPS5_SYNC_SHADER_PREPARE=1.ARCHITECTURE.md. Added a paragraph in the shader section.Still missing: a performance trace of a title scene in the template's form. I have not captured one for this head yet.
Benchmark harness (built as a local test executable linked like
agc_driver_prepared_shaders_tests; arguments: pixels computes length work_us)Run as
ANYPS5_NO_SHADER_CACHE=1 abi_resolve_bench 96 192 600 0and... 96 192 600 20000, alternating themainand PR builds.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T09:29:49Z.
Review: CHANGES_REQUESTED
Thanks for the rework. Checked on the current head (merged onto main): build,
agc_driver_prepared_shaders*including_background_abiand_background_abi_sync(12/12 pass), andagc_driverplus the prepared-shader tests repeated 40 times with no failure. The design reads sound: ABI jobs never take the preparation lock, andRead/Editwait before taking it.What is still open:
2f9697bb5); a gain or no change is enough to continue.tests/execution/PreparedShaders.cppnear line 669; one of the two will need a rebase after the other lands.Once the real-title number is in, this is ready for a final pass.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T06:05:29Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cppL675Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-14T10:53:01Z.
@gjonn @adriadam10 Repeated on
2f9697bb5with 007 First Light (PPSA11386 v01.018.000) on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ryzen 9 7950X3D, RTX 3080 with NVIDIA 615.71.09), Release withANYPS5_ENABLE_SPIRV_TOOLS=OFF.Result, warm shader cache, 6 runs each: with
2f9697bb5the first presented frame comes at about the same time (6.4 against 6.5 s, ranges overlap), frame 169 comes 0.5 s later, and from frame 300 to 1000 007 renders about 3% slower (2.21 against 2.27 FPS).On
main007 stops about 2 s after start, before any draw (#5714), so both builds are my integration branchshare/007-v5at 65c5b68f3 (main34355eeeaplus the open PRs and local changes listed in #5714), with and without2f9697bb5cherry-picked. The binaries were built from a local commit whose tree differs from 65c5b68f3 only in one code comment. Only the prepared-shader test list inlibSceAgcDriver/CMakeLists.txtconflicted; it takes both sides' entries.34355eeeaincludes #4404, which this PR's base does not;ShaderRegistry.cppmerged without conflict. Both builds have the sameShaderCacheVersion.On this branch 007 gets past the stop of the earlier series (its first flip), so the measure changed, and the earlier 1.5 to 2.3 s gap does not compare directly. The measure is the time from exec to the first presented frame and to frames 169, 724 and 1000, back-dated from the window-title FPS counter. No input is given, so none of these frames is gameplay. By frame 169 most shader preparation is done: the first 10-second shader cache report, before frame 125 in every run, has 2,091 of the run's 2,193 to 2,199 lookups, and about 100 more follow by frame 200. From about frame 230 007 renders at mostly 2.0 to 2.5 FPS, so frames 724 and 1000 measure draw throughput after that. Runs alternated between the builds (BAAB).
Only warm runs: a cold boot on this branch takes about 13 minutes, most of it NVIDIA compiling a few compute pipelines of up to 4.3 million SPIR-V words on the queue worker. Every run starts from a fresh copy of the same shader cache (the recompiler cache plus the Vulkan pipeline cache), so shader preparations, this PR's ABI jobs included, are cache hits (no miss in 11 of the 12 runs, 6 in the other). The recompile work this PR moves to the background pool is therefore not exercised here. The sandbox gives the driver an empty home, so NVIDIA's own disk cache is empty in every run. Switches:
APS5_AHEAD_PIPELINES=2(a local 2-thread pool that creates compute pipelines ahead of queued dispatches),APS5_SPARSE_RUN_IMPORT=1,APS5_NO_IDLE_STORE=1,APS5_HOST_IMPORT_MIB=12288,APS5_RESOURCE_CACHE_ENTRIES=64,APS5_NO_DRAW_SRT_ENTRIES=1,APS5_PIPELINED_DRAWS=8,APS5_LOCAL_LARGE_CLASSES=1; no trace or profile switches.share/007-v52f9697bb5Medians in seconds (FPS in the last column), ranges in parentheses. Counts below are of the 36 pairs of a run without the commit and a run with it.
perf record -e cycles:u) of one more run of each build, from frame 400, shows the same split by library (libSceAgcDriver24.9% and 24.6% of samples). No driver or recompiler function differs by more than 0.1 points,ResolveShaderAbiis under 0.01% of samples in both builds, andAwaitAbiPreparationsunder 0.01% with the commit. It does not count waiting time, and that pair did not show the gap: frame 1000 at 383.0 and 384.2 s, frames 724 to 1000 in 123.5 and 123.7 s.Each run (seconds, in run order)
2f9697bb5share/007-v5share/007-v52f9697bb52f9697bb5share/007-v5share/007-v52f9697bb52f9697bb5share/007-v5share/007-v52f9697bb5AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-14T13:14:14Z.
Thanks @SleglJan for repeating the series, and @adriadam10 for the reviews.
The bar was a gain or no change on a real title, and this head misses it. On PPSA11386 it is about 3% slower in steady rendering, and frame 169 arrives 0.5 s later. The run without the commit wins 31 to 34 of the 36 pairs.
The profile shows no extra CPU work, so the cost is waiting. My reading is this: since #3159,
mainno longer prepares the ABI under the writer lock. A title that callsResolveShaderAbiwell ahead of the draw already prepares off the critical path. This PR moves that work onto the pool, and the draw that needs it then waits for it or runs it. It only helps when the thread callingResolveShaderAbiis itself the bottleneck, and that does not hold generally.Closing. The test for a failure raised in the background (
agc_driver_prepared_shaders_background_abi) is tied to this design, so it goes too.AI-assisted: yes
Pull request closed