diff --git a/crates/metal2vulkan/src/lower/access/atomic.rs b/crates/metal2vulkan/src/lower/access/atomic.rs index a27cf0174..173016d02 100644 --- a/crates/metal2vulkan/src/lower/access/atomic.rs +++ b/crates/metal2vulkan/src/lower/access/atomic.rs @@ -1264,32 +1264,40 @@ pub fn emit( LocalPtr::Value(v) => cx.operand(fb, *v)?, LocalPtr::Offset(c) => k32(fb, *c)?, }; - let two = k32(fb, 2)?; - let w = fb.binary(BinOp::ShiftRightLogical, off, two)?; - let at = At { - p: ArenaMem(decl, WordOps::default()).ptr(fb, w)?, - shift: field_shift(fb, o, off)?, - }; - let inb = match *contained { - true => { - let size = fb.module().const_value(decl.size)?; - Some(contain::in_bounds(fb, off, size, u64::from(bytes))?) + // An unchecked atomic at a constant offset outside the arena lies outside every object + // in it, where Metal leaves it undefined, and no access chain may index the arena's + // words with a constant past them: it writes nothing and yields zero, as a contained + // one out of bounds does (`guarded`). + if !*contained && local::outside_arena(fb, off, &decl, u64::from(bytes))? { + o.yields().then(|| konst(fb, word, 0)).transpose()? + } else { + let two = k32(fb, 2)?; + let w = fb.binary(BinOp::ShiftRightLogical, off, two)?; + let at = At { + p: ArenaMem(decl, WordOps::default()).ptr(fb, w)?, + shift: field_shift(fb, o, off)?, + }; + let inb = match *contained { + true => { + let size = fb.module().const_value(decl.size)?; + Some(contain::in_bounds(fb, off, size, u64::from(bytes))?) + } + false => None, + }; + // A word of a tile's cell the pass does not keep reads as zero and keeps no write + // (`access::tile::KeptCells`). + let kept = match super::tile::KeptCells::of(cx, fb)? { + Some(cells) => Some(cells.kept(fb, w)?), + None => None, + }; + let guard = match (inb, kept) { + (Some(inb), Some(kept)) => Some(fb.binary(BinOp::LogicalAnd, inb, kept)?), + (inb, kept) => inb.or(kept), + }; + match guard { + Some(guard) => guarded(fb, guard, at, o)?, + None => apply(fb, at, o)?, } - false => None, - }; - // A word of a tile's cell the pass does not keep reads as zero and keeps no write - // (`access::tile::KeptCells`). - let kept = match super::tile::KeptCells::of(cx, fb)? { - Some(cells) => Some(cells.kept(fb, w)?), - None => None, - }; - let guard = match (inb, kept) { - (Some(inb), Some(kept)) => Some(fb.binary(BinOp::LogicalAnd, inb, kept)?), - (inb, kept) => inb.or(kept), - }; - match guard { - Some(guard) => guarded(fb, guard, at, o)?, - None => apply(fb, at, o)?, } } AtomicMem::Wide { ptr, .. } => { diff --git a/crates/metal2vulkan/src/lower/access/local.rs b/crates/metal2vulkan/src/lower/access/local.rs index fbf44a4f0..4362dd73d 100644 --- a/crates/metal2vulkan/src/lower/access/local.rs +++ b/crates/metal2vulkan/src/lower/access/local.rs @@ -45,9 +45,10 @@ //! whose `ThreadgroupMemory` is `Native` ([`contained`]): there no threadgroup access is contained, an arena's, its atomics', memory //! intrinsics', pointer cells' and imageblock's alike, nor a typed threadgroup object's index, and //! one past the memory is undefined, as in Metal (no robustness feature covers arenas). An -//! unchecked load or store at a constant offset outside the arena, which lies outside every object -//! in it and may lie past its words, where no access chain may index with a constant, is a zero -//! load or no store, as a contained one is. Contained, +//! unchecked load, store or atomic at a constant offset outside the arena, which lies outside every +//! object in it and may lie past its words, where no access chain may index with a constant, is a +//! zero load, no store, or an atomic that writes nothing and yields zero, as a contained one is +//! (the atomic in `atomic`). Contained, //! with `inb` the normative check against the arena's exact byte //! size, a load reads at `select(inb, off, 0)` and yields `select(inb, v, 0)`, and a store sits in //! a builder-closed `if_(inb)`. A dereference of a null private pointer (undefined in Metal) reads @@ -2091,7 +2092,7 @@ pub(super) fn emit_access_at( /// Whether a `bytes`-byte access at `off` lies outside `decl`'s arena by the normative check /// (`contain::in_bounds`), decided at translation time: `off` and the arena's size are plain /// constants. `false` when either is not one. -fn outside_arena( +pub(super) fn outside_arena( fb: &mut FunctionBuilder<'_>, off: SValue, decl: &ArenaDecl, diff --git a/docs/API.md b/docs/API.md index 01e79c0c4..dfbdc5653 100644 --- a/docs/API.md +++ b/docs/API.md @@ -650,9 +650,10 @@ through buffer arguments it reads at any index. - **Threadgroup memory.** No threadgroup memory access is bounded: a load or store, atomic, memory intrinsic, pointer cell or imageblock access of a threadgroup arena, and a typed threadgroup object's index, run as the program states them, and - one past the memory is undefined, as in Metal. A load or store of the arena at a constant offset - outside it, undefined in Metal too, is a zero load or no store: no access chain may index the - arena's words with a constant past them. A kernel whose every threadgroup access is a + one past the memory is undefined, as in Metal. A load, store or atomic of the arena at a constant + offset outside it, undefined in Metal too, is a zero load, no store, or an atomic that writes + nothing and returns zero: no access chain may index the arena's words with a constant past them. + A kernel whose every threadgroup access is a whole, aligned `float` (a scalar or a vector of two to four), with no atomic, memory intrinsic, imageblock or pointer kept in it, and whose SIMD groups are not emulated, holds its threadgroup memory as a `Workgroup` array of `float`: each access is one typed load or store a lane, with no