From 5c1a4aabe6be387fd8a49086391d611a45b3d00a Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 23 Nov 2026 20:43:18 -0800 Subject: [PATCH] Translator: specialize gives a LocalSize mode beside the WorkgroupSize composite the composite's values The pass refused any module that had a LocalSize execution mode beside its WorkgroupSize composite. Such a module is valid: the WorkgroupSize built-in "takes precedence over any LocalSize or LocalSizeId execution mode" (vkspec, the WorkgroupSize built-in), and glslang writes both when a shader uses local_size_x_id. rt's hand-made test kernels (tests/kernels.rs) are glslang output of that shape. In check job m3-max-72835 the pass refused every one of them, and 38 of rt's 714 lavapipe device tests failed with "specialize: a WorkgroupSize ... beside a local-size mode". kernel_variants and the compute-encoder tests were among them. The one LocalSize mode now takes the composite's values, which is what the precedence rule already makes the module run with. A module without a mode still gets one after its entry point. A second LocalSize mode, or a LocalSize mode of other than three sizes, is refused by name. The format fix the check's fmt step asked for is in the same file. Test: a_local_size_mode_beside_the_workgroup_composite_takes_its_values adds a LocalSize 1 1 1 mode to the deep kernel. Specialized at 8x4x2, the module must hold exactly one mode, LocalSize 8 4 2, and pass spirv-val under Vulkan 1.2 and 1.3. A copy with two modes must be refused by name. The next check job runs it, with rt's lavapipe device suite. --- .../src/spirv/serialize/tests/specialize.rs | 53 ++++++++++++++++++- crates/metal2vulkan/src/spirv/specialize.rs | 52 +++++++++++------- 2 files changed, 83 insertions(+), 22 deletions(-) diff --git a/crates/metal2vulkan/src/spirv/serialize/tests/specialize.rs b/crates/metal2vulkan/src/spirv/serialize/tests/specialize.rs index 511b667d5..608c64584 100644 --- a/crates/metal2vulkan/src/spirv/serialize/tests/specialize.rs +++ b/crates/metal2vulkan/src/spirv/serialize/tests/specialize.rs @@ -274,8 +274,14 @@ fn a_module_of_an_instruction_the_pass_does_not_specialize_is_refused() { } subtract[at + 3] = Op::ISub as u32; for (module, named) in [ - (inserted(&words, Op::EntryPoint, &mode_id, 0), "OpExecutionModeId"), - (inserted(&words, Op::Decorate, &group, 1), "OpDecorationGroup"), + ( + inserted(&words, Op::EntryPoint, &mode_id, 0), + "OpExecutionModeId", + ), + ( + inserted(&words, Op::Decorate, &group, 1), + "OpDecorationGroup", + ), (subtract, "OpSpecConstantOp of opcode 130"), ] { match specialize(&module, &[]) { @@ -286,3 +292,46 @@ fn a_module_of_an_instruction_the_pass_does_not_specialize_is_refused() { } } } + +/// A module with a `LocalSize` mode beside its `WorkgroupSize` composite, as `glslang` writes one +/// from `local_size_x_id`: the composite takes precedence, so the one mode takes the composite's +/// values, and `spirv-val` accepts the words. A second mode is refused. +#[test] +fn a_local_size_mode_beside_the_workgroup_composite_takes_its_values() { + let (words, ids, opts) = linked(deep_kernel()); + let entry = (insts(&words).into_iter()) + .find(|(op, _)| *op == Op::EntryPoint as u32) + .map(|(_, operands)| operands[1]) + .expect("entry point"); + let mode = [ + (6 << 16) | Op::ExecutionMode as u32, + entry, + ExecutionMode::LocalSize as u32, + 1, + 1, + 1, + ]; + let both = inserted(&words, Op::EntryPoint, &mode, 0); + assert_valid(&both, opts); + let value = |key: SpecKey| match key { + SpecKey::WorkgroupSize(Axis::X) => 8, + SpecKey::WorkgroupSize(Axis::Y) => 4, + SpecKey::WorkgroupSize(Axis::Z) => 2, + _ => 1, + }; + let values: Vec<(u32, u64)> = ids.iter().map(|&(key, id)| (id, value(key))).collect(); + let frozen = specialize(&both, &values).expect("specialize"); + assert_valid(&frozen, opts); + let modes: Vec> = (insts(&frozen).into_iter()) + .filter(|(op, _)| *op == Op::ExecutionMode as u32) + .map(|(_, operands)| operands) + .collect(); + assert_eq!(modes, [vec![entry, ExecutionMode::LocalSize as u32, 8, 4, 2]]); + let twice = inserted(&both, Op::EntryPoint, &mode, 0); + match specialize(&twice, &values) { + Err(crate::error::Error::Internal(error)) => { + assert!(error.detail.contains("more than one LocalSize"), "{error}"); + } + other => panic!("two modes: {:?}", other.map(|w| w.len())), + } +} diff --git a/crates/metal2vulkan/src/spirv/specialize.rs b/crates/metal2vulkan/src/spirv/specialize.rs index b400f4ceb..87f237edc 100644 --- a/crates/metal2vulkan/src/spirv/specialize.rs +++ b/crates/metal2vulkan/src/spirv/specialize.rs @@ -53,14 +53,17 @@ const READ: [Op; 17] = [ /// `OpSpecConstantComposite` an `OpConstantComposite`; the `SpecId` decorations go. Where the /// module declares a `WorkgroupSize` composite, its `BuiltIn` decoration goes too and the entry /// point takes the composite's values as its `LocalSize` execution mode, as a compute shader of a -/// literal workgroup size states it. The result is valid where `words` is, under the same -/// requirements, and runs as `words` specialized by `values` does. +/// literal workgroup size states it: a `LocalSize` mode the module has takes them, as the +/// composite "takes precedence over any LocalSize or LocalSizeId execution mode" (the +/// `WorkgroupSize` built-in's rule; a module `glslang` writes from `local_size_x_id` has both). +/// The result is valid where `words` is, under the same requirements, and runs as `words` +/// specialized by `values` does. /// /// Err: [`Error::Internal`], naming what it met, where the words are not such a module: a /// malformed instruction; an instruction the translator does not write, whose constants the pass /// would leave half specialized (an `OpExecutionModeId`, a `LocalSizeId` mode among them; an -/// `OpDecorationGroup`; an `OpSpecConstantOp` of another opcode); or a `WorkgroupSize` beside a -/// `LocalSize` mode or beside other than one entry point. +/// `OpDecorationGroup`; an `OpSpecConstantOp` of another opcode); or a `WorkgroupSize` beside +/// more than one `LocalSize` mode or beside other than one entry point. pub fn specialize(words: &[u32], values: &[(u32, u64)]) -> Result, Error> { let header = words .get(..5) @@ -72,7 +75,8 @@ pub fn specialize(words: &[u32], values: &[(u32, u64)]) -> Result, Erro let mut spec_ids = BTreeMap::new(); let mut workgroup = None; let mut entries = Vec::new(); - let mut local_size = false; + // Where each `LocalSize` mode starts in `out`. + let mut modes = Vec::new(); let mut types = BTreeMap::new(); // Each scalar constant's value and type, by result id. let mut known: BTreeMap = BTreeMap::new(); @@ -115,8 +119,11 @@ pub fn specialize(words: &[u32], values: &[(u32, u64)]) -> Result, Erro entries.push((operand(2)?, out.len())); continue; } - Some(Op::ExecutionMode) => { - local_size |= operand(2)? == ExecutionMode::LocalSize as u32; + Some(Op::ExecutionMode) if operand(2)? == ExecutionMode::LocalSize as u32 => { + if count != 6 { + return Err(malformed("a LocalSize mode of other than three sizes")); + } + modes.push(out.len()); } Some(Op::TypeBool) => { types.insert(operand(1)?, Scalar::Bool); @@ -204,22 +211,27 @@ pub fn specialize(words: &[u32], values: &[(u32, u64)]) -> Result, Erro None => Ok(out), }; }; - let ([(entry, at)], false, [x, y, z]) = (entries.as_slice(), local_size, sizes.as_slice()) - else { + let ([(entry, at)], [x, y, z]) = (entries.as_slice(), sizes.as_slice()) else { return Err(malformed( - "a WorkgroupSize of other than three parts, beside a local-size mode, or beside other \ - than one entry point", + "a WorkgroupSize of other than three parts, or beside other than one entry point", )); }; - let mode = [ - (6 << 16) | Op::ExecutionMode as u32, - *entry, - ExecutionMode::LocalSize as u32, - *x, - *y, - *z, - ]; - out.splice(*at..*at, mode); + match modes.as_slice() { + // The composite takes precedence over the mode, which takes its values. + [mode] => out[mode + 3..mode + 6].copy_from_slice(&[*x, *y, *z]), + [] => { + let mode = [ + (6 << 16) | Op::ExecutionMode as u32, + *entry, + ExecutionMode::LocalSize as u32, + *x, + *y, + *z, + ]; + out.splice(*at..*at, mode); + } + _ => return Err(malformed("a WorkgroupSize beside more than one LocalSize mode")), + } Ok(out) }