From 0f854aef987eeab0cfadff29d98747a9df554013 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 17 Nov 2026 14:44:57 -0800 Subject: [PATCH] translator: the direct form adds the sign-extended offset in 64-bit integers, with no pointer bitcast A bounded slot's access was a byte array's element at its 32-bit offset, the element pointer bitcast to a pointer to the unit's one-member Block. The UHD 630's Windows driver (Vulkan 1.3.215) ends the process in that shape (exit 139, every kind), and passes the integer address with OpConvertUToPtr to the same Block. Each access is now the slot's address plus its offset sign-extended (OpSConvert), then one OpConvertUToPtr: the integer form of the access chain's signed index, which RADV lowers alike (iadd of an i2i64), with no zero-extended offset for GFX11 to split into a scalar base and a vector offset. The window is at most 2 GiB (direct_window, tested at 2^31 and one byte past), so the sign extension is exact. The byte array and its element place (device_byte_place, device_element) have no other user and are deleted, and the identity domains no longer need storageBuffer8BitAccess. --- crates/metal2vulkan/src/device/derive.rs | 12 +-- .../metal2vulkan/src/device/tests/profile.rs | 14 ++- crates/metal2vulkan/src/lower/access/mod.rs | 16 ++-- crates/metal2vulkan/src/lower/access/psb.rs | 21 +++-- .../src/spirv/builder/ops/memory.rs | 88 ------------------- .../metal2vulkan/src/tests/engine_identity.rs | 7 -- .../src/tests/engine_offsets_past_2gib.rs | 29 +++++- docs/API.md | 18 ++-- 8 files changed, 68 insertions(+), 137 deletions(-) diff --git a/crates/metal2vulkan/src/device/derive.rs b/crates/metal2vulkan/src/device/derive.rs index c6efee7bb..669ad71f3 100644 --- a/crates/metal2vulkan/src/device/derive.rs +++ b/crates/metal2vulkan/src/device/derive.rs @@ -209,10 +209,8 @@ impl Device { } } -/// The identity domains come as a pair, and need device addresses and 64-bit integers, as a -/// pointer is a 64-bit device address, and 8-bit storage, as a buffer slot whose address the -/// binder holds to its argument's alignment is reached through a byte array at that address -/// (`lower::access::psb::prepare_direct`). +/// The identity domains come as a pair, and need device addresses and 64-bit integers: a +/// pointer is a 64-bit device address. fn identity_domains(desc: &DeviceDescription, policy: &DevicePolicy) -> Result<(), Error> { let pointers = policy.pointer_domain == PointerDomain::Identity; let handles = policy.handle_domain == HandleDomain::Identity; @@ -223,11 +221,7 @@ fn identity_domains(desc: &DeviceDescription, policy: &DevicePolicy) -> Result<( }, )); } - let needs = [ - VkFeature::BufferDeviceAddress, - VkFeature::ShaderInt64, - VkFeature::StorageBuffer8BitAccess, - ]; + let needs = [VkFeature::BufferDeviceAddress, VkFeature::ShaderInt64]; if pointers && !needs.iter().all(|&f| desc.has(f)) { let all = needs.iter().map(|&f| Capability::Feature(f)).collect(); return Err(Unsupported::needs_any_of(Feature::IdentityDomains, vec![all]).into()); diff --git a/crates/metal2vulkan/src/device/tests/profile.rs b/crates/metal2vulkan/src/device/tests/profile.rs index e04084f92..2ad7c89ee 100644 --- a/crates/metal2vulkan/src/device/tests/profile.rs +++ b/crates/metal2vulkan/src/device/tests/profile.rs @@ -310,8 +310,8 @@ fn below_1_2_and_contradictory_policies_are_refused() { } /// The identity domains come as a pair (`InvalidDescriptor` for one alone) and need -/// `bufferDeviceAddress`, `shaderInt64` and `storageBuffer8BitAccess` -/// (`Unsupported{IdentityDomains}` naming the three, on a device without `shaderInt64`). What they lower is `tests::engine_identity`'s. +/// `bufferDeviceAddress` and `shaderInt64` (`Unsupported{IdentityDomains}` naming both, on a +/// device without `shaderInt64`). What they lower is `tests::engine_identity`'s. #[test] fn the_identity_domains_come_as_a_pair_on_a_device_with_addresses() { let identity = DevicePolicy { @@ -343,13 +343,9 @@ fn the_identity_domains_come_as_a_pair_on_a_device_with_addresses() { match Device::new(profile("roadmap-2022"), identity) { Err(Error::Unsupported(u)) => { assert_eq!(u.feature, Feature::IdentityDomains); - let all = [ - VkFeature::BufferDeviceAddress, - VkFeature::ShaderInt64, - VkFeature::StorageBuffer8BitAccess, - ]; - let all = all.map(Capability::Feature).to_vec(); - assert_eq!(u.needs, Needs::AnyOf(vec![all])); + let both = [VkFeature::BufferDeviceAddress, VkFeature::ShaderInt64]; + let both = both.map(Capability::Feature).to_vec(); + assert_eq!(u.needs, Needs::AnyOf(vec![both])); } other => panic!("{other:?}"), } diff --git a/crates/metal2vulkan/src/lower/access/mod.rs b/crates/metal2vulkan/src/lower/access/mod.rs index 61b27d977..13c022791 100644 --- a/crates/metal2vulkan/src/lower/access/mod.rs +++ b/crates/metal2vulkan/src/lower/access/mod.rs @@ -1630,9 +1630,7 @@ fn place_slots(cx: &mut SelectCx<'_>, m: &mut Meter) -> Result= 4 && start % 16 == 0 && bounded { + if align >= 4 && start % 16 == 0 && direct_window(*window) { cx.families.access.aligned.insert(i); } } @@ -1699,12 +1697,18 @@ fn place_slots(cx: &mut SelectCx<'_>, m: &mut Meter) -> Result bool { + matches!(window, Window::Bounded { len, .. } if len <= DIRECT_WINDOW) +} + /// The alignment (2, 4, 8 or 16) the binder holds a device-address slot's address to under the /// identity domains: its argument's `air.arg_type_align_size`, at most 16. Metal's API validation /// refuses a buffer bound at an offset that is not a multiple of its argument type's alignment diff --git a/crates/metal2vulkan/src/lower/access/psb.rs b/crates/metal2vulkan/src/lower/access/psb.rs index 119a1baaf..070513ba3 100644 --- a/crates/metal2vulkan/src/lower/access/psb.rs +++ b/crates/metal2vulkan/src/lower/access/psb.rs @@ -171,11 +171,13 @@ enum Reach { /// unit at that address plus its index times its size, in 64 bits. Rounded, /// From a slot's own device address under the identity domains ([`prepare_direct`]): each - /// unit at its byte offset in a byte array at that address, that element's pointer bitcast to - /// the unit's block ([`FunctionBuilder::device_byte_place`]). The device forms the address - /// from the 32-bit byte offset itself, with no shift (an array of the units would scale its - /// index in 64 bits), and a load of an unwritten slot keeps the block's `NonWritable`. - Bytes, + /// unit at that address plus its 32-bit byte offset sign-extended, in 64-bit integers, then + /// one `OpConvertUToPtr` to the unit's block (`NonWritable` for a load of an unwritten slot). + /// The offset is below 2^31, the window being at most `DIRECT_WINDOW`, so the sign extension + /// is exact. It is the integer form of an access chain's signed index, which a device adds as + /// it would that index: no shift of the offset in 64 bits, no zero-extended offset to split + /// into a scalar base and a vector one, and no pointer bitcast. + Direct, } /// Prepare an access of a static slot whose window starts at `start` at the `u32` displacement @@ -233,7 +235,7 @@ pub fn prepare_direct( }; Ok(PsbAccess { mem: PsbMem { - reach: Reach::Bytes, + reach: Reach::Direct, ..PsbMem::at(base) }, off, @@ -342,10 +344,13 @@ impl PsbMem { let addr = fb.binary(BinOp::IAdd, self.base, d)?; self.place(fb, addr, ty, load) } - Reach::Bytes => { + Reach::Direct => { let shift = crate::memory::leaf::k32(fb, bytes.trailing_zeros())?; let at = crate::memory::leaf::u32_op(fb, BinOp::ShiftLeftLogical, index, shift)?; - fb.device_byte_place(self.base, at, ty, load && self.read_only) + let u64_ = u64_ty(fb)?; + let d = fb.convert(ConvOp::SConvert, at, u64_)?; + let addr = fb.binary(BinOp::IAdd, self.base, d)?; + self.place(fb, addr, ty, load) } } } diff --git a/crates/metal2vulkan/src/spirv/builder/ops/memory.rs b/crates/metal2vulkan/src/spirv/builder/ops/memory.rs index df0379b6c..a78c68f33 100644 --- a/crates/metal2vulkan/src/spirv/builder/ops/memory.rs +++ b/crates/metal2vulkan/src/spirv/builder/ops/memory.rs @@ -134,94 +134,6 @@ impl FunctionBuilder<'_> { self.place_at(addr, pointee, true) } - /// Element `index` (a 32-bit integer scalar) of the array of `elem` that starts at device - /// address `base` (a 64-bit integer scalar, aligned to `elem`'s stride `stride`), as a - /// `PhysicalStorageBuffer` place: `base` converted to a pointer to the one-member - /// `Block { elem[] }` (`ArrayStride` `stride`), and the access chain to that member's element. - /// The element's address is `base + index ยท stride`, which the device computes from the index - /// itself. The member is `NonWritable` where `non_writable` (memory nothing writes while the - /// module runs, as [`device_place_read_only`](Self::device_place_read_only)). - pub fn device_element( - &mut self, - base: Value, - elem: TypeId, - stride: u32, - index: Value, - non_writable: bool, - ) -> BResult { - let array = self.module().ty(Type::RuntimeArray { - elem, - stride: Some(stride), - })?; - let rule = self - .mb() - .types() - .facts(elem)? - .max_rule() - .min(LayoutRule::Std430); - let layout = StructLayout { - non_writable, - ..natural_layout(self.mb().types(), rule, true, &[array])? - }; - let block = self.module().ty(Type::Struct { - members: [array].into(), - layout: Some(layout), - })?; - let ptr = self.module().ty(Type::Pointer { - class: StorageClass::PhysicalStorageBuffer, - pointee: block, - })?; - let at = self.convert(ConvOp::ConvertUToPtr, base, ptr)?; - let zero = self.u32_const(0)?; - let zero = self.module().const_value(zero)?; - self.access_chain(at, &[zero, index]) - } - - /// A `PhysicalStorageBuffer` place of a `pointee` at byte `offset` (a 32-bit integer scalar - /// below 2^31, as SPIR-V takes an access chain's index as signed) of the memory at device - /// address `base` (a 64-bit integer scalar): element `offset` of the - /// byte array at `base` ([`device_element`](Self::device_element) of `u8`, so the device forms - /// the address from the offset itself), bitcast to a pointer to the one-member - /// `Block { pointee }`, and the access chain to that member: a place, as - /// [`device_place`](Self::device_place)'s is. SPIR-V's `OpBitcast` takes a pointer to a - /// pointer of the same storage class. The member, and the byte array's, is `NonWritable` - /// where `non_writable`. The byte array needs the 8-bit storage access. - pub fn device_byte_place( - &mut self, - base: Value, - offset: Value, - pointee: TypeId, - non_writable: bool, - ) -> BResult { - let u8_ = self.module().ty(Type::Int { - width: 8, - signed: false, - })?; - let byte = self.device_element(base, u8_, 1, offset, non_writable)?; - let rule = self - .mb() - .types() - .facts(pointee)? - .max_rule() - .min(LayoutRule::Std430); - let layout = StructLayout { - non_writable, - ..natural_layout(self.mb().types(), rule, true, &[pointee])? - }; - let block = self.module().ty(Type::Struct { - members: [pointee].into(), - layout: Some(layout), - })?; - let ptr = self.module().ty(Type::Pointer { - class: StorageClass::PhysicalStorageBuffer, - pointee: block, - })?; - let at = self.convert(ConvOp::Bitcast, byte, ptr)?; - let zero = self.u32_const(0)?; - let zero = self.module().const_value(zero)?; - self.access_chain(at, &[zero]) - } - fn place_at(&mut self, addr: Value, pointee: TypeId, non_writable: bool) -> BResult { let rule = self .mb() diff --git a/crates/metal2vulkan/src/tests/engine_identity.rs b/crates/metal2vulkan/src/tests/engine_identity.rs index d78398146..5e78ed66d 100644 --- a/crates/metal2vulkan/src/tests/engine_identity.rs +++ b/crates/metal2vulkan/src/tests/engine_identity.rs @@ -1709,7 +1709,6 @@ fn device_address_loads(words: &[u32]) -> (usize, usize, usize) { const TYPE_POINTER: u32 = 32; const PHYSICAL_STORAGE_BUFFER: u32 = 5349; const CONVERT_U_TO_PTR: u32 = 120; - const BITCAST: u32 = 124; const ACCESS_CHAIN: u32 = 65; const LOAD: u32 = 61; const STORE: u32 = 62; @@ -1742,12 +1741,6 @@ fn device_address_loads(words: &[u32]) -> (usize, usize, usize) { bases.insert(i[2], ro); } } - // A place bitcast to another block's pointer is that block's base. - BITCAST => { - if let (Some(&ro), true) = (pointers.get(&i[1]), places.contains_key(&i[3])) { - bases.insert(i[2], ro); - } - } ACCESS_CHAIN => { if let Some(&ro) = bases.get(&i[3]) { places.insert(i[2], ro); diff --git a/crates/metal2vulkan/src/tests/engine_offsets_past_2gib.rs b/crates/metal2vulkan/src/tests/engine_offsets_past_2gib.rs index e5c8ea965..0e12f1340 100644 --- a/crates/metal2vulkan/src/tests/engine_offsets_past_2gib.rs +++ b/crates/metal2vulkan/src/tests/engine_offsets_past_2gib.rs @@ -4,7 +4,8 @@ //! would place it. The CPU interpreter takes an access chain's index as signed, as SPIR-V does, //! and traps on an address that lies in no buffer. On it, `ob1_storage_buffer` loads and stores at //! word indices on both sides of 2^29 through slots whose bytes are two buffers, one at the bind -//! address and one around 2 GiB past it. +//! address and one around 2 GiB past it. The direct form, whose sign-extended offsets are exact +//! below 2 GiB only, takes a window of at most 2 GiB. //! //! `llvm-as` assembles the fixture; without it the test skips visibly. @@ -145,3 +146,29 @@ fn accesses_past_2_gib_land_at_their_own_words() { } } } + +/// The direct form's choice (`lower::access::direct_window`): a `Bounded` window of exactly +/// 2 GiB takes it, every byte offset in it below 2^31 so that its sign extension is exact; one a +/// byte longer does not, nor does a `Whole` one. +#[test] +fn the_direct_form_takes_windows_of_at_most_2_gib() { + use crate::lower::access::direct_window; + use crate::pipeline::interface::Window; + for start in [0, -16, 64] { + assert!( + direct_window(Window::Bounded { + start, + len: 1 << 31 + }), + "start {start}" + ); + assert!( + !direct_window(Window::Bounded { + start, + len: (1 << 31) + 1 + }), + "start {start}" + ); + } + assert!(!direct_window(Window::Whole)); +} diff --git a/docs/API.md b/docs/API.md index e0a1bd2dc..5d1d1acf7 100644 --- a/docs/API.md +++ b/docs/API.md @@ -109,18 +109,18 @@ compile does. `DevicePolicy::default()` is `DevicePolicy::UNTRUSTED`: `HandleDomain::Table`, the default). The identity domains (`PointerDomain::Identity` with `HandleDomain::Identity`, taken together) are the new runtime's form: pointers are device addresses used directly and a texture's or sampler's handle is its `gpuResourceID` - ([RESOURCE-ID.md](RESOURCE-ID.md)). They need `bufferDeviceAddress`, `shaderInt64` and - `storageBuffer8BitAccess`. Under them a buffer slot is reached at its own device address - (`gpuAddress` plus the bind offset), pushed as `PushField::SlotGuestBase`, with no root table, - no descriptor and no containment, as Metal contains no device-address access. A slot whose - argument states an alignment of at least 4 (`air.arg_type_align_size`) is reached from that - address as it is: each access is a 32-bit byte offset through a byte array there, whose element - pointer is bitcast to the accessed unit's, and an access of 8 or 16 bytes at an offset proven a + ([RESOURCE-ID.md](RESOURCE-ID.md)). They need `bufferDeviceAddress` and `shaderInt64`. Under + them a buffer slot is reached at its own device address (`gpuAddress` plus the bind offset), + pushed as `PushField::SlotGuestBase`, with no root table, no descriptor and no containment, as + Metal contains no device-address access. A slot whose argument states an alignment of at least + 4 (`air.arg_type_align_size`) and whose window the translator bounds to at most 2 GiB is reached + from that address as it is: each access is at the address plus its 32-bit byte offset, + sign-extended (exact below 2 GiB), and an access of 8 or 16 bytes at an offset proven a multiple of its size is one `uvec2` or `uvec4`. The function states `RuntimeFact::AddressAlignment` with that alignment (at most 16), and a binder refuses a binding whose address is not a multiple of it, as Metal's API validation refuses the bind offset. Any - other slot (an argument with no alignment or one below 4) is reached from the word below each - access. Fields past a kernel's push window (128 bytes; 256 on + other slot (an argument with no alignment or one below 4, or a window no access bounds) is + reached from the word below each access, its offset zero-extended (exact to 4 GiB). Fields past a kernel's push window (128 bytes; 256 on a device made by `Device::from_queries`, whose runtime's floor requires that much `maxPushConstantsSize`: a dispatch region and 28 buffer addresses) go to a parameter block in memory, read at the device address `PushField::ArgumentTableAddress`, which