translator: a flattened kernel takes its pinned subgroup size as a constant, never SubgroupSize

In the flattened tiers (Direct and Cluster32) a kernel's linear local index is
lin = SubgroupId * S + SubgroupLocalInvocationId, and S was the SubgroupSize builtin. If a driver
answers that builtin wrongly, lin is wrong, and so are the positions of every simdgroup in a
threadgroup larger than one subgroup. On the UHD 630's Windows driver, a SPIR-V 1.3 compute
module (glslang) read SubgroupSize as 0 under a required size of 32 with full subgroups, while its
SubgroupId and lanes were right. rt's own SPIR-V 1.5 module for the harness's reduce_sum read it
right on the same GPU: simd_reduce matched the CPU reference.

Where the tier's requirement pins a size (SubgroupRequirement::size: 32 for Direct, the cluster
size for Cluster32, on a stage in requiredSubgroupSizeStages), the pipeline is created with that
required size, so the subgroups run at it. lin now multiplies by it as a constant, and the module
declares no SubgroupSize builtin. The same holds on every device, with no query and no device
identity. Where the stage pins no size, lin still reads the builtin, as before.

The constant is a literal from the same requirement that sets the pipeline's required size, not a
specialization constant: the size is fixed when the module is lowered for the profile, and no
pipeline of the module runs at another size.

SEMANTICS_VERSION 2170: flattened modules change.

Test: lower::dispatch::tests::a_pinned_subgroup_size_reads_no_subgroup_size_builtin. With a
pinned size of 32 or 64, the serialized module holds no OpDecorate BuiltIn SubgroupSize, and every
kernel builtin keeps Metal's value on the CPU interpreter at that subgroup width. Unpinned, the
module still declares the builtin. Without the change, the pinned module declares it and the test
fails.
This commit is contained in:
Anees Iqbal committed 2026-11-17 20:45:50 -08:00
1 parent 5a8d7521f1
commit 48833908aa
2 files changed
+75 -4

No files matched your search

+26 -4
View File
@@ -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<IdentityRegion>,
/// 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<u32>,
}
/// 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<DispatchPlan, Er
cx.facts.insert(RuntimeFact::ThreadsPerThreadgroup);
cx.facts.insert(RuntimeFact::LocalSizeLimits);
cx.facts.insert(RuntimeFact::IndirectGroupCount);
let mut pinned_subgroup_size = None;
if local_size == LocalSizeForm::Flat {
let req = subgroup::requirement(tier, cx.stage, &cx.profile.fields().subgroup);
pinned_subgroup_size = req.size;
cx.facts.insert(RuntimeFact::LocalSizeConstraint {
multiple: req.local_size_multiple,
});
@@ -734,6 +742,7 @@ pub fn plan_kernel(cx: &mut SelectCx<'_>, tier: Tier) -> Result<DispatchPlan, Er
grid_edge,
identity_region: crate::lower::access::identity(cx.profile)
.then(|| cx.profile.identity_region()),
pinned_subgroup_size,
})
}
@@ -974,8 +983,11 @@ pub struct KernelDecl {
workgroup_id: Option<GlobalId>,
num_workgroups: Option<GlobalId>,
subgroup_id: Option<GlobalId>,
/// `SubgroupSize`, declared only where the stage pins no size ([`Self::pinned_subgroup_size`]).
subgroup_size: Option<GlobalId>,
subgroup_local: Option<GlobalId>,
/// The subgroup size the pipeline pins ([`DispatchPlan::pinned_subgroup_size`]).
pinned_subgroup_size: Option<u32>,
/// 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<WrapperGroups>,
@@ -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)
@@ -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]