diff --git a/crates/metal2vulkan/src/lower/simd/divergent.rs b/crates/metal2vulkan/src/lower/simd/divergent.rs index 5aa78eb74..33648c94c 100644 --- a/crates/metal2vulkan/src/lower/simd/divergent.rs +++ b/crates/metal2vulkan/src/lower/simd/divergent.rs @@ -259,7 +259,8 @@ pub const VOTE_BYTES: u32 = 4; /// barriers are uniform and whose word does not fit runs no partial threadgroup instead /// (`MeshPlan::partial_groups` cleared, so the binder refuses such a draw), and a kernel's /// partial-threadgroups variant (`LoweringInputs::partial_threadgroups`) is refused, -/// `Unsupported{PartialThreadgroups, NotImplemented}`. +/// `Unsupported{PartialThreadgroups}` needing the `Limit` of `maxComputeSharedMemorySize` its +/// arena and word pass. pub fn barriers( cx: &mut SelectCx<'_>, strategy: &[Option], @@ -330,11 +331,15 @@ pub fn barriers( match crate::lower::mesh::workgroup_limit(cx).check(required) { Ok(()) => {} // Uniform barriers need the walk only for partial threadgroups, which then stay - // refused: an object function's draw by the binder, a kernel's variant here, so its - // dispatches keep their exact split. - Err(Error::Unsupported(_)) if !divergent => { + // refused: an object function's draw by the binder, a kernel's variant here, naming + // the limit its arena and vote word pass. + Err(Error::Unsupported(limit)) if !divergent => { if cx.stage == Stage::Kernel { - return Err(Unsupported::not_implemented(Feature::PartialThreadgroups).into()); + return Err(Unsupported { + feature: Feature::PartialThreadgroups, + ..limit + } + .into()); } cx.families.mesh.partial_groups = false; return Ok(()); diff --git a/crates/metal2vulkan/src/pipeline/lower_inputs.rs b/crates/metal2vulkan/src/pipeline/lower_inputs.rs index 6967d7a39..6db83eab6 100644 --- a/crates/metal2vulkan/src/pipeline/lower_inputs.rs +++ b/crates/metal2vulkan/src/pipeline/lower_inputs.rs @@ -396,9 +396,10 @@ pub struct LoweringInputs { /// function, a stage-input region, compute derivatives, an imageblock slice write every /// invocation shares, emulated simdgroups (the input /// `emulated_simdgroups` or a kernel whose lockstep order needs them), `flat_grid`, - /// `folded_groups`, `replay_bindings`, and a body whose uniform barriers' vote word its - /// workgroup memory cannot hold, are `Unsupported{PartialThreadgroups, NotImplemented}`: their - /// dispatches keep the exact split. Any other function compiles as without it. + /// `folded_groups` and `replay_bindings` are `Unsupported{PartialThreadgroups, + /// NotImplemented}`, and a body whose uniform barriers' vote word its workgroup memory cannot + /// hold is `Unsupported{PartialThreadgroups}` needing the `Limit` of + /// `maxComputeSharedMemorySize` the word passes. Any other function compiles as without it. #[cfg_attr(feature = "serde", serde(default))] pub partial_threadgroups: bool, /// Texture slots bound in one of Apple's extended-range formats diff --git a/crates/metal2vulkan/src/tests/engine_partial_threadgroups.rs b/crates/metal2vulkan/src/tests/engine_partial_threadgroups.rs index 758834381..1a890eac0 100644 --- a/crates/metal2vulkan/src/tests/engine_partial_threadgroups.rs +++ b/crates/metal2vulkan/src/tests/engine_partial_threadgroups.rs @@ -146,3 +146,85 @@ fn partial_threadgroups_are_refused_with_another_form() { } } } + +/// `scan`: an inclusive scan of the threadgroup's threads over a declared array of `words` words: +/// thread `t` stores `t + 1` at word `t`, then for each stride `s` of 1, 2, 4 and on below +/// `threads_per_threadgroup`, past a barrier adds word `t - s` where `t >= s`, and past another +/// stores its sum. The loop holds the barriers, so the variant's predicated walk votes in it. +fn scan(words: u32) -> String { + format!( + "{HEADER}@tg = internal addrspace(3) global [{words} x i32] undef, align 4\n\n\ +define void @scan(ptr addrspace(1) noundef %out, i32 noundef %t, i32 noundef %g, i32 noundef %n) {{\n\ +entry:\n\ + %pt = getelementptr inbounds i32, ptr addrspace(3) @tg, i32 %t\n\ + %v0 = add i32 %t, 1\n\ + store i32 %v0, ptr addrspace(3) %pt, align 4\n\ + br label %head\n\ +head:\n\ + %s = phi i32 [ 1, %entry ], [ %s2, %body ]\n\ + %acc = phi i32 [ %v0, %entry ], [ %acc2, %body ]\n\ + %more = icmp ult i32 %s, %n\n\ + br i1 %more, label %body, label %done\n\ +body:\n\ + tail call void @air.wg.barrier(i32 2, i32 1) #0\n\ + %has = icmp uge i32 %t, %s\n\ + %back = sub i32 %t, %s\n\ + %at = select i1 %has, i32 %back, i32 %t\n\ + %pa = getelementptr inbounds i32, ptr addrspace(3) @tg, i32 %at\n\ + %x = load i32, ptr addrspace(3) %pa, align 4\n\ + %add = select i1 %has, i32 %x, i32 0\n\ + %acc2 = add i32 %acc, %add\n\ + tail call void @air.wg.barrier(i32 2, i32 1) #0\n\ + store i32 %acc2, ptr addrspace(3) %pt, align 4\n\ + %s2 = shl i32 %s, 1\n\ + br label %head\n\ +done:\n\ + %o = getelementptr inbounds i32, ptr addrspace(1) %out, i32 %g\n\ + store i32 %acc, ptr addrspace(1) %o, align 4\n\ + ret void\n\ +}}\n\n\ +declare void @air.wg.barrier(i32, i32) #0\n\n\ +attributes #0 = {{ convergent mustprogress nounwind willreturn }}\n\n\ +!air.kernel = !{{!0}}\n!air.version = !{{!7}}\n!air.language_version = !{{!8}}\n\ +!0 = !{{ptr @scan, !1, !2}}\n!1 = !{{}}\n!2 = !{{!3, !4, !5, !6}}\n\ +!3 = !{{i32 0, !\"air.buffer\", !\"air.location_index\", i32 0, i32 1, !\"air.read_write\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 4, !\"air.arg_type_align_size\", i32 4, !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"out\"}}\n\ +!4 = !{{i32 1, !\"air.thread_index_in_threadgroup\", !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"t\"}}\n\ +!5 = !{{i32 2, !\"air.thread_position_in_grid\", !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"g\"}}\n\ +!6 = !{{i32 3, !\"air.threads_per_threadgroup\", !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"n\"}}\n\ +!7 = !{{i32 2, i32 8, i32 0}}\n!8 = !{{!\"Metal\", i32 4, i32 0, i32 0}}\n" + ) +} + +/// The variant of a kernel whose barriers in a loop take the vote word beside its threadgroup +/// memory is refused where the two pass the device's `maxComputeSharedMemorySize` (32,768 bytes +/// on the MoltenVK M3 profile), naming that limit and the bytes, Metal's own 32 KiB and the word; +/// with 16 bytes less memory it is made. +#[test] +fn a_vote_word_past_the_shared_memory_limit_refuses_the_variant_naming_the_limit() { + use crate::device::VkLimit; + use crate::error::{Capability, Needs}; + let dev = device("moltenvk-m3", |_| {}); + let mut inputs = LoweringInputs::new(); + inputs.partial_threadgroups = true; + let Some(full) = assemble_text("scan_full", &scan(8192)) else { + return; + }; + match pipeline(&dev, &full, "scan", &inputs) { + Err(Error::Unsupported(u)) => { + assert_eq!(u.feature, Feature::PartialThreadgroups); + let limit = Capability::Limit { + limit: VkLimit::MaxComputeSharedMemorySize, + required: 32_772, + available: 32_768, + }; + assert_eq!(u.needs, Needs::AnyOf(vec![vec![limit]])); + } + other => panic!("32,768 bytes: {:?}", other.map(|_| ())), + } + let Some(less) = assemble_text("scan_less", &scan(8188)) else { + return; + }; + if let Err(error) = pipeline(&dev, &less, "scan", &inputs) { + panic!("32,752 bytes: {error:?}"); + } +}