From 674cf321a9e528d524e14f2df70054876df937db Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 23 Nov 2026 00:31:19 -0800 Subject: [PATCH] Provenance: an aggregate that stays a value, and a whole-slot zero fill beside run-time-indexed pointers, keep a cell closed What: two rules of the pointer-cell verification (memory/provenance/cells.rs). - An insertvalue whose aggregate stays a value (its every use, and every use of each aggregate built on it, is an extractvalue, the aggregate operand of an insertvalue, or an incoming value of a phi of such aggregates) no longer opens the allocas of the pointer it inserts. Provenance already reads each extractvalue through its insertion chain as the inserted pointer, and the arith family forwards it, so the pointer's accesses are the extraction's, which the walk sees. - A zero fill beside pointer accesses at an unknown offset keeps the cell closed when every such access lies at a multiple of the pointer's size (its offset's proven alignment) and so does each end of the fill; exact slots must still be covered wholly or not at all. A load at an unknown offset already reads null wherever the cell has zero bytes. Why: a thread's array of entry buffer pointers stayed an open cell in two shapes, and under the identity domains a static buffer pointer stored into an open cell is refused (Unsupported{Lowering(Access)} from pointer::store_pointer, which takes only a buffer destination). Closed, each element holds its static fat pointer (root, offset) and each access through a loaded element switches among the slots stored there, the form an array of buffer pointers indexed at run time already took: no device address, the same form on every device. In the private corpus's census (runtime-floor) this was 6 functions in 2 libraries: 4 that put the array's address into an array_ref of a struct passed by value to inlined helpers (refused under every specialization: the real-make rows), and 2 that zero the array with a memset beside a loop that loads, moves and stores back an element at a run-time index (plain census only; their specializations translate). The other route, storing the static pointer's address into the open cell, would make each loaded element a device address; it is not taken. How verified: the owned kernel_buffer_pointer_arrays' by-value and zero-filled kernels were refused at the guest rows' site at the parent (CLI on runtime-floor and gate-lavapipe, m3-max-70315; the test, m3-max-70329) and compile, pass L3 and compute Metal's words on the CPU interpreter on every identity profile here (m3-max-70341); the plain array kernel compiled before and after. Lint: fmt, clippy (plain, --cfg m2v_gpu, --cfg m2v_cpu_icd), workspace Rustdoc (m3-max-70346). The translator suites, the census and the golden cells that move are in the SEMANTICS commit's body. --- .../src/memory/provenance/cells.rs | 152 ++++++++++++++++-- 1 file changed, 135 insertions(+), 17 deletions(-) diff --git a/crates/metal2vulkan/src/memory/provenance/cells.rs b/crates/metal2vulkan/src/memory/provenance/cells.rs index 6248375aa..0ddee54f4 100644 --- a/crates/metal2vulkan/src/memory/provenance/cells.rs +++ b/crates/metal2vulkan/src/memory/provenance/cells.rs @@ -17,12 +17,17 @@ //! (`getelementptr`, casts, `phi`, `select`, `freeze`), a plain load or store address, a memory //! intrinsic's operand (`memset`, `memcpy`, `memmove`: its bytes are a data access), a compare, //! or a pointer stored into a forwarding cell: an atomic, any other call, `ptrtoint`, an -//! aggregate holding it, a load or store of an aggregate that holds pointers, or a store of its -//! pointer into other memory. It is open too when its slots are not disciplined: two pointer -//! slots overlap, one slot is accessed as two pointer types, a pointer access at an unknown -//! offset meets any data access, a data access (a load or store of a non-pointer, a memory -//! intrinsic's bytes) overlaps a pointer slot, or a data access's bytes are not -//! translation-time constants; unless it is a pool (below). And every alloca is open when some +//! aggregate holding it that leaves SSA, a load or store of an aggregate that holds pointers, or +//! a store of its pointer into other memory. An aggregate stays a value when its every use, and +//! every use of each aggregate built on it, is an `extractvalue`, the aggregate operand of an +//! `insertvalue`, or an incoming value of a `phi` of such aggregates (a struct of `array_ref`s +//! passed by value to an inlined function): a pointer inserted into it reaches only the +//! extractions of it, which provenance reads as the pointer itself, so its accesses are theirs. +//! It is open too when its slots are not disciplined: two pointer slots overlap, one slot is +//! accessed as two pointer types, a pointer access at an unknown offset meets any data access, a +//! data access (a load or store of a non-pointer, a memory intrinsic's bytes) overlaps a pointer +//! slot, or a data access's bytes are not translation-time constants; unless it is a pool +//! (below). And every alloca is open when some //! access goes through a thread-space pointer that may be `Dynamic` (it may alias any alloca). A //! verification that opens allocas or makes pools reruns the fixpoint with them open or pooled; a //! second one opens every alloca, so the analysis runs at most three rounds. @@ -84,15 +89,17 @@ //! as well as every pointer stored there, and a load at an unknown offset reads null when its cell //! has any zero bytes. A slot copy moves the zero bytes inside the copied bytes to the destination //! at the same distance from the copy's start. Such a fill keeps its cells closed when it covers -//! each pointer slot wholly or not at all and no pointer access of the cell is at an unknown offset -//! ([`disciplined`]); any other `memset` is a data access, as above. The lowered fill writes the +//! each pointer slot wholly or not at all and, where some pointer access of the cell is at an +//! unknown offset, every such access lies at a multiple of the pointer's size (its offset's proven +//! alignment), as each end of the fill does, so the fill covers whole slots wherever the access +//! lands ([`disciplined`]); any other `memset` is a data access, as above. The lowered fill writes the //! zero bytes as it would into any private memory. use std::collections::{BTreeMap, BTreeSet}; use crate::air::intrinsics::LlvmIntrinsic; use crate::air::ir::{ - AddrSpace, Body, Call, Callee, CastOp, Inst, InstId, InstKind, TypeId, Value, + AddrSpace, Body, Call, Callee, CastOp, Inst, InstId, InstKind, Type, TypeId, Value, }; use crate::budget::{tree_bytes, Meter}; use crate::error::Error; @@ -787,6 +794,9 @@ struct Use { slots: BTreeMap, /// The pointer type of a pointer access at an unknown offset. wild: Option, + /// Some pointer access at an unknown offset is not proven to lie at a multiple of the + /// pointer's size. + wild_unaligned: bool, /// The bytes of each data access, `[lo, hi)`, ordered, each once. data: BTreeSet<(i64, i64)>, /// Some data access has bytes that are not translation-time constants. @@ -816,7 +826,10 @@ pub(super) fn verify(cx: &mut Cx<'_>, body: &Body, m: &mut Meter) -> Result()); + drop(held); let copied_bytes = copied.0.len() * map_entry_bytes::>() + copied.1.len() * map_entry_bytes::(); drop(copied); @@ -949,6 +962,88 @@ fn pointer_bits(cx: &mut Cx<'_>, m: &mut Meter) -> Result { Ok((by_store, by_cast)) } +/// The `insertvalue`s whose aggregate stays a value (module doc): every use of it, and of each +/// aggregate built on it, is an `extractvalue`, the aggregate operand of an `insertvalue`, or an +/// incoming value of a `phi` of such aggregates. One work unit per instruction and per aggregate +/// that leaves; the set is charged to the ledger, which [`verify`] returns. +fn held_insertions(cx: &Cx<'_>, body: &Body, m: &mut Meter) -> Result, Error> { + let types = cx.e.module.types(); + // An `insertvalue` or a `phi` of an aggregate that holds a pointer. + let builds = |v: Value| match v { + Value::Inst(c) => body.insts.get(c).is_some_and(|i| { + matches!(i.kind, InstKind::InsertValue { .. } | InstKind::Phi { .. }) + && types.get(i.ty).is_some_and(Type::is_aggregate) + && types.holds_pointer(i.ty) + }), + _ => false, + }; + // The aggregates some use takes out of SSA, and those still to follow to what they are built on. + let mut left: BTreeSet = BTreeSet::new(); + let mut work: Vec = Vec::new(); + let mut grown = 0usize; + for (_, inst) in body.insts.iter() { + m.tick(1)?; + let keeps = |v: Value| match &inst.kind { + InstKind::ExtractValue { agg, .. } | InstKind::InsertValue { agg, .. } => *agg == v, + // A `phi` of an aggregate that holds a pointer is itself such an aggregate. + InstKind::Phi { .. } => true, + _ => false, + }; + let mut found = Ok(()); + inst.kind.for_each_value(|v| { + if found.is_err() || !builds(v) || keeps(v) { + return; + } + if let Value::Inst(c) = v { + found = leave(c, (&mut left, &mut work, &mut grown), m); + } + }); + found?; + } + // An aggregate built on one that leaves leaves with it. + while let Some(c) = work.pop() { + m.tick(1)?; + let (agg, incoming) = match body.insts.get(c).map(|i| &i.kind) { + Some(InstKind::InsertValue { agg, .. }) => (Some(*agg), &[][..]), + Some(InstKind::Phi { incoming, .. }) => (None, &incoming[..]), + _ => (None, &[][..]), + }; + for v in agg.into_iter().chain(incoming.iter().map(|(v, _)| *v)) { + if let (Value::Inst(a), true) = (v, builds(v)) { + leave(a, (&mut left, &mut work, &mut grown), m)?; + } + } + } + m.release(grown); + let mut held = BTreeSet::new(); + for (id, inst) in body.insts.iter() { + m.tick(1)?; + if matches!(inst.kind, InstKind::InsertValue { .. }) + && builds(Value::Inst(id)) + && !left.contains(&id) + { + m.charge(map_entry_bytes::())?; + held.insert(id); + } + } + m.release(left.len() * map_entry_bytes::()); + Ok(held) +} + +/// Record that aggregate `c` leaves SSA ([`held_insertions`]): new, it is charged to the ledger and +/// queued to follow. +fn leave( + c: InstId, + (left, work, grown): (&mut BTreeSet, &mut Vec, &mut usize), + m: &mut Meter, +) -> Result<(), Error> { + if left.insert(c) { + m.charge(map_entry_bytes::())?; + push_working(work, c, grown, m)?; + } + Ok(()) +} + /// [`verify`]'s walk of one round, with the round's pointer bits. Its working tables are /// returned to the ledger when it returns ([`Round::charged`]). fn verify_round( @@ -957,10 +1052,11 @@ fn verify_round( bits: &Bits, words: &BTreeMap, ends: &BitCopies, + held: &BTreeSet, m: &mut Meter, ) -> Result { let mut round = Round::default(); - let r = round.walk(cx, body, (bits, words, ends), m); + let r = round.walk(cx, body, (bits, words, ends, held), m); m.release(round.charged()); r } @@ -976,8 +1072,14 @@ struct Round { grown: usize, } -/// What a round reads besides the body: the pointer bits, the pointer words, and the bit copies. -type RoundInputs<'r> = (&'r Bits, &'r BTreeMap, &'r BitCopies); +/// What a round reads besides the body: the pointer bits, the pointer words, the bit copies, and +/// the insertions into aggregates that stay values. +type RoundInputs<'r> = ( + &'r Bits, + &'r BTreeMap, + &'r BitCopies, + &'r BTreeSet, +); impl Round { /// The ledger bytes the tables hold: every cell's entry, exact slot, data range, and zero @@ -1006,7 +1108,7 @@ impl Round { &mut self, cx: &mut Cx<'_>, body: &Body, - ((by_store, by_cast), words, (copy_ends, copy_stores)): RoundInputs<'_>, + ((by_store, by_cast), words, (copy_ends, copy_stores), held): RoundInputs<'_>, m: &mut Meter, ) -> Result { // The pointer a store of pointer bits stores, when it is a store of that pointer. @@ -1132,6 +1234,9 @@ impl Round { op: CastOp::PtrToInt, .. } if by_cast.get(&id).is_some_and(|e| e.1) => {} + // A pointer inserted into an aggregate that stays a value reaches only the + // extractions of it, which are the pointer itself (module doc). + InstKind::InsertValue { .. } if held.contains(&id) => {} other => { // Any other operand use reaches the memory it points to: an atomic, a call, // `ptrtoint`, an aggregate. @@ -1740,7 +1845,15 @@ fn disciplined(u: &Use, m: &mut Meter) -> Result { return Ok(false); } if let Some(w) = u.wild { - if !u.data.is_empty() || u.data_unknown || !u.zero.is_empty() { + if !u.data.is_empty() || u.data_unknown { + return Ok(false); + } + // A zero fill beside them covers whole slots: every access at an unknown offset lies at + // a multiple of the pointer's size, and so does each end of every fill. + let whole = |&(lo, hi): &(i64, i64)| { + lo.rem_euclid(POINTER_BYTES) == 0 && hi.rem_euclid(POINTER_BYTES) == 0 + }; + if !u.zero.is_empty() && (u.wild_unaligned || !u.zero.iter().all(whole)) { return Ok(false); } for t in u.slots.values() { @@ -1749,7 +1862,7 @@ fn disciplined(u: &Use, m: &mut Meter) -> Result { return Ok(false); } } - return Ok(true); + return fills_cover_slots(u, m); } if u.data_unknown { return Ok(false); @@ -1769,7 +1882,11 @@ fn disciplined(u: &Use, m: &mut Meter) -> Result { return Ok(false); } } - // Every zero fill covers each pointer slot wholly or not at all. + fills_cover_slots(u, m) +} + +/// Whether every zero fill of `u` covers each exact pointer slot wholly or not at all. +fn fills_cover_slots(u: &Use, m: &mut Meter) -> Result { for &(lo, hi) in &u.zero { m.tick(1)?; if lo >= hi { @@ -1858,6 +1975,7 @@ fn note_access( if u.wild.replace(ty).is_some_and(|t| t != ty) { u.open = true; } + u.wild_unaligned |= (1i64 << f.align) < POINTER_BYTES; } } } else if cx.e.module.types().holds_pointer(ty) {