diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index b52f6f6e4..04fc0d88e 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,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 = 2342; +pub const SEMANTICS_VERSION: u32 = 2343; /// 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/mod.rs b/crates/metal2vulkan/src/lower/access/mod.rs index 7673069c5..e631f9ee2 100644 --- a/crates/metal2vulkan/src/lower/access/mod.rs +++ b/crates/metal2vulkan/src/lower/access/mod.rs @@ -713,6 +713,12 @@ pub struct AccessPlan { /// ([`note_variable`]): each reads its `PushField::SlotBounds`, and each of those accesses /// whose emitted offset is not a constant is checked against its buffer (`emit::prepare`). pub variable: BTreeSet, + /// Under the identity domains with containment on ([`identity_contained`]), the buffer slots + /// a load, a copy's read or a pulled fetch may reach, at any offset ([`note_variable`]): a + /// read of one of `variable` whose emitted offset is not a constant is checked, and where the + /// slot is reached by its device address reads `PushField::ZeroAddress` outside its buffer + /// (`emit::prepare`, `pulled`), whatever offset its own access proved. + pub read: BTreeSet, /// Under the identity domains, a kernel on a device that binds its buffer arguments /// (`DeviceProfile::binds_kernel_buffers`): its storage views are its slots' buffers, each /// bound over its buffer, and its pipeline's robust buffer access 2 contains them @@ -1165,17 +1171,45 @@ pub(crate) fn identity_contained(cx: &SelectCx<'_>) -> bool { /// An access of buffer slot `slot`, a read where `read`, whose byte offset is a translation-time /// constant only where `fixed`. Under the identity domains with containment on /// ([`identity_contained`]), a slot an access may reach at another offset is checked -/// ([`AccessPlan::variable`]): it reads its `PushField::SlotBounds`, and a read of it reads -/// `PushField::ZeroAddress` outside. An access at a constant offset stays unchecked. +/// ([`AccessPlan::variable`]) and reads its `PushField::SlotBounds`, and a read is noted at any +/// offset ([`AccessPlan::read`]): which reads take `PushField::ZeroAddress` is known once the +/// slots' forms are ([`place_zero_address`]). An access at a constant offset of a slot no other +/// access reaches at another stays unchecked. pub(crate) fn note_variable(cx: &mut SelectCx<'_>, slot: SlotRef, fixed: bool, read: bool) { - if fixed || !identity_contained(cx) { + if !identity_contained(cx) { + return; + } + if read { + cx.families.access.read.insert(slot); + } + if fixed { return; } cx.families.access.variable.insert(slot); cx.push.insert(PushField::SlotBounds(slot)); - if read { - cx.push.insert(PushField::ZeroAddress); - } +} + +/// Place `PushField::ZeroAddress` where some read may be checked on a slot reached by its device +/// address (`emit::prepare`, `pulled`): a slot of `variable` that a read reaches +/// ([`AccessPlan::read`]), in the device-address form of `lowering` (by memory-plan slot index) +/// or `own` (a pulled fetch's own source slots); and nowhere else. A read of a storage view +/// clamps its offset instead, and an atomic outside its buffer runs nothing. +fn place_zero_address( + cx: &mut SelectCx<'_>, + lowering: &[Option], + own: &[(SlotRef, SlotLowering)], +) { + let access = &cx.families.access; + let checked_read = + |slot: &SlotRef| access.variable.contains(slot) && access.read.contains(slot); + let shared = (cx.memory.slots.iter().zip(lowering)) + .any(|(plan, l)| matches!(l, Some(SlotLowering::Psb { .. })) && checked_read(&plan.slot)); + let pulled = + (own.iter()).any(|(s, l)| matches!(l, SlotLowering::Psb { .. }) && checked_read(s)); + match shared || pulled { + true => cx.push.insert(PushField::ZeroAddress), + false => cx.push.remove(&PushField::ZeroAddress), + }; } /// Whether static-root pointer `ptr`'s byte offset from its roots is a translation-time @@ -1467,12 +1501,8 @@ fn place_slots(cx: &mut SelectCx<'_>, m: &mut Meter) -> Result