Translator: a GEP into an array field of a thread variable stays inside its array
A thread struct that holds pointers or heap handles beside an array field indexed at run time was open to pointer forwarding: provenance made the offset of any run-time index unknown, so the data access could overlap a pointer slot. A heap handle stored into such a struct then fell to the handle family's catch-all, Lowering(Access). That is the refusal of a post-tessellation vertex function a walk met (two render pipeline states refused). The Metal Shading Language leaves an index past an array undefined. So a thread-space getelementptr whose first index is a constant, each of whose run-time indices steps over an array its own type path names, and whose every use is a load or store address, is a GEP into an array field (provenance::array_geps). Over a base whose roots are stack allocations at constant offsets, its offsets are its displacement plus at most (len - 1) * stride per run-time index. These keep unknown offsets: a flattened offset (a run-time pointer index), a run-time vector lane, an address another GEP offsets or that is merged or stored, a base that may be null, and memory outside the thread space. A run-time index at a struct level has no IR form. Lowering holds every run to the bound. In the private arena, where objects lie side by side, such a GEP takes each run-time index as min(index, len - 1) (access.private.gep.array). A typed object's access already never leaves its levels. docs/API.md records the accepted difference: an index past the end of an array field of a local struct does not reach its neighbouring fields; it is undefined in MSL. The struct then closes as a forwarding cell, and its heap-handle stores that no load reads take the cell's unread store (the previous commit). Verified: - red: m3-max-80079, b9d9dfc9ba (parked_heap_handles refused at its handle store, block 0 instruction 11). - green: gate m3-max-80143 at the same tree on a scratch branch (lavapipe tests 47 passed; validation 1315 passed, 3 skipped; conformance 1047 passed; the translator suite's only failures are the tables development re-blesses: the engine inventory with four new parked_heap_handles rows, the golden printout, and 315 golden hash cells under SEMANTICS_VERSION 2319). - L0: a_gep_into_an_array_field_is_bounded_to_the_array (the bound, and each disqualifying shape staying unknown). - read-back, whole-struct copy (a memcpy) and a stepped address stay Lowering(Access). - the walk's two metallibs on the XTX node (m3-max-80156): the handle store's refusal is gone; the vertex function next met the two refusals the following commits remove. Cost: one UMin per run-time index into a private-arena array, the bound itself. The device-arena budget test's word bounds move by one each (13 for a word load, 12 for a word store, 24 for a float4 load). SEMANTICS_VERSION 2320 (development holds 2319).
This commit is contained in:
1 parent
728e4c9b1d
commit
a992aa6929
8 files changed
+348
-15
No files matched your search
Vendored
+1
-1
@@ -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 = 2319;
|
||||
pub const SEMANTICS_VERSION: u32 = 2320;
|
||||
|
||||
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
|
||||
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
|
||||
|
||||
@@ -15,8 +15,11 @@
|
||||
//! constant `getelementptr` of one adds the plan's exact displacement); a `[[threadgroup(n)]]`
|
||||
//! 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`); `bitcast`, `addrspacecast`, and `freeze` copy the offset,
|
||||
//! and `select` selects it (a constant side is its global's offset);
|
||||
//! 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);
|
||||
//! - 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
|
||||
@@ -237,12 +240,15 @@ pub enum LocalStrategy {
|
||||
home: Home,
|
||||
offset: u32,
|
||||
},
|
||||
/// `getelementptr` on an offset: `base + konst + Σ index · stride` (wrapping `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).
|
||||
Gep {
|
||||
home: Home,
|
||||
base: LocalPtr,
|
||||
konst: u32,
|
||||
terms: GepTerms,
|
||||
limits: Option<Box<[u32]>>,
|
||||
},
|
||||
/// `bitcast`, `addrspacecast`, or `freeze` of a pointer.
|
||||
Copy {
|
||||
@@ -339,6 +345,9 @@ impl LocalStrategy {
|
||||
"access.payload.alloca",
|
||||
],
|
||||
),
|
||||
L::Gep {
|
||||
limits: Some(_), ..
|
||||
} => "access.private.gep.array",
|
||||
L::Gep { home, .. } => by_home(
|
||||
*home,
|
||||
[
|
||||
@@ -428,8 +437,9 @@ impl LocalStrategy {
|
||||
pub fn words(&self) -> u32 {
|
||||
match self {
|
||||
LocalStrategy::Alloca { .. } | LocalStrategy::Copy { .. } => 4,
|
||||
LocalStrategy::Gep { terms, .. } => {
|
||||
12u32.saturating_add(16u32.saturating_mul(terms.len() as u32))
|
||||
LocalStrategy::Gep { terms, limits, .. } => {
|
||||
let each = if limits.is_some() { 24u32 } else { 16 };
|
||||
12u32.saturating_add(each.saturating_mul(terms.len() as u32))
|
||||
}
|
||||
LocalStrategy::Select { .. } | LocalStrategy::Compare { .. } => 6,
|
||||
LocalStrategy::NullCompare { .. } => 2,
|
||||
@@ -1454,6 +1464,23 @@ fn shape_of(cx: &mut SelectCx<'_>, v: Value, m: &mut Meter) -> Result<Sh, Error>
|
||||
}
|
||||
}
|
||||
|
||||
/// The last element of the array each run-time index of a GEP into an array field steps over, in
|
||||
/// the order of its offset's terms (`access::gep_offset`), as a `u32` index.
|
||||
fn array_limits(
|
||||
cx: &mut SelectCx<'_>,
|
||||
src_ty: TypeId,
|
||||
indices: &[Value],
|
||||
) -> Result<Box<[u32]>, Error> {
|
||||
let (_, terms) = super::gep_offset_indexed(cx, src_ty, indices)?;
|
||||
terms
|
||||
.iter()
|
||||
.map(|t| match t.over {
|
||||
Indexed::Array(len) => Ok(u32::try_from(len.saturating_sub(1)).unwrap_or(u32::MAX)),
|
||||
_ => Err(cx.invariant("a GEP into an array field steps over no array")),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The dynamic indices of the `getelementptr` that defines typed access pointer `ptr`, one per
|
||||
/// dynamic level of `path` in order, where they line up one to one with those levels; `None` where
|
||||
/// they do not (or `ptr` is no `getelementptr`), and the access recovers each index from its
|
||||
@@ -1744,11 +1771,22 @@ pub fn select(
|
||||
use_home(cx, home, m)?;
|
||||
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
|
||||
// checks its levels itself (`memory::alloca`).
|
||||
let limits = match home {
|
||||
Home::Arena(Arena::Private) if cx.memory.is_array_gep(at) => {
|
||||
Some(array_limits(cx, *src_ty, indices)?)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
LocalStrategy::Gep {
|
||||
home,
|
||||
base,
|
||||
konst,
|
||||
terms,
|
||||
limits,
|
||||
}
|
||||
}
|
||||
InstKind::Cast {
|
||||
@@ -2406,16 +2444,24 @@ pub fn emit(
|
||||
let r = match s {
|
||||
LocalStrategy::Alloca { offset, .. } => k32(fb, *offset)?,
|
||||
LocalStrategy::Gep {
|
||||
base, konst, terms, ..
|
||||
base,
|
||||
konst,
|
||||
terms,
|
||||
limits,
|
||||
..
|
||||
} => {
|
||||
let mut off = ptr_value(cx, fb, *base)?;
|
||||
if *konst != 0 {
|
||||
let c = k32(fb, *konst)?;
|
||||
off = fb.binary(BinOp::IAdd, off, c)?;
|
||||
}
|
||||
for (v, stride, form) in terms {
|
||||
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])?;
|
||||
}
|
||||
if *stride != 1 {
|
||||
let s = k32(fb, *stride)?;
|
||||
x = fb.binary(BinOp::IMul, x, s)?;
|
||||
|
||||
@@ -372,6 +372,10 @@ pub struct MemoryPlan {
|
||||
pub needs: MemNeeds,
|
||||
/// Which allocas forward the pointers stored into them (pointer cells).
|
||||
pub cells: CellFacts,
|
||||
/// The GEPs into array fields, sorted, each with its extent (`provenance::array_geps`):
|
||||
/// provenance bounds their offsets to their arrays, so lowering keeps each run-time index of
|
||||
/// one into the private arena inside its array ([`MemoryPlan::is_array_gep`]).
|
||||
pub array_geps: Vec<(InstId, i64)>,
|
||||
/// The entry's bindings by parameter and layouts' members by offset, indexed once.
|
||||
pub abi: AbiIndex,
|
||||
/// The `<64 x T>` instructions that are simdgroup matrices' fragments, sorted
|
||||
@@ -559,6 +563,15 @@ impl MemoryPlan {
|
||||
+ self.layouts.retained_bytes()
|
||||
+ self.cells.retained_bytes()
|
||||
+ self.abi.retained_bytes()
|
||||
+ self.array_geps.capacity() * std::mem::size_of::<(InstId, i64)>()
|
||||
}
|
||||
|
||||
/// Whether `getelementptr` `id` is a GEP into an array field (`provenance::array_geps`),
|
||||
/// whose offsets provenance bounds to its array.
|
||||
pub fn is_array_gep(&self, id: InstId) -> bool {
|
||||
self.array_geps
|
||||
.binary_search_by_key(&id, |(g, _)| *g)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
/// `(slot index, exact byte offset)` when `v` is a buffer pointer whose only root is one
|
||||
@@ -820,6 +833,7 @@ impl Planner<'_> {
|
||||
},
|
||||
cells: prov.cells,
|
||||
abi: prov.abi,
|
||||
array_geps: prov.array_geps,
|
||||
fragment_values,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -625,6 +625,99 @@ fn typed_allocas_and_the_private_arena() {
|
||||
assert!(!plan.priv_arena.null_word);
|
||||
}
|
||||
|
||||
/// A GEP into an array field of a stack allocation, whose run-time index steps over the array its
|
||||
/// own type path names and whose only use is a load's address, has its offsets bounded to the
|
||||
/// array. A flattened index (the array's address, then a run-time pointer index), a GEP whose
|
||||
/// result another GEP offsets, a run-time lane of a vector field and the same path over a device
|
||||
/// pointer are no GEPs into an array field, and their offsets stay unknown; so does one over a
|
||||
/// base that may be null, which is one by structure. A run-time index at a struct level has no IR
|
||||
/// form (a struct index is a constant).
|
||||
#[test]
|
||||
fn a_gep_into_an_array_field_is_bounded_to_the_array() {
|
||||
let mut k = Kernel::new();
|
||||
k.param(AddrSpace::Device, buffer(0, AddrSpace::Device, None));
|
||||
let mut ptrs = Vec::new();
|
||||
let e = k.build(|fb| {
|
||||
let i1 = fb.module().int(1);
|
||||
let i32t = fb.module().int(32);
|
||||
let i64t = fb.module().int(64);
|
||||
let f = fb.module().float();
|
||||
let f4 = fb.module().vector(f, 4);
|
||||
let weights = fb.module().array(f, 4);
|
||||
let p0 = fb.module().ptr(AddrSpace::Thread);
|
||||
// { float4, float[4], thread pointer }: the weights at [16, 32).
|
||||
let st = fb.module().struct_ty(&[f4, weights, p0]);
|
||||
let one32 = fb.module().const_int(i32t, 1);
|
||||
let zero32 = fb.module().const_int(i32t, 0);
|
||||
let (z, one) = (
|
||||
fb.module().const_int(i64t, 0),
|
||||
fb.module().const_int(i64t, 1),
|
||||
);
|
||||
let a = fb.push(
|
||||
p0,
|
||||
InstKind::Alloca {
|
||||
ty: st,
|
||||
count: one32,
|
||||
align: align(16),
|
||||
},
|
||||
);
|
||||
let i = load(fb, i64t, fb.arg(0));
|
||||
let bounded = gep(fb, st, a, &[z, one32, i]);
|
||||
let _ = load(fb, f, bounded);
|
||||
let field = gep(fb, st, a, &[z, one32, z]);
|
||||
let flat = gep(fb, f, field, &[i]);
|
||||
let _ = load(fb, f, flat);
|
||||
let fed = gep(fb, st, a, &[z, one32, i]);
|
||||
let after = gep(fb, f, fed, &[one]);
|
||||
let _ = load(fb, f, after);
|
||||
let lane = gep(fb, st, a, &[z, zero32, i]);
|
||||
let _ = load(fb, f, lane);
|
||||
let device = gep(fb, st, fb.arg(0), &[z, one32, i]);
|
||||
let _ = load(fb, f, device);
|
||||
let c = fb.module().const_int(i1, 0);
|
||||
let null = Value::Const(fb.module().constant(p0, Const::Null));
|
||||
let maybe = fb.push(
|
||||
p0,
|
||||
InstKind::Select {
|
||||
c,
|
||||
t: a,
|
||||
f: null,
|
||||
fmf: FastMath::NONE,
|
||||
},
|
||||
);
|
||||
let nullable = gep(fb, st, maybe, &[z, one32, i]);
|
||||
let _ = load(fb, f, nullable);
|
||||
ptrs.extend([bounded, flat, fed, after, lane, device, nullable]);
|
||||
fb.ret(None);
|
||||
});
|
||||
let plan = ok(&e);
|
||||
let gep_id = |v: Value| match v {
|
||||
Value::Inst(id) => id,
|
||||
other => panic!("not an instruction: {other:?}"),
|
||||
};
|
||||
let [bounded, flat, fed, after, lane, device, nullable] = ptrs[..] else {
|
||||
unreachable!()
|
||||
};
|
||||
// Element 0 at 16, element 3 at 28.
|
||||
assert_eq!(
|
||||
plan.offset(bounded),
|
||||
Some(OffsetRange::Span { lo: 16, hi: 28 })
|
||||
);
|
||||
assert!(plan.is_array_gep(gep_id(bounded)));
|
||||
for (what, v) in [
|
||||
("flattened", flat),
|
||||
("fed", fed),
|
||||
("offset after", after),
|
||||
("vector lane", lane),
|
||||
("device", device),
|
||||
] {
|
||||
assert_eq!(plan.offset(v), Some(OffsetRange::Top), "{what}");
|
||||
assert!(!plan.is_array_gep(gep_id(v)), "{what}");
|
||||
}
|
||||
assert_eq!(plan.offset(nullable), Some(OffsetRange::Top));
|
||||
assert!(plan.is_array_gep(gep_id(nullable)));
|
||||
}
|
||||
|
||||
/// A private pointer that may be null (a select of an alloca's address and null) puts its alloca
|
||||
/// in the private arena, and the arena keeps its first word for null: the alloca lies past it.
|
||||
#[test]
|
||||
|
||||
@@ -11,6 +11,20 @@
|
||||
//! that `phi`, `select`, and `freeze` merge: a class is `Byte`, one [`HandleClass`], or a conflict,
|
||||
//! which is `Unsupported{HandleAsPointer}`.
|
||||
//!
|
||||
//! **Array fields.** A run-time index makes an offset unknown, but for one shape. A thread-space
|
||||
//! `getelementptr` whose first index is a constant, each of whose run-time indices steps over the
|
||||
//! elements of an array its own type path names, and whose every use is the address of a load or
|
||||
//! store, is a GEP into an array field ([`array_geps`]). The Metal Shading Language leaves an
|
||||
//! 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
|
||||
//! 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.
|
||||
//!
|
||||
//! A buffer table (an `array_ref` of buffers passed as a thread pointer) is read one element
|
||||
//! pointer at a time; each element is its own static root ([`Root::BufferElement`]), bound to its
|
||||
//! own argument-table slot.
|
||||
@@ -593,6 +607,8 @@ pub struct Provenance {
|
||||
pub cells: CellFacts,
|
||||
/// The entry's bindings and layouts, indexed once.
|
||||
pub abi: AbiIndex,
|
||||
/// The GEPs into array fields, sorted, each with its extent ([`array_geps`]).
|
||||
pub array_geps: Vec<(InstId, i64)>,
|
||||
}
|
||||
|
||||
fn invariant(detail: &str) -> Error {
|
||||
@@ -772,6 +788,111 @@ pub(crate) fn gep_displacement(
|
||||
Ok((konst.and_then(|c| i64::try_from(c).ok()), strides))
|
||||
}
|
||||
|
||||
/// The bytes past its array's first element that the run-time indices of `getelementptr src_ty,
|
||||
/// idx..` may add, `Σ (len − 1) · stride`, when its first (pointer) index is a constant and every
|
||||
/// run-time index steps over the elements of an array its own type path names. `None` for any
|
||||
/// other GEP: one with no run-time index, a run-time pointer index (a flattened or byte offset),
|
||||
/// a run-time index over a vector's lanes, or a sum outside `i64`.
|
||||
fn array_extent(e: &Entry, layouts: &TypeLayouts, src_ty: TypeId, idx: &[Value]) -> Option<i64> {
|
||||
let mut cur = src_ty;
|
||||
let mut extent: Option<i64> = None;
|
||||
for (n, &i) in idx.iter().enumerate() {
|
||||
let k = const_index(e, i);
|
||||
if n == 0 {
|
||||
k?;
|
||||
continue;
|
||||
}
|
||||
match e.module.types().get(cur)? {
|
||||
Type::Struct { fields, .. } => cur = *fields.get(usize::try_from(k?).ok()?)?,
|
||||
Type::Array { elem, len } => {
|
||||
cur = *elem;
|
||||
if k.is_none() {
|
||||
let past = i64::try_from(len.checked_sub(1)?).ok()?;
|
||||
let stride = i64::try_from(layouts.alloc_size(*elem)?).ok()?;
|
||||
let add = past.checked_mul(stride)?;
|
||||
extent = Some(extent.unwrap_or(0).checked_add(add)?);
|
||||
}
|
||||
}
|
||||
Type::Vector { elem, .. } => {
|
||||
k?;
|
||||
cur = *elem;
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
extent
|
||||
}
|
||||
|
||||
/// The GEPs into array fields, sorted, each with its extent ([`array_extent`]): a thread-space
|
||||
/// `getelementptr` whose run-time indices each step over an array its own type path names, and
|
||||
/// whose every use is the address of a load or store (no further address arithmetic, merge or
|
||||
/// escape of the element's address). MSL leaves an index past an array undefined, so such an
|
||||
/// access stays inside its array; provenance bounds its offsets there ([`Cx::array_bound`]).
|
||||
fn array_geps(
|
||||
e: &Entry,
|
||||
layouts: &TypeLayouts,
|
||||
body: &Body,
|
||||
m: &mut Meter,
|
||||
) -> Result<Vec<(InstId, i64)>, Error> {
|
||||
let thread = |t: TypeId| {
|
||||
matches!(
|
||||
e.module.types().get(t),
|
||||
Some(Type::Ptr {
|
||||
space: AddrSpace::Thread
|
||||
})
|
||||
)
|
||||
};
|
||||
let mut out: Vec<(InstId, i64)> = Vec::new();
|
||||
for (id, inst) in body.insts.iter() {
|
||||
m.tick(1)?;
|
||||
let InstKind::Gep {
|
||||
src_ty, indices, ..
|
||||
} = &inst.kind
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if !thread(inst.ty) {
|
||||
continue;
|
||||
}
|
||||
if let Some(x) = array_extent(e, layouts, *src_ty, indices) {
|
||||
grow(&mut out, 1, m)?;
|
||||
out.push((id, x));
|
||||
}
|
||||
}
|
||||
if out.is_empty() {
|
||||
return Ok(out);
|
||||
}
|
||||
// `insts` iterates by ascending id: `out` is sorted.
|
||||
let mut dropped: Vec<InstId> = Vec::new();
|
||||
for (_, inst) in body.insts.iter() {
|
||||
m.tick(1)?;
|
||||
let (ptr, val) = match &inst.kind {
|
||||
InstKind::Load { ptr, .. } => (Some(*ptr), None),
|
||||
InstKind::Store { ptr, val, .. } => (Some(*ptr), Some(*val)),
|
||||
_ => (None, None),
|
||||
};
|
||||
// A use of a GEP into an array field other than the address of a load or store.
|
||||
let other = |v: Value| match v {
|
||||
Value::Inst(g) if out.binary_search_by_key(&g, |(x, _)| *x).is_ok() => {
|
||||
(Some(v) != ptr || Some(v) == val).then_some(g)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let mut n = 0usize;
|
||||
inst.kind
|
||||
.for_each_value(|v| n += usize::from(other(v).is_some()));
|
||||
if n > 0 {
|
||||
grow(&mut dropped, n, m)?;
|
||||
inst.kind.for_each_value(|v| dropped.extend(other(v)));
|
||||
}
|
||||
}
|
||||
if !dropped.is_empty() {
|
||||
dropped.sort_unstable();
|
||||
out.retain(|(g, _)| dropped.binary_search(g).is_err());
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The base-2 logarithm of the alignment (at most [`ALIGN_LOG_CAP`]) that the displacement of
|
||||
/// `getelementptr src_ty, _, idx..` is known to have: the least over its terms, a constant index's
|
||||
/// displacement and a dynamic index's stride times the factor of two its index carries
|
||||
@@ -903,6 +1024,8 @@ struct Cx<'a> {
|
||||
cells: Cells,
|
||||
/// The entry's bindings and layouts, indexed once.
|
||||
abi: AbiIndex,
|
||||
/// The GEPs into array fields ([`array_geps`]), with each one's extent, sorted.
|
||||
array_geps: Vec<(InstId, i64)>,
|
||||
}
|
||||
|
||||
impl Cx<'_> {
|
||||
@@ -1396,6 +1519,44 @@ impl<'a> Cx<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The fact of GEP `id` into an array field ([`array_geps`]) over `base` whose roots are
|
||||
/// stack allocations only: its offsets are its constant displacement plus at most the array
|
||||
/// field's bytes past its first element; `f`, the unbounded fact, for any other GEP. Lowering
|
||||
/// clamps each such GEP's run-time indices into the private arena to their arrays
|
||||
/// (`lower::access::local`), and a typed object's access never leaves its levels
|
||||
/// (`memory::alloca`), so no run reaches past the field.
|
||||
fn array_bound(
|
||||
&self,
|
||||
id: InstId,
|
||||
base: PtrFact,
|
||||
f: PtrFact,
|
||||
src_ty: TypeId,
|
||||
idx: &[Value],
|
||||
) -> Result<PtrFact, Error> {
|
||||
let Ok(k) = self.array_geps.binary_search_by_key(&id, |(g, _)| *g) else {
|
||||
return Ok(f);
|
||||
};
|
||||
let extent = self.array_geps.get(k).map_or(0, |(_, x)| *x);
|
||||
let roots = self.sets.get(base.roots);
|
||||
if roots.is_empty() || !roots.iter().all(|r| matches!(r, Root::Alloca(_))) {
|
||||
return Ok(f);
|
||||
}
|
||||
let OffsetRange::Span { lo, hi } = base.range else {
|
||||
return Ok(f);
|
||||
};
|
||||
let (Some(d), _) = gep_displacement(self.e, self.layouts, src_ty, idx)? else {
|
||||
return Ok(f);
|
||||
};
|
||||
let range = match (
|
||||
lo.checked_add(d),
|
||||
hi.checked_add(d).and_then(|h| h.checked_add(extent)),
|
||||
) {
|
||||
(Some(lo), Some(hi)) => OffsetRange::Span { lo, hi },
|
||||
_ => OffsetRange::Top,
|
||||
};
|
||||
Ok(PtrFact { range, ..f })
|
||||
}
|
||||
|
||||
/// The transfer function of pointer-typed instruction `id`.
|
||||
fn transfer(&mut self, id: InstId, inst: &Inst, m: &mut Meter) -> Result<PtrFact, Error> {
|
||||
// An element of a handle-array parameter is one of that parameter's handles: the
|
||||
@@ -1466,7 +1627,8 @@ impl<'a> Cx<'a> {
|
||||
..
|
||||
} => {
|
||||
let b = self.get(*base, m)?;
|
||||
self.gep(b, *src_ty, indices)?
|
||||
let f = self.gep(b, *src_ty, indices)?;
|
||||
self.array_bound(id, b, f, *src_ty, indices)?
|
||||
}
|
||||
InstKind::Phi { incoming, .. } => {
|
||||
let mut f = PtrFact::BOTTOM;
|
||||
@@ -2039,6 +2201,7 @@ pub fn analyze(
|
||||
extracts: Vec::new(),
|
||||
cells: Cells::default(),
|
||||
abi: AbiIndex::of(e, m)?,
|
||||
array_geps: array_geps(e, layouts, body, m)?,
|
||||
};
|
||||
for &t in ¶m_tys {
|
||||
cx.check_ty(t)?;
|
||||
@@ -2419,6 +2582,7 @@ fn classify(
|
||||
dynamic_deref,
|
||||
cells: cx.cells.facts,
|
||||
abi: cx.abi,
|
||||
array_geps: cx.array_geps,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,8 @@
|
||||
//! 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
|
||||
//! slot, or a data access's bytes are not translation-time constants (a GEP into an array field
|
||||
//! reaches its array's bytes, `provenance`'s **Array fields**); 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
|
||||
|
||||
@@ -16,8 +16,9 @@
|
||||
//! What one access of the arena emits, on a generated module: each shape is two kernels, one that
|
||||
//! 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), its
|
||||
//! containment and the add that keeps its value live included. The shapes: a word load and a word
|
||||
//! 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 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 within its bound on every
|
||||
@@ -192,7 +193,7 @@ const SHAPES: &[Shape] = &[
|
||||
)
|
||||
},
|
||||
value: Some(("i32", "add")),
|
||||
bound: 12,
|
||||
bound: 13,
|
||||
},
|
||||
Shape {
|
||||
name: "word store at a run-time index",
|
||||
@@ -203,7 +204,7 @@ const SHAPES: &[Shape] = &[
|
||||
)
|
||||
},
|
||||
value: None,
|
||||
bound: 11,
|
||||
bound: 12,
|
||||
},
|
||||
Shape {
|
||||
name: "float4 load at a run-time index",
|
||||
@@ -214,7 +215,7 @@ const SHAPES: &[Shape] = &[
|
||||
)
|
||||
},
|
||||
value: Some(("<4 x float>", "fadd")),
|
||||
bound: 23,
|
||||
bound: 24,
|
||||
},
|
||||
Shape {
|
||||
name: "word load at a constant index",
|
||||
|
||||
+14
@@ -2942,6 +2942,20 @@ named states the rule in full.
|
||||
0xcc)`, `crates/metal2vulkan-conformance/probes/swizzled_view_stores.m`) and Metal's validation
|
||||
refuses the write, the view reporting no `ShaderWrite` usage.
|
||||
|
||||
**Thread memory**
|
||||
|
||||
- **An index past the end of an array field of a local struct does not reach its neighbouring
|
||||
fields; undefined in MSL.** Where a function indexes an array of its own thread-space variable
|
||||
(a field of a struct, or the whole variable) at an index only the run knows, through a
|
||||
`getelementptr` whose run-time index steps over that array's elements and whose address is
|
||||
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.
|
||||
|
||||
**Queues and command buffers**
|
||||
|
||||
- **The runtime schedules a command buffer once its submit thread is free**
|
||||
|
||||
Reference in new issue
Block a user