From 3994ea73dce6df13479b1a72bd60d4da7c813f74 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 3 Dec 2026 06:44:30 -0800 Subject: [PATCH] Fixtures: coherent_texture_handoff, a device-coherent texture handed between threadgroups (source) Our own MSL (metal4.1, macOS 27.0): two kernels over a texture2d, a device atomic_uint flag and an output buffer, dispatched as two threadgroups of 64 threads. Each thread of threadgroup 0 writes its texel (1000 + i), fences texture memory at device scope with release order and adds 1 to the flag (relaxed); each thread of threadgroup 1 loads the flag until it reads 64 or for at most 4096 loads, fences texture memory at device scope with acquire order and reads the texel of its index. coherent_texture_handoff stores 2 where a reader saw all 64 writers' flag and read anything but the written texel, else 1: its words are 1 whatever the scheduling, so the case stays deterministic for Metal qualification. coherent_texture_handoff_seen stores each reader's flag value and texel, for the runtime's device test to count the reads that saw the flag. The fences are MSL 4.1's release and acquire, not seq_cst: the runtime refuses a seq_cst fence beyond the thread under the identity domains (IdentityDomains, "a sequentially consistent fence", a known gap in its README), which a first form of this fixture met on the Iris Xe (m3-max-83655). --- .../engine/coherent_texture_handoff.metal | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 crates/metal2vulkan-validation/fixtures/public/engine/coherent_texture_handoff.metal diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/coherent_texture_handoff.metal b/crates/metal2vulkan-validation/fixtures/public/engine/coherent_texture_handoff.metal new file mode 100644 index 000000000..612fb9ef5 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/coherent_texture_handoff.metal @@ -0,0 +1,63 @@ +// metal-std: metal4.1 +// metal-macos-min: 27.0 +// Owned fixture: a texture of MSL's `memory_coherence_device` handed between threadgroups. In a +// dispatch of two threadgroups of 64 threads over a 64 x 1 texture, each thread of threadgroup 0 +// writes its texel, fences texture memory at device scope with release order and raises a +// device-scope flag (a relaxed atomic add); each thread of threadgroup 1 loads the flag a bounded +// number of times, fences texture memory at device scope with acquire order and reads the texel of +// its own index. It stores 2 where it saw every writer's flag and still read the texel's earlier +// value, and 1 otherwise: under device coherence every word is 1, however the threadgroups are +// scheduled, and no thread waits without bound. `coherent_texture_handoff_seen` is the same +// handoff storing each reader's flag count and texel, for the runtime's device test to count the +// reads that saw every writer's flag and, of those, the ones that read the written texel. The +// fences are MSL 4.1's release and acquire: the runtime refuses a `seq_cst` fence beyond the +// thread (a known gap its README records). +#include +using namespace metal; + +kernel void coherent_texture_handoff( + texture2d tex [[texture(0)]], + device atomic_uint *flag [[buffer(0)]], + device uint *out [[buffer(1)]], + uint group [[threadgroup_position_in_grid]], + uint i [[thread_position_in_threadgroup]]) { + const uint WRITERS = 64; + const uint TRIES = 4096; + uint2 at = uint2(i, 0); + if (group == 0) { + tex.write(uint4(1000 + i), at); + atomic_thread_fence(mem_flags::mem_texture, memory_order_release, thread_scope_device); + atomic_fetch_add_explicit(flag, 1, memory_order_relaxed); + return; + } + uint seen = 0; + for (uint k = 0; k < TRIES && seen < WRITERS; k++) { + seen = atomic_load_explicit(flag, memory_order_relaxed); + } + atomic_thread_fence(mem_flags::mem_texture, memory_order_acquire, thread_scope_device); + uint v = tex.read(at).x; + out[i] = (seen == WRITERS && v != 1000 + i) ? 2 : 1; +} + +kernel void coherent_texture_handoff_seen( + texture2d tex [[texture(0)]], + device atomic_uint *flag [[buffer(0)]], + device uint2 *out [[buffer(1)]], + uint group [[threadgroup_position_in_grid]], + uint i [[thread_position_in_threadgroup]]) { + const uint WRITERS = 64; + const uint TRIES = 4096; + uint2 at = uint2(i, 0); + if (group == 0) { + tex.write(uint4(1000 + i), at); + atomic_thread_fence(mem_flags::mem_texture, memory_order_release, thread_scope_device); + atomic_fetch_add_explicit(flag, 1, memory_order_relaxed); + return; + } + uint seen = 0; + for (uint k = 0; k < TRIES && seen < WRITERS; k++) { + seen = atomic_load_explicit(flag, memory_order_relaxed); + } + atomic_thread_fence(mem_flags::mem_texture, memory_order_acquire, thread_scope_device); + out[i] = uint2(seen, tex.read(at).x); +}