Translator: a device-address leaf of whole words is one access

A load or store of a Path::Vec4, Vec2 or Aligned leaf through a device
address (psb::PsbMem: static slots, wide pointers, the vertex fetch's
sources) moved each 32-bit word with its own address arithmetic, place and
access: a float4 at an unproven 16-byte offset was four loads and about 38
instructions. It is now one PhysicalStorageBuffer access per 16 bytes at the
device address of its first word: the leaf's own type where its register
value is its words' bits in 32-bit lanes (float, int and their vectors), else
a u32 or uvecN converted as the words were. The Aligned operand is what the
offset is proven to be (16 or 8 from a slot's own address, else 4), which
VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314 allows for every type the
form uses. The words read and written are the word path's, at the word
holding the leaf's first byte, so Metal's placement holds. A store ending
inside a word keeps the word path. Descriptor views keep theirs. The
device-address uvec2/uvec4 units this replaces are deleted, and the word
below an access is one mask (psb::split_word) instead of a mask and a
subtract.

SEMANTICS_VERSION 2258 -> 2259: device-address modules change.
This commit is contained in:
Anees Iqbal committed 2026-11-21 11:48:03 -08:00
1 parent 550eb670b4
commit d97bf60431
8 files changed
+310 -77

No files matched your search

+1 -1
View File
@@ -61,7 +61,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 = 2258;
pub const SEMANTICS_VERSION: u32 = 2259;
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
+3 -2
View File
@@ -17,8 +17,9 @@
//! every buffer endpoint's call-site `align` at least 16 and its offset proven a multiple of 16
//! (`MemoryPlan::proven_align`: the call-site `align` alone is no proof, `memory::leaf`'s
//! placement), and every other endpoint word-aligned, moves 16-byte `uvec4` units instead: a
//! buffer slot with a `uvec4` view moves each unit as one element of it, and every other memory as
//! its four words. Every unit is one leaf load and store
//! buffer slot with a `uvec4` view moves each unit as one element of it, a device address as one
//! access (`memory::leaf`'s whole words), and every other memory as its four words. Every unit is
//! one leaf load and store
//! (`memory::leaf`), each contained exactly as a plain access of that leaf, so a byte store never
//! disturbs a neighbour (the sub-word atomics); an arena unit at a constant offset that ends inside
//! the arena's translation-time size has no check, which could only pass. `memset` stores the byte
@@ -1196,15 +1196,8 @@ pub(super) fn wide_at(
Some(d) => fb.binary(BinOp::IAdd, b.at, d)?,
None => b.at,
};
let three = k64(fb, 3)?;
let low = fb.binary(BinOp::BitwiseAnd, at, three)?;
let aligned = fb.binary(BinOp::ISub, at, low)?;
let (aligned, low) = super::psb::split_word(fb, at)?;
let base = fb.binary(BinOp::IAdd, b.host, aligned)?;
let u32_ = fb.module().ty(SType::Int {
width: 32,
signed: false,
})?;
let low = fb.convert(ConvOp::UConvert, low, u32_)?;
let (base, low, inb) = match b.bounds {
Some((size, sink)) => {
let inb = contain::in_bounds(fb, at, size, u64::from(leaf.bytes))?;
+38 -8
View File
@@ -245,6 +245,41 @@ impl WordMem for Mem {
Mem::Psb(m) => m.write_unit(fb, bytes, index, value),
}
}
fn whole(&self) -> bool {
match self {
Mem::View(m) => m.whole(),
Mem::Psb(m) => m.whole(),
}
}
fn read_whole(
&mut self,
fb: &mut FunctionBuilder<'_>,
off: SValue,
at: u32,
ty: TypeId,
align: u32,
) -> BResult<SValue> {
match self {
Mem::View(m) => m.read_whole(fb, off, at, ty, align),
Mem::Psb(m) => m.read_whole(fb, off, at, ty, align),
}
}
fn write_whole(
&mut self,
fb: &mut FunctionBuilder<'_>,
off: SValue,
at: u32,
value: SValue,
align: u32,
) -> BResult<()> {
match self {
Mem::View(m) => m.write_whole(fb, off, at, value, align),
Mem::Psb(m) => m.write_whole(fb, off, at, value, align),
}
}
}
/// One candidate slot, ready to access: its word memory, the byte offset in it, and the
@@ -290,8 +325,8 @@ pub(super) fn prepare(
let ops = WordOps::of(crate::lower::sync::memory_model::coherence(cx.plan));
if let Some(SlotLowering::Psb { start }) = lowering {
// Under the identity domains a slot whose address the binder holds to its argument's
// alignment is reached from that address as it is (its biased word), with `uvec2` and
// `uvec4` units at offsets proven multiples of their size; any other from the word below
// alignment is reached from that address as it is (its biased word), a leaf at an offset
// proven a multiple of 8 or 16 accessed at that alignment; any other from the word below
// each access.
let aligned = cx.plan.families.access.aligned.contains(&t.slot);
let a = if cx.plan.families.access.identity {
@@ -312,12 +347,7 @@ pub(super) fn prepare(
};
let units = cx.plan.families.access.psb_units;
let read_only = cx.plan.families.access.unwritten.contains(&t.slot);
let mut mem = a
.mem
.with(ops)
.with_units(units)
.with_vectors(aligned)
.read_only(read_only);
let mut mem = a.mem.with(ops).with_units(units).read_only(read_only);
let checked = cx.plan.families.access.variable.contains(&choice.slot)
&& leaf::known(fb, m)?.is_none();
if checked {
+4 -3
View File
@@ -771,9 +771,10 @@ pub struct AccessPlan {
/// Memory-plan slots reached under the identity domains from their address as it is, which
/// the binder holds to a multiple of their argument's alignment ([`address_alignment`],
/// `RuntimeFact::AddressAlignment`), at least the granularity their accesses reach
/// ([`AccessPlan::reach`]): no access rounds the address (`psb::prepare_direct`), and `uvec2`
/// and `uvec4` units are taken at offsets proven multiples of their size. Every other
/// device-address slot is reached from the word below each access (`psb::prepare_at`).
/// ([`AccessPlan::reach`]): no access rounds the address (`psb::prepare_direct`), and a leaf
/// at an offset proven a multiple of 8 or 16 is accessed at that alignment (`memory::leaf`'s
/// whole words). Every other device-address slot is reached from the word below each access
/// (`psb::prepare_at`).
pub aligned: BTreeSet<usize>,
/// The granularity in bytes at which the static-root accesses of each memory-plan slot reach
/// its memory, the widest over them ([`note_reach`]): an access whose leaf is one narrow unit
+85 -55
View File
@@ -33,6 +33,12 @@
//! `storageBuffer8BitAccess` and `storageBuffer16BitAccess` cover the `PhysicalStorageBuffer`
//! storage class as they cover `StorageBuffer`. A sub-word store there is one narrow store; without
//! the access it stays the two word atomics ([`PsbMem`]'s `write_bits`).
//!
//! **Whole words.** A leaf of whole words is one access of up to 16 bytes at the device address of
//! its first word (`memory::leaf`'s whole words; [`PsbMem`]'s `whole_place`), not one per word:
//! from the word below the access, that word; from a slot's own address, the slot's address plus
//! the offset, which a leaf proven a multiple of 8 or 16 takes as it is, with that `Aligned`
//! operand, and any other rounds down to a multiple of 4.
use super::SLOT_ADDRESS_BIAS;
use crate::device::DeviceProfile;
@@ -96,6 +102,21 @@ fn entry_field(
)
}
/// `x` (a `u64`) rounded down to a multiple of 4, and its remainder (0 to 3) as a `u32`: the word
/// below a byte and the byte's offset in it. A leaf of whole words reads only the word, and the
/// remainder's instructions are then removed as dead.
pub fn split_word(fb: &mut FunctionBuilder<'_>, x: Value) -> BResult<(Value, Value)> {
let mask = k64(fb, !3)?;
let word = fb.binary(BinOp::BitwiseAnd, x, mask)?;
let three = k64(fb, 3)?;
let low = fb.binary(BinOp::BitwiseAnd, x, three)?;
let u32_ = fb.module().ty(Type::Int {
width: 32,
signed: false,
})?;
Ok((word, fb.convert(ConvOp::UConvert, low, u32_)?))
}
/// The 64-bit displacement from the bind offset (a `u64`) that the `u32` offset `m` of a
/// static-root fat pointer stands for, in a slot whose window starts at `start` (module doc:
/// `start + zext((m − start) mod 2³²)`, wrapping).
@@ -139,15 +160,8 @@ pub fn prepare(
let bind = entry_field(fb, table, root, BIND_OFFSET)?;
let disp = displacement(fb, m, start)?;
let off = fb.binary(BinOp::IAdd, bind, disp)?;
let three = k64(fb, 3)?;
let low = fb.binary(BinOp::BitwiseAnd, off, three)?;
let aligned = fb.binary(BinOp::ISub, off, low)?;
let (aligned, low) = split_word(fb, off)?;
let base = fb.binary(BinOp::IAdd, host, aligned)?;
let u32_ = fb.module().ty(Type::Int {
width: 32,
signed: false,
})?;
let low = fb.convert(ConvOp::UConvert, low, u32_)?;
if !contained {
return Ok(PsbAccess {
mem: PsbMem::at(base),
@@ -207,14 +221,7 @@ pub fn prepare_at(fb: &mut FunctionBuilder<'_>, address: Value, m: Value) -> BRe
let u64_ = u64_ty(fb)?;
let disp = fb.convert(ConvOp::SConvert, m, u64_)?;
let at = fb.binary(BinOp::IAdd, address, disp)?;
let three = k64(fb, 3)?;
let low = fb.binary(BinOp::BitwiseAnd, at, three)?;
let base = fb.binary(BinOp::ISub, at, low)?;
let u32_ = fb.module().ty(Type::Int {
width: 32,
signed: false,
})?;
let low = fb.convert(ConvOp::UConvert, low, u32_)?;
let (base, low) = split_word(fb, at)?;
Ok(PsbAccess {
mem: PsbMem {
reach: Reach::Below,
@@ -351,21 +358,6 @@ impl PsbMem {
}
}
/// The same words, also loading and storing 8- and 16-byte units (`uvec2`, `uvec4`) at
/// offsets proven multiples of their size, where `vectors`: a memory whose base the offsets
/// are proven from, under the identity domains, where a slot's device address is its Metal
/// address.
pub fn with_vectors(self, vectors: bool) -> PsbMem {
PsbMem {
units: Units {
vec2: vectors,
vec4: vectors,
..self.units
},
..self
}
}
/// A `PhysicalStorageBuffer` place of the `bytes`-byte unit `index` (a `u32` index of such
/// units, `bytes` 1, 2, 4, 8 or 16) of type `ty`, to load from where `load`.
fn place_of(
@@ -459,22 +451,52 @@ impl PsbMem {
}
}
/// The `PhysicalStorageBuffer` place of the `uvecN` unit `index` (`N = lanes`, 2 or 4), to
/// load from where `load`.
fn vec_ptr(
/// The `PhysicalStorageBuffer` place of a `pointee` (at most 16 bytes) at byte `at` past the
/// word holding byte `off` of the memory, to load from where `load`, and the `Aligned` operand
/// of an access through it: `align` where the memory's base is its Metal address and `off`
/// is proven a multiple of `align`, else 4.
///
/// A memory based at the word below one access ([`Reach::Rounded`], [`Reach::Below`]) holds
/// that access's byte offset in the word, below 4: its first word is the base. A slot's own
/// address ([`Reach::Direct`]) is held to its argument's alignment, so a proven multiple of 8
/// or 16 is the address's own; any other offset is rounded down to a multiple of 4, as the
/// word path reads it.
fn whole_place(
&self,
fb: &mut FunctionBuilder<'_>,
lanes: u32,
index: Value,
load: bool,
) -> BResult<Value> {
let u32_ = fb.module().ty(Type::Int {
width: 32,
signed: false,
})?;
let len = if lanes == 2 { 2 } else { 4 };
let ty = fb.module().ty(Type::Vector { elem: u32_, len })?;
self.place_of(fb, 4 * lanes, index, ty, load)
(off, at): (Value, u32),
pointee: TypeId,
(load, align): (bool, u32),
) -> BResult<(Value, u32)> {
match self.reach {
Reach::Rounded | Reach::Below => {
let addr = match at {
0 => self.base,
at => {
let d = k64(fb, u64::from(at))?;
fb.binary(BinOp::IAdd, self.base, d)?
}
};
Ok((self.place(fb, addr, pointee, load)?, 4))
}
Reach::Direct => {
let (off, align) = match align {
8 | 16 => (off, align),
_ => {
let mask = k32(fb, !3)?;
(u32_op(fb, BinOp::BitwiseAnd, off, mask)?, 4)
}
};
let (x, c) = fb.sum_of(off).unwrap_or((off, 0));
let bias = c.wrapping_add(at).wrapping_add(SLOT_ADDRESS_BIAS as u32);
let bias = k32(fb, bias)?;
let biased = u32_op(fb, BinOp::IAdd, x, bias)?;
let u64_ = u64_ty(fb)?;
let d = fb.convert(ConvOp::UConvert, biased, u64_)?;
let addr = fb.binary(BinOp::IAdd, self.base, d)?;
Ok((self.place(fb, addr, pointee, load)?, align))
}
}
}
/// The `PhysicalStorageBuffer` place of a `pointee` at word `word` (a `u32` index) of the
@@ -530,26 +552,34 @@ impl WordMem for PsbMem {
self.units
}
fn read_vec(
fn whole(&self) -> bool {
true
}
fn read_whole(
&mut self,
fb: &mut FunctionBuilder<'_>,
lanes: u32,
index: Value,
off: Value,
at: u32,
ty: TypeId,
align: u32,
) -> BResult<Value> {
let p = self.vec_ptr(fb, lanes, index, true)?;
let access = self.ops.access(fb, unit_access(4 * lanes), false)?;
let (p, align) = self.whole_place(fb, (off, at), ty, (true, align))?;
let access = self.ops.access(fb, unit_access(align), false)?;
fb.load(p, access)
}
fn write_vec(
fn write_whole(
&mut self,
fb: &mut FunctionBuilder<'_>,
lanes: u32,
index: Value,
off: Value,
at: u32,
value: Value,
align: u32,
) -> BResult<()> {
let p = self.vec_ptr(fb, lanes, index, false)?;
let access = self.ops.access(fb, unit_access(4 * lanes), true)?;
let ty = fb.value_ty(value)?;
let (p, align) = self.whole_place(fb, (off, at), ty, (false, align))?;
let access = self.ops.access(fb, unit_access(align), true)?;
fb.store(p, value, access)
}
@@ -155,6 +155,27 @@ impl WordMem for SourceMem {
Err(BuildError::Dangling(Dangling::Value(value)))
}
fn whole(&self) -> bool {
match &self.0 {
Mem::View(_) => false,
Mem::Psb(m) => m.whole(),
}
}
fn read_whole(
&mut self,
fb: &mut FunctionBuilder<'_>,
off: SValue,
at: u32,
ty: crate::spirv::ids::TypeId,
align: u32,
) -> BResult<SValue> {
match &mut self.0 {
Mem::View(v) => Err(BuildError::Dangling(Dangling::Global(v.global))),
Mem::Psb(m) => m.read_whole(fb, off, at, ty, align),
}
}
fn write_bits(
&mut self,
_: &mut FunctionBuilder<'_>,
+157
View File
@@ -41,6 +41,19 @@
//! Every word read or written holds at least one byte of the leaf, so an in-bounds leaf never
//! touches a word past the padded range (sizes are multiples of 16).
//!
//! **Whole words**. A memory at a device address ([`WordMem::whole`]: a `PhysicalStorageBuffer`
//! place, `lower::access::psb`) moves a [`Path::Vec4`], [`Path::Vec2`] or [`Path::Aligned`] leaf's
//! words in one access per 16 bytes, not one per word: a leaf whose register value is its words'
//! bits in 32-bit lanes (a `float`, an `int`, or a vector of 2 to 4 of them) is loaded and stored
//! as that type, any other as one `u32` or `uvecN` converted as its words are. The access reads or
//! writes exactly the words the word path does, at the word holding the leaf's first byte (the
//! multiple of 4 at or below its offset, the placement below), and its `Aligned` operand is what
//! the offset is proven to be: 16 or 8 for [`Path::Vec4`] and [`Path::Vec2`] where the memory's
//! base is the slot's own address, 4 otherwise. Vulkan asks an `Aligned` operand to be a multiple
//! of the access type's widest scalar (`VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314`),
//! which 4 is for every type the form uses (a 64-bit lane goes through a `uvecN`). A store whose
//! bytes end inside a word keeps the word path, which merges that word.
//!
//! **Size**. Address arithmetic on translation-time constants is folded ([`u32_op`]), so a constant
//! offset costs no instruction; a vector's lanes are converted to or from their words as one vector
//! (one bitcast, one exact narrow-float conversion, one shift placing sub-word lanes), not lane by
@@ -556,6 +569,40 @@ pub trait WordMem {
) -> BResult<()> {
Err(dangling())
}
/// Whether this memory also loads and stores a leaf's whole words directly
/// ([`WordMem::read_whole`]): a device address, where one `PhysicalStorageBuffer` access of a
/// type of up to 16 bytes moves them.
fn whole(&self) -> bool {
false
}
/// Load the value of type `ty` (a `u32`, a `uvecN`, or a type of their bits; at most 16
/// bytes, its scalars at most 4 bytes wide) at byte `at` (a multiple of 16) past the word
/// that holds byte `off`, the first word of a leaf at offset `off` (a multiple of 4 by its
/// alignment: an offset that is not one reads the word below it, the module doc's placement).
/// The leaf's offset is proven a multiple of `align` (4, 8 or 16). Called only where
/// [`WordMem::whole`].
fn read_whole(
&mut self,
_fb: &mut FunctionBuilder<'_>,
_off: Value,
_at: u32,
_ty: TypeId,
_align: u32,
) -> BResult<Value> {
Err(dangling())
}
/// Store `value` (as [`WordMem::read_whole`]'s `ty`) at byte `at` past the word that holds
/// byte `off`. Called only where [`WordMem::whole`].
fn write_whole(
&mut self,
_fb: &mut FunctionBuilder<'_>,
_off: Value,
_at: u32,
_value: Value,
_align: u32,
) -> BResult<()> {
Err(dangling())
}
}
/// A `u32` constant.
@@ -1222,6 +1269,105 @@ fn funnel_up(fb: &mut FunctionBuilder<'_>, prev: Value, cur: Value, sh: Value) -
or(fb, x, y)
}
/// The `Aligned` operand of a whole leaf's access: the multiple of 16 or 8 its offset is proven to
/// be ([`Path::Vec4`], [`Path::Vec2`]), else 4.
fn whole_align(leaf: &Leaf) -> u32 {
match leaf.path {
Path::Vec4 => 16,
Path::Vec2 => 8,
_ => 4,
}
}
/// Whether `leaf`'s register value is its words' own bits in a type of 32-bit lanes (a `float`,
/// a `u32` of an `i32`, or a vector of 2 to 4 of them, filling its bytes): its whole access loads
/// and stores that type, with no conversion.
fn native32(leaf: &Leaf) -> bool {
vec_bits(leaf) && leaf.form.bits() == 32
}
/// The pieces of `words` words a whole leaf moves in: up to four words each, at byte `16 · k`
/// of piece `k`.
fn pieces(words: u32) -> impl Iterator<Item = (u32, u32)> {
(0..words.div_ceil(4)).map(move |k| (16 * k, (words - 4 * k).min(4)))
}
/// The type of a piece of `n` words (1 to 4): `u32` or `uvecN`.
fn piece_ty(fb: &mut FunctionBuilder<'_>, n: u32) -> BResult<TypeId> {
match n {
1 => u32_ty(fb),
n => uvec_ty(fb, u8::try_from(n).map_err(|_| dangling())?),
}
}
/// Load the leaf of whole words at `off` of a memory that moves them directly
/// ([`WordMem::whole`]): one access of its register type where that is its words' bits in 32-bit
/// lanes ([`native32`]), else one `u32` or `uvecN` per 16 bytes, converted.
fn load_whole(
fb: &mut FunctionBuilder<'_>,
mem: &mut dyn WordMem,
leaf: &Leaf,
off: Value,
) -> BResult<Value> {
let align = whole_align(leaf);
if native32(leaf) {
let ty = ops::ty(fb, leaf.form)?;
return mem.read_whole(fb, off, 0, ty, align);
}
let mut words = Vec::with_capacity(leaf.words() as usize);
for (at, n) in pieces(leaf.words()) {
let ty = piece_ty(fb, n)?;
let v = mem.read_whole(fb, off, at, ty, align)?;
if n == leaf.words() && vec_bits(leaf) {
let ty = ops::ty(fb, leaf.form)?;
return as_bits(fb, v, ty);
}
match n {
1 => words.push(v),
_ => {
for i in 0..n {
words.push(fb.composite_extract(v, &[i])?);
}
}
}
}
value_of(fb, leaf, &words)
}
/// Store `v` as the leaf of whole words (its bytes a multiple of 4) at `off` of a memory that moves
/// them directly ([`WordMem::whole`]), as [`load_whole`] loads it.
fn store_whole(
fb: &mut FunctionBuilder<'_>,
mem: &mut dyn WordMem,
leaf: &Leaf,
off: Value,
v: Value,
) -> BResult<()> {
let align = whole_align(leaf);
if native32(leaf) {
return mem.write_whole(fb, off, 0, v, align);
}
if leaf.words() <= 4 && vec_bits(leaf) {
let ty = piece_ty(fb, leaf.words())?;
let x = as_bits(fb, v, ty)?;
return mem.write_whole(fb, off, 0, x, align);
}
let words = words_of(fb, leaf, v)?;
for (at, n) in pieces(leaf.words()) {
let first = (at / 4) as usize;
let part = words.get(first..first + n as usize).ok_or_else(dangling)?;
let x = match part {
[w] => *w,
_ => {
let ty = piece_ty(fb, n)?;
fb.composite_construct(ty, part)?
}
};
mem.write_whole(fb, off, at, x, align)?;
}
Ok(())
}
/// Load `leaf` at byte offset `off` (a `u32`) of `mem`; the register value.
pub fn load(
fb: &mut FunctionBuilder<'_>,
@@ -1229,6 +1375,9 @@ pub fn load(
leaf: &Leaf,
off: Value,
) -> BResult<Value> {
if mem.whole() && matches!(leaf.path, Path::Vec4 | Path::Vec2 | Path::Aligned) {
return load_whole(fb, mem, leaf, off);
}
let words = match leaf.path {
// The leaf's one `uvec4` unit at `off / 16`, or `uvec2` unit at `off / 8`.
Path::Vec4 | Path::Vec2 if mem.units().has(leaf.bytes) => {
@@ -1302,6 +1451,14 @@ pub fn store(
off: Value,
v: Value,
) -> BResult<bool> {
// Whole words moved directly: no partial word to merge.
if mem.whole()
&& matches!(leaf.path, Path::Vec4 | Path::Vec2 | Path::Aligned)
&& leaf.bytes % 4 == 0
{
store_whole(fb, mem, leaf, off, v)?;
return Ok(false);
}
// The leaf's one `uvec4` unit at `off / 16`, or `uvec2` unit at `off / 8`.
if matches!(leaf.path, Path::Vec4 | Path::Vec2) && mem.units().has(leaf.bytes) {
let x = vec_of(fb, leaf, v)?;