Skip to content

fix(shader): apply returning LDS atomics in lane order on MoltenVK - #14218

Draft
frankea wants to merge 19 commits into
boykopovar:mainfrom
frankea:fix/lds-atomic-return-lane-order
Draft

frankea wants to merge 19 commits into
boykopovar:mainfrom
frankea:fix/lds-atomic-return-lane-order

Conversation

@frankea

@frankea frankea commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

What

A wave's same-address LDS atomics with a used return value are now applied in ascending lane order on MoltenVK. Programs that rank lanes with the returned value, such as a radix sort that takes each record's position inside its wave from ds_add_rtn_u32 on byte-packed per-wave counters, were unstable on Apple GPUs.

Semantics and sources

  • RDNA 2 ISA reference guide (30 November 2020), section 12.13 "LDS & GDS Instructions", p. 193: DS_ADD_RTN_U32 is tmp = MEM[ADDR]; MEM[ADDR] += DATA; RETURN_DATA = tmp, per lane, and the other returning DS atomics follow the same pattern. Section 10.4.3 (p. 94) says only that atomic operations can return the LDS "pre-op" value. Neither states in which order the lanes of one wave that hit one address are applied. The only place the guide characterises LDS atomic ordering, section 2.3.1 "Local Data Share (LDS)" (printed page 9), calls them "fast, unordered atomic operations".
  • LLVM's AMDGPU atomic optimizer (which covers LDS) and Mesa's nir_opt_uniform_atomics replace same-address atomics with one atomic plus a lane-order exclusive scan, so AMD's compilers treat lane order as an allowed result. That does not say what the hardware does when every lane issues its own atomic.
  • This PR assumes ascending lane order, and the hardware oracle has now measured it. angrysandhill ran it on an AMD Radeon RX 6750 XT (gfx1031, RDNA2; Windows 10 22H2, Adrenalin 26.10.1) for ds_add_rtn_u32 with data 1 from every lane on one LDS dword, ds_wrxchg_rtn_b32, and the add under EXEC 0x55555555, in wave32 and wave64, with 5 identical runs each: lane i returned i (i/2 under the mask) in every case (comment). Jirka135 ran 12 returning forms (ds_add, sub, rsub, inc, dec, min_u32, max_u32, xor, mskor, wrxchg, cmpst, ds_add_rtn_u64) on an AMD Radeon 8060S (gfx1151, RDNA 3.5), 8,192 lanes per case in wave32 and wave64, all lanes and with about 1 lane in 8 masked off: 0 returns and 0 final LDS values differ from ascending lane order (comment). The console's gfx1013 is not measured; the debt entry now cites both runs and closes on that measurement. A shipped radix sort pass (GT7's BVH build, below) is only stable under this order: each record's rank is "lanes of this wave that hit this address before me".
  • Vulkan memory model: a scoped modification order totally orders the atomic writes to a location but "relates no other operations", and program order is defined per invocation. Nothing ties that order to SubgroupLocalInvocationId, so one OpAtomicIAdd per invocation may be applied in any lane order.
  • On MoltenVK, SPIRV-Cross emits that atomic as atomic_fetch_add_explicit(..., memory_order_relaxed) on threadgroup memory. Relaxed atomics give no order between threads, and the M5 Max uses that freedom (measured below).

Measured with the new agc_driver_lds_atomic_lane_order test on main (hand-encoded RDNA; 128 workgroups per case; for the order columns every lane of a wave does ds_add_rtn_u32 with data 1 on one LDS dword per wave, with and without an EXEC mask; the forms column uses random operands on up to 8 addresses per wave with about 1 lane in 8 masked off, in workgroup LDS and again with 16,384 LDS dwords, which puts the LDS in a storage buffer on every device):

GPU wave64, 64/128/256 threads wave32, 32/64/128/256 threads 18 returning 32-bit integer forms + ds_add_rtn_u64, wave32 and wave64, 256 threads, workgroup and device-memory LDS
Apple M5 Max, MoltenVK 1.4.2 0 waves in lane order; one lane-to-rank map in every wave: 0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27,4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31, then the same +32 for lanes 32-63 (lanes 0-31 always before 32-63) 0 waves in lane order, the same 32-lane map every 32-bit form differs from lane order (4,003 to 18,132 of 32,768 returns per form, the same in both LDS placements); ds_add_rtn_u64 is in lane order (the 64-bit LDS lock already serializes lanes with OpGroupNonUniformElect)
NVIDIA RTX 4090, 616.92 all waves in lane order all waves in lane order all in lane order, in both LDS placements

The M5 Max serves each 32-lane SIMD-group in quads 0,4,1,5,2,6,3,7. For ds_rsub, ds_inc, ds_dec, ds_mskor, ds_wrxchg, ds_wrxchg2, ds_cmpst and ds_wrap, the final LDS values differ as well, not just the returns. ds_wrxchg2_rtn_b32 and ds_wrxchg2st64_rtn_b32 lower to two ds_wrxchg_rtn_b32 halves; the test keeps their two addresses apart, so the order of the halves does not matter.

Change

  • New SpirvTarget::laneOrderedLdsAtomics, plumbed like ldsLockFences: SpirvTargetOptions, SpirvEmitterState, the shader cache key, agc_shader_replay --lane-ordered-lds-atomics.
  • The new SPIR-V test is registered in its own fragment, tests/cmake/agc_spirv_lds_atomic_lane_order_tests.cmake (written by tools/agc_test_split.py resolve during the rebase); the new execution test is picked up by the tests/execution glob.
  • VulkanDevice::buildTarget sets it only on VK_DRIVER_ID_MOLTENVK (LdsAtomicLaneOrderRequired). The RTX 4090 on Windows (616.92) was in lane order in every case measured here; domx4q's RTX 4090 run on Linux (620.70.06) was not for wave64 in one-wave 64-thread workgroups (the spanned mode), see Tested. Other drivers keep main's order in this PR.
  • It applies to an LDS atomic (SharedAtomic*) only when:
    • its result is used;
    • the LDS is workgroup memory or the device-memory LDS buffer;
    • the program does not already take the 64-bit LDS lock.
  • With laneCount 2 (wave64 on a 32-lane host), both halves are handled in one loop at the first half's emission, so lanes 0-31 rank before lanes 32-63 by construction rather than by issue order.
  • 32-bit add, sub, smin, umin, smax, umax, and, or and xor (aggregated path):
    • The leader is the first pending lane, found with a ballot and FindLSB.
    • Every pending lane on the leader's address joins the round.
    • The leader does one OpAtomic* with the combined operand of the round. Each lane returns the old value combined with an exclusive scan of the lower lanes' operands.
    • Add and sub use OpGroupNonUniformIAdd ExclusiveScan. The others use a Hillis-Steele scan over OpGroupNonUniformShuffle, with only the totals from Reduce. SPIRV-Cross refuses the other scans for Metal: Metal doesn't support ExclusiveScan for OpGroupNonUniformSMin appeared while testing on MoltenVK.
    • Cost: one loop round, with one atomic, per distinct address the wave touches, so 1 to 64 rounds per instruction. GT7's sort pass (8-bit digits) touches about 57 addresses per wave with random keys.
  • Every other returning LDS atomic, and every form on a device without subgroup arithmetic (serialized path):
    • Covers swap, inc, dec, rsub, mskor, cmpst, wrap, the float forms, and 64-bit forms in device-memory LDS.
    • Each loop iteration runs the existing emission for the first pending lane only, so 1 to 64 rounds per instruction.
    • Each round ends with OpControlBarrier whose execution and memory scopes are both the wave LDS scope (WaveLdsScope: Subgroup when one host subgroup holds the guest wave, Workgroup when the workgroup is one wave, including the spanned mode), with AcquireRelease|WorkgroupMemory, plus UniformMemory when the LDS is in device memory. Every invocation of the wave executes it after the leader branch and before the next ballot, so under the Vulkan memory model round k synchronizes with round k+1 and comes first in the address's modification order; the order does not rest on SIMD-group issue order. SPIRV-Cross emits it as simdgroup_barrier or threadgroup_barrier. The derivation is in this comment.
    • SPIR-V 1.6 (section 3.3.20) makes a Workgroup-scope OpControlBarrier undefined unless every invocation of the workgroup executes the same dynamic instance. Guest branches are scalar: s_cbranch_execz, execnz, vccz and vccnz become subgroup ballots and scc branches scalar values, and EXEC-inactive lanes run every round with nothing pending, so the rounds sit in uniform control flow. The emitter checks this on every module with rounds: it runs the barrier uniformity analysis the spanned mode uses, counting a subgroup-scope collective with an invocation-independent result (ballot, broadcast, Reduce, a shuffle from a uniform index) in uniform control flow as uniform, and throws if a control barrier sits in control flow a per-invocation value decides.
    • Where there is no wave LDS scope (non-compute stages, or several waves per workgroup on a host subgroup that is not 32 lanes wide), the serialized forms take the plain path and keep the host order. MoltenVK reports 32-lane subgroups, so it does not reach that case.
  • Spanned waves take the serialized path through the existing spanned ballot.
  • Split subgroups (wave32 on a host subgroup wider than 32 lanes): min, max, and, or and xor cannot use the shuffle scan, which reads lower host lanes that belong to the other wave and may be inactive, and the wave's ballot word is chosen by invocation index, which the uniformity check cannot prove uniform; so all per-lane forms take the plain path there. Add and sub keep the aggregated path, since their native scans count only active invocations. No tested GPU has such subgroups; agc_spirv_lds_atomic_lane_order checks the emission.
  • Measured cost on the M5 Max (the test's program as a microkernel: 8,192 workgroups of 256 threads, wave64, the returning atomic plus 9 other memory operations per thread; best of 7 dispatches, two runs, main vs this PR; the GPU was shared with a running game, so the numbers are noisy):
    • ds_add_rtn_u32, 64 distinct addresses per wave: 4.46-4.65 vs 5.35-5.73 ms (+15-29%).
    • ds_min_rtn_u32, 64 distinct addresses (shuffle scan): 4.58-4.69 vs 6.72-6.85 ms (+43-50%).
    • ds_wrxchg_rtn_b32 (serialized), 64 distinct addresses: 4.49-4.78 vs 4.63-4.77 ms (within noise; an earlier pair of runs gave +4-20%).
    • One address per wave, all three: within noise.
    • Serving several addresses per round would cut the many-address cost; that is left for a later change.
  • Not covered, recorded in docs/dev/debt/changes/fix-lds-atomic-return-lane-order.md:
    • LDS atomics whose result is unused keep the host order. For add, sub, min, max, and, or and xor the final LDS value does not depend on the order. For rsub, inc, dec, mskor, cmpst, wrxchg, wrap and the float forms it can: with per-lane operands that differ, the M5 Max leaves 572 to 3,683 of 32,768 final dwords out of lane order for the non-returning ds_rsub_u32, ds_inc_u32, ds_dec_u32, ds_mskor_b32 and ds_cmpst_b32. Serializing those would cost up to 64 rounds per instruction on a still unmeasured assumption, and no title is known to need it. With one operand for every lane, as a ring counter's ds_inc_u32 has, the final value is the same in any order.
    • GDS atomics.
    • Flat atomics whose address lands in the LDS aperture.
    • AMD hosts, which are not reordered. angrysandhill's run of the three oracle bodies through the recompiler on the RX 6750 XT's Vulkan driver returned the same values as the hardware.

Tested

Run on 2026-10-31 with this branch at 0700fe6c79, rebased onto main b08c539. It was then rebased onto main 8a4fc62 (head 2053545) with no conflicts; on the M5 Max the focused set on that head gave the same result as below (41 pass, 6 skip, the same 2 LDS failures; 0 VUID- lines). Since then the branch has only gained commits: main merged at e07db72 (11/07, including #10858), then the round barrier (98d69b6) and its execution test (9c36d01); see the head 9c36d01 paragraph after this list. Builds: macOS 27.0.1 on an Apple M5 Max (x86_64 under Rosetta, AppleClang 21, MoltenVK 1.4.2 from LunarG SDK 1.4.363.0), and Windows 11 10.0.26200 on an RTX 4090 (driver 616.92, winlibs GCC 15.2). main does not build the AGC driver on macOS by itself, so both Mac trees carry the same local macOS build aids (27 files, not part of this pull request); only this change differs between them. Both machines run the same focused set on main (b08c539 plus only the new execution test, in the version without the two forced passes, which need the new flag) and on this branch, with and without VK_LAYER_KHRONOS_validation, using ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -V. The set is 49 tests on the branch and 48 on main (no agc_spirv_lds_atomic_lane_order there): agc_driver_lds_*, pixel_lds*, buffer and global atomics, wave, lane and subgroup tests, agc_spirv_lds_atomic_*, agc_subgroup_barrier and agc_shader_disk_cache. A VK_LOADER_DEBUG=layer probe shows Insert instance layer "VK_LAYER_KHRONOS_validation" on both sides on both machines. The rebase changed only where the SPIR-V test is registered; the cost figures and the non-returning counts above (a local probe, not part of this pull request) and the GT7 replay below come from the same emission code before the rebase.

  • Apple M5 Max:
    • On main, agc_driver_lds_atomic_lane_order fails (87 cases out of lane order; 0 of 14 same-address configurations in lane order), with and without the validation layer.
    • On the branch it passes, in all three passes: the device target, the flag forced on, and the flag forced on without GroupNonUniformArithmetic (serialized path). Each pass checks every form in workgroup and in device-memory LDS: 228 op lines, 0 differences.
    • agc_driver_lds_subword_store_branch and agc_driver_lds_tree_reduction fail identically on main and the branch. These are wave-LDS barriers in SIMD-group-divergent code, the issue fix(agc): order wave LDS accesses with memory barriers on MoltenVK #10858 addresses; they are not touched here.
    • 6 tests skip on both sides because MoltenVK lacks the feature: agc_driver_buffer_atomics, agc_driver_global_atomics64, agc_driver_global_atomics_float and agc_driver_global_atomics_lanes (no shaderBufferInt64Atomics), agc_driver_mesh_vector_window_waves (no VK_EXT_mesh_shader) and agc_driver_ngg_vertex_wave_reduction (no subgroup arithmetic in vertex shaders). So the branch passes 41 of the 43 tests that run and main 39 of 42.
    • 0 VUID-, SYNC-HAZARD or Validation Error lines on either side.
  • RTX 4090:
    • main: 47 of 48 pass and 1 is skipped, with and without the validation layer. The new execution test's device-target pass is in lane order for every case: 14 of 14 same-address configurations, and 76 op lines with 0 differences, in workgroup and device-memory LDS.
    • The branch: 48 of 49 pass and 1 is skipped, with and without the validation layer. That includes the new execution test's three passes (228 op lines, 0 differences); the forced passes run the aggregated path and, without subgroup arithmetic, the serialized path on NVIDIA as well.
    • The skip is agc_driver_vertex_elected_atomic_max on both sides (the device runs vertex programs in subgroups). tools/check_skips.py --job windows passes on all four reports.
    • 0 VUID-, SYNC-HAZARD or Validation Error lines on either side.
  • lavapipe (CI) was not run here. The new execution test skips on devices whose subgroups are narrower than 32 lanes, as agc_driver_ds_append_consume does, since a wave then spans several host subgroups; whether CI's lavapipe skips it or runs it is not checked. agc_spirv_lds_atomic_lane_order needs no GPU.
  • agc_spirv_lds_atomic_lane_order (new, no GPU) checks the emitted SPIR-V:
    • Wave64 ds_add_rtn_u32 emits one OpAtomicIAdd for both halves, with an exclusive scan and a reduce per half (wave32: one of each).
    • ds_min_rtn_u32 reduces with OpGroupNonUniformUMin and never scans with it.
    • ds_inc_rtn_u32 gains exactly one lane loop.
    • Wave32 on a 64-lane host: ds_add_rtn_u32 still scans once and adds once; ds_min_rtn_u32 in a one-wave workgroup compiles with the atomic, loop and control-barrier counts of the unordered compile; with several waves per workgroup ds_wrxchg_rtn_b32 and ds_min_rtn_u32 compile with the atomic, loop and control-barrier counts of the unordered compile.
    • Round barrier (head 9c36d01): for each serialized atomic the test walks the control-flow graph and requires that every path from the atomic to the lane loop's latch passes an OpControlBarrier with execution scope equal to memory scope and to the expected wave scope, with the semantics above, in a block outside the leader branch that dominates the latch. Cases: masked ds_wrxchg_rtn_b32 for wave32 with 256 and 32 threads, wave64 on a 32-lane host, wave64 with device-memory LDS, spanned wave64 with workgroup and device-memory LDS, masked ds_cmpst_rtn_b32, and serialized ds_add_rtn_u32 without subgroup arithmetic. The aggregated ds_add_rtn_u32 must have no control barrier in its loop. The same module with its control barriers replaced by OpNop must fail the check; the emitter before 98d69b6 fails it.
    • Divergent guest flow (head ea559cd): ds_wrxchg_rtn_b32 behind s_cbranch_execz, s_cbranch_scc0 and s_cbranch_vccz after s_and_saveexec, and inside a loop that drops lanes from EXEC and repeats with s_cbranch_execnz, for wave32 with 256 and 32 threads, wave64 on a 32-lane host with 256 and 64 threads, and spanned wave64: all 20 compile and pass the round barrier check and the uniformity analysis; the 16 non-spanned modules with their ballots turned into OpGroupNonUniformInverseBallot are all rejected. spanned_uniformity adds 9 cases for the subgroup-collective analysis.
    • Without subgroup arithmetic, ds_add_rtn_u32 takes the serialized path.
    • An unused ds_add_u32 and programs under the 64-bit lock compile byte-identically with and without the flag.
  • agc_subgroup_barrier checks that only MoltenVK requires the order. agc_shader_disk_cache checks that the flag changes the cache key.

Head 9c36d01, on the Apple M5 Max only (MoltenVK 1.4.2, both a locally built MoltenVK ICD and the Vulkan SDK 1.4.363.0 ICD; the RTX 4090 was not rerun on this head). The new CheckSwapChain case of agc_driver_lds_atomic_lane_order runs two back-to-back ds_wrxchg_rtn_b32 with unique data on one dword under 9 EXEC patterns (wave32) or 13 (wave64), with 256 threads and with a one-wave workgroup, in workgroup and device-memory LDS. The lane-ordered target gave 28,672 of 28,672 chains in lane order on each ICD; the unordered target, as a negative control, gave valid chains in another order (quads 0-3, 16-19, 4-7, 20-23, ...) for about 60% of chains and 0 broken chains. The emitter before the round barrier also passed, so these runs do not show the barrier's effect; the CPU test above is what checks it. The full LDS suites, including agc_driver_lds_subword_store_branch and agc_driver_lds_tree_reduction now that #10858 is in the branch, the subgroup barrier, LDS lock, spanned uniformity and shader disk cache tests pass on both ICDs. Details in this comment.

Head ea559cd (main 70bb367 merged), on the Apple M5 Max only (MoltenVK 1.4.2, the locally built ICD and the SDK 1.4.363.0 ICD, VK_LAYER_KHRONOS_validation loaded; the RTX 4090 was not rerun). agc_driver_lds_atomic_lane_order adds the swap pair behind s_and_saveexec and s_cbranch_execz: 28,672 of 28,672 chains in lane order, 0 reordered, 0 broken on each ICD, with 38 waves per ICD that had no active lane and skipped the block. ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -V over the agc_driver_lds_* suites, pixel LDS read-modify-write, spanned ballot sequence, agc_spirv_lds_atomic_*, agc_subgroup_barrier, agc_shader_disk_cache, the shader replay tests and spanned_uniformity: 25 of 25 pass on each ICD, 0 VUID lines. The device has no full 32-lane compute subgroups, so the spanned execution modes skip.

Head f9ae674 (main ef7dc44 merged in de7cde2, which also puts 3rdparty/SDL2 back to main's commit; the previous merge 124e8bf had left the branch's older pointer). domx4q ran the focused set with the validation layer on an RTX 4090 under Linux (driver 620.70.06; comment): 0 VUID- lines, every lane-ordered target case in lane order, but the device-target pass of agc_driver_lds_atomic_lane_order failed in two wave64 cases with one 64-thread wave per workgroup, where the two 32-lane halves of the wave interleave (lane-to-rank map 0,1,3,5,...,61 for lanes 0-31 and 2,4,...,62,63 for lanes 32-63). That pass required lane order from targets that do not enable laneOrderedLdsAtomics, which this PR does not change. f9ae674 makes the device-target pass a failure only when the device target enables the flag (MoltenVK) and otherwise prints the count, as the unordered-target swap chains already do. On the M5 Max with the locally built MoltenVK ICD and VK_LAYER_KHRONOS_validation (ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -V over the lane order, LDS atomics, 64-bit, mixed width, condxchg, read/write, tree reduction, LDS lock, spanned uniformity, subgroup barrier, shader disk cache and replay tests): 17 of 17 pass, the layer is inserted in all 8 device tests, 0 VUID- lines; agc_driver_lds_atomic_lane_order also passes with the SDK 1.4.363.0 ICD. The RTX 4090 was not rerun.

Head 62895dc (main c0d8d6c merged in 020f2ae). Two conflicts: ShaderReplay.cpp, where main now reads wide arguments through an arguments vector and the --lane-ordered-lds-atomics check is kept on top of it, and SpirvEmitter.cpp, where main added its own refusal after the spanned-wave check (NarrowReadFirstLaneUnsupported) at the same place as this PR's SubgroupBarrierInDivergentFlow check; both refusals now apply when the wave is not spanned. 62895dc moves the debt entry, unchanged, from docs/dev/debt/shader.md into a fragment, as main's CONTRIBUTING now requires. On the M5 Max (MoltenVK, SDK 1.4.363.0 ICD, VK_LAYER_KHRONOS_validation loaded, confirmed with VK_LOADER_DEBUG=layer): the lane order, LDS atomics, 64-bit, integer, mixed width, condxchg, tree reduction, beyond-device-limit and SPIR-V lock tests, spanned_uniformity, agc_subgroup_barrier, agc_shader_disk_cache and agc_shader_replay, 15 of 15 pass, 0 VUID- lines. The RTX 4090 was not rerun.

  • Title, offline (no game data in this PR): GT7's BVH radix sort pass, replayed outside the game on the M5 Max from a captured request with 1,152 synthetic records. Each row is a 4-pass sort compared with a CPU stable sort:
emission result
flag off (as on main) with the wave-LDS barrier fix of #10858 every pass is a permutation but unstable: 24, 21, 20 and 12 records out of place in passes 0 to 3; with 8-bit keys, up to 864
this PR + #10858 all 4 passes equal the CPU stable sort in 3 of 3 repeats; the same holds for seeds 1 to 5 with the equivalent waveLdsMemoryBarriers target knob in place of #10858
main before #10858, or this PR without it corrupted by the wave-LDS barrier issue that #10858 fixes: pass 0 loses about 600 of 1,152 records, and some runs hang in the look-back spin

In the game, on the M5 Max: this branch was not run by itself. A local GT7 build with this change, the GLC atomic load change #14217, a subgroup-scope variant of the #10858 barrier fix and other unmerged changes went through the Fit presentation, the scene that builds the large ray-tracing BVHs: no cyclic tree in about 1,800 checks of GT7's parent walk. In an earlier run without these fixes, 32 of the 32 trees it checked were cyclic.

Checklist

A wave's same-address ds_*_rtn returns rank its lanes only when the
lanes are applied in ascending lane order. On an Apple M5 Max (MoltenVK
1.4.2) a 32-lane SIMD-group serves same-address threadgroup atomics in
quads 0,4,1,5,2,6,3,7, so a radix sort that ranks records with
ds_add_rtn_u32 is unstable there; an RTX 4090 (NVIDIA 616.92) returns
lane order for every returning form.

On MoltenVK the driver sets SpirvTarget::laneOrderedLdsAtomics. A
returning 32-bit add, sub, min, max, and, or or xor then loops over the
wave's distinct addresses, does one atomic per address with the
combined operand and returns the old value combined with an exclusive
scan of the lower lanes' operands. The other returning LDS atomics run
one lane per loop iteration in lane order. Atomics whose result is
unused, and programs that already serialize through the 64-bit LDS
lock, are unchanged.
…plit subgroups

A wave32 program on a host subgroup wider than 32 lanes shares the
subgroup with another wave. The shuffle-based exclusive scan that the
aggregated min, max, and, or and xor paths use reads lower host lanes,
so when only the upper wave runs the atomic its first lanes read
invocations of the other wave, which are inactive and undefined. These
ops now take the serialized path in split subgroups. Add and sub keep
the aggregated path: their native scans count only active invocations.
Measure ds_wrxchg2_rtn_b32 and ds_wrxchg2st64_rtn_b32, which lower to
two ds_wrxchg_rtn_b32 halves, and repeat every operation with 16384
LDS dwords, which no device holds in workgroup memory, so the
storage-buffer path of each target is checked too. Skip on devices
whose subgroups are narrower than 32 lanes, where a wave spans several
host subgroups.
…e check

Name the ISA section that gives no lane order, extend the 4090
measurement to the 18 returning 32-bit integer forms in workgroup and
device-memory LDS, state the cost per round, and list what keeps the
host order: unused results, whose final value can still depend on the
order for rsub, inc, dec, mskor, cmpst, wrxchg, wrap and the float
forms, GDS and flat atomics. The entry closes on a hardware oracle
measurement on RDNA2.
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@frankea At 205354577202938a92836de7a3187ae14760ff02, SerializedLdsAtomic selects increasing leaders, but each leader runs the existing relaxed atomic in a different invocation. The loop's ballots and branches do not supply a cross-invocation memory-ordering edge. The existing post-atomic memory barrier runs inside the elected invocation; it does not by itself connect that operation to the next leader's relaxed operation.

Could you provide the documented MoltenVK/MSL guarantee that makes those issued rounds a same-address modification order, or enforce that order explicitly? Please cover a serialized noncommutative operation such as swap, with masked waves of 32 and 64 lanes, and check the ordering mechanism in the emitted code. The M5 Max measurements are useful evidence for that device, but the instruction-count tests and a lower lane being elected first do not establish the memory contract for every MoltenVK target. See the Vulkan synchronization rules.

@github-actions github-actions Bot added build shader core/shader docs agc libSceAgc* labels Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 23244 core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp L51, L376-378
PR 27744 core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp L306
PR 27811 core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp L306
PR 28102 core/shader/recompiler/tests/SpannedUniformityTests.cpp L107
PR 28295 core/shader/recompiler/tests/SpannedUniformityTests.cpp L107
PR 28638 core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp L309
PR 29000 core/shader/recompiler/tests/SpannedUniformityTests.cpp L107

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 1395 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 4448 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 4472 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 4847 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 5609 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 5925 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.hpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.hpp
PR 6280 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
PR 6412 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 6488 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 6924 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
PR 7085 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
PR 7392 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 7643 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 7684 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
PR 7689 core/shader/recompiler/Recompiler.hpp
PR 9289 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9356 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 9387 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 9790 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 9852 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 9897 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 9969 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 10359 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 10867 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 11435 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 11483 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 11551 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 12324 core/shader/recompiler/Recompiler.cpp
PR 12828 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 12944 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 13148 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 13863 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 13920 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 13928 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 13929 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 14032 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 14853 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
PR 14944 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 15343 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 15548 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 15555 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 16391 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 16571 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 16935 core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 16953 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 17056 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
PR 17611 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 17943 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 18029 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 18082 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 18106 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 18107 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 18173 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 18829 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 19557 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 19710 core/shader/recompiler/Recompiler.hpp
PR 20030 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 20111 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 20153 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.hpp
PR 20172 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 20518 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 21211 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 21923 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 22001 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 22029 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 22034 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22071 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 22251 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 22328 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 22424 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22439 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22446 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 22531 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 22628 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 22647 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 22697 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 23242 core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 23258 core/shader/recompiler/Recompiler.hpp
PR 23281 core/shader/recompiler/Recompiler.hpp
PR 23553 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.hpp
PR 23811 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 24049 core/shader/recompiler/Recompiler.hpp
PR 24661 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 24704 core/shader/recompiler/Recompiler.cpp
PR 24714 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 25010 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 25489 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 26101 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 26686 core/libs/prx/libSceAgcDriver/Execution/include/SubgroupBarrier.hpp
core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/SubgroupBarrier.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
core/shader/recompiler/tests/SpannedUniformityTests.cpp
PR 27782 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
PR 28142 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
PR 28176 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 28193 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 28228 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
PR 28234 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 28372 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
PR 28376 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 28384 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
PR 28387 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvSpannedUniformity.cpp
PR 28400 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.hpp
PR 28469 core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvSpannedUniformity.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvSpannedUniformity.cpp
core/shader/recompiler/tests/SpannedUniformityTests.cpp
PR 28470 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28487 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28488 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 28586 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28590 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28659 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 28668 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28673 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitter.hpp
core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp
PR 28808 core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 28962 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 28964 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp

@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 — changes requested

The new path imposes ascending guest-lane order for returning LDS atomics without RDNA2 evidence that this is the PS5 behavior.

What I ran

Reviewed head 205354577202938a92836de7a3187ae14760ff02, based on PR base cc26ab44380db3cd7b53715b84f13945227ce28b; its merge preview onto current main c763ea56aae3bb60cd6e22064a6a21482a58a980 was clean. Environment: Debian Linux x86_64, Python 3.11.2, Git 2.39.5. No compiler or build was used.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14218
git diff --check cc26ab44380db3cd7b53715b84f13945227ce28b...205354577202938a92836de7a3187ae14760ff02
python3 tools/pr_stack_check.py 14218 --repo boykopovar/AnyPS5

Results

Check Result
Diff whitespace check Passed (exit 0, no output).
Repository conventions and PR template Passed.
Current-main merge preview Clean; no declared dependency.
Precheck's ABI and test-quality helper invocations Could not run because those helper files are absent from this PR's checkout; these are local-tool availability failures, not findings against the change.
Hosted CI Windows and relinker Linux/Windows/macOS/ASan jobs passed; Linux product, register-context, and write-watch jobs were skipped.
Unedited local check output

git diff --check exited 0 and printed no output.

Conventions check output:

All conventions checks passed.

Stack-check command output:

/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14218-205354577202/stack/stack-report.md
/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14218-205354577202/stack/stack-report.json

Findings

Blocking

  • CONTRIBUTING.md says shader semantics that the source does not settle must throw; the new path instead assigns ascending order to same-address lanes for used-return LDS atomics on MoltenVK. The PR states that RDNA2 ISA section 12.13 does not specify this order and that AMD hardware has not been measured. M5 Max measurements establish MoltenVK's host order, not the guest PS5/RDNA2 order. The debt entry records this uncertainty but does not prevent the implementation from selecting one guest behavior. Please either attach an RDNA2 hardware-oracle run for the stated lane-order case and record GPU/driver/OS, or leave the unsettled guest behavior unsupported until that measurement is available.

Non-blocking

  • The author reports matched focused runs on an M5 Max/MoltenVK and RTX 4090, including validation-layer runs. I did not independently reproduce them.

Sources

Not verified

No local build or C++/Vulkan tests were run. I did not test AMD hardware or a PS5; Linux product CI was skipped. The author-reported hardware results were read but not independently confirmed.

To get this to a good state

Provide the RDNA2 hardware-oracle result for returning LDS atomics, or keep the unsettled guest ordering unsupported. I will rerun the same relevant checks on the updated head.

@TechLevelZero

Copy link
Copy Markdown
Contributor

✅ NVIDIA GPU test: passed

PR head 2053545 merged onto main 3741ca9 · tested 2026-11-01 07:26 UTC · 35 min

GPU NVIDIA RTX 2000 Ada Generation, driver 595.99.02, Vulkan 1.4.329
Host Ubuntu 26.04.1 LTS, kernel 7.0.0-38-generic, container ubuntu:24.04
Logs build and test logs for this run
Build ✅ passed (Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON)
Tests ✅ 2,477 passed · 4 failed (0 new, 3 also fail on main, 1 unstable on main) · 44 skipped (ANYPS5_REQUIRE_VULKAN=1)
Test setup CPU-only tests without a GPU; GPU tests (and any CPU-only test that failed or skipped without one) once on the GPU with VK_LAYER_KHRONOS_validation and ANYPS5_NO_SHADER_CACHE=1; main ran the same way
Test selection 2,525 of 3,121 tests run: the ones this change can reach, chosen by tools/select_tests.py from main's build graph
Vulkan validation ✅ no new validation IDs vs main (932 tests run on the GPU)
Slow tests (>30 s, GPU tests timed with validation layers) ⚠️ agc_driver_pipeline_journal 47.1 s, ⚠️ agc_driver_graphics_maps_fallback 30.6 s
Unstable on main (1, these also failed in recent main runs on this machine; not counted against this PR)
agc_submit_progress: failed in 6 of the last 10 main runs
Failing on main too (3, not counted against this PR)
agc_driver_image_table
guest_mmap_executable
guest_socket_options_multicast
Validated but not compared (2, not in the main baseline's validation pass)
agc_driver_lds_atomic_lane_order
agc_spirv_lds_atomic_lane_order
Validation IDs already present on main (5 IDs in 480 tests)
VUID-VkDeviceCreateInfo-pNext-pNext (480 tests): agc_driver, agc_driver_address_passes, agc_driver_alias_copy_label, agc_driver_alpha_to_coverage, agc_driver_array_layer_target, agc_driver_bc_block_copy, agc_driver_bda_anchor_reads, agc_driver_bda_span_reads, and 472 more
VUID-vkCmdCopyImage-srcImage-01548 (7 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_sampling, agc_driver_depth_storage_dispatch, agc_driver_depth_surface_from_storage, agc_driver_dispatch_move_depth
VUID-vkCmdCopyImage-srcImage-01551 (7 tests): agc_driver_depth_clear_hle, agc_driver_depth_plane_buffer_memory, agc_driver_depth_plane_partial_store, agc_driver_depth_plane_sampling, agc_driver_depth_storage_dispatch, agc_driver_depth_surface_from_storage, agc_driver_dispatch_move_depth
VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (1 test): agc_driver_prepared_exports
VUID-VkShaderModuleCreateInfo-pCode-08737 (1 test): agc_driver_image_sample_derivatives
Skipped (44)
agc_driver_bda_reads_shadowed_results: skipped, the device reports watched host memory written when it imports or reads it
agc_driver_bda_reads_shadowed_results_per_build: skipped, the device reports watched host memory written when it imports or reads it
agc_driver_sample_order_grid_reference: skipped, the device does not keep depth drawn at custom sample locations at those locations, even after a transition that names them: the composite of uniform renders holds the guest's depth in 0 of 3005 sloped samples, so no path can be compared with it bit for bit
agc_host_import_sparse: skipped, host imports are unavailable: /dev/udmabuf is not accessible
agc_import_settle_initial_dcc: skipped, the device has no deferred settle of host imports: imports under 1 MiB wait for one only on Windows
agc_unit_shadow_completion_store: skipped, the device reports watched host memory written when it imports or reads it
guest_http_uri_locale: 
guest_net_ctl: skipped, the host has no IPv4 address
guest_video_out_color_space: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_hdr: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_high_frame_rate: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_output_mode: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_resolution_status: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
guest_video_out_wqhd_detection: skipped, no display or Vulkan device: SDL_InitSubSystem(VIDEO | GAMECONTROLLER) failed: wayland,x11 not available
video_out_flip_aftergpu: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_backtoback: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_controls: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_cpuflip_in_order: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_dcc: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_deferred: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop_aftergpu: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_eop_main: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_flip_fresh: skipped, no display or Vulkan device: DisplayWindow: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
video_out_f
Reproduce
git fetch origin 205354577202938a92836de7a3187ae14760ff02 3741ca9327246d0df81638726a986cef862b6db5
git checkout 3741ca9327246d0df81638726a986cef862b6db5 && git merge --no-ff 205354577202938a92836de7a3187ae14760ff02
git submodule update --init --recursive
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON
cmake --build build && cmake --build build --target libs
VK_DRIVER_FILES=/nonexistent/icd.json ctest --test-dir build -E '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 32 --output-on-failure
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation \
  ctest --test-dir build -R '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 4 --output-on-failure
# failures are then re-run alone: --timeout 360 --parallel 2; new validation IDs: the test alone, twice, --parallel 1

Automated build and test on a contributor's NVIDIA machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 2, 2026
@oneandonlydean oneandonlydean self-assigned this Nov 3, 2026

@XairRrR XairRrR 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 — changes requested

This pull request does not merge cleanly into main (4f41f4c): head 2053545 conflicts in

  • core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp

Please rebase it onto current main. I will review the change once it merges cleanly.

…lds-atomic-return-lane-order

# Conflicts:
#	core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
#	core/libs/prx/libSceAgcDriver/tools/ShaderReplay.cpp
#	core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hpp
#	core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 7, 2026
…arrier

The per-lane loop that serves a returning LDS atomic one lane per round had
no synchronizes-with edge between rounds: the leader's in-branch fence pairs
with no later acquire, and ballots carry no memory semantics, so the Vulkan
memory model left the order of same-address rounds open. Every invocation
of the wave now executes OpControlBarrier with execution scope and memory
scope both equal to the wave LDS scope after each round's leader branch, so
round k happens-before round k+1 and precedes it in the location's
modification order. Equal scopes keep SPIRV-Cross from widening a subgroup
barrier to threadgroup_barrier. Where no scope covers exactly one guest wave
(non-compute stages, or several waves per workgroup on a host subgroup that
is not 32 lanes wide) the loop's trip count is not uniform over any barrier
scope, so those forms take the plain atomic path. The aggregated path is
unchanged.

The lane order test now reads the module's control-flow graph and checks,
for masked wave32 and wave64 swaps, cmpst and serialized add under the
MoltenVK target flags, that a barrier with equal execution and memory scope
lies on every path from the atomic to the loop latch, outside the leader
branch, and that removing it fails the check.
Two back-to-back ds_wrxchg_rtn_b32 put every active lane of a wave on one
dword with unique data, under 9 EXEC patterns for wave32 and 13 for wave64,
in multi-wave and single-wave workgroups and in workgroup and device-memory
LDS. The returns and the final value must form one chain in lane order; a
valid chain in another order and a broken chain are reported separately.
The unordered target runs as a negative control and is reported, not
failed. A host-side self-test feeds the classifier synthetic in-order,
reordered, duplicated and lost chains.
@frankea

frankea commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

At 2053545 the serialized path had no ordering edge between rounds. Each round ballots the pending lanes, takes the lowest with OpGroupNonUniformBallotFindLSB, and that lane alone runs OpAtomicExchange %ptr Workgroup None %data, then OpMemoryBarrier Workgroup AcquireRelease|WorkgroupMemory inside its own branch. Between rounds there are only ballots and the loop branch. Under the Vulkan memory model, two same-address atomics are ordered in the scoped modification order only when one is location-ordered before the other. Across invocations that needs a synchronizes-with edge, and only atomics and barriers create one. A ballot does not, and neither does a release fence that no later acquire pairs with. So consecutive rounds could land in either order. Two cases differ. In wave64 on a 32-lane host, lanes j and j+32 are one invocation, so those two rounds are program-ordered. In the spanned wave64 mode, every ballot already runs a workgroup OpControlBarrier, but only as a side effect of the ballot implementation, and without UniformMemory for LDS in device memory. I found nothing documented in MoltenVK or MSL that turns issue order or lockstep into a memory order: SPIRV-Cross lowers every atomic order to memory_order_relaxed. So I did not argue for the old code and changed it.

Each round now ends with OpControlBarrier S S AcquireRelease|WorkgroupMemory, plus UniformMemory when LDS lives in device memory. Every invocation in the loop executes it, after the leader branch merges and before the exit ballot. EXEC-inactive lanes stay in the loop with nothing pending, so they execute it too. S is the wave LDS scope the emitter already computes (WaveLdsScope): Subgroup when one host subgroup holds the guest wave (wave32 on 32 lanes, wave64 as two lanes per invocation), Workgroup when the workgroup is a single wave (including the spanned mode). Leader k's atomic is program-ordered before the barrier, and the barrier is program-ordered before leader k+1's atomic. Both leaders are in the barrier's scope instance. So the atomics are linked by the control-barrier case of synchronizes-with: round k happens-before round k+1 and comes first in the location's modification order. The barrier at the end of the last round also orders the instruction's atomics before everything that follows in the wave, including a following lane-ordered atomic on the same address. Execution and memory scope are equal on purpose: SPIRV-Cross's emit_barrier uses the wider of the two scopes as the execution scope (exe_scope = min(exe_scope, mem_scope) in spirv_msl.cpp, also in the copy MoltenVK bundles), so Subgroup with Workgroup memory would become threadgroup_barrier, the widening behind 5138b1b. With equal scopes, SPIRV-Cross (vulkan-sdk-1.4.357.0-39-gf11ba9f0) emits simdgroup_barrier(mem_flags::mem_threadgroup); at MSL 3.0, 3.2 and 4.0 for the masked wave32 and wave64 swaps, at loop-body level after the leader if and right before the spvSubgroupBallot of the next pending mask, simdgroup_barrier(mem_flags::mem_device | mem_flags::mem_threadgroup); for device-memory LDS, and threadgroup_barrier(mem_flags::mem_threadgroup); for one-wave workgroups and the spanned mode. In the Metal compiler headers on this Mac (/System/Library/PrivateFrameworks/GPUCompiler.framework/Versions/32023/Libraries/lib/clang/32023/include/metal/metal_compute), the MSL 3.2 and 4.0 overload (lines 98-101, enabled by __HAVE_BARRIER_MEMORY_SCOPE__, which metal_config defines for those versions at lines 1505 and 1684) is simdgroup_barrier(mem_flags flags, thread_scope scope = thread_scope_simdgroup) and calls __metal_simdgroup_barrier with __METAL_MEMORY_ORDER_SEQ_CST__, so it is a seq_cst barrier at SIMD-group scope; MSL 4.1 adds an explicit memory_order parameter with the same defaults (lines 87-91), and below 3.2 the barrier is seq_cst at threadgroup memory scope (lines 102-106).

A control barrier has to be executed by every invocation in its execution scope, and the change keeps it where that holds. The loop's exit test is a ballot over the guest wave, so every invocation in scope runs the same rounds. Guest branches are scalar, EXEC is a predicate, and a nonzero wave LDS scope already disables early-stopping invocations. These are the same conditions main relies on for its existing wave-LDS control barriers. In the spanned mode the existing uniformity check (SpannedBarrierInDivergentFlow) accepts the new barrier; the spanned compiles in both tests go through it. When the scope is 0 (non-compute stages, or several waves per workgroup on a host subgroup that is not 32 lanes wide, for example wave32 on a 64-lane subgroup, where two guest waves share it with different trip counts), the serialized forms now take the plain path instead of the loop. That takes back the split-subgroup serialization of 34a8651 for multi-wave workgroups; one-wave workgroups keep the loop with a workgroup barrier. MoltenVK does not reach the plain case: MoltenVK at db66022 reports a maximum subgroup size of 32 for Apple, AMD and Intel (MVKDevice.mm), and only MoltenVK enables lane order. The aggregated add/sub/min/max/and/or/xor path is unchanged. It issues one atomic per address per wave instruction, so its rounds never share an address.

Tests. SpirvLdsAtomicLaneOrder now reads the module's control-flow graph instead of counting opcodes. For each serialized atomic it finds the innermost loop in which the atomic does not run every round (the lane loop, not the compare-exchange loop inside ds_cmpst), then checks that every path from the atomic to the loop latch passes an OpControlBarrier whose execution scope equals its memory scope and the expected wave scope, with AcquireRelease|WorkgroupMemory (plus UniformMemory for device-memory LDS), that this barrier's block dominates the latch, and that the atomic's block does not dominate it, so it sits outside the leader branch. The cases are masked ds_wrxchg_rtn_b32 for wave32 with 256 and with 32 threads, wave64 on a 32-lane host, wave64 with LDS in device memory, spanned wave64 with workgroup and device-memory LDS, masked ds_cmpst_rtn_b32 and serialized ds_add_rtn_u32 (no subgroup arithmetic) for wave32 and wave64, all under the MoltenVK target flags. The aggregated ds_add_rtn_u32 must have no control barrier in its loop, and on a 64-lane host with several waves per workgroup ds_wrxchg_rtn_b32 and ds_min_rtn_u32 must have the atomic, loop and control-barrier counts of the unordered compile. The same module with its control barriers replaced by OpNop must fail the check, so it cannot pass vacuously. Against the previous emitter the check fails on the first case ("the latch %44 is reached from the atomic without a round barrier"). In the execution test, CheckSwapChain puts every active lane of a wave on one dword with unique data and runs two back-to-back ds_wrxchg_rtn_b32 under the same EXEC. Wave32 uses 9 EXEC patterns (all, even, odd, first and last lane, last lane only, lanes 8-15, random 1/2, random 1/8, every third lane) and wave64 adds the low half, the high half, {31,32} and {0,63}. It runs with 256 threads and with a one-wave workgroup (the spanned mode for wave64 on MoltenVK), with LDS in workgroup and in device memory, 8 dispatches of 128 workgroups per case with the patterns rotated. It requires the returns and the final value to form one chain in lane order, and reports a valid chain in another order (with the observed order) separately from a broken chain. A host-side self-test feeds the classifier in-order, reordered, duplicated and lost chains.

Results on an Apple M5 Max, MoltenVK 1.4.2 (the local ICD's startup log reports Metal Shading Language 4.0; the SDK ICD logs no MSL version), with both the locally built MoltenVK ICD and the Vulkan SDK 1.4.363.0 ICD: the lane-ordered target gave 28,672 of 28,672 wave chains in lane order (855,264 same-address swaps), 0 reordered and 0 broken, on each ICD. The unordered target, as a negative control, gave valid chains in another order for 2,450 of 4,096 wave64 chains with 256 threads, 617 of 1,024 with 64 threads, 5,045 of 8,192 wave32 chains with 256 threads and 639 of 1,024 with 32 threads, the same in device-memory LDS, and 0 broken chains; the order is quads of four lanes, 0-3, 16-19, 4-7, 20-23 and so on. So the test can see a reordering on this device. The previous emitter, without the round barrier, also gave 28,672 of 28,672 chains in lane order on both ICDs, so these runs do not show the barrier's effect. Dispatch-plus-wait time was 277-329 µs per dispatch in every case with and without the barrier, which is submission overhead; the cost per round is below what this test resolves. The full LDS suites (atomics, 64-bit, mixed width, float modes, condxchg, src2, addtid, read/write, mask waterfall, subword store branch, tree reduction, pixel LDS read-modify-write, beyond device limit, spanned ballot sequence), the subgroup barrier, LDS lock, spanned uniformity and shader disk cache tests pass on both ICDs, and the CPU-only shader replay request and replay tool tests pass.

What is contract and what is device evidence. The round order now rests on the Vulkan synchronizes-with rule for a control barrier and on SPIRV-Cross's lowering of an equal-scope barrier, both checked structurally by the CPU test. The execution test shows only what this device does, and here the previous code passed it as well. Two gaps outside this change remain. First, the edge from an earlier plain LDS write by another lane into the first round uses the existing wave-LDS ordering, which on MoltenVK is a subgroup OpMemoryBarrier (a fence with no execution component, since 5138b1b), the same as all other wave-LDS traffic there. Second, below MSL 3.2 the per-atomic OpMemoryBarrier inside the leader branch becomes threadgroup_barrier in a lane-dependent branch (also in the dumps above at MSL 3.0), which main's plain path already does. I can open an issue for both. The emitter change alters the recompiler sources, so the shader cache source version changes with it, and no new target flag or cache key field is involved. The debt entry now describes the round barrier instead of in-order SIMD-group issue.

AI-assisted: yes (Claude Code)

@frankea
frankea marked this pull request as draft November 10, 2026 19:09
@frankea

frankea commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

MarcZX10, sorry for the slow answer; my 11/07 reply and the round-barrier commits dealt with oneandonlydean's ordering question, not with yours. You are right that nothing settles the guest order. I went through the RDNA2 ISA guide again: 12.13 gives only per-lane pseudocode, 10.1 says same-bank atomics are serialized without saying in which order, and the only place it describes LDS atomic ordering at all (2.3.1, printed page 9) calls them "fast, unordered atomic operations". LLVM's AMDGPU atomic optimizer and Mesa's nir_opt_uniform_atomics both turn same-address atomics into one atomic plus a lane-order exclusive scan, so AMD's compilers treat lane order as an allowed result, but that says nothing about what the hardware does when every lane issues its own ds_add_rtn_u32. GT7's sort only works on PS5 if the console applies them in lane order, but that is an inference from a title, not a measurement.

On the second option: I don't see a compile-time throw that separates the order-dependent case. Whether a returned value depends on the order depends on how the program uses it and on how many lanes hit one address at run time, and neither is known when the shader is compiled. The narrowest throw would refuse every LDS atomic whose result is read on MoltenVK, including the many uses where any order is fine (unique slot allocation, counters) that main compiles and runs today. That is a regression, not a refusal of the new behaviour, and main's current MoltenVK order (Apple's quad order) is no better verified than lane order.

So I'd rather take the first option. I don't have an AMD GPU (my machines are an RTX 4090 and an M5 Max), so I'm moving this to draft until someone with an RDNA2 card can run the case below with the hardware oracle. If the hardware is in lane order, the debt entry closes on that run. If it isn't, I'll change the emitter to the measured order or make the case throw. Would a gfx103x run be enough for you, given that HW_ORACLE.md already treats the DS encodings as shared with the console's gfx1013?

The case, one wave per dispatch so every lane hits LDS byte 0, run with 32 rows and again with 64 rows and --wave64, flags --outs 1 --ieee 0 --dx10-clamp 1 --denorm32 0 --denorm16 3 --round32 0 --round16 0 --fp16-overflow 0:

v_mov_b32 v8, 0
v_mov_b32 v9, 0
ds_write_b32 v8, v9
s_waitcnt lgkmcnt(0)
v_mov_b32 v9, 1
ds_add_rtn_u32 v10, v8, v9
s_waitcnt lgkmcnt(0)

Lane order means every lane's first output dword equals its lane id. Two variants would cover the rest: v_add_nc_u32 v30, 1, v0 and ds_wrxchg_rtn_b32 v10, v8, v30 in place of the add (lane i returns i in lane order), and the add with EXEC set to 0x55555555 in each half around the atomic (active lane i returns i/2). Please include the GPU, driver and OS with the output.

The current head is 9c36d01 (main merged on 11/07, including #10858). XairRrR's 11/03 conflict request no longer applies; the branch merges cleanly.

AI-assisted: yes (Claude Code)

…lds-atomic-return-lane-order

# Conflicts:
#	docs/dev/debt/shader.md
@frankea
frankea requested a review from MarcZX10 November 10, 2026 21:36
…-lane-order

# Conflicts:
#	core/shader/recompiler/SpirvBackend/src/SpirvSpannedUniformity.cpp
#	core/shader/recompiler/tests/SpannedUniformityTests.cpp
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 26, 2026
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@frankea

frankea commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Merged main (f171065) in 78ac787. Two files conflicted with main's #22432, which names the root of a per-invocation value in the spanned-uniformity rejection. In SpirvSpannedUniformity.cpp the default case keeps this PR's SubgroupUniform early-out first and then takes main's version, which records why a result varies. In SpannedUniformityTests.cpp both helpers are kept, this PR's expectSubgroup and main's expectReason.
On the M5 Max (Rosetta x86-64 Release, local MoltenVK ICD, VK_LAYER_KHRONOS_validation loaded and confirmed with VK_LOADER_DEBUG=layer, ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1), the merge passes spanned_uniformity, agc_spirv_lds_atomic_lane_order, agc_driver_lds_atomic_lane_order, lds_atomics, lds_integer_atomics and spanned_ballot_sequence, with no VUID-. Main does not build on macOS right now (Runtime.cpp uses libc++abi sub-kind members that Apple's libc++abi does not declare), so these builds had #27201 applied locally; it is not part of this branch. check_conventions.py --base origin/main passes.
Gonic0's GTA V run (PPSA04264, M5 Pro) is a second title where the unstable onesweep ranking from ds_add_rtn_u32 gives cyclic BVH trees on MoltenVK and this PR removes them; thanks for capturing it.
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

Nothing blocks this change; the review supports merging, with the limits below recorded as not verified.

What I ran

Reviewed head 78ac787dfebb84fbfa1b00761f2706f6c4e61bc2. I did not run commands or tests; repo_read.py was used to read the prepared diff and targeted repository ranges at the pinned head.

Results

Check Result Note
Diff and targeted code review passed Emission, driver flag, and uniformity paths inspected
Programmatic checks not run No build or tests run in this review

Findings

Blocking

None.

Non-blocking

None.

Not verified

Hardware behavior, builds, and test results were not independently verified. The author reports these in the PR description. VulkanDevice.cpp was read only at the three changed ranges because its full text was excluded as too large; the other 20 changed files listed as reviewed in the manifest were read from the bundle or targeted head ranges. No other changed files were excluded.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 26, 2026
…-lane-order

# Conflicts:
#	core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvSpannedUniformity.hpp
#	core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp
#	core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp
#	core/shader/recompiler/SpirvBackend/src/SpirvSpannedUniformity.cpp

@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 — changes requested

Changes are required before merge: remove mesh-stage lane ordering or provide a valid wave LDS scope for mesh shaders before emitting serialized round barriers.

What I ran

Reviewed head c5eb84a11e7d9601612cd20c60fede657f9598fe using repo_read.py against the prepared diff; no build or tests were run. The description’s hardware and test results are author-reported.

Results

Check Result Note
Diff and targeted source review passed Core paths inspected
Build and tests not run No execution performed

Findings

Blocking

  • core/shader/recompiler/SpirvBackend/src/SpirvEmitter.cpp:54 permits Mesh stage through the lane-ordering gate, but WaveLdsScope at lines 67–68 returns zero for every non-Compute stage. For a used returning LDS atomic that takes SerializedLdsAtomic, core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp:1922-1924 emits OpControlBarrier using state.waveLdsScope for both scope operands. A zero scope is invalid, so a MoltenVK mesh shader using such an atomic can fail SPIR-V validation or compilation. Exclude Mesh from LaneOrdersLdsAtomics or establish a valid mesh-stage barrier scope before selecting this path.

Non-blocking

None.

Not verified

No build, test, GPU, or console checks were run. Hardware and CI results in the PR description were not independently verified. The complete text of core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp was read through the pinned reader; files.json was unavailable in the reader, so the excluded-file inventory could not be checked. Other changed files outside the targeted paths were not read in full.

To get this to a good state

Exclude Mesh stage from lane-ordered LDS atomics or implement and validate the appropriate mesh-stage barrier scope, then run the relevant SPIR-V and execution checks.

@frankea

frankea commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

MarcZX10, on the mesh-stage finding at c5eb84a: a zero waveLdsScope does not reach the serialized round barrier. EmitLaneOrderedSharedAtomic (SpirvMemoryInstructions.cpp:2518) returns the plain emission, in the host's order, whenever the operation has no aggregated form and state.waveLdsScope == 0u, before it can call SerializedLdsAtomic. WaveLdsScope (SpirvEmitter.cpp:67-68) returns 0 for every stage other than Compute, so a mesh shader never emits the OpControlBarrier at lines 1922-1924. That check came in with 98d69b6, which added the round barrier, for this reason: a round barrier needs a scope over which the guest wave's control flow is uniform, and outside compute the emitter has none.

What the Mesh term in LaneOrdersLdsAtomics enables is the aggregated path, AggregatedLdsAtomic, for used results of 32-bit add, sub, min, max, and, or and xor when subgroup arithmetic is available. That path has no control barrier. Its only barrier is an OpMemoryBarrier at LdsMemoryBarrierScope, which is Workgroup, or Device on drivers where a Workgroup-scope fence is not usable, and both are valid in a mesh shader. A returning exchange or compare-swap in a mesh shader keeps the host order, as the debt entry says ("In non-compute stages ... the per-lane forms keep the host order").

So I am leaving the gate as it is. If you see a path to SerializedLdsAtomic with a zero scope that bypasses line 2518, please point me to it.

AI-assisted: yes (Claude Code)

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I compiled the exact request serializer and checked a capture round trip with laneOrderedLdsAtomics=true. It deserializes as false. ShaderReplay has a manual override, so replay without that flag can exercise host lane order instead of the captured MoltenVK path even though the cache key distinguishes them. Please record the target bit in captures or make the required override explicit in replay diagnostics. The trial merge onto c1601e56d5 was clean. Paired shader validation is pending.

AI-assisted: yes.

@frankea

frankea commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

The request format does not carry any device-derived SpirvTarget flag: RequestSerializer.cpp writes none of ldsLockFences, robustBufferAccess2, spannedShuffleThroughMemory, meshInlineFunctions or coherentBufferAtomics, and agc_shader_replay sets them from its command line after it deserializes a request. laneOrderedLdsAtomics follows that pattern with --lane-ordered-lds-atomics, as the PR body lists. At run time the shader cache key includes the flag, so a MoltenVK module and a host-order module cannot be swapped. The serializer only feeds failure text and .req dumps.

Recording device flags in captures, or printing them in replay diagnostics, would apply to all of these flags at once, so I would rather make that change in its own PR than in this one.

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

Nothing blocks this; the reviewed code and focused coverage support merging.

What I ran

Reviewed head 62895dca015ebae40dd459a2745557a838231a6a in the review container. I read the prepared description, commits, manifest, and diff, then used repo_read.py to inspect source context. I ran no builds or tests.

python3 repo_read.py search <name> <repository-path>
python3 repo_read.py cat <repository-path> <start> <end>

Results

Check Result Note
Source review passed All 21 manifest entries reviewed
Tests not run Would require compilation or GPU execution
Negative control not run Author-reported controls were not independently verified

Negative control

I did not run a negative control. The description reports SPIR-V barrier-removal and unordered-target controls; those results are author-reported and were not independently verified.

Findings

Blocking

None.

Non-blocking

None.

Sources

The driver flag is set for MoltenVK and passed into the shader target in VulkanDevice.cpp and SpirvEmitter.cpp. The lowering gates on used LDS atomic results and chooses aggregated or serialized handling in SpirvMemoryInstructions.cpp and SpirvFlowEmitter.cpp. The change also adds SPIR-V and execution coverage in SpirvLdsAtomicLaneOrder.cpp and LdsAtomicLaneOrder.cpp.

Not verified

No tests were run. The hardware measurements, CI behavior, and test results described by the author were not independently confirmed. All 21 files listed in manifest.json were reviewed; VulkanDevice.cpp was read using repo_read.py because it was not included in the prepared files. files.json lists no excluded paths.

To get this to a good state

Nothing is required.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs shader core/shader unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants