fix(agc): store labels on the host once their batch completed on MoltenVK, whose GPU writes reach the host out of order #20172
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/20172"
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 XIIYAZ on 2026-11-14T19:11:13Z.
What
Fixes the ordering in #20118. On MoltenVK, a label recorded on the GPU after a copy can be seen by the host before part of the copy:
This is how Apple GPUs make writes visible, not something a barrier can fix. A standalone Metal program (no AnyPS5, no MoltenVK) copies 256 KiB into a shared buffer, then copies a 4-byte label after it, and the CPU spins on the label and then checks the data. I ran it four ways, 300 rounds each:
MTLFencebetween two encodersMTLFenceacross two command buffersIn every round, the data is complete once the command buffer has completed. A full
ALL_COMMANDSmemory barrier and aVkEventset/wait before the label store do not help through MoltenVK either. Only completion makes a command buffer's writes visible to the host in order. So a label stored by the GPU can be seen before the results it follows.On MoltenVK the host now stores small labels (8 bytes or less) once their batch has completed (
Recorder::HostLabel, reason 7):agc_driver_unimported_label_orderdoes).WaitIdlethe label is always in memory.CompletionLabelInmakes a later fill or write to its bytes wait for its batch, and a later GPU write is not overwritten by the host store. It does not count as a pending completion store, so the next labels still take this path instead of the reap-only one.DUMP_CONST_RAM,COPY_DATAandDMA_DATApayloads that later GPU work reads) stay on the GPU.VK_DRIVER_ID_MOLTENVKwith a timeline semaphore takes this path.APS5_MOLTENVK_GPU_LABELS=1restores the GPU path, for comparison.Tests:
LabelAfterImportCopy,UnregisteredLabelOnGpu,CopiedWriterLabelOrder,IndirectThreadDimensionsandLinearTargetCompletionStoresexpect reason 7 instead of 0 on such a device (VulkanDevice::HostLabels()). Their other checks are unchanged.LabelAfterImportCopystill checks every copied byte after the label.IndirectThreadDimensionsstill checks the group counts a later indirect dispatch reads from the label.Submit.cpp's "reset stored behind the fill" check counts a completion label as well as a GPU label. Either one orders the reset after the fill.FillCompletionLabels: a later fill over a GPU label does not wait for a completion; a later fill over a host label must.IndirectArgumentsPendingProducer: an indirect dispatch reads a GPU label still pending in the table. A host label is behind its completion and is never a trusted pending value.Tested
Setup:
-DCMAKE_OSX_ARCHITECTURES=x86_64; tests run under RosettaANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1mainwith the open macOS build fixes appliedResults:
agc_driver_label_after_import_copyfails before this change, and withAPS5_MOLTENVK_GPU_LABELS=1. It passes after the change in 6 of 6 runs, checking all 32 rounds of 256 KiB.ctest -R 'label|fill'(62 tests): all pass exceptagc_driver_label_ordered_flushandagc_driver_label_ordered_flush_unimported. Both fail the same way before this change; they concern the image store before the label (the GPU-ordered flush, andAPS5_NO_HOST_IMPORT=1) and are a separate fix.ctest -R '^(agc|shader|job)'(1578 tests,-j 1) and compared it with the same run before this change:agc_driver_label_after_import_copyis fixed and nothing that passed before fails because of this change.agc_driver_scratch_poolis intermittent on this machine with and without this change.Not run on lavapipe, AMD or NVIDIA; other drivers keep the GPU path.
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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T15:52:56Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change based on the code reviewed; the author’s hardware results are reported, not independently verified.
What I ran
Reviewed head
50daea2664968f974ed3213c26f3300b39ea2504. No build or tests were run. The supplied change description reports testing on Apple M4 with MoltenVK; I did not reproduce those runs.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The complete text of
core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cppwas not read because it was excluded from the review bundle; I inspected the changed regions through the pinned-head reader. I read the other 14 changed files listed asincludedin the manifest, along with targeted context fromRecorder.cpp,VulkanDevice.cpp, andPacketMemory.cpp. The full repository test results and behavior on non-Apple drivers were not independently verified. No negative control was run.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T00:34:14Z.
Review: COMMENTED
Tested on macOS 27.2 / Apple M4 with MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1, validation layer 1.4.363), x86-64 build under Rosetta. This is the only GPU I tested. Head
50daea2664merged alone onto main3b17977ba6, compared with3b17977ba6. Environment:ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer. Every Vulkan test loggedInsert instance layer "VK_LAYER_KHRONOS_validation"and none loggedwas not found but was requested by env var VK_INSTANCE_LAYERS. 0VUID-lines on either side in everything below.The flake is fixed. I built main, this PR and #20183 side by side and ran the
agc_driver_label_after_import_copy_testsbinary interleaved (A, B, C, A, B, C, ...), 20 runs each, so all three saw the same machine load:A sequential 15-run repeat gave main 6/15 and this PR 0/15.
Label tests:
ctest -R '^agc_driver_.*label.*$' -V, 31 tests, 3 runs per side. No test that passes on main fails with this PR, but 8 tests that pass on main are now skipped on this device:agc_driver_fill_completion_labelsand its_chain,_cpu_disjoint,_cpu_overlap,_inline,_late,_submittedvariantsagc_driver_alias_copy_labelThe description explains the
FillCompletionLabelsskips. Thealias_copy_labelskip isn't mentioned. It comes from that test's existing "not recorded on the GPU" skip branch, which reason 7 now always takes on MoltenVK, so the alias-copy ordering is no longer checked on this device. If a host label can still be checked meaningfully there, consider asserting that ordering for reason 7 too. Otherwise, mention it in the description.The other 22 label tests give the same result on both sides. Four fail identically on both:
label_ordered_flushandlabel_ordered_flush_unimported(fixed by #20183), andunregistered_label_behind_write_backandunregistered_label_on_gpu(fixed by #20057). Across the 29 AGC tests that fail on main on this Mac, the only change islabel_after_import_copy.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T17:15:29Z.
Review: APPROVED
Reviewed — looks good
The host-label path and its completion ordering are supported by the inspected code; no verified blocking defect was found.
What I ran
Reviewed head
eda37555ed57bf37b2acd9e330ff4520f8c6e1d3usingrepo_read.pyfor prepared review artifacts and targeted source ranges. No build or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Full text was read for all changed files marked included in the manifest except
VulkanDevice.cpp, which was read only at relevant ranges throughrepo_read.py. Changed files read in full:VulkanDevice.hpp,PacketMemory.cpp,DeferredLabels.cpp,SynchronizationStatistics.cpp,Recorder.hpp,Recorder.cpp,Submit.cpp,CopiedWriterLabelOrder.cpp,FillCompletionLabels.cpp,IndirectArgumentsPendingProducer.cpp,IndirectThreadDimensions.cpp,LabelAfterImportCopy.cpp,LinearTargetCompletionStores.cpp,UnregisteredLabelOnGpu.cpp, anddocs/dev/ENVIRONMENT.md.VulkanDevice.cppwas excluded from the review folder as too large; targeted ranges were read through the reader. No files were excluded by the manifest’s review rules. Build, tests, and non-MoltenVK driver behavior were not independently verified; test outcomes in the PR description are author-reported.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:23:57Z.
Review: COMMENTED
HostLabelStore::Storesetsstoredbefore checking the range and storing the bytes. If the watcher claims the store and is then delayed, batch completion seestrueand returns while the label is still unwritten. If the watcher fails afterward, it resets the flag after the completion’s opportunity to retry. Please distinguish an in-progress store from a published label and preserve a retry on failure. The claimed-but-unpublished case also matters for the guarantee that a label is visible whenWaitIdlereturns.The trial merge onto
a1b75b0947conflicts in the packet, label, recorder and execution-test files. My finding is from source review. I have no MoltenVK result.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.