fix(agc): store a label behind pending copied write-backs without draining #8180
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8180"
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 ntamas94 on 2026-10-20T01:50:52Z.
What
A written buffer the device holds as a CPU copy (a V# that no host import serves in place) gets its results back to guest memory through a CPU write-back when its batch is reaped. A label recorded after that dispatch was stored on the GPU in the same batch, so the label became visible first. A title that polled the label and then rewrote the region had its bytes overwritten by the late write-back:
GuestMemory::WriteChangedstores every byte the shader changed against the bytes saved at upload.While a copied writer is listed,
WriteLabelOnGpunow stores a label of 8 bytes or less only through a completion of its batch (AfterCompletionswithstoredOnGpufalse, reason 5), so it lands at reap after every earlier write-back, without draining the device.Recorder::PendingCompletionStoreskeeps later small labels behind an earlier pending completion store. Stores over 8 bytes keep the GPU store and go behind the completions only over a copied writer's range or a pending completion store. Queue 0 reaps while it spins on REWIND.APS5_DRAIN_COMPLETION_LABELS=1drains instead (reason 2), as the first commit did. Written buffers the device imports in place bind directly and pay nothing.#6185 makes
WriteChangedkeep a CPU store made after the upload. That cannot see a rewrite of the bytes the region held at upload, which is what a title does when it rebuilds the same command buffer every frame. This change orders the label after the write-back instead, so the two are complementary.Tested
Windows 11 Pro (26200), WinLibs GCC 15.2.0 r7, NVIDIA RTX 2080. The
CopiedWriterLabelOrderexecution test dispatches a store through a CPU-copied V# (misaligned in its import, withAPS5_CPU_COPIES=1), records a label, and checks that it is a pending completion store, that it is not visible before the batch that writes back the record is reaped, and that a CPU store made after the label survives. A cross-batch pass checks that a later label to the address of a pending completion store is neither visible before it nor overwritten by it. Without the change the label is stored on the GPU (reason 0) and the test fails. It skips on devices whoseminStorageBufferOffsetAlignmentlets the record bind in place (alignment 4: AMD's driver, RADV).agc_driver_copied_writer_label_order,agc_driver_copied_writer_label_order_drain,agc_driver_copied_writer_copy,agc_driver_unimported_label_order,agc_driver_alias_copy_label,agc_driver_indirect_arguments_pending_producerand allagc_driver_recorder_testsentries pass. Linux was not run locally.Found by reading the code while bringing up Gang Beasts (CUSA04865, a PS4 title through #6977), which binds a V# into its command buffer and rebuilds that buffer every frame.
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 ntamas94 on 2026-10-20T05:33:19Z.
Reviewed the shared files the overlap check could not compare. Only #7464 touches the same function: it sends a label whose destination has no host import through
recorder.Sync()and a CPU store before this PR's check runs.Sync()finishes every in-flight batch, write-backs included, so that path already keeps a pending copied write-back ahead of the label, and imported labels still reach this check. The two merge without conflicts. #4449, #7618, #7668 and #8099 do not touch label or copied-writer handling.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T12:18:48Z.
The fix holds on NVIDIA, but on an AMD device the new test fails where it should skip.
Setup: Windows 11 (26300), WinLibs GCC 15.2.0, Release. The base is
main02fb1ce53, and the candidate is that base with87ee5e245merged (clean). The GPUs are an RTX 5070 Ti (NVIDIA 617.42,minStorageBufferOffsetAlignment0x10) and an AMD Radeon iGPU (AMD proprietary 2.0.353,minStorageBufferOffsetAlignment0x4). Both runs usedANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.NVIDIA:
agc_driver_copied_writer_label_orderand the other label tests (copied_writer_copy,unimported_label_order,same_queue_label_wait,alias_copy_label,pipelined_predicated_label_order) pass 6 of 6, three times.mainfails 2 of 2 withcopied writer: the label after the dispatch was handled with reason 0, expected 2.main. It fails 3 of 3 withthe label is visible but the record holds 0xc0001000, the dispatch stored 0x00000001, so onmainthe label becomes visible before the copied write-back lands.agc_driver_graphics) passes 59 of 59.stencil_ref_exportskips because the GPU has no stencil export.AMD iGPU:
agc_driver_copied_writer_label_orderfails 3 of 3 with the PR, and the same way onmain:copied writer: 0 write-back completions are pending after the dispatch, expected 1 (the test needs APS5_CPU_COPIES=1 and a storage buffer offset alignment above 4).VulkanTestSkipped. RADV also reports 4, so Linux AMD runs should hit this as well.VulkanTestSkippedwhen no write-back completion is pending (or whenminStorageBufferOffsetAlignment <= 4)?From the code: the new early
return 2runs whenevercopiedWritersorDrawCopiedWriters()is non-empty. The overlap branch just below it (CopiedWriterOverlaps, reason 5, the completion memcpy) needs a listed writer, so it can now be reached only withAPS5_LABEL_GATE_ALL=1and no writers listed. Every label recorded while any copied writer is listed now drains the device, even when the label does not overlap the writer. If that is intended, the reason-5 path is now dead in normal runs and could go.Not run: the validation layer, Linux, and a title.
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T01:29:30Z.
@ntamas94 thanks for the fix. The ordering bug is real: wachi35676's control shows the label becoming visible on
mainwhile the dispatch's record still holds the old value. Two things before this can go further:The new test should skip where its precondition can't hold. On devices whose
minStorageBufferOffsetAlignmentis 4 (AMD proprietary, and RADV), the misaligned buffer is imported in place, no CPU copy is made, andCopiedWriterLabelOrderfails at "0 write-back completions are pending after the dispatch, expected 1". Please returnVulkanTestSkippedwith askipped, ...line naming the device limitation when no write-back is pending (or when the alignment is 4 or less). Then the test fails only on the ordering it checks.The early
return 2drains on every label while any copied writer is listed, including labels that don't overlap it. CPU-copied writers also occur in default runs (regions aboveAPS5_GPU_COPY_MAX_KIBor outside imports), so this can add a device drain per label in normal play. It also leaves the reason-5 path below (the deferred completion store, no drain) reachable only withAPS5_LABEL_GATE_ALL=1, and the comment above it no longer matches the code. Could the label take that existing deferred path whenever any copied writer is listed, instead of draining? The completion store runs at reap after the earlier write-back, so it should keep the order your test checks without a drain. If you keep the drain, please add a frame-time comparison on a title that hits it.Imported from boykopovar/AnyPS5. Originally posted by ntamas94 on 2026-10-21T11:08:52Z.
@oneandonlydean thanks, both points addressed in
baa0ca3df.1. Test skip.
CopiedWriterLabelOrdernow returnsVulkanTestSkippedwhen no write-back is pending after the dispatch and the record offset (+0x1004) is a multiple of the device'sminStorageBufferOffsetAlignment, withskipped, the device reports a minStorageBufferOffsetAlignment of 4, so the dispatch binds its record at +0x1004 in place and nothing is written back; .... On a device whose alignment is above 4, a missing write-back still fails the test, so a regression there is not hidden.2. No drain per label. I tried the reason-5 path as you suggested, but it doesn't keep the order for the case the test checks. That path first records the label on the GPU into the host import (
recordStore), so a polling title sees it as soon as the GPU runs the batch.AfterCompletions(..., storedOnGpu=true)then skips its CPU store unless a write-back overlapped the label's own bytes. For a label away from the copied writer, which is the case here, it behaves like reason 0, and the test fails at "the label is visible before the batch that writes back the dispatch's record was reaped".What the PR does now:
AfterCompletions(..., storedOnGpu=false), andWriteLabelOnGpureturns 5. Completions run in push order and batches finish front-first, so it lands after every earlier write-back. There is no WaitIdle: every caller already treats 5 as stored.Recorder::PendingCompletionStorescounts only guest stores still to be made by completions (not EOP deliveries or write-backs). While it is non-zero, later small labels take the same route, so a later GPU label can't become visible before an earlier completion store or land under it at one address. A new cross-batch pass in the test covers this.APS5_DRAIN_COMPLETION_LABELS=1brings back the drain (reason 2); a second ctest entry runs the test with it.The debt entry lists what this still leaves out of order: larger stores and GPU-made DMA_DATA copies away from those ranges, a small label landing after GPU writes recorded behind it, and a nested sync from inside a write-back over a pending storage image.
Tested on Windows 11, RTX 2080:
agc_driver_copied_writer_label_order,agc_driver_copied_writer_label_order_drain,agc_driver_copied_writer_copy,agc_driver_unimported_label_order,agc_driver_alias_copy_label,agc_driver_indirect_arguments_pending_producerand allagc_driver_recorder_testsentries pass, also after merging currentmain(95968952e); conventions pass. Since there's no drain left I didn't add a frame-time comparison; I can post Gang Beasts numbers if you'd still like them.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T11:26:50Z.
@ntamas94 thanks, both points are addressed, and you're right about reason 5. It records the label on the GPU first, so a label away from the copied writer becomes visible before the write-back, exactly as in reason 0. My suggestion couldn't have kept the order the test checks.
The new route reads correctly. A small label goes into the open or newest in-flight batch's completions. Completions run in push order and batches finish front-first, so it lands after every earlier write-back, whatever range that write-back covers.
PendingCompletionStoresis counted inAfterCompletionsand in the late count inNoteWrittenBack, and dropped infinish,discardand the destructor, so it can't leak. Larger stores keep the existing overlap path. Queue 0 reaping on REWIND covers a title that spins there. The test skip and the new_drainvariant also look right. Merged onto currentmain, the changed files compile.One question, and it's the reason we'd like a frame-time check before merging.
PendingCompletionStoresis process-wide and stays above zero until the batch holding the store finishes. Each small label that takes this route adds one to the open batch. With the GPU a frame or two behind, which is the normal state, can the count fall back to zero once the copied writers are gone? If not, every RELEASE_MEM and fence label after the first copied writer is stored at reap for as long as the queue stays busy, on every queue, not only the one with the writer. Default runs keep CPU-copied writers (regions aboveAPS5_GPU_COPY_MAX_KIB, or outside imports), so titles will hit this. The[sync]profile line's "behind completions" count on a title run would show how often it happens. If it stays high after the writers are gone, counting per queue would at least keep the other queues on GPU stores.Not blocking on the code. Once CI is green, we'll measure frame time on our own titles before merging.
Imported from boykopovar/AnyPS5. Originally posted by ntamas94 on 2026-10-22T07:37:26Z.
@oneandonlydean I measured whether
PendingCompletionStoresdrops back to zero once the copied writers are gone, on Gang Beasts (CUSA04865). I used a local counter (not part of this PR) that sorts every imported label of 8 bytes or less by the route it takes. There were two 5-minute runs through the menu, level0, level4 and level16:APS5_CPU_COPIES=1)The pending-only route comes in short bursts during level loads, then stays flat for minutes while small labels keep arriving. With copied writers forced it went 0 → 23 in the first load and 29 → 60 in a later one, then stayed at 60 for the remaining ~30k labels. In the default run it reached 34, later 65, and stayed at 65 for the last ~60k labels. So the counter does fall back to zero between bursts, and the chain does not keep itself going here. All of these labels were on queue 0.
Frame time, A/B on the same build and the same scene:
APS5_TRACE_SECONDS=40:20(aps5-summary.jsonl).Packetms / frameThe scene is vsync-bound (16.68 ms) when a run is clean. The spread between runs of the same variant is larger than any difference between the variants:
Packettime: 11.03 vs 11.19 ms per frame.PR run 2 has one 440 ms hitch. I can't tie it to this path: the pending-only route is taken only during level loads, and there was no load in that window. I have the six traces if you want them.
🤖 Generated with Claude Code