diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index e4f7d36dc..31053c0bf 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -4158,9 +4158,7 @@ cache is answered compile-required on both. refused (`Gap::PartialThreadgroups`) at a threadgroup that is not whole subgroups of a kernel that runs only whole subgroups, and for a kernel that observes its threadgroup partition and whose partial-threadgroups variant the translator refuses. It refuses the - variant of a kernel whose SIMD groups it emulates: one with SIMD-group operations where the - device's compute subgroups are neither 32 lanes nor 64 or 128 split into 32-lane clusters. It - refuses the variant of a kernel whose barriers in a + variant of a kernel whose barriers in a loop need a vote word that does not fit beside its threadgroup memory in `maxComputeSharedMemorySize`, which `Device::max_threadgroup_memory_length` leaves room for: a kernel that takes more threadgroup memory than that answer, as one built for a Mac's 32,768 diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index d3e847412..7fc90b857 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -2058,9 +2058,9 @@ pub enum PartialThreadgroupsSite { /// held against. max: u32, }, - /// The translator refused the variant for another of the kernel's structures: SIMD groups it - /// emulates, compute derivatives, an imageblock slice write every invocation shares, or - /// another refusal of the variant's lowering or compile. + /// The translator refused the variant for another of the kernel's structures: compute + /// derivatives, an imageblock slice write every invocation shares, or another refusal of the + /// variant's lowering or compile. Translator, /// The threadgroup is not whole SIMD groups of the variant, whose tier runs only whole ones. SimdGroups { diff --git a/crates/metal2vulkan/src/lower/dispatch.rs b/crates/metal2vulkan/src/lower/dispatch.rs index 86404b0ab..5e8f99724 100644 --- a/crates/metal2vulkan/src/lower/dispatch.rs +++ b/crates/metal2vulkan/src/lower/dispatch.rs @@ -829,7 +829,7 @@ pub fn emit_entry(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>) -> BResult< Ok(ArmEnd::FallThrough(Vec::new())) }); fb.if_(&mut (), ragged, test, None)?; - } else if let Some(live) = decl.live(fb, &mut region)? { + } else if let Some((live, threads)) = decl.live(fb, &mut region)? { let walked = cx .plan .families @@ -839,6 +839,7 @@ pub fn emit_entry(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>) -> BResult< .is_some_and(|p| p.partial_groups); if walked { cx.entry_live = Some(live); + cx.entry_threads = Some(threads); } else { let past = fb.unary(UnOp::LogicalNot, live)?; let leave: ArmFn<'_, ()> = Box::new(|fb, _| { @@ -1623,13 +1624,14 @@ impl KernelDecl { } /// Whether the invocation is one of its threadgroup's threads: `lin` below the threads a - /// partial object threadgroup runs ([`Self::partial_threads`]), every invocation elsewhere. - /// `None` but for an object function of [`DispatchPlan::partial_groups`]. + /// partial threadgroup runs ([`Self::partial_threads`]), every invocation elsewhere; and + /// those threads, `N` where the threadgroup is whole. `None` where + /// [`Self::partial_threads`] is. pub fn live( &self, fb: &mut FunctionBuilder<'_>, region: &mut RegionFn<'_>, - ) -> BResult> { + ) -> BResult> { let Some((partial, threads)) = self.partial_threads(fb, region)? else { return Ok(None); }; @@ -1637,7 +1639,7 @@ impl KernelDecl { let lin = self.lin(fb)?; let below = fb.binary(BinOp::ULessThan, lin, n)?; let yes = Self::flag(fb, true)?; - Ok(Some(fb.select(partial, below, yes)?)) + Ok(Some((fb.select(partial, below, yes)?, n))) } /// Of [`DispatchPlan::grid_edge`], the invocation's position in the grid (a `uvec3`). Under diff --git a/crates/metal2vulkan/src/lower/emit.rs b/crates/metal2vulkan/src/lower/emit.rs index 874f5a28a..ea0bc5e27 100644 --- a/crates/metal2vulkan/src/lower/emit.rs +++ b/crates/metal2vulkan/src/lower/emit.rs @@ -201,6 +201,11 @@ pub struct EmitCx<'a> { /// the invocation is one of its partial object threadgroup's threads (`dispatch::emit_entry`). /// `None`: every invocation runs the body. pub(crate) entry_live: Option, + /// With [`entry_live`](Self::entry_live): the threads of the invocation's threadgroup, the + /// count `entry_live` compares its linear index with (`N` where the threadgroup is whole). The + /// emulated simdgroup tier's lanes at or past it are outside the threadgroup + /// (`simd::workgroup::Emu`). + pub(crate) entry_threads: Option, /// The memory-plan slots a buffer store emitted so far merges a partial word into, through /// the sub-word store's two atomics, which read the word they modify. Whether a store merges /// depends on the leaf the lowering chose and on the units the slot's views hold on the @@ -294,6 +299,7 @@ impl<'a> EmitCx<'a> { live: None, active: None, entry_live: None, + entry_threads: None, merged: BTreeSet::new(), fault: None, sampling: Default::default(), @@ -984,6 +990,7 @@ impl<'a> Emitter<'a> { live: None, active: None, entry_live: None, + entry_threads: None, merged: BTreeSet::new(), fault: None, sampling: Default::default(), diff --git a/crates/metal2vulkan/src/lower/select.rs b/crates/metal2vulkan/src/lower/select.rs index 1109918f9..cccbc1b82 100644 --- a/crates/metal2vulkan/src/lower/select.rs +++ b/crates/metal2vulkan/src/lower/select.rs @@ -1736,12 +1736,6 @@ fn finish( return Err(Unsupported::not_implemented(Feature::GpuEncodedIndirectCommands).into()); } } - // A kernel's partial threadgroups walk their invocations past the grid on a subgroup tier - // (`simd::divergent::barriers`); the emulated tier's exchanges are not walked so, and its - // dispatches keep their exact split. - if cx.inputs.partial_threadgroups && stage == Stage::Kernel && tier == Tier::Emulated { - return Err(Unsupported::not_implemented(Feature::PartialThreadgroups).into()); - } if tier == Tier::Emulated { match simd::workgroup::check( &mut cx, diff --git a/crates/metal2vulkan/src/lower/simd/divergent.rs b/crates/metal2vulkan/src/lower/simd/divergent.rs index e72c122d9..69a63134b 100644 --- a/crates/metal2vulkan/src/lower/simd/divergent.rs +++ b/crates/metal2vulkan/src/lower/simd/divergent.rs @@ -48,11 +48,13 @@ //! threadgroup's threads (`dispatch::KernelDecl::live`, which answers every invocation of a //! threadgroup that is not partial), so the invocations past them walk to each barrier as //! invocations that returned at entry, executing nothing else of the body: no store, load or -//! atomic of theirs is made. Outside the emulated tier only the workgroup barriers are synchronizations -//! ([`SyncSet::WorkgroupBarriers`]): a subgroup operation runs where `live` holds, so it sees the -//! live invocations that execute it, its tangle in the source under maximal reconvergence. A loop -//! vote there uses a workgroup word of its own (`RuntimeFact::PredicationVote`), declared when a -//! synchronization lies in a loop. +//! atomic of theirs is made. A kernel of the emulated tier is walked so where it holds any of the +//! tier's synchronizations (`workgroup::check`), and its exchanges take the lanes past its +//! threadgroup's threads as outside it (`workgroup::Emu`). Outside the emulated tier only the +//! workgroup barriers are synchronizations ([`SyncSet::WorkgroupBarriers`]): a subgroup operation +//! runs where `live` holds, so it sees the live invocations that execute it, its tangle in the +//! source under maximal reconvergence. A loop vote there uses a workgroup word of its own +//! (`RuntimeFact::PredicationVote`), declared when a synchronization lies in a loop. use std::collections::BTreeMap; diff --git a/crates/metal2vulkan/src/lower/simd/workgroup.rs b/crates/metal2vulkan/src/lower/simd/workgroup.rs index 83a9fd3e8..af55c27ce 100644 --- a/crates/metal2vulkan/src/lower/simd/workgroup.rs +++ b/crates/metal2vulkan/src/lower/simd/workgroup.rs @@ -22,7 +22,9 @@ //! workgroup-uniform control flow. In a block [`super::uniform`] proves uniform every thread of //! the threadgroup executes the operation, so the active lanes of a simdgroup are exactly its //! lanes below `N` (the last simdgroup of a threadgroup whose size is not a multiple of 32 is -//! partially occupied, as in Metal). A kernel with an operation in any other block is emitted +//! partially occupied, as in Metal; in a kernel's partial threadgroup, `N` is the threads Metal +//! runs in it, and the invocations past them walk to each exchange never live, as +//! `super::divergent` walks them). A kernel with an operation in any other block is emitted //! predicated ([`super::divergent`]): every invocation executes the operation, and the invocations //! that do not execute it in the source publish an inactive flag beside their words //! (`EmitCx::active`), so the active lanes are the lanes below `N` whose flag is set; a read of a @@ -208,7 +210,10 @@ pub fn arena_bytes(arena: Option, regions: u64) -> u64 { /// scratch must fit `maxComputeSharedMemorySize` (else `ThreadgroupMemorySize`). A function that /// exchanges values sets the plan's exchange width and depends on /// `RuntimeFact::SimdgroupEmulation`. An entry whose only tangled instructions are emulated sites -/// and threadgroup barriers the lockstep pass placed keeps no reconvergence mode. `given` is the +/// and threadgroup barriers the lockstep pass placed keeps no reconvergence mode. A kernel of +/// partial threadgroups with any of the tier's synchronizations is planned predicated too, its +/// walk starting from Metal's threads (`Predication::partial_groups`), whose count is the `N` of +/// its exchanges ([`Emu`]). `given` is the /// body's uniformity when the caller already holds it (`simd::partly_live`, which keeps its ledger /// bytes); otherwise it is computed and released here. /// @@ -238,16 +243,25 @@ pub fn check( &computed } }; + // A kernel's partial threadgroups (`LoweringInputs::partial_threadgroups`) walk their + // invocations past Metal's threads to each synchronization, as on the subgroup tiers + // (`divergent::barriers`): they take part in the exchanges, never live. + let partial = cx.stage == Stage::Kernel + && cx.families.mesh.partial_groups + && strategy.iter().flatten().any(divergent::is_sync); // The guards' demotions join `cross` only once nothing refuses. - let planned = match divergent_site(cx, strategy, u) { - Some(_) => divergent::plan(cx, strategy, u, divergent::SyncSet::Emulated, m).map(Some), - None => Ok(None), + let planned = match (divergent_site(cx, strategy, u), partial) { + (None, false) => Ok(None), + _ => divergent::plan(cx, strategy, u, divergent::SyncSet::Emulated, m).map(Some), }; if given.is_none() { m.release(u.ledger_bytes()); } let (predication, demoted) = match planned? { - Some((p, extra)) => (Some(p), Some(extra)), + Some((mut p, extra)) => { + p.partial_groups = partial; + (Some(p), Some(extra)) + } None => (None, None), }; // An emulated site's result does not depend on subgroup reconvergence, nor does a threadgroup @@ -431,7 +445,9 @@ pub fn declare( /// The emulated tier's exchange state in the open block. #[derive(Clone, Copy, Debug)] pub struct Emu { - /// The threadgroup's threads. + /// The threadgroup's threads: `N`, or, in a partial threadgroup's walk, the threads Metal runs + /// in it (`EmitCx::entry_threads`), so the lanes past them are outside it as the lanes past + /// `N` are. n: SValue, scratch: Option, /// Words per lane. @@ -458,7 +474,10 @@ pub(super) fn emu(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>) -> BResult< let Some(d) = cx.decl.families.simd.emu else { return Err(cx.fail("the emulated simdgroup tier is not declared")); }; - let n = fb.module().const_value(d.threads)?; + let n = match cx.entry_threads { + Some(threads) => threads, + None => fb.module().const_value(d.threads)?, + }; let several = match d.several { Some(c) => Some(fb.module().const_value(c)?), None => None, diff --git a/crates/metal2vulkan/src/pipeline/lower_inputs.rs b/crates/metal2vulkan/src/pipeline/lower_inputs.rs index e8b1e95b2..b97df63d0 100644 --- a/crates/metal2vulkan/src/pipeline/lower_inputs.rs +++ b/crates/metal2vulkan/src/pipeline/lower_inputs.rs @@ -396,12 +396,13 @@ pub struct LoweringInputs { /// threads' positions, and its invocations past them run none of the body (they return before /// it, or, in a body that holds a workgroup barrier, walk it predicated to each barrier), so /// its simdgroup operations run on the live lanes, Metal's partial simdgroup. Every lane read - /// ballots its live lanes (`simd::partly_live`). It declares `MultiRegion` and reads the + /// ballots its live lanes (`simd::partly_live`). On emulated simdgroups (the input + /// `emulated_simdgroups`, a kernel whose lockstep order needs them, or the profile's tier) the + /// invocations past Metal's threads walk to each exchange too, and the exchanges take Metal's + /// threads as the threadgroup's (`simd::workgroup`). It declares `MultiRegion` and reads the /// region whatever builtins it reads, and states `RuntimeFact::PartialThreadgroups`. A tile - /// function, 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` and `replay_bindings` are `Unsupported{PartialThreadgroups, + /// function, compute derivatives, an imageblock slice write every invocation shares, + /// `flat_grid`, `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, diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index b5a3f4a07..5d5517f2e 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -68,7 +68,7 @@ Columns: | `Linking` | Linkage signatures / stitching / dynamic libraries | every device (not implemented) | `ours` | 3: `crates/metal2vulkan-air/src/canon/link.rs (not_implemented)`, `crates/metal2vulkan-air/src/canon/mod.rs (post_check)` (+1) | `rare`: dynamic libraries and linked functions | | `SimdWidth` | Simdgroup width 32 is not achievable for this usage, stage, and device | devices without a capability the site computes (`subgroup::width_needs(cx.stage, model.stage(cx.stage))`); devices without a capability the site computes (`width_needs(stage, st)`) | `device` | 2: `crates/metal2vulkan/src/device/subgroup.rs (subgroup_tier)`, `crates/metal2vulkan/src/lower/simd/mod.rs (finish)` | `app`: simdgroup functions on a device whose subgroups cannot run 32 wide for the use | | `LocalSize` | A local size beyond a device limit on this axis (`None` | - | `-` | 0 | `app`: a threadgroup size past a smaller device's limit | -| `PartialThreadgroups` | A kernel's partial-threadgroups variant (`LoweringInputs::partial_threadgroups`) that the lowering cannot make | devices without a capability the site computes (`limit`); every device (not implemented) | `ours` | 4: `crates/metal2vulkan/src/lower/mesh/mod.rs (plan_partial_groups)`, `crates/metal2vulkan/src/lower/select.rs (finish)` (+2) | `app`: a dispatchThreads grid that is not whole threadgroups, of a kernel whose partial-threadgroups variant cannot be made | +| `PartialThreadgroups` | A kernel's partial-threadgroups variant (`LoweringInputs::partial_threadgroups`) that the lowering cannot make | devices without a capability the site computes (`limit`); every device (not implemented) | `ours` | 3: `crates/metal2vulkan/src/lower/mesh/mod.rs (plan_partial_groups)`, `crates/metal2vulkan/src/lower/simd/divergent.rs (barriers)` (+1) | `app`: a dispatchThreads grid that is not whole threadgroups, of a kernel whose partial-threadgroups variant cannot be made | | `IrreducibleConvergent` | An irreducible region containing a convergent instruction | every device (no Vulkan mechanism) | `device` | 1: `crates/metal2vulkan/src/structurize/irreducible.rs (refuse_tangled)` | `rare`: irreducible loops around barriers or simd operations | | `ComputeDerivativesWithSimd` | Compute derivatives in a flattened simd tier | every device (no Vulkan mechanism) | `device` | 1: `crates/metal2vulkan/src/lower/dispatch.rs (plan_kernel)` | `rare`: derivatives in a kernel | | `ComputeDerivatives` | Derivatives outside fragment without compute-derivative support | devices without VkFeature::ComputeDerivativeGroupLinear; every device (no Vulkan mechanism); every device (not implemented) | `ours` | 3: `crates/metal2vulkan/src/lower/sync/barrier.rs (select_derivative, 3 sites)` | `rare`: derivatives in a kernel | @@ -396,7 +396,7 @@ Each refusal's sites, with the function that builds it. - `feature::InlineAsm`: `crates/metal2vulkan-air/src/canon/mod.rs (post_check)` - `feature::Linking`: `crates/metal2vulkan-air/src/canon/link.rs (not_implemented)`; `crates/metal2vulkan-air/src/canon/mod.rs (post_check)`; `crates/metal2vulkan-air/src/canon/mod.rs (specialize_dynamic)` - `feature::SimdWidth`: `crates/metal2vulkan/src/device/subgroup.rs (subgroup_tier)`; `crates/metal2vulkan/src/lower/simd/mod.rs (finish)` -- `feature::PartialThreadgroups`: `crates/metal2vulkan/src/lower/mesh/mod.rs (plan_partial_groups)`; `crates/metal2vulkan/src/lower/select.rs (finish)`; `crates/metal2vulkan/src/lower/simd/divergent.rs (barriers)`; `crates/metal2vulkan/src/pipeline/compile.rs (analyze_one)` +- `feature::PartialThreadgroups`: `crates/metal2vulkan/src/lower/mesh/mod.rs (plan_partial_groups)`; `crates/metal2vulkan/src/lower/simd/divergent.rs (barriers)`; `crates/metal2vulkan/src/pipeline/compile.rs (analyze_one)` - `feature::IrreducibleConvergent`: `crates/metal2vulkan/src/structurize/irreducible.rs (refuse_tangled)` - `feature::ComputeDerivativesWithSimd`: `crates/metal2vulkan/src/lower/dispatch.rs (plan_kernel)` - `feature::ComputeDerivatives`: `crates/metal2vulkan/src/lower/sync/barrier.rs (select_derivative, 3 sites)`