Skip to content

fix(shader): fence the scratch pool slot release on MoltenVK - #24201

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Jazeupoi:fix/scratch-pool-moltenvk-release-fence
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Jazeupoi:fix/scratch-pool-moltenvk-release-fence

Conversation

@Jazeupoi

Copy link
Copy Markdown
Contributor

What

On MoltenVK, agc_driver_scratch_pool fails most runs, as XIIYAZ listed in #1 (comment):

AGC graphics: scratch pool eviction over the total budget: first program lane 709 dword 0 read 0x00002414, expected 0x00000b14

A workgroup gives its scratch pool slot back with an OpAtomicIDecrement (AcquireRelease, Uniform). The last invocation then stores 0 to the slot's lock word. SPIRV-Cross turns these atomics into memory_order_relaxed with no fence. So a workgroup can release its slot before its own scratch loads and stores complete, and the next workgroup that takes the slot overwrites them.

This change emits an OpMemoryBarrier (Device, AcquireRelease | UniformMemory) before the decrement. It does so only when ldsLockFences is set, which is MoltenVK only. That is the flag the LDS lock fences already use for the same reason. With MSL 3.2 and later, SPIRV-Cross emits it as atomic_thread_fence(mem_flags::mem_device, memory_order_seq_cst, thread_scope_device). Other drivers get the same SPIR-V as before, so the AMD fix in #16274 is untouched.

Tested

Apple M4, macOS 27.0.1, MoltenVK 1.4.2, x86_64 Release build under Rosetta 2, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, each run being ctest -R '^agc_driver_scratch_pool$' -j1. The tree was main with #17425 and #11049, which the macOS build needs. Neither touches the shader recompiler.

Tree agc_driver_scratch_pool
without this change fails 17 of 20 (14 of 20 in an earlier run)
this PR passes 50 of 50

agc_flat_scratch, agc_guarded_memory_loads, agc_spirv_lds_atomic_lock and agc_shader_disk_cache pass with the change.

Not run on Windows, Linux or a non-Apple GPU. There ldsLockFences is false, so the SPIR-V does not change. Not run with MSL older than 3.2, where SPIRV-Cross emits the barrier as threadgroup_barrier(mem_flags::mem_device).

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 M4 (MoltenVK 1.4.2, macOS 27.0.1)
  • AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

The changed fence is emitted before scratch slot release only when the MoltenVK-specific ldsLockFences option is enabled; no blocking defect was verified.

What I ran

Reviewed head 47e6c7c87cdf6cda91bf9363d94edec28504bacf. Inspected the prepared diff and source context using repo_read.py reader tools. No tests were run.

Results

Check Result Note
Diff and changed code passed One changed line reviewed
Target option wiring passed MoltenVK flag reaches emitter
Tests not run No local test execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

Author-reported device test results were not independently verified. No build, test, or physical-console run was performed. The review bundle excluded no changed files; the changed file's full text and relevant option wiring were read.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot added the shader core/shader label Nov 20, 2026
@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 7642 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvScratchPool.cpp
PR 9508 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvScratchPool.cpp

@Colewiz7

Copy link
Copy Markdown
Contributor

Head 47e6c7c87c merged onto main 0a025bd3c6 as 8d870f0c8f, no conflict. Linux 7.3.4, GCC 16.2.1 Release, Mesa 26.2.6 RADV, RX 7900 XT (gfx1100) and 7800X3D iGPU (gfx1036).

No new failures and no new validation messages on either GPU.

RX 7900 XT iGPU
tests run (6388 on the 7900 XT, the 2048 GPU ones on the iGPU), new failures vs main none none
-R '^agc' ( tests) with the layer, new failures vs main none none
VUID- / SYNC-HAZARD lines 0 / 0 0 / 0

Layer runs used VK_LAYER_KHRONOS_validation 1.4.363.1 with VK_LOADER_DEBUG=layer and ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1; the loader inserted the layer in every run, and the set of distinct validation messages matches main.

AI-assisted: yes

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 47e6c7c87c, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#17478, #21223, #24030, #14202, #18889, #19531, #21641, #21773, #22468, #22471, #23239, #23255, #23484, #23869, #23992, #24311, #18425, #21195, #21255, #22989, #23507, #23886, #14582, #17878, #19666, #19899, #20138, #21656, #21746). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
none: no test of its own in this PR - -

Whole batch: the full ctest -j 12 (6522 tests) on the RTX 3080 has 4 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked. The 2125 agc_/video tests on the iGPU have 2 failing: agc_driver_stencil_plane_memory, agc_driver_store_over_untracked (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

This was referenced Nov 21, 2026
@Gonic0

Gonic0 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Independent check on another Apple GPU: Apple M5 Pro, macOS 27.3, MoltenVK from Vulkan SDK 1.4.363.0, x86_64 Release under Rosetta 2, main 0538bc2 with #17425 and #11049, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, ctest -R '^agc_driver_scratch_pool$'.

Tree agc_driver_scratch_pool
without this change fails 10 of 10 (discarded-batch retry and eviction subtests, a lane reading another workgroup's value)
this PR passes 20 of 20

With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer the log shows the layer inserted and no VUID-. On KosmicKrisp (Mesa, also on Metal) the test already passes without the change.

The dumped SPIR-V cross-compiled with the SDK's spirv-cross --msl --msl-version 30200 shows what the description says: every atomic becomes memory_order_relaxed, and the added OpMemoryBarrier becomes atomic_thread_fence(mem_flags::mem_device, memory_order_seq_cst, thread_scope_device).

@oneandonlydean
oneandonlydean merged commit 8632281 into boykopovar:main Nov 27, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants