diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 555275ed2..3d47be926 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -61,7 +61,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2101; +pub const SEMANTICS_VERSION: u32 = 2102; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/lower/simd/lockstep.rs b/crates/metal2vulkan/src/lower/simd/lockstep.rs index 9f2322d77..c4caf4e3d 100644 --- a/crates/metal2vulkan/src/lower/simd/lockstep.rs +++ b/crates/metal2vulkan/src/lower/simd/lockstep.rs @@ -138,12 +138,13 @@ //! Vulkan orders nothing (NVIDIA's independent thread scheduling and its compiler's reordering of //! accesses it proves apart within one thread included). The analysis runs a second pass over //! device memory ([`facts::Space::Device`]): its objects are the buffer parameters, device globals, -//! and the buffer a pointer loaded from memory names (two loads of one argument-buffer member name -//! one), its intervals end at the barriers that order device or threadgroup memory, and every -//! barrier it inserts carries `mem_device` beside `mem_threadgroup` where the threadgroup pass -//! needed one too, so one barrier serves both. Two bindings of one buffer, or a binding and a -//! pointer loaded into it, are taken as distinct objects: a pair that meets only through such an -//! overlap is not ordered. `constant` memory is never written; texture memory (`mem_texture`) is +//! and the buffer a pointer loaded from buffer memory names (two loads of one argument-buffer +//! member name one; a pointer loaded from thread, threadgroup or payload memory is a copy and names +//! no object of its own, so it meets every object), its intervals end at the barriers that order +//! device or threadgroup memory, and every barrier it inserts carries `mem_device` beside +//! `mem_threadgroup` where the threadgroup pass needed one too, so one barrier serves both. Two +//! bindings of one buffer, or a binding and a pointer loaded from buffer memory into it, are taken +//! as distinct objects: a pair that meets only through such an overlap is not ordered. `constant` memory is never written; texture memory (`mem_texture`) is //! not ordered. //! //! **Residue.** Lockstep also fixes which thread's store wins when threads of one simdgroup diff --git a/crates/metal2vulkan/src/lower/simd/lockstep/facts.rs b/crates/metal2vulkan/src/lower/simd/lockstep/facts.rs index 00d8c2328..b660e0560 100644 --- a/crates/metal2vulkan/src/lower/simd/lockstep/facts.rs +++ b/crates/metal2vulkan/src/lower/simd/lockstep/facts.rs @@ -57,12 +57,14 @@ pub(super) enum Space { } /// An object of the memory a pass orders: a global, a buffer parameter, or (in device memory) the -/// buffer a pointer loaded from memory names, by the canonical spelling of the address it is -/// loaded from, so two loads of one argument-buffer member name one object. Distinct objects are -/// taken never to overlap. In threadgroup memory that holds (Metal allocates each object apart); -/// in device memory it is the analysis's reading: two bindings of one buffer, or a buffer and a -/// pointer loaded from memory into it, may overlap, and a pair reaching one byte through two -/// such objects is not ordered. +/// buffer a pointer loaded from buffer memory names, by the canonical spelling of the address it +/// is loaded from, so two loads of one argument-buffer member name one object. A pointer loaded +/// from thread, threadgroup or payload memory names none: it is a copy of a pointer a store put +/// there (two slots may hold one buffer's), so its object is unknown and it meets every object. +/// Distinct objects are taken never to overlap. In threadgroup memory that holds (Metal allocates +/// each object apart); in device memory it is the analysis's reading: two bindings of one buffer, +/// or a buffer and a pointer loaded from buffer memory into it, may overlap, and a pair reaching +/// one byte through two such objects is not ordered. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub(super) enum Root { Global(GlobalId), @@ -405,6 +407,22 @@ fn space_ptr(e: &Entry, v: Value, space: Space) -> bool { .is_some_and(|t| matches!(t, Type::Ptr { space: at } if of_space(at))) } +/// Whether `v` is a pointer into buffer memory (`device` or `constant`): where an argument buffer's +/// members live, rather than a copy a store made in thread, threadgroup or payload memory. +fn buffer_ptr(e: &Entry, v: Value) -> bool { + e.module + .value_ty(e.func, v) + .and_then(|t| e.module.types().get(t)) + .is_some_and(|t| { + matches!( + t, + Type::Ptr { + space: AddrSpace::Device | AddrSpace::Constant + } + ) + }) +} + fn rmw_update(op: AtomicRmwOp) -> Option { Some(match op { AtomicRmwOp::Add | AtomicRmwOp::Sub => Update::Add, @@ -2594,9 +2612,14 @@ impl<'a> AddrFacts<'a> { let entry = tree_bytes(1, std::mem::size_of::()); m.charge(entry)?; charged = charged.saturating_add(entry); - // A device pointer loaded from memory: the object its source names. + // A device pointer loaded from buffer memory: the object its source names. One + // loaded from any other memory is a copy of a pointer a store put there, of + // an object the load does not name. if let Some(InstKind::Load { ptr: src, .. }) = self.kind(v) { if space_ptr(self.e, v, Space::Device) { + if !buffer_ptr(self.e, *src) { + break None; + } let at = self.canon(*src, 0, m)?; if !out.contains(&Root::Loaded(at)) { if out.len() == MAX_ROOTS { diff --git a/crates/metal2vulkan/src/tests/engine_simd_lockstep.rs b/crates/metal2vulkan/src/tests/engine_simd_lockstep.rs index 42505f72b..410a60404 100644 --- a/crates/metal2vulkan/src/tests/engine_simd_lockstep.rs +++ b/crates/metal2vulkan/src/tests/engine_simd_lockstep.rs @@ -1072,6 +1072,41 @@ done: } "#; +/// `device_neighbor` with `out` reached through two thread slots that each hold it, written on two +/// paths so each load keeps its slot: the store and the read go through pointers loaded from +/// different slots, which name one buffer. Lockstep: `out[t] = t ^ 8`. +const DEVICE_SLOTS: &str = r#" +define void @device_slots(ptr addrspace(1) %0, i32 %1, i32 %sg) #0 { + %a = alloca ptr addrspace(1), align 8 + %b = alloca ptr addrspace(1), align 8 + store ptr addrspace(1) %0, ptr %a, align 8 + store ptr addrspace(1) %0, ptr %b, align 8 + %c = icmp eq i32 %sg, 7 + br i1 %c, label %again, label %go + +again: + store ptr addrspace(1) %0, ptr %a, align 8 + store ptr addrspace(1) %0, ptr %b, align 8 + br label %go + +go: + %3 = zext i32 %1 to i64 + %pa0 = load ptr addrspace(1), ptr %a, align 8 + %4 = getelementptr inbounds i32, ptr addrspace(1) %pa0, i64 %3 + store i32 %1, ptr addrspace(1) %4, align 4 + tail call void @air.wg.barrier(i32 1, i32 1) + %5 = xor i32 %1, 8 + %6 = zext i32 %5 to i64 + %pb = load ptr addrspace(1), ptr %b, align 8 + %7 = getelementptr inbounds i32, ptr addrspace(1) %pb, i64 %6 + %8 = load i32, ptr addrspace(1) %7, align 4 + %pa = load ptr addrspace(1), ptr %a, align 8 + %9 = getelementptr inbounds i32, ptr addrspace(1) %pa, i64 %3 + store i32 %8, ptr addrspace(1) %9, align 4 + ret void +} +"#; + /// Each thread stores to its own word of `out`, reads it back and overwrites it, with no barrier: /// no two threads touch one byte, so the device pass inserts nothing. `out[t] = t + 1`. const DEVICE_OWN: &str = r#" @@ -2101,6 +2136,31 @@ fn a_device_memory_conflict_takes_the_barrier_lockstep_stood_for() { assert_eq!(plan.simd.1, Tier::Direct); } +/// A buffer reached through pointers loaded from two thread slots is one object to the device +/// pass: the read of the neighbour's word and the overwrite of its own take the threadgroup barrier +/// lockstep stood for, as through the parameter itself. Without it the chunked schedule reads a +/// word another chunk already overwrote. +#[test] +fn a_buffer_through_two_thread_slots_is_one_object() { + for profile_name in ["lavapipe", "moltenvk-m3", "vk12-floor"] { + let Some(plan) = check( + "device_slots", + DEVICE_SLOTS, + profile_name, + 32, + &CHUNKED, + |t| t ^ 8, + ) else { + return; + }; + assert_eq!( + uses(&plan, "barrier.lockstep.threadgroup"), + 1, + "{profile_name}" + ); + } +} + /// Each thread's own device word takes no barrier. #[test] fn a_threads_own_device_word_takes_no_barrier() { diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 702c0dc97..04197d523 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -2209,3 +2209,9 @@ compares with Metal's. `Depth32Float` and `Stencil8` textures their earlier texels in every tile (an M3 Max under macOS 27.0.1, with and without API validation). Which tiles Metal clears follows which draws access the aspect; the runtime's clear follows the draws' rectangles alone. +- **The lockstep device pass takes two buffer bindings, and a binding against a pointer loaded + from device or constant memory, as distinct objects** (`lower/simd/lockstep/facts.rs`): + a program whose simdgroup threads store to the same bytes through two bindings of one buffer, or + through a binding and an argument-buffer pointer to it, may see an order other than the one + Apple's simdgroup lockstep gives, and no barrier is added for it. A pointer loaded from thread, + threadgroup or payload memory names no object of its own and meets every object.