From e2e76d8879a63c2a0055c772fd99fbc6bf891635 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 01:58:07 -0800 Subject: [PATCH] Translator: a slot's direct base and address are made once where they dominate; an access adds its carry alone PR 22's carry form cost about 10 SPIR-V instructions for each direct access against 2 before, and engine_access_size::a_device_access_of_whole_words_is_one_access failed its bounds at 2f5f35ca47 (m3-max-77753, first identity profile: the uint-offset float4 load and store 24 against a bound of 16, the float4-offset load 23 against 16, the pointer chase 33 against 24; the old form 16, 16, 15 and 24). Most of that was work repeated on every access: the base's u64 bitcast back to words and its split, and an add of the offset's zero high word; and slot_address added the bias with the same sequence at every use. Now: - pointer::direct_base: the slot's address less the bias as its two words (the SlotGuestBase push word split, or in a replay variant that word less 2^31 with a borrow), remembered under SlotMemo::DirectBase and recalled where its block dominates (EmitCx's invariant memo, as slot_bounds does). Reach::Direct holds the words; biased_slot_address, which only the direct reach called, goes. - psb::add_slot_offset: OpIAddCarry of the low word and the offset, its two words, one add of the carry to the high word, the words joined and bitcast: six instructions, with no add of a zero word and no 64-bit add of a constant. - pointer::slot_address: remembered under SlotMemo::Address, so a pointer compare, a converted or stored pointer or an access from the word below adds the bias once where it dominates. The arithmetic is unchanged: the address plus the unsigned biased offset modulo 2^64, carried into the high word as PR 22 made it. SEMANTICS_VERSION 2309 -> 2310 (module bytes change). Verified: nothing run yet for this commit; the access-size table, the engine suites and the merge check run next, and the table is reported against the bounds. --- crates/metal2vulkan/src/cache/mod.rs | 2 +- crates/metal2vulkan/src/lower/access/emit.rs | 2 +- .../metal2vulkan/src/lower/access/pointer.rs | 67 ++++++++++++++----- crates/metal2vulkan/src/lower/access/psb.rs | 54 +++++++-------- crates/metal2vulkan/src/lower/emit.rs | 23 +++++++ 5 files changed, 100 insertions(+), 48 deletions(-) diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 91c8efb77..6d7815bce 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 = 2309; +pub const SEMANTICS_VERSION: u32 = 2310; /// 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/emit.rs b/crates/metal2vulkan/src/lower/access/emit.rs index 16c49e4bf..c6c9e9a9b 100644 --- a/crates/metal2vulkan/src/lower/access/emit.rs +++ b/crates/metal2vulkan/src/lower/access/emit.rs @@ -346,7 +346,7 @@ fn prepare_access( let a = if cx.plan.families.access.identity { match aligned { Some(held) => { - let base = super::pointer::biased_slot_address(cx, fb, choice.slot)?; + let base = super::pointer::direct_base(cx, fb, choice.slot)?; psb::prepare_direct(base, m, held) } None => { diff --git a/crates/metal2vulkan/src/lower/access/pointer.rs b/crates/metal2vulkan/src/lower/access/pointer.rs index 3dd97b0aa..93251304d 100644 --- a/crates/metal2vulkan/src/lower/access/pointer.rs +++ b/crates/metal2vulkan/src/lower/access/pointer.rs @@ -61,7 +61,7 @@ use crate::device::profile::BufferAccess; use crate::error::{Error, Feature, Unsupported}; use crate::lower::arith::wide::{self, Pair}; use crate::lower::arith::{Form, Held}; -use crate::lower::emit::EmitCx; +use crate::lower::emit::{EmitCx, SlotMemo}; use crate::lower::select::SelectCx; use crate::memory::leaf::{k32, Leaf, Path}; use crate::memory::provenance::{HandleClass, OffsetRange, Repr}; @@ -786,35 +786,66 @@ fn slot_word(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, slot: SlotRef) - /// Buffer slot `slot`'s Metal address, the bound buffer's `gpuAddress` plus the bind offset, as a /// `u64` (needs `shaderInt64`): its `SlotGuestBase` word, plus -/// [`SLOT_ADDRESS_BIAS`](super::SLOT_ADDRESS_BIAS) where the word is biased ([`biased`]). What a -/// pointer compare, a converted or stored pointer, and an access from the word below it read. +/// [`SLOT_ADDRESS_BIAS`](super::SLOT_ADDRESS_BIAS) where the word is biased ([`biased`]), added to +/// the low word with the carry into the high word (`psb::add_slot_offset`). Made once where it +/// dominates. What a pointer compare, a converted or stored pointer, and an access from the word +/// below it read. pub(super) fn slot_address( cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, slot: SlotRef, ) -> BResult { - let word = slot_word(cx, fb, slot)?; - if !biased(cx) { - return Ok(word); + let key = SlotMemo::Address(slot); + if let Some(address) = cx.recall_slot(fb, key) { + return Ok(address); } - let bias = k32(fb, super::SLOT_ADDRESS_BIAS as u32)?; - psb::add_slot_offset(fb, word, bias) + let address = match biased(cx) { + false => slot_word(cx, fb, slot)?, + true => { + let base = direct_base(cx, fb, slot)?; + let bias = k32(fb, super::SLOT_ADDRESS_BIAS as u32)?; + psb::add_slot_offset(fb, base, bias)? + } + }; + cx.remember_slot(fb, key, address); + Ok(address) } -/// Buffer slot `slot`'s Metal address less [`SLOT_ADDRESS_BIAS`](super::SLOT_ADDRESS_BIAS), a -/// `u64`: its `SlotGuestBase` word where the word is biased ([`biased`]), the base an access -/// reached from the slot's own address adds its biased offset to (`psb::prepare_direct`). -pub(super) fn biased_slot_address( +/// Buffer slot `slot`'s Metal address less [`SLOT_ADDRESS_BIAS`](super::SLOT_ADDRESS_BIAS), as its +/// low and high words: its `SlotGuestBase` word where the word is biased ([`biased`]), else that +/// word less the bias, borrowing from the high word. The base an access reached from the slot's +/// own address adds its biased offset to (`psb::prepare_direct`), made once where it dominates, +/// so each access adds its offset to the low word and the carry to the high word, and no more. +pub(super) fn direct_base( cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, slot: SlotRef, -) -> BResult { - let word = slot_word(cx, fb, slot)?; - if biased(cx) { - return Ok(word); +) -> BResult { + let (lo, hi) = ( + SlotMemo::DirectBase(slot, false), + SlotMemo::DirectBase(slot, true), + ); + if let (Some(lo), Some(hi)) = (cx.recall_slot(fb, lo), cx.recall_slot(fb, hi)) { + return Ok(Pair { lo, hi }); } - let bias = k64(fb, super::SLOT_ADDRESS_BIAS)?; - fb.binary(BinOp::ISub, word, bias) + let v = cx.push_field(fb, PushField::SlotGuestBase(slot))?; + let word = wide::split(fb, v)?; + let base = match biased(cx) { + true => word, + false => { + let bias = k32(fb, super::SLOT_ADDRESS_BIAS as u32)?; + let less = fb.binary(BinOp::ISubBorrow, word.lo, bias)?; + let low = fb.composite_extract(less, &[0])?; + let borrow = fb.composite_extract(less, &[1])?; + Pair { + lo: low, + hi: fb.binary(BinOp::ISub, word.hi, borrow)?, + } + } + }; + cx.remember_slot(fb, lo, base.lo); + cx.remember_slot(fb, hi, base.hi); + Ok(base) } /// The start of the 2³² bytes a pointer into plan slot `slot` whose offset range the analysis diff --git a/crates/metal2vulkan/src/lower/access/psb.rs b/crates/metal2vulkan/src/lower/access/psb.rs index f7630f499..5a951c9c3 100644 --- a/crates/metal2vulkan/src/lower/access/psb.rs +++ b/crates/metal2vulkan/src/lower/access/psb.rs @@ -44,7 +44,7 @@ use super::SLOT_ADDRESS_BIAS; use crate::device::DeviceProfile; -use crate::lower::arith::{ops, wide}; +use crate::lower::arith::wide; use crate::memory::leaf::{k32, u32_op, Units, WordMem, WordOps}; use crate::spirv::builder::FunctionBuilder; use crate::spirv::error::BResult; @@ -70,26 +70,21 @@ fn u64_ty(fb: &mut FunctionBuilder<'_>) -> BResult { }) } -/// Add an unsigned byte offset to a device address, carrying out of the low word. -/// A biased offset often has bit 31 set, including at the first byte of a binding. +/// A device address as a `u64`: `base` (its low and high words) plus the `u32` `offset` read as +/// unsigned, added to the low word with the carry into the high word, so no 64-bit add takes a +/// constant. One `OpIAddCarry`, its two words, one add of the carry, and the words joined. pub(super) fn add_slot_offset( fb: &mut FunctionBuilder<'_>, - base: Value, + base: wide::Pair, offset: Value, ) -> BResult { - let form = wide::wide_form(64, 1); - let words = ops::ty(fb, form)?; - let base = fb.convert(ConvOp::Bitcast, base, words)?; - let base = wide::split(fb, base)?; - let zero = k32(fb, 0)?; - let offset = wide::Pair { - lo: offset, - hi: zero, - }; - let sum = wide::add(fb, base, offset)?; - let sum = wide::join(fb, form, sum)?; + let sum = fb.binary(BinOp::IAddCarry, base.lo, offset)?; + let lo = fb.composite_extract(sum, &[0])?; + let carry = fb.composite_extract(sum, &[1])?; + let hi = fb.binary(BinOp::IAdd, base.hi, carry)?; + let words = wide::join(fb, wide::wide_form(64, 1), wide::Pair { lo, hi })?; let address = u64_ty(fb)?; - fb.convert(ConvOp::Bitcast, sum, address) + fb.convert(ConvOp::Bitcast, words, address) } /// A `u64` constant. @@ -228,10 +223,11 @@ enum Reach { /// read as signed is in `[-2^31, 2^31)`, the biased one is that offset plus 2^31 exactly, and /// the sum is the address plus the offset. A unit index `x + c` with a constant `c` folds /// `c`'s bytes into the bias: `(x + c) * s + 2^31` is `x * s + (c * s + 2^31)` modulo 2^32. - /// The low address word takes that unsigned offset and the high word takes its carry - /// ([`add_slot_offset`]). The address holds the binder's alignment - /// (`RuntimeFact::AddressAlignment`). - Direct(u32), + /// The base's low word takes that unsigned offset and its high word the carry + /// ([`add_slot_offset`]); the base's words are made once where they dominate + /// (`pointer::direct_base`). The address holds the binder's alignment + /// (`RuntimeFact::AddressAlignment`), the first field. + Direct(u32, wide::Pair), } /// Prepare an access of a static slot at the `u32` displacement `m` from its bind offset, read as @@ -258,18 +254,18 @@ pub fn prepare_at(fb: &mut FunctionBuilder<'_>, address: Value, m: Value) -> BRe } /// Prepare an access of a static slot at the `u32` displacement `m` from its bind offset, under -/// the identity domains, from `base` (a `u64`), the slot's own device address less +/// the identity domains, from `base` (its low and high words), the slot's own device address less /// [`SLOT_ADDRESS_BIAS`], an address the binder holds to a multiple of `held` /// (`RuntimeFact::AddressAlignment`): the access is the byte offset `m`, read as signed, from that /// address, as a descriptor view's is `m` from its base ([`Reach::Direct`]). Every byte of a /// buffer lies within 2^31 bytes of each bind address in it, the runtime making no buffer longer /// than 2 GiB (`metal2vulkan_runtime::MAX_BUFFER_LENGTH`), so the offset is exact whatever the slot's /// window. Nothing is rounded. The check, where the access takes one, is `emit::prepare`'s. -pub fn prepare_direct(base: Value, m: Value, held: u32) -> PsbAccess { +pub fn prepare_direct(base: wide::Pair, m: Value, held: u32) -> PsbAccess { PsbAccess { mem: PsbMem { - reach: Reach::Direct(held), - ..PsbMem::at(base) + reach: Reach::Direct(held, base), + ..PsbMem::at(base.lo) }, off: m, inb: None, @@ -322,6 +318,8 @@ pub fn narrow_units(p: &DeviceProfile) -> Units { /// ([`PsbMem::read_only`]). #[derive(Clone, Copy, Debug)] pub struct PsbMem { + /// The `u64` address a [`Reach::Rounded`] or [`Reach::Below`] memory adds its offsets to; + /// unread under [`Reach::Direct`], whose base is its own two words. base: Value, ops: WordOps, units: Units, @@ -415,7 +413,7 @@ impl PsbMem { let addr = fb.binary(BinOp::IAdd, self.base, d)?; self.place(fb, addr, ty, load) } - Reach::Direct(_) => { + Reach::Direct(_, base) => { let shift = bytes.trailing_zeros(); let (x, c) = fb.sum_of(index).unwrap_or((index, 0)); let s = k32(fb, shift)?; @@ -423,7 +421,7 @@ impl PsbMem { let bias = (c << shift).wrapping_add(SLOT_ADDRESS_BIAS as u32); let bias = k32(fb, bias)?; let at = u32_op(fb, BinOp::IAdd, at, bias)?; - let addr = add_slot_offset(fb, self.base, at)?; + let addr = add_slot_offset(fb, base, at)?; self.place(fb, addr, ty, load) } } @@ -503,7 +501,7 @@ impl PsbMem { }; Ok((self.place(fb, addr, pointee, load)?, 4)) } - Reach::Direct(held) => { + Reach::Direct(held, base) => { let (off, align) = match align { 8 | 16 => (off, align.min(held)), _ => { @@ -515,7 +513,7 @@ impl PsbMem { 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 addr = add_slot_offset(fb, self.base, biased)?; + let addr = add_slot_offset(fb, base, biased)?; Ok((self.place(fb, addr, pointee, load)?, align)) } } diff --git a/crates/metal2vulkan/src/lower/emit.rs b/crates/metal2vulkan/src/lower/emit.rs index a526d00f7..c3e7e46b6 100644 --- a/crates/metal2vulkan/src/lower/emit.rs +++ b/crates/metal2vulkan/src/lower/emit.rs @@ -259,6 +259,19 @@ enum Invariant { Limit(SlotRef, u32), /// A 16-byte line of the function's parameter block ([`EmitCx::block_line`]). BlockLine(u32), + /// A value of a buffer slot's address ([`SlotMemo`]). + Slot(SlotMemo), +} + +/// A value of a buffer slot's address under the identity domains that a block makes once +/// (`access::pointer`): recalled where the block that made it dominates. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum SlotMemo { + /// The slot's address less `SLOT_ADDRESS_BIAS`, the base a direct access adds its biased + /// offset to (`access::pointer::direct_base`): its low (`false`) or high (`true`) word. + DirectBase(SlotRef, bool), + /// The slot's Metal address, a `u64` (`access::pointer::slot_address`). + Address(SlotRef), } impl<'a> EmitCx<'a> { @@ -619,6 +632,16 @@ impl<'a> EmitCx<'a> { Ok((b, l)) } + /// The value of `key` where the block that made it dominates the open block. + pub(crate) fn recall_slot(&self, fb: &FunctionBuilder<'_>, key: SlotMemo) -> Option { + self.recall(fb, Invariant::Slot(key)) + } + + /// Record `v`, made in the open block, as the value of `key` there. + pub(crate) fn remember_slot(&mut self, fb: &FunctionBuilder<'_>, key: SlotMemo, v: SValue) { + self.remember(fb, Invariant::Slot(key), v); + } + /// Checked slot `slot`'s buffer bytes before the bind offset and its limit for `n`-byte /// accesses (`psb::limit`), where they dominate the open block: the pair an access checks /// with (`psb::in_buffer`). The limit is made once per slot and size, in the function's first