From 46e3215e555332eccfa31f5d49e33e8da381ebe6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 17 Nov 2026 08:12:23 -0800 Subject: [PATCH] identity: kernels over buffers lower under the identity domains Under PointerDomain::Identity with HandleDomain::Identity every buffer slot of a kernel is its device address used directly. Each slot is the Psb form, its address read from its own push field, PushField::SlotGuestBase(slot): gpuAddress plus the bind offset, which under the identity domains is the Vulkan device address. No root table is read, and no access is contained, as Metal contains no device-address access (psb::prepare_at). Selection admits, under the identity domains, only the plans lowered so far (select::identity_admits): a kernel whose push fields are its slot addresses and its dispatch region, all in the push window, with no descriptor and every slot addressed. Anything else is still Unsupported{IdentityDomains}, not implemented, now refused there rather than for every function at analysis. SEMANTICS_VERSION 2146: a refusal under the identity domains became a module. The new tests (src/tests/engine_identity.rs): - a1_bool_from_float under the identity domains on lavapipe and moltenvk-m3 has no descriptor, no root table and three SlotGuestBase fields. It passes L3, and it computes the program's answer on the CPU interpreter, with its buffers above 4 GiB at bind offsets 32, 16 and 4. It writes nothing past its output, with its region pushed. - a2_dynamic_texel_offset, which samples a texture, compiles under the default domains and is refused under the identity ones. Verified: - m3-max-41149 (msi, check commit eeecc4492b, the same tree but for the test's region bytes): clippy -p metal2vulkan --all-targets --all-features, rustdoc -D warnings, and nextest -p metal2vulkan --all-features, 3664 of 3665. The one failure was this test, which met the kernel's DispatchRegion field and did not fill it. - m3-max-41195 (msi, a5dbfd408f, this tree): clippy and rustdoc; tests::engine_identity and the domain pair test, 3 of 3. - m3-max-41150 (msi, eeecc4492b): clippy under --cfg m2v_gpu and --cfg m2v_cpu_icd. --- crates/metal2vulkan/src/cache/mod.rs | 2 +- crates/metal2vulkan/src/device/profile.rs | 12 +- .../metal2vulkan/src/device/tests/profile.rs | 27 +-- crates/metal2vulkan/src/error/mod.rs | 3 +- crates/metal2vulkan/src/lower/access/emit.rs | 12 +- crates/metal2vulkan/src/lower/access/mod.rs | 53 ++++- crates/metal2vulkan/src/lower/access/psb.rs | 28 +++ crates/metal2vulkan/src/lower/select.rs | 28 +++ crates/metal2vulkan/src/pipeline/compile.rs | 4 - .../metal2vulkan/src/tests/engine_identity.rs | 213 ++++++++++++++++++ crates/metal2vulkan/src/tests/mod.rs | 1 + docs/API.md | 8 +- docs/REFUSALS.md | 4 +- scripts/refusal-surface/reach.tsv | 2 +- 14 files changed, 346 insertions(+), 51 deletions(-) create mode 100644 crates/metal2vulkan/src/tests/engine_identity.rs diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index d93e8ff7a..d3983cbf2 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -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 = 2145; +pub const SEMANTICS_VERSION: u32 = 2146; /// 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/device/profile.rs b/crates/metal2vulkan/src/device/profile.rs index 1e91bde4a..a8a7ca35f 100644 --- a/crates/metal2vulkan/src/device/profile.rs +++ b/crates/metal2vulkan/src/device/profile.rs @@ -718,9 +718,11 @@ pub enum PointerDomain { /// The sorted resident set of bound arguments and used resources. ResidentTable, /// Pointers are device addresses used directly: a `device` or `constant` pointer is the - /// 64-bit Vulkan device address of the byte it names, as Metal's `gpuAddress` is. Needs - /// `bufferDeviceAddress` and `shaderInt64`. No function compiles under it yet: a compile is - /// refused as [`Feature::IdentityDomains`](crate::error::Feature::IdentityDomains). + /// 64-bit Vulkan device address of the byte it names, as Metal's `gpuAddress` is, and a + /// buffer slot is reached at its own address, pushed, uncontained. Needs + /// `bufferDeviceAddress` and `shaderInt64`. So far it lowers a kernel that reaches memory + /// only through its buffer slots; any other function is refused as + /// [`Feature::IdentityDomains`](crate::error::Feature::IdentityDomains). Identity, } @@ -734,8 +736,8 @@ pub enum HandleDomain { Table, /// A texture's or sampler's handle is its `gpuResourceID`: the low 32 bits index the /// pipeline's descriptor arrays and the high 32 bits are the word of its Metal descriptor - /// (`docs/RESOURCE-ID.md`). No function compiles under it yet: a compile is refused as - /// [`Feature::IdentityDomains`](crate::error::Feature::IdentityDomains). + /// (`docs/RESOURCE-ID.md`). No function that reads a texture or a sampler compiles under it + /// yet: it is refused as [`Feature::IdentityDomains`](crate::error::Feature::IdentityDomains). Identity, } diff --git a/crates/metal2vulkan/src/device/tests/profile.rs b/crates/metal2vulkan/src/device/tests/profile.rs index 6ba40e8cb..0636fc856 100644 --- a/crates/metal2vulkan/src/device/tests/profile.rs +++ b/crates/metal2vulkan/src/device/tests/profile.rs @@ -309,11 +309,9 @@ fn below_1_2_and_contradictory_policies_are_refused() { assert_eq!(dev.profile().fields().containment, Containment::UnsafeOff); } -/// The identity domains come as a pair (`InvalidDescriptor` for one alone), need +/// The identity domains come as a pair (`InvalidDescriptor` for one alone) and need /// `bufferDeviceAddress` and `shaderInt64` (`Unsupported{IdentityDomains}` naming both, on a -/// device without `shaderInt64`), and lower no function yet: a compile under them is -/// `Unsupported{IdentityDomains}`, not implemented, where the same function compiles under the -/// other domains. +/// 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 { @@ -351,27 +349,6 @@ fn the_identity_domains_come_as_a_pair_on_a_device_with_addresses() { } other => panic!("{other:?}"), } - use crate::pipeline::testing::{compile, engine_dir, llvm_assemble, AssembleError}; - let bc = match llvm_assemble(&engine_dir().join("a1_bool_from_float.ll")) { - Ok(bc) => bc, - Err(AssembleError::Missing(why)) => { - eprintln!("SKIPPED: {why}"); - return; - } - Err(e) => panic!("{e:?}"), - }; - let under = |policy| { - let dev = Device::new(profile("lavapipe"), policy).unwrap(); - compile(&bc, "a1_bool_from_float", dev.profile()) - }; - assert!(under(DevicePolicy::default()).is_ok()); - match under(identity) { - Err(Error::Unsupported(u)) => { - assert_eq!(u.feature, Feature::IdentityDomains); - assert_eq!(u.needs, Needs::NotImplemented); - } - other => panic!("{other:?}"), - } } #[test] diff --git a/crates/metal2vulkan/src/error/mod.rs b/crates/metal2vulkan/src/error/mod.rs index 7cfc1bf71..2792ef089 100644 --- a/crates/metal2vulkan/src/error/mod.rs +++ b/crates/metal2vulkan/src/error/mod.rs @@ -1412,7 +1412,8 @@ pub enum Feature { VulkanVersion, /// The identity pointer and handle domains (`PointerDomain::Identity`, /// `HandleDomain::Identity`): on a device without `bufferDeviceAddress` and `shaderInt64`, - /// and, on any device, a compile of a function they do not lower yet. + /// and, on any device, a compile of a function they do not lower yet: anything but a kernel + /// that reaches memory only through its buffer slots. IdentityDomains, /// A constants-block record LLVM defines that the typed model has no form for (a block /// address, a `select` and the other constant expressions of [`ConstantRecord`]), by LLVM's diff --git a/crates/metal2vulkan/src/lower/access/emit.rs b/crates/metal2vulkan/src/lower/access/emit.rs index 8e1359888..1cf10e794 100644 --- a/crates/metal2vulkan/src/lower/access/emit.rs +++ b/crates/metal2vulkan/src/lower/access/emit.rs @@ -274,9 +274,15 @@ pub(super) fn prepare( let lowering = cx.plan.families.access.slots.get(t.slot).copied().flatten(); let ops = WordOps::of(crate::lower::sync::memory_model::coherence(cx.plan)); if let Some(SlotLowering::Psb { start }) = lowering { - let table = cx.push_field(fb, PushField::RootTableAddress)?; - let contained = cx.plan.families.access.psb_contained; - let a = psb::prepare(fb, table, t.root, m, start, n, contained)?; + let a = if cx.plan.families.access.identity { + let address = cx.push_field(fb, PushField::SlotGuestBase(choice.slot))?; + let address = super::pointer::slot_guest_base_u64(fb, address)?; + psb::prepare_at(fb, address, m, start)? + } else { + let table = cx.push_field(fb, PushField::RootTableAddress)?; + let contained = cx.plan.families.access.psb_contained; + psb::prepare(fb, table, t.root, m, start, n, contained)? + }; let units = cx.plan.families.access.psb_units; return Ok(Prepared { mem: Mem::Psb(a.mem.with(ops).with_units(units)), diff --git a/crates/metal2vulkan/src/lower/access/mod.rs b/crates/metal2vulkan/src/lower/access/mod.rs index ff8e1f91d..905877f85 100644 --- a/crates/metal2vulkan/src/lower/access/mod.rs +++ b/crates/metal2vulkan/src/lower/access/mod.rs @@ -697,6 +697,9 @@ pub struct AccessPlan { /// The narrow units device-address accesses load and store directly (`psb::narrow_units` of /// the profile): a sub-word store there is one narrow store instead of two word atomics. pub psb_units: Units, + /// The function compiles under the identity domains: a device-address slot's address is its + /// own field ([`address_field`]), uncontained. + pub identity: bool, /// A storage view's slot declares its `RangeAtBindOffset` specialization, as a uniform view's /// does: the profile takes `DriverWorkaround::PushReadsLengthenThreadPayload`, so a read of /// the slot at its bind offset alone, which reads no push word, is worth the key @@ -1196,6 +1199,9 @@ struct Census { uniform: u32, texel: u32, fields: BTreeSet, + /// Under the identity domains a device-address slot reads its own address + /// ([`address_field`]), not the root table. + identity: bool, } impl Census { @@ -1203,7 +1209,7 @@ impl Census { /// pointer reads the root table. fn new( push: &BTreeSet, - dynamic: bool, + (dynamic, identity): (bool, bool), forms: impl Iterator, m: &mut Meter, ) -> Result { @@ -1212,6 +1218,7 @@ impl Census { uniform: 0, texel: 0, fields: push.clone(), + identity, }; let mut psb = dynamic; for (slot, l) in forms { @@ -1224,7 +1231,12 @@ impl Census { SlotLowering::View(_) => c.uniform = c.uniform.saturating_add(1), SlotLowering::Psb { .. } => { c.fields.remove(&PushField::SlotBindOffset(slot)); - psb = true; + match identity { + true => { + c.fields.insert(address_field(slot)); + } + false => psb = true, + } } } } @@ -1263,10 +1275,26 @@ impl Census { fn addressed(&mut self, slot: SlotRef) { self.storage = self.storage.saturating_sub(1); self.fields.remove(&PushField::SlotBindOffset(slot)); - self.fields.insert(PushField::RootTableAddress); + self.fields.insert(match self.identity { + true => address_field(slot), + false => PushField::RootTableAddress, + }); } } +/// The field a slot's device address is read from under the identity domains: its Metal address, +/// the bound buffer's `gpuAddress` plus the bind offset, which is its Vulkan device address there +/// (`SlotGuestBase`'s value, a `uvec2`, low word first). +fn address_field(slot: SlotRef) -> PushField { + PushField::SlotGuestBase(slot) +} + +/// Whether profile `p` takes the identity domains, where a pointer is a device address used +/// directly. +pub(crate) fn identity(p: &DeviceProfile) -> bool { + p.fields().pointer_domain == crate::device::PointerDomain::Identity +} + /// [`finish_slots`]'s placement, in its charged working tables. fn place_slots(cx: &mut SelectCx<'_>, m: &mut Meter) -> Result>, Error> { let p = cx.profile; @@ -1322,7 +1350,9 @@ fn place_slots(cx: &mut SelectCx<'_>, m: &mut Meter) -> Result, m: &mut Meter) -> Result, m: &mut Meter) -> Result, slot: SlotRef| { cx.facts.insert(RuntimeFact::DeviceAddresses { slot }); cx.push.remove(&PushField::SlotBindOffset(slot)); - cx.push.insert(PushField::RootTableAddress); + cx.push.insert(match identity(cx.profile) { + true => address_field(slot), + false => PushField::RootTableAddress, + }); }; for (plan, (l, w)) in memory.slots.iter().zip(lowering.iter().zip(&windows)) { let (Some(l), Some(window)) = (l, w) else { @@ -1611,8 +1649,11 @@ fn place_slots(cx: &mut SelectCx<'_>, m: &mut Meter) -> Result, + address: Value, + m: Value, + start: i64, +) -> BResult { + let disp = displacement(fb, m, start)?; + 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_)?; + Ok(PsbAccess { + mem: PsbMem::at(base), + off: low, + inb: None, + }) +} + /// The narrow units a device-address memory loads and stores directly on profile `p`: the 1- and /// 2-byte units of [`Units::storage_on`] (the width's storage access and integer type). No `uvec2` /// or `uvec4` unit: a device address below an access is only 4-byte aligned. diff --git a/crates/metal2vulkan/src/lower/select.rs b/crates/metal2vulkan/src/lower/select.rs index 345267d36..bbfeddfd7 100644 --- a/crates/metal2vulkan/src/lower/select.rs +++ b/crates/metal2vulkan/src/lower/select.rs @@ -1856,9 +1856,37 @@ fn finish( None => 0, }; plan.estimate.add(walk); + if access::identity(p) { + identity_admits(&plan)?; + } Ok(plan) } +/// Under the identity domains, the plans lowered so far: a kernel that reaches memory only +/// through its buffer slots' device addresses, all pushed, with no descriptor. +fn identity_admits(plan: &Plan) -> Result<(), Error> { + let pushed = plan.push.fields.iter().all(|f| { + matches!( + f.field, + PushField::SlotGuestBase(_) | PushField::DispatchRegion + ) && matches!(f.loc, crate::pipeline::interface::PushLoc::Push { .. }) + }); + let addressed = plan + .slots + .iter() + .flatten() + .all(|c| c.form == BufferForm::Psb); + if plan.stage == Stage::Kernel + && pushed + && addressed + && plan.push.argument_table_size == 0 + && plan.descriptors == DescriptorCounts::default() + { + return Ok(()); + } + Err(Unsupported::not_implemented(Feature::IdentityDomains).into()) +} + /// The words the predicated walk of `plan` adds for the constructs of its structured tree /// (`structurize::emit::construct_words`): the tree emission builds from the same view, here /// built, measured and released before emission. diff --git a/crates/metal2vulkan/src/pipeline/compile.rs b/crates/metal2vulkan/src/pipeline/compile.rs index ec8f06c78..d91aa5d87 100644 --- a/crates/metal2vulkan/src/pipeline/compile.rs +++ b/crates/metal2vulkan/src/pipeline/compile.rs @@ -2853,10 +2853,6 @@ impl Analyzed { /// [`plan_one`]'s reads before it selects ([`Analyzed`]). fn analyze_one(r: &Request<'_>, p: &DeviceProfile, m: &mut Meter) -> Result { - // The identity domains lower no function yet. - if p.fields().pointer_domain == crate::device::PointerDomain::Identity { - return Err(Unsupported::not_implemented(Feature::IdentityDomains).into()); - } let Specialized { air_hash, entry, diff --git a/crates/metal2vulkan/src/tests/engine_identity.rs b/crates/metal2vulkan/src/tests/engine_identity.rs new file mode 100644 index 000000000..73d8cde00 --- /dev/null +++ b/crates/metal2vulkan/src/tests/engine_identity.rs @@ -0,0 +1,213 @@ +//! The identity domains (`PointerDomain::Identity` with `HandleDomain::Identity`) in the lowering. +//! +//! - A kernel over buffers alone (`engine/a1_bool_from_float`) compiles with each slot reached +//! through its own device address, pushed (`SlotGuestBase`), with no root table and no +//! descriptor. It passes L3, and on the CPU interpreter, with its buffers above 4 GiB and bound +//! at offsets that are not 0, it computes the program's answer and writes nothing else. +//! - A kernel that reads a texture (`engine/a2_dynamic_texel_offset`) is still refused as +//! `Unsupported{IdentityDomains}`, not implemented, where the default domains compile it. +//! +//! `llvm-as` assembles the kernels; without it the tests skip visibly. + +use std::collections::BTreeMap; + +use crate::device::profile::{HandleDomain, PointerDomain}; +use crate::device::DevicePolicy; +use crate::error::{Error, Feature, Needs}; +use crate::lower::dispatch::region_bytes; +use crate::pipeline::interface::PushLoc; +use crate::pipeline::sym::{Axis, PushField, SlotClass, SlotRef, SpecKey, Stage}; +use crate::pipeline::testing::{ + compile, compile_kept, engine_dir, link_and_validate, llvm_assemble, profile, AssembleError, +}; +use crate::spirv::interp::{run, Buffer, Config, Resources}; + +/// Profiles with `bufferDeviceAddress` and `shaderInt64`. +const PROFILES: [&str; 2] = ["lavapipe", "moltenvk-m3"]; + +fn identity() -> DevicePolicy { + DevicePolicy { + pointer_domain: PointerDomain::Identity, + handle_domain: HandleDomain::Identity, + ..DevicePolicy::default() + } +} + +fn assemble(name: &str) -> Option> { + assembled( + name, + llvm_assemble(&engine_dir().join(format!("{name}.ll"))), + ) +} + +fn assembled(name: &str, r: Result, AssembleError>) -> Option> { + match r { + Ok(b) => Some(b), + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + None + } + Err(e) => panic!("{name}: {e:?}"), + } +} + +/// The spec map of a dispatch of `group` threads a threadgroup, in one dimension. +fn local_size(group: u32) -> BTreeMap { + BTreeMap::from([ + (SpecKey::LocalSize(Axis::X), u64::from(group)), + (SpecKey::LocalSize(Axis::Y), 1), + (SpecKey::LocalSize(Axis::Z), 1), + (SpecKey::FlatLocalSize, u64::from(group)), + ]) +} + +fn slot(index: u32) -> SlotRef { + SlotRef { + stage: Stage::Kernel, + class: SlotClass::Buffer, + index, + } +} + +/// `float` patterns none of which is a NaN or a denormal (the fixture compiles with fast math). +const FLOATS: [f32; 6] = [0.0, -0.0, 0.5, 2.0, 1.0, -3.0]; +/// The same values' `half` bits. +const HALVES: [u16; 6] = [0x0000, 0x8000, 0x3800, 0x4000, 0x3c00, 0xc200]; + +/// Thread `i`'s input lanes: indices into [`FLOATS`] and [`HALVES`]. +fn lanes(i: usize) -> ([usize; 4], [usize; 4]) { + ( + std::array::from_fn(|j| (i + j) % FLOATS.len()), + std::array::from_fn(|j| (3 * i + j) % HALVES.len()), + ) +} + +/// What `a1_bool_from_float` writes for thread `i`: each lane's `x != 0`, the `float` lanes in +/// bits 0-3 and 4-7, the `half` lanes in bits 8-11 and 12-15. +fn want(i: usize) -> u32 { + let (f, h) = lanes(i); + let mut r = 0u32; + for (j, (&fi, &hi)) in f.iter().zip(&h).enumerate() { + let fb = u32::from(FLOATS[fi] != 0.0); + let hb = u32::from(HALVES[hi] & 0x7fff != 0); + r |= fb << j | fb << (4 + j) | hb << (8 + j) | hb << (12 + j); + } + r +} + +/// A kernel over buffers alone compiles under the identity domains to a module that reads each +/// slot's device address from its own push field and nothing else, and computes the program's +/// answer there. +#[test] +fn a_buffer_kernel_reads_each_slot_at_its_pushed_device_address() { + let Some(bc) = assemble("a1_bool_from_float") else { + return; + }; + const GROUP: u32 = 8; + const GROUPS: u32 = 4; + let threads = (GROUP * GROUPS) as usize; + // (the buffer's device address, the bind offset): above 4 GiB, so a dropped high word shows. + let at = [ + (0x1_0000_0000u64, 32u64), + (0x2_0000_1000, 16), + (0x3_0000_2000, 4), + ]; + let mut bits = vec![0u8; at[0].1 as usize]; + let mut halves = vec![0u8; at[1].1 as usize]; + for i in 0..threads { + let (f, h) = lanes(i); + for (&fi, &hi) in f.iter().zip(&h) { + bits.extend_from_slice(&FLOATS[fi].to_bits().to_le_bytes()); + halves.extend_from_slice(&HALVES[hi].to_le_bytes()); + } + } + // One spare word past the output's end, which the kernel must not write. + let out = vec![0u8; at[2].1 as usize + 4 * threads + 4]; + for stem in PROFILES { + let p = profile(stem, identity()).unwrap_or_else(|e| panic!("{e}")); + let (a, plan, module) = + compile_kept(&bc, "a1_bool_from_float", &p).unwrap_or_else(|e| panic!("{stem}: {e}")); + assert!(a.interface.descriptors.is_empty(), "{stem}"); + assert!(!a.interface.needs_pointer_translation, "{stem}"); + assert_eq!(plan.push.argument_table_size, 0, "{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)]; + let mut read = Vec::new(); + // The grid builtin's region, as the identity domains fill it: base 0, the threadgroup + // size, and the grid. + let region = region_bytes([0; 3], [GROUP * GROUPS, 1, 1], [GROUP, 1, 1]).expect("packs"); + for f in &plan.push.fields { + let o = match f.loc { + PushLoc::Push { offset } => offset as usize, + PushLoc::ArgumentTable { .. } => panic!("{stem}: {f:?} spilled"), + }; + match f.field { + PushField::SlotGuestBase(s) => { + let (base, bind) = at[s.index as usize]; + push[o..o + 8].copy_from_slice(&(base + bind).to_le_bytes()); + read.push(s); + } + PushField::DispatchRegion => push[o..o + 32].copy_from_slice(®ion), + _ => panic!("{stem}: unexpected push field {f:?}"), + } + } + read.sort(); + assert_eq!(read, [slot(0), slot(1), slot(2)], "{stem}"); + let buffer = |(address, _): (u64, u64), bytes: &Vec| Buffer { + slot: None, + address: Some(address), + bytes: bytes.clone(), + }; + let mut expected = vec![0u8; at[2].1 as usize]; + for i in 0..threads { + expected.extend_from_slice(&want(i).to_le_bytes()); + } + expected.extend_from_slice(&[0; 4]); + // The region read (`MultiRegion` 1) and not (0) give the same grid. + for multi in [0, 1] { + let mut spec = local_size(GROUP); + spec.insert(SpecKey::MultiRegion, multi); + let res = Resources { + buffers: vec![ + buffer(at[0], &bits), + buffer(at[1], &halves), + buffer(at[2], &out), + ], + push: push.clone(), + spec, + }; + let cfg = Config { + workgroups: [GROUPS, 1, 1], + ..Config::default() + }; + let got = run(&module, &cfg, res).unwrap_or_else(|e| panic!("{stem}: {e}")); + assert_eq!(got.buffers[2], expected, "{stem} {multi}: out"); + assert_eq!(got.buffers[0], bits, "{stem} {multi}: bits"); + assert_eq!(got.buffers[1], halves, "{stem} {multi}: halves"); + } + } +} + +/// A kernel that reads a texture is not lowered under the identity domains yet. +#[test] +fn a_texture_kernel_is_refused_under_the_identity_domains() { + let Some(bc) = assemble("a2_dynamic_texel_offset") else { + return; + }; + for stem in PROFILES { + let default = profile(stem, DevicePolicy::default()).unwrap_or_else(|e| panic!("{e}")); + compile(&bc, "a2_dynamic_texel_offset", &default) + .unwrap_or_else(|e| panic!("{stem}: default domains: {e}")); + let p = profile(stem, identity()).unwrap_or_else(|e| panic!("{e}")); + match compile(&bc, "a2_dynamic_texel_offset", &p) { + Err(Error::Unsupported(u)) => { + assert_eq!( + (u.feature, u.needs), + (Feature::IdentityDomains, Needs::NotImplemented), + "{stem}" + ); + } + other => panic!("{stem}: {other:?}"), + } + } +} diff --git a/crates/metal2vulkan/src/tests/mod.rs b/crates/metal2vulkan/src/tests/mod.rs index 75b773d21..1bc0350f1 100644 --- a/crates/metal2vulkan/src/tests/mod.rs +++ b/crates/metal2vulkan/src/tests/mod.rs @@ -82,6 +82,7 @@ mod engine_heap_texel_buffers; mod engine_heap_ycbcr_writes; mod engine_helper_picks; mod engine_helper_threads; +mod engine_identity; mod engine_image_shapes; mod engine_imageblock; mod engine_index_words; diff --git a/docs/API.md b/docs/API.md index 0dfd5d2fb..d718a2dd3 100644 --- a/docs/API.md +++ b/docs/API.md @@ -109,9 +109,11 @@ 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` and `shaderInt64`, and - they lower no function yet: a compile under them is refused as - `Unsupported{IdentityDomains}`. + ([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. So far they lower a kernel that reaches memory only + through its buffer slots; any other function is refused as `Unsupported{IdentityDomains}`. - **The model's linear sample of planes read in place** (`YCbCrInPlacePolicy::Model`, the default; changes emitted code). On an integrated GPU or a CPU device a function that only samples, gathers or sizes a `YCBCR8_420_2P` or `YCBCR10_420_2P` texture reads its planes' codes diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index ad3a10998..56172d6e8 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -141,7 +141,7 @@ Columns: | `RasterizationRateMap` | | devices without VkFeature::MultiViewport; devices without VkFeature::ShaderDrawParameters; devices without VkFeature::ShaderOutputViewportIndex; devices without VkLimit::MaxGeometryTotalOutputComponents; devices without a capability the site computes; devices without a capability the site computes (`vec![Capability::Feature(lacks)]`); every device (not implemented) | `ours` | 10: `crates/metal2vulkan/src/lower/stage_io/plan.rs (region_copy, 3 sites)`, `crates/metal2vulkan/src/lower/stage_io/plan.rs (region_viewport, 2 sites)` (+3) | `rare`: rasterization rate maps | | `GpuEncodedIndirectCommands` | | devices short of a Vulkan feature the site names; devices without buffer device addresses (`addressed_needs`); every device (no Vulkan mechanism); every device (not implemented) | `ours` | 20: `crates/metal2vulkan/src/bind/replay.rs (preprocess)`, `crates/metal2vulkan/src/bind/replay.rs (replay_preprocess_module)` (+13) | `rare`: indirect command buffers encoded by a kernel | | `VulkanVersion` | The device's Vulkan version is below the 1.2 floor | devices without ApiVersion::V1_2 | `device` | 2: `crates/metal2vulkan/src/ash_interop/query.rs (query)`, `crates/metal2vulkan/src/device/derive.rs (with_deviations)` | `none`: a device below Vulkan 1.2 | -| `IdentityDomains` | The identity pointer and handle domains (`PointerDomain::Identity`, `HandleDomain::Identity`) | devices without a capability the site computes (`all`); every device (not implemented) | `ours` | 2: `crates/metal2vulkan/src/device/derive.rs (identity_domains)`, `crates/metal2vulkan/src/pipeline/compile.rs (analyze_one)` | `none`: the identity domains, which the new runtime alone takes and which lower no function yet | +| `IdentityDomains` | The identity pointer and handle domains (`PointerDomain::Identity`, `HandleDomain::Identity`) | devices without a capability the site computes (`all`); every device (not implemented) | `ours` | 2: `crates/metal2vulkan/src/device/derive.rs (identity_domains)`, `crates/metal2vulkan/src/lower/select.rs (identity_admits)` | `none`: the identity domains, which the new runtime alone takes and which lower only kernels over buffer slots so far | | `Constant` | A constants-block record LLVM defines that the typed model has no form for (a block address, a `select` and the other constant expressions of... | - | `-` | via a helper: 3: `crates/metal2vulkan-air/src/bitcode/constants.rs (record, 2 sites)`, `crates/metal2vulkan/src/error/mod.rs (from)` | `none`: LLVM constant forms AIR does not emit | | `Attribute` | An LLVM attribute kind that changes the calling ABI and that AIR does not use (`inalloca`, `preallocated`, `byref`, `nest`, the `swift*` kinds) | - | `-` | via a helper: 2: `crates/metal2vulkan-air/src/bitcode/attrs.rs (known)`, `crates/metal2vulkan/src/error/mod.rs (from)` | `none`: LLVM ABI attributes AIR does not use | | `BitcodeRecord` | A bitcode record LLVM defines in `block` that the typed model does not cover and that has no more specific variant (legacy spellings predating... | - | `-` | via a helper: 8: `crates/metal2vulkan-air/src/bitcode/attrs.rs (decode_lists)`, `crates/metal2vulkan-air/src/bitcode/function.rs (symtab)` (+5) | `none`: bitcode records AIR does not emit | @@ -707,7 +707,7 @@ Each refusal's sites, with the function that builds it. - `feature::RasterizationRateMap`: `crates/metal2vulkan/src/lower/stage_io/plan.rs (region_copy, 3 sites)`; `crates/metal2vulkan/src/lower/stage_io/plan.rs (region_viewport, 2 sites)`; `crates/metal2vulkan/src/pipeline/link.rs (region_box)`; `crates/metal2vulkan/src/pipeline/link/geometry.rs (check_limits)`; `crates/metal2vulkan/src/pipeline/link/geometry.rs (fan_plan, 3 sites)` - `feature::GpuEncodedIndirectCommands`: `crates/metal2vulkan/src/bind/replay.rs (preprocess)`; `crates/metal2vulkan/src/bind/replay.rs (replay_preprocess_module)`; `crates/metal2vulkan/src/bind/replay/draws.rs (draw_replay_module)`; `crates/metal2vulkan/src/bind/replay/draws.rs (draw_replay_preprocess)`; `crates/metal2vulkan/src/bind/replay/flat.rs (flat_dispatch_module)`; `crates/metal2vulkan/src/bind/replay/merged.rs (draw_merge_module)`; `crates/metal2vulkan/src/lower/access/encode.rs (needs, 2 sites)`; `crates/metal2vulkan/src/lower/access/encode.rs (select_command)`; `crates/metal2vulkan/src/lower/access/mod.rs (plan_replay, 3 sites)`; `crates/metal2vulkan/src/lower/mesh/mod.rs (plan_partial_groups)`; `crates/metal2vulkan/src/lower/select.rs (finish, 2 sites)`; `crates/metal2vulkan/src/lower/simd/divergent.rs (barriers)`; `crates/metal2vulkan/src/lower/stage_io/plan.rs (merged)`; `crates/metal2vulkan/src/lower/stage_io/plan.rs (param)`; `crates/metal2vulkan/src/pipeline/compile.rs (analyze_one, 2 sites)` - `feature::VulkanVersion`: `crates/metal2vulkan/src/ash_interop/query.rs (query)`; `crates/metal2vulkan/src/device/derive.rs (with_deviations)` -- `feature::IdentityDomains`: `crates/metal2vulkan/src/device/derive.rs (identity_domains)`; `crates/metal2vulkan/src/pipeline/compile.rs (analyze_one)` +- `feature::IdentityDomains`: `crates/metal2vulkan/src/device/derive.rs (identity_domains)`; `crates/metal2vulkan/src/lower/select.rs (identity_admits)` - `feature::Constant`: `crates/metal2vulkan-air/src/bitcode/constants.rs (record, 2 sites)`; `crates/metal2vulkan/src/error/mod.rs (from)` - `feature::Attribute`: `crates/metal2vulkan-air/src/bitcode/attrs.rs (known)`; `crates/metal2vulkan/src/error/mod.rs (from)` - `feature::BitcodeRecord`: `crates/metal2vulkan-air/src/bitcode/attrs.rs (decode_lists)`; `crates/metal2vulkan-air/src/bitcode/function.rs (symtab)`; `crates/metal2vulkan-air/src/bitcode/metadata.rs (decode, 2 sites)`; `crates/metal2vulkan-air/src/bitcode/module.rs (check_bodies)`; `crates/metal2vulkan-air/src/bitcode/module.rs (read)`; `crates/metal2vulkan-air/src/bitcode/module.rs (symtab)`; `crates/metal2vulkan/src/error/mod.rs (from)` diff --git a/scripts/refusal-surface/reach.tsv b/scripts/refusal-surface/reach.tsv index aa6a81477..0950db638 100644 --- a/scripts/refusal-surface/reach.tsv +++ b/scripts/refusal-surface/reach.tsv @@ -82,7 +82,7 @@ feature Tensors rare shader tensors feature RasterizationRateMap rare rasterization rate maps feature GpuEncodedIndirectCommands rare indirect command buffers encoded by a kernel feature VulkanVersion none a device below Vulkan 1.2 -feature IdentityDomains none the identity domains, which the new runtime alone takes and which lower no function yet +feature IdentityDomains none the identity domains, which the new runtime alone takes and which lower only kernels over buffer slots so far feature Constant none LLVM constant forms AIR does not emit feature Attribute none LLVM ABI attributes AIR does not use feature BitcodeRecord none bitcode records AIR does not emit