diff --git a/crates/metal2vulkan/src/lower/dispatch.rs b/crates/metal2vulkan/src/lower/dispatch.rs index ea2acd467..17268e346 100644 --- a/crates/metal2vulkan/src/lower/dispatch.rs +++ b/crates/metal2vulkan/src/lower/dispatch.rs @@ -15,8 +15,9 @@ //! **Builtins** ([`KernelBuiltin`]). Every Metal kernel builtin derives from a handful of Vulkan //! ones and the linear local index `lin`: //! -//! - `lin` is `LocalInvocationIndex`, or, flattened, `SubgroupId · SubgroupSize + -//! SubgroupLocalInvocationId`, so every Metal identity (`tid = sg · 32 + lane`) holds whatever +//! - `lin` is `LocalInvocationIndex`, or, flattened, `SubgroupId · S + SubgroupLocalInvocationId` +//! with `S` the subgroup size the pipeline pins, a constant (the `SubgroupSize` builtin only +//! where the stage pins none), so every Metal identity (`tid = sg · 32 + lane`) holds whatever //! order the driver gives subgroups; //! - the position in the threadgroup is `LocalInvocationId`, or, flattened, `lin` unflattened over //! `(sx, sy, sz)`; @@ -527,6 +528,11 @@ pub struct DispatchPlan { /// region's grid, and reads its base and `L` as the profile's form says /// ([`RegionSelect::Grid`], [`RegionSelect::Sized`]). `None` outside them. pub identity_region: Option, + /// The subgroup size the pipeline pins in a flattened tier + /// (`SubgroupRequirement::size`): `lin` multiplies the subgroup index by it as a constant and + /// never reads `SubgroupSize`. `None` outside the flattened tiers and where the stage pins no + /// size, where `lin` reads the builtin. + pub pinned_subgroup_size: Option, } /// The words a flat grid adds: its thread's index, position, and early return, derived once for the @@ -671,8 +677,10 @@ pub fn plan_kernel(cx: &mut SelectCx<'_>, tier: Tier) -> Result, tier: Tier) -> Result, num_workgroups: Option, subgroup_id: Option, + /// `SubgroupSize`, declared only where the stage pins no size ([`Self::pinned_subgroup_size`]). subgroup_size: Option, subgroup_local: Option, + /// The subgroup size the pipeline pins ([`DispatchPlan::pinned_subgroup_size`]). + pinned_subgroup_size: Option, /// The words the entry wrapper stores the threadgroup's position and grid into /// ([`DispatchPlan::wrapper_groups`]), with the dispatch's own builtins it reads. wrapper: Option, @@ -1317,8 +1329,13 @@ impl KernelDecl { workgroup_id, num_workgroups, subgroup_id: var(subgroups, B::SubgroupId, u32_)?, - subgroup_size: var(subgroups, B::SubgroupSize, u32_)?, + subgroup_size: var( + subgroups && dp.pinned_subgroup_size.is_none(), + B::SubgroupSize, + u32_, + )?, subgroup_local: var(subgroups, B::SubgroupLocalInvocationId, u32_)?, + pinned_subgroup_size: dp.pinned_subgroup_size, wrapper, object_grid: dp.object_grid, partial_groups: dp.partial_groups, @@ -1437,7 +1454,12 @@ impl KernelDecl { LocalSizeForm::Spec => self.load(fb, self.local_index), LocalSizeForm::Flat => { let sg = self.load(fb, self.subgroup_id)?; - let size = self.load(fb, self.subgroup_size)?; + // The size the pipeline pins, a constant: where a size is pinned the module never + // reads `SubgroupSize`. + let size = match self.pinned_subgroup_size { + Some(size) => Self::k(fb, size)?, + None => self.load(fb, self.subgroup_size)?, + }; let lane = self.load(fb, self.subgroup_local)?; let base = fb.binary(BinOp::IMul, sg, size)?; fb.binary(BinOp::IAdd, base, lane) diff --git a/crates/metal2vulkan/src/lower/dispatch/tests.rs b/crates/metal2vulkan/src/lower/dispatch/tests.rs index 453641799..5051ca57f 100644 --- a/crates/metal2vulkan/src/lower/dispatch/tests.rs +++ b/crates/metal2vulkan/src/lower/dispatch/tests.rs @@ -40,6 +40,7 @@ fn plan(form: LocalSizeForm, multi_region: bool) -> DispatchPlan { partial_groups: false, grid_edge: false, identity_region: None, + pinned_subgroup_size: None, } } @@ -399,6 +400,54 @@ fn single_region_builtins_match_metal() { } } +/// A flattened kernel whose pipeline pins its subgroup size reads no `SubgroupSize` builtin: its +/// module declares none, and its `lin` takes the pinned size as a constant, so every builtin keeps +/// Metal's value at either modelled width with and without regions. Unpinned, it declares and +/// reads the builtin. +#[test] +fn a_pinned_subgroup_size_reads_no_subgroup_size_builtin() { + let declares_subgroup_size = |m: TypedModule| { + let words = crate::spirv::serialize::serialize(m) + .expect("serialize") + .words; + let mut at = 5; + let mut found = false; + while let Some(&first) = words.get(at) { + let (count, op) = ((first >> 16) as usize, first & 0xffff); + found |= op == crate::spv::Op::Decorate as u32 + && words.get(at + 2) == Some(&(crate::spv::Decoration::BuiltIn as u32)) + && words.get(at + 3) == Some(&(crate::spv::BuiltIn::SubgroupSize as u32)); + at += count.max(1); + } + found + }; + for mr in [false, true] { + let dp = plan(LocalSizeForm::Flat, mr); + assert!( + declares_subgroup_size(module(&dp)), + "unpinned, MultiRegion {mr}" + ); + for (size, sg) in [(32, SubgroupSize::W32), (64, SubgroupSize::W64)] { + let mut dp = plan(LocalSizeForm::Flat, mr); + dp.pinned_subgroup_size = Some(size); + assert!( + !declares_subgroup_size(module(&dp)), + "pinned {size}, MultiRegion {mr}" + ); + let m = module(&dp); + for (l, groups) in [([8, 4, 2], [3, 2, 1]), ([64, 1, 1], [2, 1, 2])] { + let threads = std::array::from_fn(|i| l[i] * groups[i]); + let r = Region { + local: l, + groups, + multi: None, + }; + check_dispatch(&m, LocalSizeForm::Flat, &[r], threads, l, sg); + } + } + } +} + /// `MultiRegion = 1`: `dispatchThreads` with partial edges on two axes, as its four regions, gives /// every thread Metal's position, partial threadgroup size, and grid sizes. #[test]