From 79698bdd67dd910296888631afd8aa340b55a54c Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 4 Dec 2026 22:14:34 -0800 Subject: [PATCH] Lowering: a speculative load through a wide pointer is checked against its slot canon marks a load speculative where it runs in lanes the program did not run it in: each load of a branch arm it speculates (a constant reference's, at offset 0) and each constant-reference load the hoist moves. Lowering checks such a load at any offset against its slot's buffer, but only on the static-root path. Where a phi, select or pointer cell joins the reference's pointer with one loaded from memory, the reference is a wide pointer, and every access through a wide pointer went unchecked: a speculated arm's load read the reference's first bytes in every lane of the branch's block, through a binding the program never read there, past its end where the guest bound it shorter than the load. The fix: the dynamic family selects a speculative load through a wide pointer, of a leaf or of a pointer, as DynStrategy::SpeculativeLoad: the static-root access of the one slot the pointer's root set names, at the pointer's fixed offset, marked speculative, so note_variable gives the slot its SlotBounds, the zero range is placed, and emit::prepare checks the load and reads zero outside, as for a static-root speculative load. The slot still joins the dynamic plan's slots (plan_entry), so it keeps the device address every wide-reached slot has, on a bound-form device too. A speculative load through a wide pointer of another root set or of an offset not fixed is an invariant violation: canon marks no such load. Its census name is the static-root load's it emits (access.load.psb.checked, or access.dynamic.load_pointer.psb for a pointer). Which fix, and why this one: (a) lowering checks the load, against (b) speculate.rs leaving such an arm unflattened. (a) is the smaller structural change: about 90 lines in the dynamic family, all reusing the static-root check, and it moves only the loads the bug names. (b) cannot see width: a wide pointer is lowering's decision (the memory plan's dynamic component), so canon would have to over-approximate with const_hoist's shared-pointer walk, which also unflattens arms whose loads go through narrow joined pointers and are checked today, giving their group operations the AIR's lanes where Metal speculates them (rule 1), or carry the memory plan's provenance into canon. const_hoist's module docs no longer say lowering checks a wide pointer's access nowhere. SEMANTICS_VERSION 2396. Verified: engine_hoist_contain's two wide-pointer tests fail at f4a0f4e628 (lavapipe's address form and every committed profile, jobs m3-max-87967 and m3-max-87970): no SlotBounds for k's slot, and the interpreter's OutOfBounds trap in the short-binding case of both forms (word: 4 bytes read from a 2-byte binding; pointer: the second word of an 8-byte load past a 4-byte one), while the whole, unbound and flag-on cases pass. The green run with this commit is named in the lane's report. --- .../metal2vulkan-air/src/canon/const_hoist.rs | 4 +- crates/metal2vulkan/src/cache/mod.rs | 2 +- .../metal2vulkan/src/lower/access/dynamic.rs | 101 +++++++++++++++++- 3 files changed, 103 insertions(+), 4 deletions(-) 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"));