perf(agc): create compute pipelines before taking the GPU lock #4897
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4897"
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 karanvora2599 on 2026-10-13T02:40:22Z.
What
A compute pipeline is now created in
PrepareDispatch, before the dispatch takesGuestMemory::GpuMutex, instead of inVulkanDevice::dispatchunder it.Background.
main, the first dispatch of a compiled variant creates its pipeline inVulkanDevice::dispatch, whichDriver::dispatchcalls underGpuMutex. While the driver compiles, everything else that takesGpuMutexwaits: other queues' dispatches and draws,Driver::Present, label writes and memory waits. Two queues that each need a new pipeline compile one after the other.VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BITchanges neither compile time nor run time for compute on that driver, so a quick unoptimized pipeline first doesn't help there.How.
VulkanDevice::computePipeline: module, layout, specialization, subgroup size, #3717's capture and the statistics.computePipelinereturns the pipeline fromcomputePipelines, or creates and inserts it.shared_futureper key incomputePipelinesBuilding. So two queues that need the same new variant create it once.PrepareDispatchcalls it once the descriptor set layout is known: from stage A, or from the cachedShaderResources. The result travels inPreparedDispatch::objects, anddispatch()uses it.dispatch()still creates the pipeline under the lock, through the same function, in three cases:Complete());APS5_LOCKED_BUILD=1;APS5_LOCKED_PIPELINE=1, which keepsmain's behaviour for comparisons.APS5_PROFILE_DRAW=1, a pipeline over 100K words now also prints[dispatch] created a pipeline for N SPIR-V words in M ms (program 0x...).Why creating it without the lock is safe.
ShaderResourcesthat the dispatch owns, or thatPreparedDispatch::cachedkeeps alive;workgroupMemoryLayout, made at device creation;ShaderResourcessets its layout in its build and clears it only inrelease(), which runs from the destructor or a failed build. A cached object has finished its build.VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT, so creating pipelines with it from several threads is valid (pipeline cache). Graphics library prebuilds (PrebuildStageLibrary) and the library job thread inPipelineLibrary.cppalready create pipelines with it outside the lock.main, the map entry made by the first dispatch of a variant already serves its later dispatches the same way.Before / after on an RTX 4060 Laptop (Ada, driver 591.86).
sceAgcDriverSubmitAcb.APS5_SPIRV_OPT=none.APS5_LOCKED_PIPELINE=1(main's behaviour) and this change.APS5_LOCKED_PIPELINE=1Limits.
PrepareDispatch. It has since been narrowed to the indirect draw checks and leaves that lookup to this PR.CreateComputePipelinecall takes that lock incomputePipeline.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release. Devices:
GALLIUM_OVERRIDE_CPU_CAPS=avx.The full runs were on
mainat43e3a70f8. The branch is now rebased ontod1fd9372b, where I reran the tests under Dispatch path on the three devices, plusagc_driver_graphics, and repeated the measurement once each way (21.1 s → 14.1 s; worst wait 7.8 s → 4 ms). They pass.PrepareDispatchand the pipeline map. They pass on the three devices, withAPS5_VERIFY_RECIPE=1on the RTX 4060, and withAPS5_LOCKED_PIPELINE=1(the old path) on the RTX 4060:agc_driver_prepared_*,agc_driver_compute_user_data_calls*,agc_driver_gated_dispatch_capture*;agc_driver_oversized_*,agc_driver_flip_compute,agc_driver_wave32_subgroup,agc_driver_resource_cache_oversized_storage_admission;agc_compute_pipeline_capture,agc_compute_pipeline_replayandagc_compute_pipeline_capture_run(#3717's capture, which moved intocomputePipeline).agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*,agc_graphics; 352 tests):agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures*,agc_driver_pixel_interlock.agc_driver_ds_swizzle, which fails the same way onmain.agc_driver_depth_fast_clear_submit. It segfaults within 0.4 s in about half its runs, at the same address invulkan_lvp.dll(+0x10fe588) asmain's build of the test.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T02:40:34Z.
The measurement test behind the Before / after table. It isn't part of this PR.
To run it:
core/libs/prx/libSceAgcDriver/tests/execution/. The test glob picks it up asagc_driver_nvidia_queue_compile_measure.APS5_SPIRV_OPT=none ANYPS5_REQUIRE_VULKAN=1 APS5_PROFILE_DRAW=1 ctest -R '^agc_driver_nvidia_queue_compile_measure$' -V, once withAPS5_LOCKED_PIPELINE=1and once without.APS5_EXP_LENGTHsets the program length; the default 8192 gives 771,233 SPIR-V words.Each run builds new programs from a time-based seed, so the pipeline cache never serves them.
NvidiaQueueCompileMeasure.cpp
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T15:56:38Z.
@Celegans12, your Ghost of Yōtei set from #4339 is the case this PR is about. On
main, each of those 80 to 470 s compiles runs underGpuMutex, so presentation and the other queues stop for the whole compile. Could you compare a Yōtei scene where giant compute pipelines get created, on the base and on the base plus this PR?Dispatch.cpp,VulkanDevice.cppandVulkanDevice.hpp.VkPipelineCacheand NVIDIA store, as in your #4339 runs. The form's warm-cache rule would hide the effect, since a cached pipeline is never created under the lock.APS5_TRACE_SECONDS=<start>:<length>over the stretch where the giants compile, and post the twoaps5-summary.jsonllines plus the traces.APS5_LOCKED_PIPELINE=1on the PR build givesmain's behaviour, so one build covers both arms. A separate run withAPS5_PROFILE_DRAW=1prints[dispatch] created a pipeline for N SPIR-V words in M msfor each large pipeline.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T23:05:52Z.
Three things before this can go in:
APS5_LOCKED_PIPELINE(VulkanDevice.cpp:3769); compare against amainbuild instead of adding an env switch.GPUs tested:line to the checklist.mainand of this change from one title scene with a cold pipeline cache, with the traces, as the Performance report form describes. The Yōtei run you asked Celegans12 for would do.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T00:51:27Z.
Review: COMMENTED
I read through this and it looks correct. It is the right shape for moving a compile out of
GpuMutex.ShaderResourceslayout the dispatch binds (owned by the dispatch, or kept alive byPreparedDispatch::cached). It can't diverge from whatdispatch()would have built, so no extra compiles.computePipelinesBuildingmeans two queues that miss on the same variant compile it once. On failure both get the exception through the promise, and the entry is erased under the same mutex.PrepareDispatchhas one caller (Driver::dispatch), so no pipeline is built for a dispatch that never runs.deviceReplacementshared for the dispatch, socomputePipelinesBuilding.clear()can't race a build still in flight.Small points:
finishitself throws (try_emplaceallocation),buildingis destroyed without a value and waiters getbroken_promiseinstead of the original error. That is unlikely, butset_exceptionbefore the map update would avoid it.built.Layout() != VK_NULL_HANDLE && (pushStages == 0 || maxPushConstantsSize >= ComputePushConstantBytes)) is duplicated from the checks indispatch(). A shared helper would keep the two from drifting apart.perf(evidence that CONTRIBUTING asks for. The synthetic test already shows the mechanism.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T01:04:26Z.
I compared this moved compute-pipeline path with #4795. That PR adds
PipelineCacheSynchronizationto protectvkCreateComputePipelinesfrom a concurrent persistent-cache snapshot, and passes the guard from the existing dispatch path. This PR relocates the call intocomputePipeline()(around the new call toCreateComputePipeline), but that call currently uses the default null synchronization argument.If #4795 lands first, this pre-dispatch path would bypass its guard; if this lands first, rebasing #4795 will move the call site it currently patches. Please thread the context's pipeline-cache synchronization through
computePipeline()and intoCreateComputePipeline, and note the dependency/coordination with #4795 so the snapshot race remains covered after integration.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T01:05:44Z.
Update: #4795 has now merged, and current
maincontains theCreateComputePipeline(..., PipelineCacheSynchronization*)guard. This PR's moved call still relies on the default null argument, and GitHub now reports the branch conflicts withmain. Please rebase and passcontext.pipelineCacheSynchronizationthroughcomputePipeline()to that call; the lock must protect this pre-dispatch creation too.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T18:34:37Z.
Thanks @karanvora2599. Carried in #5989: your commit rebased onto current
mainwith your authorship kept,context.pipelineCacheSynchronizationpassed intocomputePipeline()as MarcZX100 asked, andAPS5_LOCKED_PIPELINEdropped. It also creates the address-table shaders' pipelines before the lock, which this branch still left under it (#4977). Closing this one in its favour.Pull request closed