diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index ac3421f43..b7c3d0fa9 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 = 2266; +pub const SEMANTICS_VERSION: u32 = 2267; /// 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/access/local.rs b/crates/metal2vulkan/src/lower/access/local.rs index 1c78d7797..0475a415f 100644 --- a/crates/metal2vulkan/src/lower/access/local.rs +++ b/crates/metal2vulkan/src/lower/access/local.rs @@ -2095,10 +2095,10 @@ pub(super) fn emit_access_at( } /// The words of a contained store into the private arena (the module docs): each word written at -/// `select(inb, word, 0)` as `select(inb, value, old)`, `old` read from that word first, and each -/// partial word's mask taken as `select(inb, mask, 0)`, so a store whose bytes are out of bounds -/// rewrites word 0 with its own bits. It offers no narrow or whole-word units, so every store is -/// of words. +/// `select(inb, word, 0)` as `select(inb, value, old)`, `old` read from that word first through +/// the pointer the write takes (`KeptMem::write_or_keep`), and each partial word's mask taken as +/// `select(inb, mask, 0)`, so a store whose bytes are out of bounds rewrites word 0 with its own +/// bits. It offers no narrow or whole-word units, so every store is of words. struct Guarded<'m> { mem: &'m mut KeptMem, inb: SValue, @@ -2112,9 +2112,7 @@ impl WordMem for Guarded<'_> { fn write(&mut self, fb: &mut FunctionBuilder<'_>, word: SValue, value: SValue) -> BResult<()> { let zero = k32(fb, 0)?; let at = fb.select(self.inb, word, zero)?; - let old = self.mem.read(fb, at)?; - let x = fb.select(self.inb, value, old)?; - self.mem.write(fb, at, x) + self.mem.write_or_keep(fb, at, self.inb, value) } fn write_bits( diff --git a/crates/metal2vulkan/src/lower/access/tile.rs b/crates/metal2vulkan/src/lower/access/tile.rs index 8c3989e81..75297f1a7 100644 --- a/crates/metal2vulkan/src/lower/access/tile.rs +++ b/crates/metal2vulkan/src/lower/access/tile.rs @@ -708,6 +708,29 @@ pub(crate) struct KeptMem { pub(crate) kept: Option, } +impl KeptMem { + /// [`ArenaMem::write_or_keep`] of word `word`, a word the kept cells exclude not written. + pub(crate) fn write_or_keep( + &self, + fb: &mut FunctionBuilder<'_>, + word: SValue, + inb: SValue, + value: SValue, + ) -> BResult<()> { + let Some(cells) = self.kept else { + return self.mem.write_or_keep(fb, word, inb, value); + }; + let kept = cells.kept(fb, word)?; + let mem = self.mem; + let then: ArmFn<'_, ()> = Box::new(move |fb, _| { + mem.write_or_keep(fb, word, inb, value)?; + Ok(ArmEnd::FallThrough(Vec::new())) + }); + fb.if_(&mut (), kept, then, None)?; + Ok(()) + } +} + impl WordMem for KeptMem { fn read(&mut self, fb: &mut FunctionBuilder<'_>, word: SValue) -> BResult { let v = self.mem.read(fb, word)?; diff --git a/crates/metal2vulkan/src/memory/arena.rs b/crates/metal2vulkan/src/memory/arena.rs index fe7c6ae11..3bf61e2a6 100644 --- a/crates/metal2vulkan/src/memory/arena.rs +++ b/crates/metal2vulkan/src/memory/arena.rs @@ -672,6 +672,23 @@ impl ArenaMem { fn atomic_words(&self) -> bool { self.0.arena == Arena::Payload && self.1.coherence != Coherence::Plain } + + /// Write `select(inb, value, old)` at word `word` of the private arena, `old` the word it + /// holds, read and written through one pointer (`lower::access::local`'s guarded store). + pub fn write_or_keep( + &self, + fb: &mut FunctionBuilder<'_>, + word: Value, + inb: Value, + value: Value, + ) -> BResult<()> { + let p = self.ptr(fb, word)?; + let load = self.access(fb, false)?; + let old = fb.load(p, load)?; + let x = fb.select(inb, value, old)?; + let store = self.access(fb, true)?; + fb.store(p, x, store) + } } impl WordMem for ArenaMem { diff --git a/crates/metal2vulkan/src/tests/engine_device_arena.rs b/crates/metal2vulkan/src/tests/engine_device_arena.rs index bff66f2d0..b1bd99c86 100644 --- a/crates/metal2vulkan/src/tests/engine_device_arena.rs +++ b/crates/metal2vulkan/src/tests/engine_device_arena.rs @@ -22,7 +22,10 @@ //! at a constant index. Each word is an access chain from the slot's words, placed once //! (`EmitCx::arena_slot`), and a load or store, so each shape stays within its bound on every //! identity profile and adds no block: a store whose bytes are out of bounds rewrites word 0 as -//! it was. The table of the first profile is printed. +//! it was. That makes a store the load's equal: it reads the word it writes through the access +//! chain it writes through, and selects between its value and the word it read, so its bound is +//! the word load's, 12, where a store in a branch needed 11 and two blocks. The table of the first +//! profile is printed. //! //! `llvm-as` assembles the kernels; without it the tests skip visibly. @@ -204,7 +207,7 @@ const SHAPES: &[Shape] = &[ ) }, value: None, - bound: 11, + bound: 12, }, Shape { name: "float4 load at a run-time index",