Translator: an index past an array field's array sends its private-arena access to the sink, not onto the last element

a992aa6929 bounded a GEP into an array field of a thread variable to its
array, so a struct holding handles beside a run-time-indexed array closes
as a forwarding cell. In the private arena it held every run to that bound
by clamping each run-time index to the array's last element. A store past
the array then landed on the array's own last word. The runtime's
private_sinks contract wants such a store kept off every word a thread
reads, as a store past a private array is. On lavapipe at development
8a2f155883 (job m3-max-80535),
every_device_keeps_a_store_past_a_private_array_off_its_words failed: the
fold over the arrays moved by about 193,700, which is b[k]'s store of
t + 200000 over b[N - 1]. The same test at 8a2f155883 with a992aa6929
reverted (scratch tree, job m3-max-80554) is the cause check.

The bound only promises that no run reaches outside the variable; where an
out-of-range index lands is undefined in MSL. So the GEP's offset is now
the sink's, 2^32 - 1, where any run-time index is above its array's last
element: ULessThanEqual per index, LogicalAnd across them, and one select.
No arena holds that offset, so the access's existing containment check
makes it a zero load or no store, as an access past a private array is,
off the array's words and every other's. The forwarding analysis keeps
relying on the in-field assumption alone (memory::provenance, unchanged).
parked_heap_handles' run-time-indexed array fields stay bounded and the
struct still closes (access.private.gep.array, 3 uses).

docs/API.md's accepted difference says it: a read past the end of an array
field reads zero and a write is dropped, in the private arena as in a
variable of its own. The local, provenance and parked-handles docs say the
same. The device-arena budget test's run-time-index shapes cost one
instruction more than the clamp (a compare and a select in place of a
UMin): bounds 14, 13 and 25. SEMANTICS_VERSION 2321 (development holds
2320).
This commit is contained in:
Anees Iqbal committed 2026-12-02 15:16:20 -08:00
1 parent 77ec1b3bb6
commit 511f31b0ba
6 files changed
+49 -27

No files matched your search

+1 -1
View File
@@ -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 = 2320;
pub const SEMANTICS_VERSION: u32 = 2321;
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
+32 -12
View File
@@ -16,10 +16,12 @@
//! parameter is its region's offset, an `OpSpecConstantOp` of the specialized lengths before it;
//! - a GEP adds its datalayout offsets in wrapping `u32` arithmetic, with the buffer family's
//! index rule (`access::gep_offset`); a GEP into an array field (`memory::provenance`'s **Array
//! fields**) in the private arena first takes each run-time index as `min(index, len − 1)`, its
//! array's last element, so no run leaves the array whose bytes provenance bounds the GEP to
//! (an index past the array is undefined in Metal); `bitcast`, `addrspacecast`, and `freeze`
//! copy the offset, and `select` selects it (a constant side is its global's offset);
//! fields**) in the private arena is the sink's offset, `2^32 − 1`, where any run-time index is
//! past its array (above `len − 1`), so no run reaches past the array whose bytes provenance
//! bounds the GEP to (an index past the array is undefined in Metal): no arena holds that
//! offset, so the access's containment check makes it a zero load or no store, as an access
//! past a private array is; `bitcast`, `addrspacecast`, and `freeze` copy the offset, and
//! `select` selects it (a constant side is its global's offset);
//! - a constant `getelementptr` of a threadgroup global or of a closed constant global is the
//! global's offset plus the plan's exact displacement;
//! - `icmp` of two pointers into one arena or one typed object compares their offsets, which are
@@ -241,8 +243,8 @@ pub enum LocalStrategy {
offset: u32,
},
/// `getelementptr` on an offset: `base + konst + Σ index · stride` (wrapping `u32`). A GEP
/// into an array field in the private arena takes each index as `min(index, limit)`, its
/// array's last element (`limits`, one a term).
/// into an array field in the private arena is the sink's offset, `2^32 − 1`, where an index
/// is above its `limit`, its array's last element (`limits`, one a term).
Gep {
home: Home,
base: LocalPtr,
@@ -438,8 +440,13 @@ impl LocalStrategy {
match self {
LocalStrategy::Alloca { .. } | LocalStrategy::Copy { .. } => 4,
LocalStrategy::Gep { terms, limits, .. } => {
let each = if limits.is_some() { 24u32 } else { 16 };
12u32.saturating_add(each.saturating_mul(terms.len() as u32))
// A limit's compare and the conjunction per term, and the sink's select.
let (each, sink) = if limits.is_some() {
(26u32, 6)
} else {
(16, 0)
};
(12u32 + sink).saturating_add(each.saturating_mul(terms.len() as u32))
}
LocalStrategy::Select { .. } | LocalStrategy::Compare { .. } => 6,
LocalStrategy::NullCompare { .. } => 2,
@@ -1772,8 +1779,8 @@ pub fn select(
let base = local_ptr(cx, *base, home)?;
let (konst, terms) = super::gep_offset(cx, *src_ty, indices)?;
// Provenance bounds a GEP into an array field to its array (`memory::provenance`):
// in the private arena, whose objects lie side by side, each index is clamped to its
// array's last element so no run reaches past the field. A typed object's access
// in the private arena, whose objects lie side by side, an index past its array sends
// the access to the sink so no run reaches past the field. A typed object's access
// checks its levels itself (`memory::alloca`).
let limits = match home {
Home::Arena(Arena::Private) if cx.memory.is_array_gep(at) => {
@@ -2455,12 +2462,17 @@ pub fn emit(
let c = k32(fb, *konst)?;
off = fb.binary(BinOp::IAdd, off, c)?;
}
let mut inside = None;
for (k, (v, stride, form)) in terms.iter().enumerate() {
let x = cx.operand(fb, *v)?;
let mut x = index_u32(fb, x, *form)?;
if let Some(limit) = limits.as_ref().and_then(|l| l.get(k)) {
let l = k32(fb, *limit)?;
x = fb.glsl(GlslOp::UMin, &[x, l])?;
let fits = fb.binary(BinOp::ULessThanEqual, x, l)?;
inside = Some(match inside {
None => fits,
Some(all) => fb.binary(BinOp::LogicalAnd, all, fits)?,
});
}
if *stride != 1 {
let s = k32(fb, *stride)?;
@@ -2468,7 +2480,15 @@ pub fn emit(
}
off = fb.binary(BinOp::IAdd, off, x)?;
}
off
// An index past its array: the sink's offset, which no arena holds, so the access's
// containment check makes it a zero load or no store.
match inside {
Some(inside) => {
let sink = k32(fb, u32::MAX)?;
fb.select(inside, off, sink)?
}
None => off,
}
}
LocalStrategy::Copy { of, .. } => ptr_value(cx, fb, *of)?,
LocalStrategy::Select { c, t, f, .. } => {
+4 -3
View File
@@ -18,9 +18,10 @@
//! index past an array undefined, so over a base whose roots are stack allocations at constant
//! offsets its offsets are its constant displacement plus at most `(len − 1) · stride` for each
//! run-time index ([`Cx::array_bound`]): the access stays inside the array, apart from the
//! struct's other fields. Lowering holds every run to that: in the private arena each such index
//! is clamped to its array's last element (`lower::access::local`), and a typed object's access
//! never leaves its levels (`memory::alloca`). A flattened offset (a run-time pointer index), a
//! struct's other fields. Lowering holds every run to that: in the private arena an access
//! through such a GEP whose index is past its array goes to the sink, a zero load or no store
//! (`lower::access::local`), and a typed object's access never leaves its levels
//! (`memory::alloca`). A flattened offset (a run-time pointer index), a
//! run-time vector lane, an address that more arithmetic offsets or that is merged or stored, a
//! base that may be null, and memory outside the thread space keep unknown offsets. A run-time
//! index at a struct level has no IR form.
@@ -17,8 +17,9 @@
//! makes the access once and one that makes it twice, everything else equal, as in
//! `engine_access_size`; the second's entry weight (`spirv::roll::entry_weight`) less the first's
//! is what one access costs, its index arithmetic (a `mul` and a `urem` of the thread ID), the
//! clamp of a run-time index to its array (the array is a GEP into an array field,
//! `memory::provenance`), its containment and the add that keeps its value live included. The
//! compare of a run-time index with its array's last element and the select of the sink's offset
//! past it (the array is a GEP into an array field, `memory::provenance`), its containment and the
//! add that keeps its value live included. The
//! shapes: a word load and a word store at a run-time index, a `float4` load (four words) at a
//! run-time index, and a word load 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
@@ -193,7 +194,7 @@ const SHAPES: &[Shape] = &[
)
},
value: Some(("i32", "add")),
bound: 13,
bound: 14,
},
Shape {
name: "word store at a run-time index",
@@ -204,7 +205,7 @@ const SHAPES: &[Shape] = &[
)
},
value: None,
bound: 12,
bound: 13,
},
Shape {
name: "float4 load at a run-time index",
@@ -215,7 +216,7 @@ const SHAPES: &[Shape] = &[
)
},
value: Some(("<4 x float>", "fadd")),
bound: 24,
bound: 25,
},
Shape {
name: "word load at a constant index",
@@ -11,8 +11,8 @@
//!
//! - `parked_heap_handles`: no load reads the handle fields, directly or through a copy, so their
//! stores need no cell encoding and write nothing (`lower::access::cell`); the struct lies in the
//! private arena, where each run-time array index is clamped to its array's last element
//! (`lower::access::local`). The kernel compiles and passes L3 on every committed profile with
//! private arena, where an access at a run-time array index past its array goes to the sink, a
//! zero load or no store (`lower::access::local`). The kernel compiles and passes L3 on every committed profile with
//! the sampled heap; on the others it is refused as every argument-buffer handle is there,
//! `ArgumentBufferTextures`.
//! - `parked_heap_handles_read` reads the parked handles back from the cell, and
+4 -4
View File
@@ -2959,10 +2959,10 @@ named states the rule in full.
only loaded from or stored to, the translator takes the access to stay inside the array, as the
Metal Shading Language leaves an index past an array's end undefined. So a struct that holds
such an array beside pointers or texture and sampler handles keeps them apart from it, and is
translated where it was refused. In the private arena the index is clamped to the array's last
element (a read past the end reads the last element, a write writes it); in a variable of its
own a read past the end reads zero and a write is dropped. Metal's own access past the end
reaches whatever bytes follow the array: a neighbouring field or another variable.
translated where it was refused. A read past the end reads zero and a write is dropped, in the
private arena as in a variable of its own, as an access past a private array is: the access
reaches no word, the array's own or another's. Metal's own access past the end reaches whatever
bytes follow the array: a neighbouring field or another variable.
**Queues and command buffers**