fix(agc): keep resident heap mirrors without host copies #15296
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15296"
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 Prophet069 on 2026-11-02T23:01:25Z.
What
A resident heap mirror kept a 16 MiB host copy of every slab, so building a multi-GiB mirror needed as much host memory again, and a rebuild while the old mirror was alive needed it twice. The host copies are gone.
A refresh now only marks the changed ranges (
ResidentBuffer::MarkChanged, an interval map in which a later mark wins). The flush reads the marked ranges straight from guest memory into bounded 64 MiB staging segments, waits after every 256 MiB when the caller allows it, and fills newly bound or zeroed blocks on the device withvkCmdFillBuffer.The write-back reference of each use is a device copy of the written ranges recorded after the uploads and before the shader (
ResidentBuffer::RecordReference); the write-back compares the readback after the shader with it, so only bytes the shader changed reach the guest and CPU writes made meanwhile are kept. Non-resident heap mirrors keep their pooled host references.Reference, upload and readback buffers count as kept bytes of the recorder, so its existing bound submits before they pile up, and references made inside a batch completion are recorded in order with the uploads.
Tests: partly written 64 KiB blocks, CPU writes made while a dispatch is in flight, and two dispatches writing the same range.
Tested
NVIDIA GeForce RTX 3080 Laptop GPU, driver 616.64, Windows 10 Pro 22H2 (19045.6466).
Focused tests on
main(9090972b7e) and on this branch withVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1,ctest -V -R "heap_mirror|^agc_driver_graphics$|^agc_driver_key_miss$|^agc_driver_bda_device$|^agc_driver_recorder$|native_multisample_depth|depth_storage|^agc_host_import_retire$|^agc_host_import_sparse$":main: 17 of 20 selected tests passed (agc_host_import_sparse skips on this machine; the three agc_driver_depth_storage_* tests were not built in this run); validation layer loaded in both logs;VUID-errors: none.VUID-errors: none.agc_driver_resident_heap_mirrorincludes the new partial-block and in-flight CPU write cases.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T00:12:28Z.
Review: COMMENTED
Reviewed — comments
The PR merges and builds, the heap-mirror tests pass, and the validation comparison shows no new VUIDs. But the new tests also pass with
main's production code, and the new slab-refusal, budget-shrink and nonzero-scan-cache paths have no test.What I ran
Head
a64a9a4a280cb5be0f80bb01ce98812e56bcda6d,main5cb015caaaaf83f8ee431414ee6a457b5b307d49, merged treea5a36646c486d472781b0f1732a84113e9acecda(--no-ff), Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19).Results
git diff --checkcheck_conventions.pyagc_driver_resident_heap_mirror(with the newPartialBlockTestsandConcurrentWriteTests)agc_heap_mirror_*,agc_sparse_heap_mirror_*(10 tests)agc_driver_image_sample_lod_clamp_shader_lane(listed by the script as a new failure)main's production files, so it comes frommain(baseline was the older096c45570f), not from this PRunwrap_self_..._destination_directory_group(listed as a new failure)os.chownin a temporary directory (EINVAL); this PR changes no tool or relinker file, so it is the environment, not the PRVUID-linesgit status --porcelainemptylod_clamp_shader_lane output (same on merged tree and with main's production files)
Negative control
I put back
main's versions ofContext.hpp,GuestBufferMemory.hpp,ResidentMirror.hpp,GuestBufferMemory.cppandResidentMirror.cpp, kept the PR'stests/execution/ResidentHeapMirror.cpp, and rebuilt.agc_driver_resident_heap_mirrorstill passed (2.23 s).That can be expected: the new partial-block, in-flight CPU write and double-dispatch cases describe behaviour that
main's host-copy design already had. So they guard against regressions in the rewrite, but they don't show the change itself. Nothing tests the host-memory saving, the slab refusal, the budget shrink-and-retry loop or the nonzero-scan cache. I then put the merged files back, rebuilt the whole tree, and checked that the worktree is clean.Findings
Blocking
None.
Non-blocking
ResidentMemory::refuseSlab(core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp:176),shrinkHeapMirrorBudget(core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2814) and the 8-attempt retry loops (GuestBufferMemory.cpp:2849,GuestBufferMemory.cpp:3387) are the fix the PR depends on for PPSA02015. They run only when the device heap fills or when slabs passAPS5_HEAP_MIRROR_MIB. The existing tests pass onmainwithout any of this code. Suggested fix: add a case that sets a small budget withSetHeapMirrorBudget(whichHeapMirrorSlabBudgetalready reads). It should check that a slab past it is refused, the budget is lowered, an older mirror is evicted, and the new mirror is still built.heapMirrorMemoryLimit(GuestBufferMemory.cpp:2714) is only ever lowered, atGuestBufferMemory.cpp:2824, and nothing raises it again. So one refusal caused by a temporary peak in device-heap use leavesheapMirrorBudget()lowered for the rest of the process. The soft-target escape inreserveHeapBytes/growResidentonly applies on a retry, after a sweep has already evicted mirrors. So after one temporary peak, mirrors keep being evicted early for the whole session. Suggested fix: raise the limit again once the reported heap has headroom, or say in the description that the drop is permanent by design.shrinkHeapMirrorBudgettestserror.what()for"of a heap mirror slab". A change to either message (refuseSlaborallocate's operation string) would silently turn off the retry and bring back the stop this PR fixes. Suggested fix: use a dedicated exception type derived fromstd::runtime_error, or a flag returned byTake.Context::HeapReport::Fitsis never used (core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp:340).refuseSlabdoes its own headroom arithmetic. Either useFitsor remove it.GuestNonzeroBlocksnow keeps a process-wideunordered_mapof up to 4M entries (ResidentMirror.cpp:379-380) behind one mutex, which it takes once or twice per 64 KiB block. The whole map is dropped when it fills (ResidentMirror.cpp:408). Its correctness depends onUnchangedSincecoming back false for pages that were unmapped and mapped again (ForgetPages). I didn't find a test of that for this cache (ReusedAddressTestspasses, but it also passed before the cache existed). This is the third separate change in the first commit; see the next item.8a30d9a70f8b, "store a resident heap mirror's write-back from its readback buffer"), the slab-refusal and fallback work, fullest-slab allocation, and the scan cache (1743177ae4af). The checklist namesDepends on: #15295, #12827. If those PRs carry some of these commits, the commits would be merged twice or conflict. Please state which commits belong to which PR, or move the separate fixes into their own PRs.Not verified
RecordStagingCopiesflush that uploads newly marked ranges aftertakeHeapReferencesrecorded the device reference could put those bytes into the write-back. That would happen if the range differs between the reference and the readback without any shader store. I didn't build a test for this ordering. The newConcurrentWriteTestsdoesn't cover it.To get this to a good state
HeapReport::Fits(finding 4).Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T06:15:30Z.
Review: APPROVED
Re-checked — approved
The new commit
89e3292072("let a small heap mirror reach the slab allocation and its host fallback") does four things. It replaces the error-text match with aHeapMirrorSlabRefusedtype. It lets a lowered budget recover after 30 s. It usesHeapReport::Fits. It refuses a slab only while the heap mirrors hold more than 256 MiB. Its newFullDeviceHeapTestscase fails when that last guard is removed. The other commits were rebased onto it with the same content.What changed since my last review
89e329207201, between the slab-refusal commit and the cherry-picked write-back commit. The later commits were rebased onto it. Per the range-diff, they only change around the new test struct and thetry/catchinResidentMemory::Take. The head commit7bbf091cf8is unchanged.7c184fb3aeis the cherry-pick from #12827, and the last three are this PR's own. The checklist saysDepends on: #15295, #12827. The author also agreed with EagleBearerYS that #12840 lands after this PR.What I ran
Head
7bbf091cf83a767b16e38f966d2d72d9321b0d81,maind1ae628bc2f62459cce1e9872ba5632c938c71f2, merged treebf1ec647053b1866f995446eb40a85dcac8f0323(--no-ff). Debian 13 VM, GCC 14.2.0, lavapipe (Mesa 26.1.6, LLVM 19).git diff --checkcheck_conventions.pyheap_mirrortests (10, includingagc_driver_resident_heap_mirrorwithFullDeviceHeapTests)VUID-linesunwrap_self_..._destination_directory_group(script: new failure)os.chownon a temporary directory fails with EINVAL. This PR changes no tool file, so the cause is the environment, as in my last reviewagc_driver_resident_heap_mirrorfailsgit status --porcelainemptyNegative control output
I didn't do a full restore of
main's production files. The new test callsSetDeviceHeapFullForTests, which doesn't exist onmain, so it wouldn't compile. Removing only the guard is the narrower control, and it shows the new test exercises the headroom refusal.Previous findings
FullDeviceHeapTests(core/libs/prx/libSceAgcDriver/tests/execution/ResidentHeapMirror.cpp:370) now reachesrefuseSlab(see the negative control). But it holds less than 256 MiB, soshrinkHeapMirrorBudgetreturns atcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2841. The lowering, the eviction sweep and the 8-attempt retry are still not run by any test. Non-blocking.heapMirrorBudgetputs the limit back after 30 s (GuestBufferMemory.cpp:2734), and the time a refusal lowers it is stored atGuestBufferMemory.cpp:2842. No test covers it.dynamic_cast<const HeapMirrorSlabRefused*>(GuestBufferMemory.cpp:2834). Both the refusal and a failed slab allocation throw that type (core/libs/prx/libSceAgcDriver/Graphics/src/ResidentMirror.cpp:155-159,:199). That is the same set of errors the old text match caught.HeapReport::Fitsunused. Resolved. It is used atResidentMirror.cpp:193.ResidentMirror.cpp:381). Non-blocking.Depends on: #15295, #12827. The landing order is author-reported. I couldn't check the other PRs' heads.New findings
Blocking
None.
Non-blocking
recordTransferrecords uploads intorecorder->Commands()in sequence (ResidentMirror.cpp:61-68), and so doesRecordReference(ResidentMirror.cpp:616-627). So any range still marked whenRecordStagingCopiescallsResidentBuffer::FlushChanged(context, true)(GuestBufferMemory.cpp:6928) is uploaded after the reference thattakeHeapReferencescopied (GuestBufferMemory.cpp:6715-6718). The write-back would then treat it as a shader write. I didn't build a case that puts a mark between the two calls. Suggested fix: show that no mark can land there, or make the write-back ignore ranges flushed after the reference. Add a test either way.GuestBufferMemory.cpp:2734). On a heap that stays full, this can mean another refuse, shrink and evict cycle every 30 s. That is acceptable as a design choice, but a short note in the commit or description would help.Not verified
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T21:18:37Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the checks and source ranges I inspected support merging, though I did not run the reported tests.
What I ran
Reviewed head
7bbf091cf83a767b16e38f966d2d72d9321b0d81against the prepared diff and targeted source context. I did not run commands or tests. The author reports focused test runs onmainand this branch; those are not runs I performed.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I read all six changed files listed in the manifest:
Context.hpp,GuestBufferMemory.hpp,ResidentMirror.hpp,GuestBufferMemory.cpp,ResidentMirror.cpp, andResidentHeapMirror.cpp. No changed file was excluded. I did not read every added line in the largeGuestBufferMemory.cppor all added lines in the test file, so the review is not complete against the review folder's instruction to read every changed line. Build, automated tests, and physical-console behavior were not independently verified.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:45:11Z.
Review: COMMENTED
The trial merge onto
c1601e56d5conflicts in the mirror context, implementation, andResidentHeapMirrorfixture. Please preserve one focused case in which two queued uses of the same slab need different write-back reference bytes. Removing the host copies means the device reference must be captured after the upload and before that use’s writes. A successful current readback alone would miss reuse of the wrong reference. The Windows result in the PR is from the author.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.