fix(agc): order wave LDS accesses with memory barriers on MoltenVK #10858

Merged
maximilianfv merged 1 commit from upstream-pr/10858 into main 2026-11-02 10:24:43 +00:00
maximilianfv commented 2026-10-25 05:51:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T05:51:23Z.


What

Under MoltenVK, agc_driver_lds_tree_reduction and agc_driver_lds_subword_store_branch read LDS before another wave had written it. This makes SubgroupControlBarrierUsable (SubgroupBarrier.hpp) return false for VK_DRIVER_ID_MOLTENVK as well, so MoltenVK takes the subgroup-scope OpMemoryBarrier path that AMD's proprietary driver already uses (#8134). WorkgroupScopeFenceUsable does not change: MoltenVK keeps workgroup-scope fences, and the subword store test passes with them.

Cause, from a MoltenVK shader dump: the recompiler orders one guest wave's LDS accesses with OpControlBarrier (Subgroup execution scope, Workgroup memory scope) inside the branch after s_cbranch_execz. SPIRV-Cross's CompilerMSL::emit_barrier takes the wider of the execution and memory scopes and emits threadgroup_barrier(mem_flags::mem_threadgroup). The tree-reduction dump has 22 threadgroup_barriers for the guest's 5 s_barriers. Waves that skip the branch pair their next barrier with another wave's extra one and read LDS before it is written: the tree reduction read 904, and the subword store read changing garbage. With this change the subgroup-scope OpMemoryBarrier becomes a simdgroup-scope atomic_thread_fence with no execution barrier. That relies on a 32-lane simdgroup running in lockstep, which Metal does not guarantee; the new debt entry "Wave LDS ordering on MoltenVK" in docs/dev/debt/shader.md records this.

The agc_subgroup_barrier driver table (tests/SubgroupBarrier.cpp) used one usable flag for both functions. MoltenVK now differs between the two (false, true), so the flag is split into controlBarrier and workgroupFence. Every other row keeps the old value in both fields.

On Linux and Windows the generated code does not change: the driver ID is never MoltenVK there.

Prior art and credit:

  • Ported from Himansh Raj's (iamthehimansh) commit 50f545c17d "fix(agc): order wave LDS accesses with memory barriers under MoltenVK" (2026-10-21) in the closed #8658. The change is the same, with three differences. That version was macOS-only (#ifdef __APPLE__) in both files; this one is unconditional, because the driver ID alone already selects MoltenVK. It had code comments, which are dropped under the no-comments rule. It flipped the single usable flag, but main later added WorkgroupScopeFenceUsable, which shares the table (#8489), so this version splits the flag instead. Credited with a Co-authored-by: trailer.
  • None of the PRs #8658 was split into (#10362, #10419) carries this commit. #7201 (marion909) and the closed #7332 (ycarogabriel-lgtm) do not change SubgroupBarrier.hpp or the subgroup barrier test. Neither does mugurc's macOS series.
  • Open PRs touching SubgroupBarrier.hpp: #10159 carries only the WorkgroupScopeFenceUsable addition already on main from #8489, on an older base, and does not change MoltenVK's path. #10046 does not touch the header; its VulkanDevice.cpp diff only shows the SubgroupControlBarrierUsable call as context.

Tested

Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2, Vulkan through MoltenVK 1.4.2 from the LunarG Vulkan SDK 1.4.363. main does not build on macOS by itself, so I tested on mugurc's macOS series (ci/macos-series-2, 27e968974) merged with main at 522e32e3b6. The tree also carried other open macOS fixes (#10419 build parts, #10540, #10562, #10641, #10642, #10650, #10651, #10652) and one of my unopened test fixes (agc_driver_graphics). Only this change was toggled between the runs. I tested before rebasing onto current main; the rebased diff is the same.

Both sides: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R '^(agc_subgroup_barrier|agc_driver_lds_.*)$' -E 'audio_in|voice' -V (16 tests), three runs each.

  • Without: 14/16 passed in each run; agc_driver_lds_subword_store_branch and agc_driver_lds_tree_reduction failed.
  • With: 16/16 passed in each run.
  • Validation: neither log has the was not found but was requested by env var VK_INSTANCE_LAYERS line, and both have 0 Validation Error lines.

Not run on Linux or Windows: the generated code does not change there, and agc_subgroup_barrier is a table check that does not depend on the host. Not tested with any title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M1 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363), macOS 27.0.1
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5002225603 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10858). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-25T05:51:23Z. --- ### What Under MoltenVK, `agc_driver_lds_tree_reduction` and `agc_driver_lds_subword_store_branch` read LDS before another wave had written it. This makes `SubgroupControlBarrierUsable` (`SubgroupBarrier.hpp`) return false for `VK_DRIVER_ID_MOLTENVK` as well, so MoltenVK takes the subgroup-scope `OpMemoryBarrier` path that AMD's proprietary driver already uses (#8134). `WorkgroupScopeFenceUsable` does not change: MoltenVK keeps workgroup-scope fences, and the subword store test passes with them. Cause, from a MoltenVK shader dump: the recompiler orders one guest wave's LDS accesses with `OpControlBarrier` (Subgroup execution scope, Workgroup memory scope) inside the branch after `s_cbranch_execz`. SPIRV-Cross's `CompilerMSL::emit_barrier` takes the wider of the execution and memory scopes and emits `threadgroup_barrier(mem_flags::mem_threadgroup)`. The tree-reduction dump has 22 `threadgroup_barrier`s for the guest's 5 `s_barrier`s. Waves that skip the branch pair their next barrier with another wave's extra one and read LDS before it is written: the tree reduction read 904, and the subword store read changing garbage. With this change the subgroup-scope `OpMemoryBarrier` becomes a simdgroup-scope `atomic_thread_fence` with no execution barrier. That relies on a 32-lane simdgroup running in lockstep, which Metal does not guarantee; the new debt entry "Wave LDS ordering on MoltenVK" in `docs/dev/debt/shader.md` records this. The `agc_subgroup_barrier` driver table (`tests/SubgroupBarrier.cpp`) used one `usable` flag for both functions. MoltenVK now differs between the two (false, true), so the flag is split into `controlBarrier` and `workgroupFence`. Every other row keeps the old value in both fields. On Linux and Windows the generated code does not change: the driver ID is never MoltenVK there. Prior art and credit: - Ported from Himansh Raj's (iamthehimansh) commit 50f545c17d "fix(agc): order wave LDS accesses with memory barriers under MoltenVK" (2026-10-21) in the closed #8658. The change is the same, with three differences. That version was macOS-only (`#ifdef __APPLE__`) in both files; this one is unconditional, because the driver ID alone already selects MoltenVK. It had code comments, which are dropped under the no-comments rule. It flipped the single `usable` flag, but `main` later added `WorkgroupScopeFenceUsable`, which shares the table (#8489), so this version splits the flag instead. Credited with a `Co-authored-by:` trailer. - None of the PRs #8658 was split into (#10362, #10419) carries this commit. #7201 (marion909) and the closed #7332 (ycarogabriel-lgtm) do not change `SubgroupBarrier.hpp` or the subgroup barrier test. Neither does mugurc's macOS series. - Open PRs touching `SubgroupBarrier.hpp`: #10159 carries only the `WorkgroupScopeFenceUsable` addition already on `main` from #8489, on an older base, and does not change MoltenVK's path. #10046 does not touch the header; its `VulkanDevice.cpp` diff only shows the `SubgroupControlBarrierUsable` call as context. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2, Vulkan through MoltenVK 1.4.2 from the LunarG Vulkan SDK 1.4.363. `main` does not build on macOS by itself, so I tested on mugurc's macOS series (`ci/macos-series-2`, 27e968974) merged with `main` at 522e32e3b6. The tree also carried other open macOS fixes (#10419 build parts, #10540, #10562, #10641, #10642, #10650, #10651, #10652) and one of my unopened test fixes (`agc_driver_graphics`). Only this change was toggled between the runs. I tested before rebasing onto current `main`; the rebased diff is the same. Both sides: `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ctest --test-dir build -R '^(agc_subgroup_barrier|agc_driver_lds_.*)$' -E 'audio_in|voice' -V` (16 tests), three runs each. - Without: 14/16 passed in each run; `agc_driver_lds_subword_store_branch` and `agc_driver_lds_tree_reduction` failed. - With: 16/16 passed in each run. - Validation: neither log has the `was not found but was requested by env var VK_INSTANCE_LAYERS` line, and both have 0 `Validation Error` lines. Not run on Linux or Windows: the generated code does not change there, and `agc_subgroup_barrier` is a table check that does not depend on the host. Not tested with any title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: Apple M1 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363), macOS 27.0.1 <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 05:53:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T05:53:33Z.

Review: COMMENTED


Comment-only review of exact head 5138b1b1d25664561055b3b5b5810d9780dd93cb.

The failure mechanism is plausible and the change makes the two MoltenVK regression tests pass in the reported M1 Pro setup. I am not approving yet because the new VK_DRIVER_ID_MOLTENVK policy applies the fence-only path to every MoltenVK device, while the evidence is one M1 Pro and the debt entry itself says the path relies on 32-lane SIMD-group lockstep that Metal does not guarantee. Apple's guidance says SIMD-group size varies by GPU and advises querying it at runtime; it also distinguishes simdgroup_barrier (which waits for all threads in the SIMD group) from a memory fence. See Apple's SIMD-group and synchronization guidance.

Could you clarify whether SubgroupControlBarrierUsable is reached only when MoltenVK's reported Vulkan subgroup size matches the assumed guest wave size, and what evidence supports the fence-only lowering on other MoltenVK GPUs? If the current evidence only establishes M1 Pro, please consider limiting the policy to a supported subgroup/device condition or documenting the exact tested support boundary. I have not independently run the Metal/Vulkan tests; the three-run results and generated shader observations are author-reported.

AI-assisted review: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641771592 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10858#pullrequestreview-5641771592). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T05:53:33Z. **Review:** COMMENTED --- Comment-only review of exact head `5138b1b1d25664561055b3b5b5810d9780dd93cb`. The failure mechanism is plausible and the change makes the two MoltenVK regression tests pass in the reported M1 Pro setup. I am not approving yet because the new `VK_DRIVER_ID_MOLTENVK` policy applies the fence-only path to every MoltenVK device, while the evidence is one M1 Pro and the debt entry itself says the path relies on 32-lane SIMD-group lockstep that Metal does not guarantee. Apple's guidance says SIMD-group size varies by GPU and advises querying it at runtime; it also distinguishes `simdgroup_barrier` (which waits for all threads in the SIMD group) from a memory fence. See [Apple's SIMD-group and synchronization guidance](https://developer.apple.com/documentation/apple-silicon/porting-your-metal-code-to-apple-silicon). Could you clarify whether `SubgroupControlBarrierUsable` is reached only when MoltenVK's reported Vulkan subgroup size matches the assumed guest wave size, and what evidence supports the fence-only lowering on other MoltenVK GPUs? If the current evidence only establishes M1 Pro, please consider limiting the policy to a supported subgroup/device condition or documenting the exact tested support boundary. I have not independently run the Metal/Vulkan tests; the three-run results and generated shader observations are author-reported. AI-assisted review: yes.
jotitadev commented 2026-10-27 06:39:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T06:39:13Z.

Review: COMMENTED


A second Apple GPU (M2 Max) on a main-based tree, with both MoltenVK builds. The other runs here are Windows, where the change is a no-op.

Machine: Apple M2 Max, macOS 27.2, x86_64 Release build under Rosetta 2.
Tree: main b986c11a34 + the open macOS PRs needed to build and open a device (#10366, #7631, #10659, #10660, #11249, #11358, #10669, #10552, #7804, #10676, #11447, #11437, #11554, #11705) + this PR at 5138b1b1d2 (clean merge). Control: the same tree without this PR. ANYPS5_NO_SHADER_CACHE=1, -j1 --timeout 30.
Drivers: MoltenVK 1.4.2 (LunarG SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704.

Without With (3 runs)
agc_driver_lds_tree_reduction fails on both drivers: "round 0: thread 32 (wave 1) read 904, expected 32896" passes 3/3 on both drivers
agc_driver_lds_subword_store_branch passes, 6/6 on each driver passes 3/3 on both drivers
agc_subgroup_barrier passes passes
  • The tree-reduction failure is the same value you saw on the M1 Pro (904), so that one reproduces on a second Apple GPU and on MoltenVK main.
  • The subword store branch test did not fail here without the change (1 + 5 repeated runs per driver). On this machine it does not show the garbage reads, so it is weaker evidence for the fence-only path than the tree reduction.
  • Full ^agc suite, 1037 tests, MoltenVK 1.4.2: 41 failures without, 40 with. lds_tree_reduction is the only difference, so nothing new fails.
  • Validation layer (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, SDK 1.4.363.0, loader output shows it inserted) on the three tests: no VUID- on either side.

GPUs tested: Apple M2 Max (MoltenVK 1.4.2 and MoltenVK main + #2704)

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5663156622 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10858#pullrequestreview-5663156622). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T06:39:13Z. **Review:** COMMENTED --- A second Apple GPU (M2 Max) on a `main`-based tree, with both MoltenVK builds. The other runs here are Windows, where the change is a no-op. **Machine:** Apple M2 Max, macOS 27.2, x86_64 Release build under Rosetta 2. **Tree:** `main` `b986c11a34` + the open macOS PRs needed to build and open a device (#10366, #7631, #10659, #10660, #11249, #11358, #10669, #10552, #7804, #10676, #11447, #11437, #11554, #11705) + this PR at `5138b1b1d2` (clean merge). Control: the same tree without this PR. `ANYPS5_NO_SHADER_CACHE=1`, `-j1 --timeout 30`. **Drivers:** MoltenVK 1.4.2 (LunarG SDK 1.4.363.0) and MoltenVK `main` + SPIRV-Cross #2704. | | Without | With (3 runs) | |---|---|---| | `agc_driver_lds_tree_reduction` | fails on both drivers: "round 0: thread 32 (wave 1) read 904, expected 32896" | passes 3/3 on both drivers | | `agc_driver_lds_subword_store_branch` | **passes**, 6/6 on each driver | passes 3/3 on both drivers | | `agc_subgroup_barrier` | passes | passes | - The tree-reduction failure is the same value you saw on the M1 Pro (904), so that one reproduces on a second Apple GPU and on MoltenVK `main`. - The subword store branch test did not fail here without the change (1 + 5 repeated runs per driver). On this machine it does not show the garbage reads, so it is weaker evidence for the fence-only path than the tree reduction. - Full `^agc` suite, 1037 tests, MoltenVK 1.4.2: 41 failures without, 40 with. `lds_tree_reduction` is the only difference, so nothing new fails. - Validation layer (`VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, SDK 1.4.363.0, loader output shows it inserted) on the three tests: no `VUID-` on either side. GPUs tested: Apple M2 Max (MoltenVK 1.4.2 and MoltenVK main + #2704) Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Sign in to join this conversation.
No reviewers
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!10858
No description provided.