diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index d5cbd7424..91c8efb77 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 = 2308; +pub const SEMANTICS_VERSION: u32 = 2309; /// 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/pointer.rs b/crates/metal2vulkan/src/lower/access/pointer.rs index c2cf85d17..3dd97b0aa 100644 --- a/crates/metal2vulkan/src/lower/access/pointer.rs +++ b/crates/metal2vulkan/src/lower/access/pointer.rs @@ -797,8 +797,8 @@ pub(super) fn slot_address( if !biased(cx) { return Ok(word); } - let bias = k64(fb, super::SLOT_ADDRESS_BIAS)?; - fb.binary(BinOp::IAdd, word, bias) + let bias = k32(fb, super::SLOT_ADDRESS_BIAS as u32)?; + psb::add_slot_offset(fb, word, bias) } /// Buffer slot `slot`'s Metal address less [`SLOT_ADDRESS_BIAS`](super::SLOT_ADDRESS_BIAS), a diff --git a/crates/metal2vulkan/src/lower/access/psb.rs b/crates/metal2vulkan/src/lower/access/psb.rs index 41927edb8..f7630f499 100644 --- a/crates/metal2vulkan/src/lower/access/psb.rs +++ b/crates/metal2vulkan/src/lower/access/psb.rs @@ -44,6 +44,7 @@ use super::SLOT_ADDRESS_BIAS; use crate::device::DeviceProfile; +use crate::lower::arith::{ops, wide}; use crate::memory::leaf::{k32, u32_op, Units, WordMem, WordOps}; use crate::spirv::builder::FunctionBuilder; use crate::spirv::error::BResult; @@ -69,6 +70,28 @@ 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. +pub(super) fn add_slot_offset( + fb: &mut FunctionBuilder<'_>, + base: Value, + 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 address = u64_ty(fb)?; + fb.convert(ConvOp::Bitcast, sum, address) +} + /// A `u64` constant. pub fn k64(fb: &mut FunctionBuilder<'_>, v: u64) -> BResult { let ty = u64_ty(fb)?; @@ -198,17 +221,16 @@ enum Reach { Below, /// From a slot's own device address under the identity domains ([`prepare_direct`]), held /// as that address less [`SLOT_ADDRESS_BIAS`] (2^31): each unit at that base plus its 32-bit - /// byte offset plus 2^31, modulo 2^32, zero-extended, in 64-bit integers, then one + /// byte offset plus 2^31, modulo 2^32, treated as unsigned, then one /// `OpConvertUToPtr` to the unit's block (`NonWritable` for a load of an unwritten slot). /// 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 /// 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 bias is one 32-bit add, which a device fuses with the index's shift; the offset is - /// zero-extended, which a device splits into a scalar base and a vector offset; no shift of - /// the offset in 64 bits and no pointer bitcast. It holds the alignment the binder holds the - /// slot's address to (`RuntimeFact::AddressAlignment`). + /// 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), } @@ -401,9 +423,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 u64_ = u64_ty(fb)?; - let d = fb.convert(ConvOp::UConvert, at, u64_)?; - let addr = fb.binary(BinOp::IAdd, self.base, d)?; + let addr = add_slot_offset(fb, self.base, at)?; self.place(fb, addr, ty, load) } } @@ -495,9 +515,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 u64_ = u64_ty(fb)?; - let d = fb.convert(ConvOp::UConvert, biased, u64_)?; - let addr = fb.binary(BinOp::IAdd, self.base, d)?; + let addr = add_slot_offset(fb, self.base, biased)?; Ok((self.place(fb, addr, pointee, load)?, align)) } } diff --git a/crates/metal2vulkan/src/tests/engine_identity.rs b/crates/metal2vulkan/src/tests/engine_identity.rs index af70dfd31..14e2bcef2 100644 --- a/crates/metal2vulkan/src/tests/engine_identity.rs +++ b/crates/metal2vulkan/src/tests/engine_identity.rs @@ -396,6 +396,9 @@ fn a_slot_pointer_used_as_a_value_is_its_metal_address() { let p = profile(stem, identity()).unwrap_or_else(|e| panic!("{e}")); let (a, plan, module) = compile_kept(&bc, name, &p).unwrap_or_else(|e| panic!("{stem}: {e}")); + // The first byte of a slot has biased offset 0x80000000. A native 64-bit + // immediate add can read 4 GiB below the buffer on the Windows Radeon driver. + assert!(super::engine_handles::count(&a, crate::spv::Op::IAddCarry as u16) > 0); assert!(a.interface.descriptors.is_empty(), "{stem}"); link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{stem}: L3: {e}")); let mut push = vec![0u8; plan.push.push_range.map_or(0, |(_, end)| end as usize)];