AGC on MoltenVK: a GPU label becomes visible to the CPU before earlier GPU writes to the same host import #20118

Open
opened 2026-11-14 16:29:09 +00:00 by XIIYAZ · 0 comments
XIIYAZ commented 2026-11-14 16:29:09 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T16:29:09Z.


What happens

On MoltenVK (Apple M4), a label that WriteLabelOnGpu records after a GPU copy can be seen by the CPU while some cache lines of that copy still hold their old bytes. agc_driver_label_after_import_copy fails with round 6: a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied.

Title, test or build step

Three ctest cases rest on the same ordering:

  • agc_driver_label_after_import_copy: a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied (the round and the count vary).
  • agc_driver_label_ordered_flush_unimported: label ordered flush: the label was visible before the image store it follows.
  • agc_driver_label_ordered_flush: label ordered flush: no label store took the GPU-ordered flush over the host import.

AnyPS5 commit

main 290ee1aa0f with the open macOS build fixes merged locally (#17425, #11049, #11437), host report commit 3f3a8df38aae. The three tests fail the same way with and without my other open MoltenVK PRs applied.

System

Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0, Release build with -DCMAKE_OSX_ARCHITECTURES=x86_64, run under Rosetta.

Host report

AnyPS5 host report (format 1)
commit: 3f3a8df38aae
[system]
os: Darwin 25.6.0 x86_64
cpu: VirtualApple @ 2.50GHz
cpu_threads: 10
memory_mib: 16384
[gpu]
device: Apple M4
type: integrated
vendor_id: 0x106b
device_id: 0x1a070209
vulkan_api: 1.1.357
driver: MoltenVK
driver_info: 1.4.2
subgroup_size: 32
device_local_mib: 16384
[vulkan]
VK_EXT_external_memory_host: extension optional present
...

Steps to reproduce

S=$HOME/VulkanSDK/1.4.363.1/macOS
export DYLD_LIBRARY_PATH=$S/lib VK_ICD_FILENAMES=$S/share/vulkan/icd.d/MoltenVK_icd.json ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1
ctest --test-dir build -R '^agc_driver_label_(after_import_copy|ordered_flush|ordered_flush_unimported)$' --output-on-failure

agc_driver_label_after_import_copy stops within its 32 rounds on most runs; the other two fail on every run.

Log

[write-watch] host imports resolve write protection: no (0 of 4 scratch pages written after a GPU read, 0 after a GPU write, 0 after the import); imported ranges stay watched
AGC graphics: round 6: a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied

What was checked

To find which bytes are stale and whether they arrive later, I instrumented LabelAfterImportCopy.cpp locally (not part of any PR). When the check fails, it records the stale range, calls WaitIdle(), and counts again:

DBG round 6  stale 192 at [0x3180..0x573f] after idle 0
DBG round 31 stale 64  at [0x3c00..0x3c3f] after idle 0
  • The stale bytes are whole 64-byte lines, scattered through the 256 KiB destination, and different in each run.
  • Every one of them holds the copied value once the batch has finished (after idle 0). No write is lost; the GPU's label store becomes CPU-visible before part of the transfer recorded ahead of it in the same batch.
  • APS5_DRAIN_COMPLETION_LABELS=1 and APS5_LABEL_GATE_ALL=1 do not change the result. In this test no copied writer is listed and no completion store is pending, so the label still goes to the GPU (reason 0).

Vulkan makes device writes visible to the host only through a device-to-host dependency: a fence or event wait plus VK_ACCESS_HOST_READ_BIT. Polling a value the GPU wrote earlier in the same batch is not such a dependency. AMD and NVIDIA happen to land the writes of one queue in order, which is what the console's labels guarantee and what these tests check. Apple's GPU through MoltenVK does not, for writes to a host-imported (VK_EXT_external_memory_host) range.

A fix I can see, and did not want to make without agreement because it changes label timing on macOS: on MoltenVK, store the labels that guest code polls behind the batch's completion. The CPU would write them when the batch is reaped, the way Recorder::AfterCompletions already handles a small label while a copied writer is listed (reason 5). That orders them after every write of the batch. The cost is that a label lands only once its batch is reaped, so a title spinning on a label depends on the reap happening promptly. Is that acceptable on macOS, or is there a preferred way to get the ordering there?

Related, but a different cause: #17937 (two stores of one label run with no barrier between them).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6392844354 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/20118). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T16:29:09Z. --- ### What happens On MoltenVK (Apple M4), a label that `WriteLabelOnGpu` records after a GPU copy can be seen by the CPU while some cache lines of that copy still hold their old bytes. `agc_driver_label_after_import_copy` fails with `round 6: a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied`. ### Title, test or build step Three ctest cases rest on the same ordering: - `agc_driver_label_after_import_copy`: `a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied` (the round and the count vary). - `agc_driver_label_ordered_flush_unimported`: `label ordered flush: the label was visible before the image store it follows`. - `agc_driver_label_ordered_flush`: `label ordered flush: no label store took the GPU-ordered flush over the host import`. ### AnyPS5 commit `main` `290ee1aa0f` with the open macOS build fixes merged locally (#17425, #11049, #11437), host report commit `3f3a8df38aae`. The three tests fail the same way with and without my other open MoltenVK PRs applied. ### System Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0, Release build with `-DCMAKE_OSX_ARCHITECTURES=x86_64`, run under Rosetta. ### Host report ``` AnyPS5 host report (format 1) commit: 3f3a8df38aae [system] os: Darwin 25.6.0 x86_64 cpu: VirtualApple @ 2.50GHz cpu_threads: 10 memory_mib: 16384 [gpu] device: Apple M4 type: integrated vendor_id: 0x106b device_id: 0x1a070209 vulkan_api: 1.1.357 driver: MoltenVK driver_info: 1.4.2 subgroup_size: 32 device_local_mib: 16384 [vulkan] VK_EXT_external_memory_host: extension optional present ... ``` ### Steps to reproduce ``` S=$HOME/VulkanSDK/1.4.363.1/macOS export DYLD_LIBRARY_PATH=$S/lib VK_ICD_FILENAMES=$S/share/vulkan/icd.d/MoltenVK_icd.json ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest --test-dir build -R '^agc_driver_label_(after_import_copy|ordered_flush|ordered_flush_unimported)$' --output-on-failure ``` `agc_driver_label_after_import_copy` stops within its 32 rounds on most runs; the other two fail on every run. ### Log ``` [write-watch] host imports resolve write protection: no (0 of 4 scratch pages written after a GPU read, 0 after a GPU write, 0 after the import); imported ranges stay watched AGC graphics: round 6: a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied ``` ### What was checked To find which bytes are stale and whether they arrive later, I instrumented `LabelAfterImportCopy.cpp` locally (not part of any PR). When the check fails, it records the stale range, calls `WaitIdle()`, and counts again: ``` DBG round 6 stale 192 at [0x3180..0x573f] after idle 0 DBG round 31 stale 64 at [0x3c00..0x3c3f] after idle 0 ``` - The stale bytes are whole 64-byte lines, scattered through the 256 KiB destination, and different in each run. - Every one of them holds the copied value once the batch has finished (`after idle 0`). No write is lost; the GPU's label store becomes CPU-visible before part of the transfer recorded ahead of it in the same batch. - `APS5_DRAIN_COMPLETION_LABELS=1` and `APS5_LABEL_GATE_ALL=1` do not change the result. In this test no copied writer is listed and no completion store is pending, so the label still goes to the GPU (reason 0). Vulkan makes device writes visible to the host only through a device-to-host dependency: a fence or event wait plus `VK_ACCESS_HOST_READ_BIT`. Polling a value the GPU wrote earlier in the same batch is not such a dependency. AMD and NVIDIA happen to land the writes of one queue in order, which is what the console's labels guarantee and what these tests check. Apple's GPU through MoltenVK does not, for writes to a host-imported (`VK_EXT_external_memory_host`) range. A fix I can see, and did not want to make without agreement because it changes label timing on macOS: on MoltenVK, store the labels that guest code polls behind the batch's completion. The CPU would write them when the batch is reaped, the way `Recorder::AfterCompletions` already handles a small label while a copied writer is listed (reason 5). That orders them after every write of the batch. The cost is that a label lands only once its batch is reaped, so a title spinning on a label depends on the reap happening promptly. Is that acceptable on macOS, or is there a preferred way to get the ordering there? Related, but a different cause: #17937 (two stores of one label run with no barrier between them).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#20118
No description provided.