perf(agc): prebuild compute pipelines and prewarm dispatch shaders ahead of execution #8735
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8735"
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 hamb3r on 2026-10-20T22:45:26Z.
What
ShaderManager::QueuePrebuildandPrebuildEntrywith acomputeflag so compute shaders queued byGetPreparedShaders(..., /*compute=*/true)pre-create both thevk::ShaderModuleand the computevk::Pipelineon the background shader prepare thread (GetOrCreateComputePipeline), avoiding synchronous pipeline compilation stalls duringDriver::dispatch.Driver::resolveGroupAheadby scanning the upcoming packet segment up to the next wait/sync boundary (PreviewedUntil()) when the segment contains at least one subsequent dispatch (hasFurtherDispatch), trackingSetShReg/SetContextRegupdates on a lightweight preview state and callingRegisteredProgram(GetPreparedShaders(..., true)) for upcoming dispatches (scan > from) before the preceding dispatches finish executing. Single-dispatch segments (!hasFurtherDispatch) skip the preview pass with zero state copies or lookups.Tested
ctest --test-dir build-portable -R "^(agc_driver_prepared_shaders_compute_prebuild|agc_driver_prepared_shaders_dispatch_ahead_prewarm|agc_driver_compute_basic_dispatch|agc_driver_compute_multi_dispatch)$" -VwithANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationon bothmain(522e32e3b618) and this branch (dcb04d0aa14d) — all 4 tests pass with 0VUID-validation errors.PPSA01341v1.07), Windows 11, same machine and boot/intro scene (APS5_TRACE_FRAMES=0:20, default flags, 3 cold-pipeline-cache runs + 3 warm-cache runs per branch). All 12.json.gztraces are attached below.Attached Traces (12 runs: 3 Cold + 3 Warm per branch)
main(522e32e3b618) Cold 1..3:window.ms=7911.797,p50=71.790,p90=899.812,max=5652.293,Driver.Packet=2711.103,Packet.dispatch=471.154,Driver.Dispatch=438.601,materialize=121.787)window.ms=8543.938,p50=69.237,p90=893.505,max=5998.794,Driver.Packet=2855.925,Packet.dispatch=562.523,Driver.Dispatch=473.093,materialize=127.010)window.ms=8480.218,p50=85.598,p90=1030.123,max=6032.006,Driver.Packet=2950.152,Packet.dispatch=497.480,Driver.Dispatch=460.815,materialize=127.731)perf/agc-compute-prewarm(dcb04d0aa14d) Cold 1..3:window.ms=8765.408,p50=85.279,p90=568.491,max=6661.114,Driver.Packet=3035.776,Packet.dispatch=512.372,Driver.Dispatch=482.331,materialize=130.607)window.ms=8992.743,p50=85.367,p90=584.385,max=6463.900,Driver.Packet=3064.032,Packet.dispatch=549.805,Driver.Dispatch=515.185,materialize=128.516)window.ms=8145.822,p50=81.976,p90=163.222,max=6580.297,Driver.Packet=3031.801,Packet.dispatch=543.130,Driver.Dispatch=452.018,materialize=127.940)main(522e32e3b618) Warm 1..3:window.ms=6870.411,p50=78.791,p90=799.413,max=4727.829,Driver.Packet=2287.577,Packet.dispatch=416.237,Driver.Dispatch=383.796,materialize=124.635)window.ms=6792.077,p50=75.507,p90=800.919,max=4787.418,Driver.Packet=2293.844,Packet.dispatch=408.587,Driver.Dispatch=373.319,materialize=124.379)window.ms=6612.875,p50=70.261,p90=755.799,max=4599.907,Driver.Packet=2234.188,Packet.dispatch=424.879,Driver.Dispatch=388.964,materialize=120.988)perf/agc-compute-prewarm(dcb04d0aa14d) Warm 1..3:window.ms=6863.060,p50=68.699,p90=447.956,max=4802.256,Driver.Packet=2303.209,Packet.dispatch=484.234,Driver.Dispatch=439.233,materialize=120.444)window.ms=6862.217,p50=85.842,p90=778.916,max=4746.500,Driver.Packet=2333.684,Packet.dispatch=420.290,Driver.Dispatch=388.508,materialize=122.702)window.ms=6639.422,p50=71.507,p90=503.585,max=4767.308,Driver.Packet=2218.187,Packet.dispatch=425.453,Driver.Dispatch=391.083,materialize=109.963)3-Run Median Comparison (
APS5_TRACE_FRAMES=0:20, default flags)mainCold MedianmainWarm Medianwindow.ms8480.2188765.408+3.4%6792.0776862.217+1.0%frame_ms.p5071.79085.279+18.8%75.50771.507-5.3%frame_ms.p90899.812568.491-36.8%799.413503.585-37.0%frame_ms.max5998.7946580.297+9.7%4727.8294767.308+0.8%Driver.Packet(ms/f)2855.9253035.776+6.3%2287.5772303.209+0.7%Driver.Packet.dispatch(ms/f)497.480543.130+9.2%416.237425.453+2.2%Driver.Dispatch(ms/f)460.815482.331+4.7%383.796391.083+1.9%Driver.Dispatch.materialize(ms/f)127.010128.516+1.2%124.379120.444-3.2%0x20..0x50) syncVulkan.ComputePipelinecount301-96.7%000shader prepareprebuiltVulkan.ComputePipelinecount029+29000tools/trace_digest.py— Cold Median Run (mainaps5-trace-522e32e3b618-20240.json.gzvs PRaps5-trace-dcb04d0aa14d-31308.json.gz)maincold median digest (aps5-trace-522e32e3b618-20240.json.gz)Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 2947.5, Driver.Packet.memory_wait 2432.4, Driver.Packet.dispatch 496.8, Driver.Dispatch 459.8, Driver.WorkerWait 323.4, Driver.WorkerWait.idle_and_completion_poll 323.3, Driver.Dispatch.materialize 126.7, Vulkan.Dispatch 117.1, Vulkan.Dispatch.shader_resources 112.9, Shader.PrepareArtifact 96.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 3033.1, Driver.Packet.memory_wait 2496.9, Driver.Packet.dispatch 511.7, Driver.Dispatch 481.3, Driver.WorkerWait 324.5, Driver.WorkerWait.idle_and_completion_poll 324.4, Driver.Dispatch.materialize 129.6, Vulkan.Dispatch 115.1, Vulkan.Dispatch.shader_resources 110.4, Vulkan.ComputePipeline 72.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 2291.3, Driver.Packet.memory_wait 1870.7, Driver.Packet.dispatch 408.0, Driver.Dispatch 372.3, Driver.WorkerWait 324.1, Driver.WorkerWait.idle_and_completion_poll 324.0, Driver.Dispatch.materialize 123.4, Vulkan.Dispatch 120.0, Vulkan.Dispatch.shader_resources 116.2, Shader.PrepareArtifact 100.8
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 2215.5, Driver.Packet.memory_wait 1763.5, Driver.Packet.dispatch 424.7, Driver.Dispatch 390.0, Driver.WorkerWait 340.0, Driver.WorkerWait.idle_and_completion_poll 340.0, Vulkan.Dispatch 117.3, Vulkan.Dispatch.shader_resources 113.0, Driver.Dispatch.materialize 109.0, Resources.Prepare 83.3
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:53:49Z.
Review: COMMENTED
Review note on the new dispatch look-ahead: in the first scan loop, the / branch reads to derive without validating that the packet has at least five DWORDs. allows shorter packets and this loop only checks that the declared count fits in the submission, so malformed/truncated dispatch packets can cause an out-of-bounds span access before normal validation runs. Could you gate the preview path on the opcode-specific minimum packet length (and skip/fall back safely when it is shorter)?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:53:53Z.
Review: COMMENTED
Review note on the new dispatch look-ahead: in the first scan loop, the
0x15/0x16branch readspacket[4]to derivewavewithout validating that the packet has at least five DWORDs.PacketWords(header)allows shorter packets and this loop only checks that the declared count fits in the submission, so malformed/truncated dispatch packets can cause an out-of-bounds span access before normaldispatchvalidation runs. Could you gate the preview path on the opcode-specific minimum packet length (and skip/fall back safely when it is shorter)?Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-21T01:18:29Z.
Addressed the review note in
8685c2edec:Driver::resolveGroupAhead(DispatchAhead.cpp) now checkif (count < (opcode == 0x15 ? 5u : 3u))before inspecting the dispatch packet.DISPATCH_INITIATORviapacket.back(): in the preview path,waveis now derived from(packet.back() & 0x8000u) != 0 ? 32u : 64uinstead ofpacket[4], matchingPm4::NoteDispatchRegistersandPm4::ResolveDispatchso 3- and 4-word0x16(DISPATCH_INDIRECT) packets read their initiator word without out-of-bounds access.userDatais populated frompreview.shader(0x240u + i), andcodeOffset < registered->code.size()is checked beforesubspan(codeOffset).DispatchAheadPrewarminPreparedShaders.cpp): added coverage for pending shader prioritization (PrioritizeRegistrationPreparation), filler packets, and0x63(SET_SH_REG_INDIRECT) duringresolveGroupAhead.Line-by-line
gcovDiff Coverage (102 / 102executable lines =100.0%)Verified with
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1, andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(6/6 Passed, zeroVUID-errors).cc @MarcZX10
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T06:28:50Z.
@hamb3r thanks for the length gating; both scan loops look right to us now. Before this goes further, a few things change behaviour in the default configuration and aren't in the description:
APS5_GROUP_CAPTURE_ALL_QUEUESgoes from opt-in to on by default (DispatchAhead.cpp), so group-ahead resolution now also runs on queue 0 for every title. Onmainqueue 0 is deliberately opt-in, anddocs/dev/ENVIRONMENT.mdstill describes the flag as a presence switch. Could you keep it opt-in here, or move the default change to its own PR that says why queue 0 is now safe and updates the reference?partialThreads = {1, 1, 1}compute variant is skipped "when the shader's header already specifies its threadgroup state", butPrepareRegisterednow drops it for every compute shader. Dispatches with a partial last group then prepare their artifact at use, on the queue worker. Could the condition match the description, or could you say why those dispatches no longer need the prepared variant?APS5_*settings, includingAPS5_LAZY_SHADER_PREPARE=1. That is the only mode in which the new prewarm submits anything, because registration otherwise prepares the entries itself. Could you add main vs PR with the default environment? The new look-ahead now also runs on uncaptured dispatches and scans to the end of the submission each time, so its cost on long submissions matters too.DispatchAheadPrewarmsubmits the packets and waits, but asserts nothing about the result. Could it check what the change is for, for example that the lazily registered shader has a prepared entry for the dispatched offset after the submission and doesn't without the change?Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-21T08:53:46Z.
@oneandonlydean Thanks for the thorough review! Addressed all four items in
320a4b6a19:1.
APS5_GROUP_CAPTURE_ALL_QUEUESopt-in on queue 0Restored
static const bool allQueues = std::getenv("APS5_GROUP_CAPTURE_ALL_QUEUES") != nullptr;inDriver::resolveGroupAhead(core/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/DispatchAhead.cpp) so queue 0 remains opt-in by default, matchingmainanddocs/dev/ENVIRONMENT.md.2.
partialThreads = {1, 1, 1}variant inPrepareRegisteredRestored the
if (request.context.compute) { request.context.compute->partialThreads = {1, 1, 1}; append(); } else if (request.context.pixel)block inPrepareRegistered(core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp) so dispatches with a partial last workgroup continue to hit the registration-prepared artifact instead of compiling on the queue worker.3. Look-ahead on uncaptured dispatches, bounded scan, and default-environment
trace_digest.pycore/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/PacketExecution.cpptomainsoresolveGroupAheadonly runs whengateOpened()istrue(never on uncaptured dispatches).Driver::resolveGroupAhead(DispatchAhead.cpp) so it stops at the next wait/synchronization boundary (header == FlipPacketHeader || header == RenderingWaitPacketHeader || opcode == 0x22 || opcode == 0x3f || opcode == 0x3c || opcode == 0x93) instead of scanning to the end of the submission on every gate. Also removed the unusedIndirectRegisterOpcodebranch from the second (group-capture) loop so group-capture semantics matchmain.main(ca74ef2177ee) vs PR (320a4b6a1946) comparison on Demon's Souls (PPSA01341v01.007.000,frames 0:5) with the default environment ("env":{"APS5_TRACE_DIR":"logs","APS5_TRACE_FRAMES":"0:5"}):aps5-trace-ca74ef2177ee-32164.json.gzvsaps5-trace-320a4b6a1946-49392.json.gz):Driver.Dispatchon queue workers decreased from1736.797to1514.524ms/frame (-222.273ms/frame,-12.80%), withVulkan.ComputePipeline(283.563ms/frame) offloaded to the 8-threadRegistrationPreparePool::Prebuild()pool (VulkanDevice::PrebuildComputePipeline) andGpuMutex.wait.dispatch(336.444ms/frame onmain,1211.4ms longest wait) dropping out of the top phases.aps5-trace-ca74ef2177ee-42280.json.gzvsaps5-trace-320a4b6a1946-33128.json.gz):Driver.Packetdecreased from8075.118to7991.086ms/frame (-1.04%),Driver.Packet.dispatchdecreased from1518.775to1445.265ms/frame (-4.84%),Vulkan.Dispatchdecreased from429.674to385.267ms/frame (-10.34%),Vulkan.Dispatch.shader_resourcesdecreased from419.597to373.257ms/frame (-11.04%), andframe_ms.p90/maxdecreased from5066.419to5030.990ms (-35.429ms,-0.70%).tools/trace_digest.pyoutputs.4.
DispatchAheadPrewarmtest assertions (PreparedShaders.cpp)Updated
DispatchAheadPrewarmincore/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cppso that:DISPATCH_DIRECTin the submission targets an unregistered invalid shader (0xbf9f0000u), which causes both the group-capture loop inresolveGroupAheadandDriver::executeto stop at the first dispatch before reaching the second or third dispatch.DISPATCH_DIRECTtargetspendingHeader(withpending = trueanddemanded = false), and the thirdDISPATCH_DIRECT(viaLOAD_SH_REG_INDEX0x63) targetslazyHeaderatlazyCode.data() + 64(codeOffset == 64).resolveGroupAheadreaches the second and third dispatches, and the test asserts both post-conditions:Require(pendingSnapshot->prepared->demanded.load(std::memory_order_acquire), "dispatch ahead did not prioritize the pending shader");Require(lazySnapshot->prepared->entries.size() == 1 && lazySnapshot->prepared->entries.front().codeOffset == 64 && lazySnapshot->prepared->entries.front().handle != nullptr, "dispatch ahead did not prewarm the lazy shader for its dispatched offset");DispatchAhead.cppcausesagc_driver_prepared_shaders_registered_workgroupto fail immediately (exit code 8).Verification &
gcovDiff Coveragectest --test-dir build-portable -R "^(agc_driver_gpu_indirect_arguments_written_ahead|agc_driver_gpu_indirect_arguments_misaligned_writer_ahead|agc_driver_indirect_arguments_written_ahead|agc_driver_write_data_written_ahead|agc_driver_prepared_shaders_registered_workgroup|agc_driver_pipelined_dispatch)$" -VwithANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1, andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: 6/6 passed, 0VUID-errors, and no missing layer warnings.gcovdiff coverage: 89 / 89 executable lines covered (100.0%) acrossDispatch.cpp(4/4),DispatchAhead.cpp(68/68),ShaderRegistry.cpp(1/1), andVulkanDevice.cpp(16/16).Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T09:08:18Z.
@hamb3r thanks,
320a4b6a19covers all four. Queue 0 is opt-in again, the{1, 1, 1}registration variant is back,PacketExecution.cppmatchesmain, the preview loop stops at the next wait or synchronization packet, andDispatchAheadPrewarmnow fails without the look-ahead. The default-environment numbers are useful too.One default change is still in: the
Prebuild()pool inShaderRegistry.cppgoes from 1 thread tomax(2, hardware_concurrency / 2)whenAPS5_PREBUILD_THREADSis unset. That is 16 threads on a 32-thread host, competing with the title's threads, and the pool now also builds compute pipelines (PrebuildComputePipeline).docs/dev/ENVIRONMENT.mdstill documents the default as 1 ("the one-thread pool"). Could the default stay at 1 here, with your measured setting given in the description (APS5_PREBUILD_THREADS=8)? If you think the larger default is right, please update theENVIRONMENT.mdrow and show that a default run doesn't slow the title's own threads.Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-21T09:38:21Z.
@oneandonlydean Restored the default
RegistrationPreparePool::Prebuild()thread count to1in0070aeafce(ShaderRegistry.cppnow has zero diff againstmain), and updatedDriver::dispatchfromif (compiledResult != nullptr)toif (!cached && compiledResult != nullptr)before callingSubmitLibraryPrebuild.Why
8 -> 1initially needed the!cachedguardWhen we first re-tested with the default 1-thread
Prebuild()pool (839cabd6c7ba), cold-cacheDriver.Dispatchwas1753.363ms/frame (vs1736.797ms/frame onmain):!cached, every dispatch that hit the dispatch cache (cached == true) still re-submittedPrebuildComputePipelinetoRegistrationPreparePool::Prebuild().computePipelinesMutex, starving genuinely new pipelines discovered ahead of time byresolveGroupAhead.Guarding
SubmitLibraryPrebuildwithif (!cached && compiledResult != nullptr)ensures only dispatch-cache misses enqueue prebuild work, so the single default prebuild thread spends its time exclusively on new compute pipelines (Vulkan.ComputePipelineon theshader prepareprebuild thread increased from28.3to35.9ms/frame,+26.9%) and achieves nearly the same result as 8 threads without extra host threads:frames 0:5, cold pipeline cache)main(ca74ef2177ee)!cached(839cabd6c7ba)!cached(0070aeafce7d)APS5_PREBUILD_THREADS=8(320a4b6a1946)Driver.Dispatch(ms/frame)1736.7971753.363(+0.95%)1533.562(-203.235ms,-11.70%)1514.524(-222.273ms,-12.80%)Driver.Dispatch.materialize(ms/frame)581.184561.787(-3.34%)542.662(-38.522ms,-6.63%)601.088(+3.42%)GpuMutex.wait.dispatch(ms/frame)336.444(max1211.4ms)314.950(max1227.3ms)Updated the PR description with the full
tools/trace_digest.pysummaries for both the default 1-thread configuration (0070aeafce7d) andAPS5_PREBUILD_THREADS=8(320a4b6a1946).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-23T18:42:56Z.
Review: CHANGES_REQUESTED
Thanks for the fixes to the earlier notes. Remaining points:
0070aeafce7d), warm cache vsmain:Driver.Dispatch1258 → 1574 ms/frame,Driver.Packet8075 → 8941 ms/frame,frame_ms.p905066 → 5574 ms; cold:frame_ms.max6360 → 6804 ms. The warm improvements in the description are fromAPS5_PREBUILD_THREADS=8, which is not the default. Please either show repeated runs (medians, same scene) where the default build is better, or scope the claim to what is measured. A single 5-frame window dominated by the first-frame stall (4.9 smemory_wait) is too noisy to separate a 10 % change.DispatchAhead.cpp:150-194,:195-202andDispatch.cpp:419: newcatch (...) {}blocks swallow every exception, including emulator invariants (mainhas none inDispatchAhead.cpp). Catch the specific exceptions the preview/prebuild can legitimately throw, or let the rest propagate.docs/dev/ARCHITECTURE.md: the new compute prebuild on the library prebuild pool and the dispatch look-ahead that prioritizes/prepares registrations change the pipeline description; please add it next to thePrebuildStageLibraryparagraph.tests/execution/PreparedShaders.cpp:1447-1448:DispatchAheadPrewarm()is hooked into the--registered-workgroupmode anddeviceis reset there; please give it its own mode/test entry instead of piggybacking on an unrelated one.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T07:24:55Z.
Review: CHANGES_REQUESTED
Thanks for the scoping and the length gating; the correctness points from MarcZX10 and oneandonlydean are resolved on
cb29f63d43, and the merge with currentmainis clean.Requesting changes on the performance claim:
main:Driver.Dispatch1258.0 → 1574.6 ms/frame,Driver.Packet8075 → 8941 ms/frame,frame_ms.p905066.4 → 5574.6 ms (aps5-trace-ca74ef2177ee-42280.json.gzvsaps5-trace-0070aeafce7d-42312.json.gz).Driver.Dispatch1736.8 → 1533.6 ms/frame), with no repetitions.Please provide at least 3 runs per side (main and this branch, same machine, same scene, default environment), with medians for cold and warm cache. If warm stays slower in the default configuration, either make the prebuild and group-ahead preview opt-in or drop the default-on claim from the description.