diff --git a/crates/metal2vulkan-air/src/canon/const_hoist.rs b/crates/metal2vulkan-air/src/canon/const_hoist.rs index 637644c11..9174876f5 100644 --- a/crates/metal2vulkan-air/src/canon/const_hoist.rs +++ b/crates/metal2vulkan-air/src/canon/const_hoist.rs @@ -18,8 +18,8 @@ //! - **A pointer no other root shares.** Neither the parameter nor an address step from it (a //! `getelementptr`, a bitcast or address-space cast, a `freeze`) is an operand of a phi, a //! `select`, or a vector or aggregate insert, the value of a store, or the function's result. -//! Such a pointer may meet one loaded from memory, and lowering then reaches the reference -//! through a device address it checks nowhere; its loads stay. +//! Such a pointer may meet one loaded from memory, which lowering then reaches by the address it +//! holds (a wide pointer); its loads stay. //! - **No write before it.** No instruction on any path from the function's start to the load may //! write device memory or order another thread's writes before it: a store or memory intrinsic //! whose destination is not thread or threadgroup memory, an atomic or atomic load of any space, diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 93c4e6ded..5bf14e618 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 = 2395; +pub const SEMANTICS_VERSION: u32 = 2396; /// 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/dynamic.rs b/crates/metal2vulkan/src/lower/access/dynamic.rs index b9896d1b2..6e9e0f2ac 100644 --- a/crates/metal2vulkan/src/lower/access/dynamic.rs +++ b/crates/metal2vulkan/src/lower/access/dynamic.rs @@ -26,7 +26,11 @@ //! 4-aligned address below it ([`wide_at`]). No access through a wide pointer is checked or //! counted: a loaded address is used as it is. Every slot a wide pointer may reach is reached by //! its device address (`BufferForm::Psb`, `access::finish_slots`), on a device that binds a -//! kernel's buffers too. +//! kernel's buffers too. A speculative load (`InstKind::Load`'s `speculative`, one canon makes +//! where the program did not run it) through a wide pointer is the exception: canon marks only a +//! load of a `constant` reference at an offset fixed at translation time, so its pointer names one +//! slot at that offset, and the load is that slot's access there, checked against the slot's buffer +//! as a static-root speculative load is ([`DynStrategy::SpeculativeLoad`]). //! //! **Compares.** `icmp` of wide pointers, or of a wide pointer and the null constant, compares //! the addresses they hold, null being 0. A compare of a wide pointer with a static one is @@ -184,6 +188,15 @@ pub enum DynStrategy { FromInt { of: ValueId }, /// A leaf load through a wide pointer. Load(WideAccess), + /// A speculative load of a leaf, or of a pointer's guest address where `pointer`, through a + /// wide pointer whose root set is one buffer slot: the static-root access of that slot at the + /// pointer's fixed byte offset `at` from its bind offset (`u32`, read as signed), checked + /// against the slot's buffer at any offset (`access::note_variable`, `emit::prepare`). + SpeculativeLoad { + access: Access, + at: u32, + pointer: bool, + }, /// A leaf store through a wide pointer. Store(WideAccess), /// `icmp` of wide pointers (or the null constant): their Metal addresses compared. @@ -264,6 +277,13 @@ impl DynStrategy { DynStrategy::ToInt { .. } => "access.dynamic.ptrtoint".into(), DynStrategy::FromInt { .. } => "access.dynamic.inttoptr".into(), DynStrategy::Load(_) => "access.dynamic.load".into(), + // Named as the static-root load it emits. + DynStrategy::SpeculativeLoad { + access, + pointer: true, + .. + } => access.reach(slots).qualify("access.dynamic.load_pointer"), + DynStrategy::SpeculativeLoad { access, .. } => access.name("access.load", slots), DynStrategy::Store(_) => "access.dynamic.store".into(), DynStrategy::Compare { .. } => "access.dynamic.compare".into(), } @@ -284,6 +304,7 @@ impl DynStrategy { DynStrategy::Load(a) | DynStrategy::Store(a) => { a.leaf.cost().saturating_add(ACCESS_WORDS) } + DynStrategy::SpeculativeLoad { access, .. } => access.words(), DynStrategy::Compare { .. } => 2 * ADDRESS_WORDS + 8, } } @@ -516,6 +537,9 @@ pub fn select( return coherent(cx, inst, call, c); } } + if let Some(s) = speculative_load(cx, inst, result_wide, m)? { + return Ok(Some(s)); + } Ok(Some(match &inst.kind { InstKind::Gep { src_ty, @@ -697,6 +721,71 @@ pub fn select( })) } +/// A speculative load (`InstKind::Load`'s `speculative`) through a wide pointer, of a leaf or of a +/// pointer (the result a wide pointer, `result_wide`): [`DynStrategy::SpeculativeLoad`], or `None` +/// for any other instruction. Canon marks a load speculative only where it reads a `constant` +/// reference at an offset fixed at translation time inside the referenced object (`air::canon`'s +/// arm speculation and constant-reference hoist), so the pointer's root set is that parameter's +/// slot and its offset is exact; a wide pointer of another set or offset is an invariant violation. +/// The slot joins the dynamic plan's, as every slot a wide pointer reaches does ([`plan_entry`]), +/// and keeps its device address. +fn speculative_load( + cx: &mut SelectCx<'_>, + inst: &Inst, + result_wide: bool, + m: &mut Meter, +) -> Result, Error> { + let InstKind::Load { + ptr, + align, + volatile: false, + atomic: None, + speculative: true, + .. + } = &inst.kind + else { + return Ok(None); + }; + let Some(roots) = wide(cx, *ptr) else { + return Ok(None); + }; + let leaf = match (result_wide, is_ptr_ty(cx, inst.ty)) { + (true, _) => pointer_leaf(cx, inst.ty, *align)?, + (false, false) => leaf::leaf_of( + cx.entry, + cx.profile, + &cx.memory.layouts, + inst.ty, + align_of(*align), + cx.memory.proven_align(cx.entry, *ptr), + )?, + (false, true) => return Ok(None), + }; + plan_entry(cx, *ptr)?; + let targets = match cx.root_sets.get(roots).map(|set| set.statics.as_ref()) { + Some(Ok(s)) if !s.null && !s.in_blob && s.targets.len() == 1 => super::held(s, m)?, + _ => { + return Err( + cx.invariant("a speculative load through a wide pointer of no one buffer slot") + ) + } + }; + let Some(at) = (cx.memory.offset(*ptr)) + .and_then(|o| o.exact()) + .and_then(|k| i32::try_from(k).ok()) + else { + return Err( + cx.invariant("a speculative load through a wide pointer at an offset not fixed") + ); + }; + let access = super::access_with_leaf(cx, (*ptr, None), leaf, targets, (false, false, true))?; + Ok(Some(DynStrategy::SpeculativeLoad { + access, + at: at.cast_unsigned(), + pointer: result_wide, + })) +} + /// A `air.{load,store}.{device,system}_coherent` access through a wide pointer: the wide leaf /// access with the coherent memory operands at `Device` scope, which selects the Vulkan memory /// model, as for a static-root pointer (`select_coherent`); `None` for another pointer. @@ -1233,6 +1322,16 @@ pub fn emit( Some(v) => v, None => return Err(cx.fail("a load without a value")), }, + DynStrategy::SpeculativeLoad { access, at, .. } => { + // One candidate slot and no blob: the access reads no part of the pointer's value, + // only its slot's address and bounds at offset `at`. + let p = cx.operand(fb, access.ptr)?; + let m = k32(fb, *at)?; + match super::emit::access_at(cx, fb, access, p, m, None)? { + Some(v) => v, + None => return Err(cx.fail("a load without a value")), + } + } DynStrategy::Store(a) => { let Some(val) = a.value else { return Err(cx.fail("a store without a value"));