fix(agc): bound the SPIR-V that background compute pipeline builds compile at once #12147
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12147"
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 milescoviello on 2026-10-27T23:40:15Z.
What
Ghost of Tsushima (PPSA02225 02.024.000) is killed at the 20 GB memory cap about 150 s in, in every run with a warm shader cache: at 121, 159, 152 and 155 s in four runs. Each time, anonymous memory climbs from about 4 GB by 2.5 to 5 GB within 8 to 10 s, and the run is killed.
A preloaded allocation hook that prints a backtrace each time one thread's allocations pass another 512 MiB shows where it goes. Eleven threads of the prebuild pool run
Driver::dispatch's resolve-ahead task, which callsVulkanDevice::PrebuildComputePipelineand thenvkCreateComputePipelines. Inside that call, NVIDIA's compiler (libnvidia-gpucomp.so) allocates 0.5 to 1 GB per thread. Ghost of Tsushima's compute modules reach 800,000 SPIR-V words (3.2 MB), so the pool, with half the hardware threads (12 here), runs eleven of these compiles at once. The pipelines never reach the driver's disk cache, because the run is killed first, so every run dies at the same place.With
APS5_PREBUILD_THREADS=2the same run goes the full 308 s, with anon peaking at 7.4 GB. So it is the number of large compiles in flight that kills it, not the compiles themselves.How
CompileBudget(Execution/include/CompileBudget.hpp) bounds the SPIR-V bytes being compiled at once. A build that would exceed the limit waits until earlier ones finish. A module larger than the whole limit builds alone: while one waits, builds that would fit wait behind it, so it runs as soon as the builds already in flight finish, and smaller builds can't starve it.VulkanDevicetakes aCompileBudget::Holdsized to the module's SPIR-V around both background compute builds:PrebuildComputePipeline, the dispatch-ahead build above. It checksHasComputePipelineagain after the wait, because a dispatch may have built the pipeline meanwhile.ReplayComputePipeline, the pipeline journal's compute replay. It has the same shape, a pool of half the hardware threads building recorded modules, and a journal holds the same giant modules. With a warm driver cache these builds are mostly cache hits, but with a cold one the replay meets the same compiles.A dispatch that needs its pipeline now never waits on the budget. If a prebuild is still queued, the dispatch builds the pipeline itself.
A compute prebuild waits for the budget inside a worker of the prebuild pool, which the graphics library prebuilds share, so a burst of large compute modules could otherwise hold every worker (review).
SubmitComputePrebuildputs compute prebuilds in the same pool marked as budgeted, and the pool starts one only while fewer than half of its workers are running them (at least one). A worker takes the first queued task it may start. So library prebuilds queued behind waiting compute prebuilds run on the other workers, compute prebuilds still start in the order they were queued, and the pool's thread count still bounds all prebuilds together.ENVIRONMENT.md'sAPS5_PREBUILD_THREADSrow says so.The limit is
APS5_BACKGROUND_COMPILE_KIB, 8192 KiB by default.0builds them one at a time, andENVIRONMENT.mddocuments it. The default comes from measurements, after review (data):#9626 (MarcZX10) bounds registration-time shader preparation by process memory, defaulting to half of physical RAM. It covers a different stage: the recompiler's preparation, not the driver's pipeline compile. The two don't touch the same code.
Tested
a937addfb8(XairRrR's review):ReplayComputePipelinechecks again under the pipeline mutex after the budget hold, asPrebuildComputePipelinedoes.agc_driver_compute_prebuild_shareruns withAPS5_PREBUILD_THREADS=4from its CTest entry. It passes 1 of 1 withAPS5_PREBUILD_THREADS=1exported in the shell.^agc_driver_compute_prebuild_share$|^agc_compile_budget$|^agc_driver_registration_prepare_stack$|^agc_driver_shutdown_threadspass 3 of 3.VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -V) over the same regex: the merge basef759e165d9passes 10 of 10, and this head 12 of 12. Neither log has a missing-layer line or aVUID-line. These tests don't print the devices they open, so I can't say how many the layer checked. XairRrR's run also shows noVUID-lines.Linux (Ubuntu 26.04.1, GCC 15.2), RTX 4070 Ti with NVIDIA 615.71.09.
main(it stops at 37 s on a native multisample depth import, #9742), so these runs use a local stack:maind13b3d7c55with #9717, #9742, #9129, #10064 and #10727 merged (5b7d732a35). With this change cherry-picked on top (89eaf64f77):APS5_PREBUILD_THREADS=2feb568b46d, onmainf759e165d9): the newagc_compile_budgetcovers the default and configured limits, eight 40-byte holds under a 100-byte limit never holding more than 80 bytes at once, a zero limit running holds one at a time, a 500-byte hold under a 100-byte limit running alone, and a waiting 500-byte hold keeping out a 40-byte hold that would fit. That last case fails with the old wait condition. It passes 3 of 3 (--repeat until-fail:3). At74fb986752, which differs from this head only in linkinglibcinto the new test target,agc_pipeline_journalandagc_pipeline_journal_long_pathsalso pass 3 of 3.agc_driver_pipeline_journalfails at both this head andmainf759e165d9, 3 of 3 each, built the same way, with the same message (AGC graphics: the oversized record was not counted as a write failure, inOversizedRecordKeepsTheJournal). So that failure predates this change. In both logs, the device opened for that case enumerates only llvmpipe, because the NVIDIA driver drops out after the earlier instances. The journal then turns itself off for a foreign device. #11721 (jotitadev) keeps the Vulkan drivers loaded across this test for that reason. Every case before it passes, including the replays that go throughReplayComputePipeline. The one after it,HeldModuleTests, doesn't run on either side.759ec763c9: the newagc_driver_compute_prebuild_sharesubmits one blocking compute prebuild per prebuild worker (12 here). It checks that exactly half start, that a library prebuild queued behind them runs while they wait, and that all of them finish once released. It passes 3 of 3. WithSubmitComputePrebuildsubmitting unbudgeted tasks, as compute prebuilds were submitted before this commit, it fails:8 of 12 blocked compute prebuilds started, not 6.agc_compile_budget,agc_driver_registration_prepare_stackand theagc_driver_shutdown_threads*andagc_driver_prepared_shaders*tests, 34 in all, pass 3 of 3 atc5193e673f. That commit has the same code; the test was then changed to release its blocked tasks when a check fails, so a failure ends in 10 s instead of hanging. Ghost of Tsushima with this commit (the setup of the 8 MiB run above) runs to the 300 s timeout with an anon peak of 6.3 GB. That compares with 6.7 GB before this commit and 9.3 GB without a limit. About 40 to 46 % of its graphics libraries are built at the draw with every setting, no limit included (counts).759ec763c9:agc_driver_compute_prebuild_share,agc_compile_budget,agc_driver_registration_prepare_stackand theagc_driver_shutdown_threads*tests (12) pass 3 of 3. The merge basef759e165d9passes the 10 of them it has, 3 of 3.agc_compile_budgetandagc_driver_pipeline_journalpass 3 of 3 atfeb568b46d.agc_driver_pipeline_journalalso passes 3 of 3 atmainf759e165d9. The first Windows build failed to link the new test (__gxx_personality_v0), because it didn't linklibcthe wayagc_handoff_spin_testsdoes.beaf0279aafixed that.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)ReplayComputePipeline; agreed on #12657: this lands first and #12657 rebases after it, taking the hold before it registers the build)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T04:25:36Z.
Review: APPROVED
Reviewed — approved
The budget and the pool share merge cleanly, build, and pass the focused tests on an RTX 4070 Ti SUPER with the validation layer, with no
VUID-lines on either side. Mutating each of the two fixes makes its new test fail. The points below are non-blocking.What I ran
759ec763c984ae6ffacd84e5fb55dfbbf0c52075main:b433c40f2f46aa2e59e19806010a839ac8572d64main+ head with--no-ff, no conflictsResults
maingit diff --checkcheck_conventions.pyAll conventions checks passed.main, validation layerVUID-linesmainwas not found but was requested by env var VK_INSTANCE_LAYERSVK_LAYER_KHRONOS_validationin bothagc_compile_budget,agc_driver_compute_prebuild_sharerepeated 10 timesagc_driver_compute_prebuild_sharewithAPS5_PREBUILD_THREADS=2and3agc_driver_compute_prebuild_sharewithAPS5_PREBUILD_THREADS=1agc_driver_pipeline_journal10.5 s; every test is under 30 sMerged tree, ctest excerpt
The device for the journal tests is
NVIDIA GeForce RTX 4070 Ti SUPERin every case. The description saysagc_driver_pipeline_journalfails inOversizedRecordKeepsTheJournalon both sides because the NVIDIA driver drops out after the earlier instances. On this host it passes on bothmainand the merged tree.APS5_PREBUILD_THREADS=1
Negative control
All production files from
main(theExecutiondirectory andENVIRONMENT.md; the newCompileBudget.hppleft in place):agc_driver_compute_prebuild_share_testsdoesn't build (SubmitComputePrebuildis not a member ofAgcDriver::DriverDetail).Mutations, built and run on the merged tree:
CompileBudget::Hold, a hold that fits waits only onheld + bytes <= limit, dropping thewaitingAlone == 0term fromf07182a387.agc_compile_budgetthen fails witha 40-byte hold that fits entered while a 500-byte hold was waiting to run alone. This confirms with a test the fix for the first point in the earlier review comment.SubmitComputePrebuildsubmits withbudgeted = false.agc_driver_compute_prebuild_sharethen fails after 10 s witha library prebuild queued behind 16 blocked compute prebuilds did not run while they waited.After that I restored the merged files.
Findings
Blocking
None.
Non-blocking
ReplayComputePipelinecheckscomputePipelinesandcomputePipelinesBuilding, then takes the hold, then compiles without checking again.PrebuildComputePipelinedoes check again after the hold. Before this change the gap between the check and the compile was short. Now it lasts as long as the budget wait, which in the scenario the PR targets is several large compiles. A dispatch that needs the same pipeline during that wait builds it itself. The replay then compiles it a second time, andtry_emplacethrows the result away. That duplicate compile is the expensive, memory-heavy work the budget exists to limit. #12657 fixes the underlying race by registering the replay incomputePipelinesBuilding. By the order agreed there, it will take this PR's hold before it registers the build, so its check needs to run after the hold to close the window. Suggested fix, here or as part of the #12657 rebase: take the hold before thecontainscheck, or repeat the check undercomputePipelinesMutexafter the hold and returnfalseon a hit, asPrebuildComputePipelinedoes.APS5_PREBUILD_THREADS=1.PrebuildThreads()copies the pool's sizing, so the test runs with 16 workers here, 12 on the author's machine, and 1 worker if a developer exportsAPS5_PREBUILD_THREADS=1. With one worker,budgetedLimitismax(1, 0) = 1, the whole pool. The library prebuild then can't run beside the blocked compute prebuild, and the test fails with the message above. That matches the documented "at least one", so the code isn't wrong. Suggested fix: have the CTest entry setAPS5_PREBUILD_THREADSto a fixed value (for example 4), so the result doesn't depend on the host or the developer's environment. Or skip with return code 77 when the pool has fewer than two workers.Testedhas no validation-layer comparison. CONTRIBUTING.md asks AGC driver changes to run the focused tests onmainand on the change withVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1, and to compare theVUID-lines. The description reports plainctestruns only. The change makes no new Vulkan calls, and my run above shows 0VUID-lines on both sides on NVIDIA. Please add your own comparison toTestedall the same, or cite this one.Not verified
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T05:42:29Z.
Review: APPROVED
Re-checked — approved
a937addfb8adds the check after the budget wait inReplayComputePipelineand pinsAPS5_PREBUILD_THREADS=4for the pool-share test, and the description now has the validation-layer comparison; all three earlier points are resolved, and the merged tree builds and passes the focused tests with noVUID-lines.What changed since my last review
a937addfb8(5 lines, on top of759ec763c9, which I reviewed before):VulkanDevice.cppL2333-L2337: afterReplayComputePipelinetakes theCompileBudget::Hold, it checkscomputePipelinesandcomputePipelinesBuildingagain undercomputePipelinesMutexand returnsfalseon a hit.CMakeLists.txtL3286:agc_driver_compute_prebuild_sharegetsAPS5_PREBUILD_THREADS=4appended to its environment.Testednow has a validation-layer run on the merge base and on the head.What I ran
a937addfb8b0229ec05ce1048db5f4985015a5e9main:52c09d567d2fa453494dd720fcf53a98149468e4main+ head with--no-ff, no conflictsPrevious findings
PrebuildComputePipeline, takes the same mutex, and tests both maps, so a dispatch that built or started building the pipeline during the wait now makes the replay returnfalseinstead of compiling a duplicate. Returningfalsethere is consistent with the existingtry_emplace(...).secondresult for a pipeline that already exists.APS5_PREBUILD_THREADS=1— Resolved. CTest lists the test's environment asANYPS5_NO_SHADER_CACHE=1;APS5_PREBUILD_THREADS=4;APS5_NO_PIPELINE_JOURNAL=1. WithAPS5_PREBUILD_THREADS=1exported,agc_compile_budgetandagc_driver_compute_prebuild_sharepass 10 of 10 underctest. Running the test binary directly withAPS5_PREBUILD_THREADS=1still fails with the same message as before, which is expected now that the CTest entry carries the setting.Testedhas no validation-layer comparison — Resolved. The description now reports the comparison on the merge base and the head (author-reported). My own run on this head agrees, see below.Results
maingit diff --checkcheck_conventions.pyAll conventions checks passed.main, validation layeragc_compile_budgetandagc_driver_compute_prebuild_sharedon't exist there)VUID-linesmainwas not found but was requested by env var VK_INSTANCE_LAYERSVK_LAYER_KHRONOS_validation384 timesmain; every test is under 30 sMerged tree, ctest excerpt
APS5_PREBUILD_THREADS=1
New findings
Blocking
None.
Non-blocking
agc_driver_pipeline_journal,agc_pipeline_journalandagc_pipeline_journal_long_pathsstill pass 3 of 3. That is expected, since hitting the window needs a dispatch to build the same pipeline while the replay waits on the budget, and the change is small and mirrorsPrebuildComputePipeline. If #12657's rebase adds a test for itscomputePipelinesBuildingregistration, a case where the replay's hold is blocked while the same key gets built would cover this check too.Not verified