diff --git a/crates/metal2vulkan-conformance/src/run.rs b/crates/metal2vulkan-conformance/src/run.rs index 4ad9ad383..d76c1d62e 100644 --- a/crates/metal2vulkan-conformance/src/run.rs +++ b/crates/metal2vulkan-conformance/src/run.rs @@ -41,7 +41,9 @@ pub fn selected(names: &[String]) -> Result>, String> { /// /// A case that made exported surfaces prints how many of each form it made /// (`Metrics::surfaces_in_place`, `Metrics::surfaces_copied`), which is the evidence of the form a -/// venue's surfaces took: nothing in a verdict reads it. +/// venue's surfaces took, and a case whose whole grids ran an observing kernel's +/// partial-threadgroups variant prints how many (`Metrics::partial_variant_whole_grids`): nothing +/// in a verdict reads either. pub fn replay_rt_on_under( venues: Venues, cases: &[Box], @@ -66,6 +68,14 @@ pub fn replay_rt_on_under( device.venue().device ); } + let whole = after.partial_variant_whole_grids - before.partial_variant_whole_grids; + if whole > 0 { + println!( + "dispatch forms: {} on {}: {whole} whole grids on the partial-threadgroups variant", + case.name(), + device.venue().device + ); + } } report }; diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 88f35bc66..d974c4052 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -2566,10 +2566,13 @@ while the driver's exit handler waited on a lock, with every runtime thread alre a thread compare its position with the grid's last thread, inside one branch on the flag. Every other dispatch, on the same pipeline, branches past that test. An indirect dispatch runs at its own threadgroup size, unflagged. A kernel that observes its partition is - lowered plainly: its whole grids, `dispatch_threadgroups` and indirect dispatches run that - module at their own size, and a `dispatch_threads` grid that is not a whole number of - threadgroups runs its partial-threadgroups variant, which the state's creation lowers too, - through the cache directory. The translator gives a kernel no order of the + lowered plainly and as its partial-threadgroups variant, which the state's creation lowers too, + through the cache directory: every direct dispatch, a whole `dispatch_threads` grid, + `dispatch_threadgroups` or a grid that is not a whole number of threadgroups, runs the variant at + its own size, a whole grid's regions unflagged, so one `VkPipeline` of a size runs them all + (`Metrics::partial_variant_whole_grids` counts the whole grids). Its indirect dispatches run the + plain module, and so does a direct dispatch where the variant is refused, or where the + threadgroup is not whole SIMD groups of a kernel that runs only whole ones (below). The translator gives a kernel no order of the accesses Apple's SIMD-group lockstep orders (docs/API.md, accepted differences), so only the kernel's own barriers, fences, atomics of threadgroup or SIMD-group scope, SIMD-group and quad functions, threadgroup memory and builtins of the partition observe it. @@ -2584,8 +2587,8 @@ while the driver's exit handler waited on a lock, with every runtime thread alre is the same with it or without: for its direct dispatches, one for the tiles of a kernel that runs its grid in tiles, 46 for an image-only one (the square one and one per tile, three of whose tiles are deeper than 64), and for one that observes its partition one per threadgroup - size and threadgroup memory lengths, and one more per size for grids that are not whole - threadgroups; for any kernel's indirect dispatches, one per size and lengths. + size and threadgroup memory lengths; for any kernel's indirect dispatches, one per size and + lengths. - A kernel whose SIMD groups run one per subgroup of 32 (pinned where the device lists compute in `requiredSubgroupSizeStages` with 32 between its sizes, or the device's own 32), or one per 32-lane cluster of a 64- or 128-lane subgroup, runs only workgroups of whole subgroups. @@ -3054,8 +3057,8 @@ cache is answered compile-required on both. of one stream and 312 of the other). The 50th, of tile 1x1x256, runs as one row, so a device that holds 64 makes the same 50. - A kernel's variants are lowered with its state, whether or not a dispatch needs them: an - observing kernel's partial-threadgroups variant, which only a `dispatch_threads` grid that is - not a whole number of threadgroups runs, and the emulated-SIMD-group variant of a kernel whose + observing kernel's partial-threadgroups variant, which its direct dispatches run, and the + emulated-SIMD-group variant of a kernel whose tier runs only whole SIMD groups, which only a threadgroup that is not whole subgroups runs. So a state's creation lowers up to three functions where Metal compiles one, each through the cache directory. For the observing kernel the device tests run, a grid template over diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index 59760b230..0c9d0c67f 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -214,6 +214,8 @@ pub(crate) enum Metric { PrivateArenaDispatches, /// [`Metrics::emulated_simdgroup_dispatches`]. EmulatedSimdgroupDispatches, + /// [`Metrics::partial_variant_whole_grids`]. + PartialVariantWholeGrids, /// [`Metrics::private_arena_pieces`]. PrivateArenaPieces, /// [`Metrics::private_arena_blocks`]. @@ -305,6 +307,7 @@ const METRIC_ORDER: [Metric; METRICS] = [ Metric::DispatchesWithoutEffects, Metric::PrivateArenaDispatches, Metric::EmulatedSimdgroupDispatches, + Metric::PartialVariantWholeGrids, Metric::PrivateArenaPieces, Metric::PrivateArenaBlocks, Metric::Translations, @@ -652,6 +655,7 @@ struct Atomics { dispatches_without_effects: AtomicU64, private_arena_dispatches: AtomicU64, emulated_simdgroup_dispatches: AtomicU64, + partial_variant_whole_grids: AtomicU64, private_arena_pieces: AtomicU64, private_arena_blocks: AtomicU64, shared_long_reuses: AtomicU64, @@ -849,6 +853,7 @@ impl Counters { Metric::DispatchesWithoutEffects => &a.dispatches_without_effects, Metric::PrivateArenaDispatches => &a.private_arena_dispatches, Metric::EmulatedSimdgroupDispatches => &a.emulated_simdgroup_dispatches, + Metric::PartialVariantWholeGrids => &a.partial_variant_whole_grids, Metric::PrivateArenaPieces => &a.private_arena_pieces, Metric::PrivateArenaBlocks => &a.private_arena_blocks, Metric::Translations => &a.translations, @@ -1680,6 +1685,7 @@ impl Counters { dispatches_without_effects: a.dispatches_without_effects.load(Relaxed), private_arena_dispatches: a.private_arena_dispatches.load(Relaxed), emulated_simdgroup_dispatches: a.emulated_simdgroup_dispatches.load(Relaxed), + partial_variant_whole_grids: a.partial_variant_whole_grids.load(Relaxed), private_arena_pieces: a.private_arena_pieces.load(Relaxed), private_arena_blocks: a.private_arena_blocks.load(Relaxed), translations: a.translations.load(Relaxed), @@ -2253,6 +2259,11 @@ pub struct Metrics { /// time a dispatch needed more than the last block held, or no command buffer kept it. A /// dispatch that reaches the last block takes none. pub private_arena_blocks: u64, + /// Direct dispatches of a whole grid, `dispatchThreads` of whole threadgroups or + /// `dispatchThreadgroups`, that ran a kernel's partial-threadgroups variant: a kernel that + /// observes its threadgroup partition runs every direct dispatch on that one module where the + /// variant takes the threadgroup. No Metal counterpart: Metal compiles one function. + pub partial_variant_whole_grids: u64, /// Functions the translator lowered, each a module of a pipeline content the device did not /// hold compiled: of a compute content, one for a kernel that does not observe its threadgroup /// partition and two for one that does; of a render content, one for each of its functions diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 193dfe6b3..42ffcbe89 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -340,7 +340,7 @@ pub(crate) struct Choice { /// - a tiling kernel ([`Form::Tiled`]): its tiles, workgroups of `GRID_TILE_THREADS`, each one /// `grid_tile` tile of the grid, which its region carries; /// - an observing kernel ([`Form::Sized`]): its own size, a ragged grid on its -/// partial-threadgroups variant. +/// partial-threadgroups variant ([`decide`] runs its whole grids there too). /// /// A size deeper than `DEEP_THREADGROUP` runs as one row of its threads /// ([`Constant::Deep`](crate::pipeline::Constant::Deep)), so every workgroup is within the @@ -875,8 +875,9 @@ impl ComputeCommandEncoder { /// `dispatchThreads:threadsPerThreadgroup:`: the threadgroups the grid's threads fill, the /// last along an axis running only the threads left, with the grid's extent a parameter of - /// the kernel. A kernel that observes its threadgroup partition runs a grid that is not a - /// whole number of threadgroups on its partial-threadgroups variant. + /// the kernel. A kernel that observes its threadgroup partition runs the grid, a whole number + /// of threadgroups or not, on its partial-threadgroups variant, as it runs + /// [`dispatch_threadgroups`](Self::dispatch_threadgroups). /// /// Err: as [`dispatch_threadgroups`](Self::dispatch_threadgroups); /// [`Gap::PartialThreadgroups`] for such a grid of a kernel whose partial-threadgroups @@ -1051,6 +1052,7 @@ impl ComputeCommandEncoder { direct, .. } = choice; + let ragged = direct.is_some_and(|(_, _, ragged)| ragged); // No thread runs: nothing is recorded, so a zero grid stays the kernel's mark of an // indirect dispatch. if direct.is_some_and(|(_, groups, _)| groups.contains(&0)) { @@ -1217,6 +1219,9 @@ impl ComputeCommandEncoder { if recorded.is_ok() && module == Module::Emulated { recording(&mut self.building).count(Metric::EmulatedSimdgroupDispatches); } + if recorded.is_ok() && module == Module::Partial && !ragged { + recording(&mut self.building).count(Metric::PartialVariantWholeGrids); + } recorded } @@ -1281,7 +1286,10 @@ pub(crate) fn size_checked(interface: &Interface, size: [u32; 3]) -> Result<(), /// [`ComputePipelineState::prepare_dispatch`]: [`choose`]'s choice, then its module, an /// observer's partial-threadgroups variant for a ragged grid, and the emulated variant for a /// threadgroup that is not whole groups of a module that runs only whole ones (the -/// partial-threadgroups variant has none), each lowered with the state. +/// partial-threadgroups variant has none), each lowered with the state. An observer's direct +/// dispatch of a whole grid, or of threadgroups, that its whole module would run runs on the +/// partial-threadgroups variant instead, unflagged, where the variant is lowered and takes the +/// threadgroup: one module, and one `VkPipeline` a size, runs every direct dispatch of it. pub(crate) fn decide( compiled: &Compiled, direct: Option, @@ -1294,18 +1302,38 @@ pub(crate) fn decide( threads_per_threadgroup: size, }) }; - let kernel = match choice.module { - Module::Partial => compiled.variant_kernel(Which::Partial, partial_threadgroups)?, - _ => &compiled.whole, - }; let threads = choice .local .iter() .map(|&axis| u64::from(axis)) .product::(); - match (kernel.interface.multiples.iter()) - .find(|&&multiple| !threads.is_multiple_of(u64::from(multiple.max(1)))) + // The first SIMD-group multiple of `kernel` the threadgroup is not whole groups of. + let short = |kernel: &Kernel| { + (kernel.interface.multiples.iter()) + .find(|&&multiple| !threads.is_multiple_of(u64::from(multiple.max(1)))) + .copied() + }; + let observes = matches!(compiled.form, Form::Sized { .. }); + if observes + && choice.module == Module::Whole + && choice.direct.is_some() + && short(&compiled.whole).is_none() { + if let Ok(partial) = compiled.variant_kernel(Which::Partial, partial_threadgroups) { + if short(partial).is_none() { + let choice = Choice { + module: Module::Partial, + ..choice + }; + return Ok((choice, Module::Partial, partial)); + } + } + } + let kernel = match choice.module { + Module::Partial => compiled.variant_kernel(Which::Partial, partial_threadgroups)?, + _ => &compiled.whole, + }; + match short(kernel).as_ref() { None => Ok((choice, choice.module, kernel)), Some(_) if choice.module == Module::Partial => Err(partial_threadgroups()), Some(&width) => { diff --git a/crates/metal2vulkan-runtime/src/pipeline.rs b/crates/metal2vulkan-runtime/src/pipeline.rs index 8a63f8d4c..5f2e64d41 100644 --- a/crates/metal2vulkan-runtime/src/pipeline.rs +++ b/crates/metal2vulkan-runtime/src/pipeline.rs @@ -12,9 +12,11 @@ //! grid and the threadgroup size pick (`encoder::image_tile`) over a deeper one, on one //! `VkPipeline` per tile made at its first dispatch. An indirect //! dispatch runs at its own threadgroup size. A kernel that observes its partition is lowered -//! plainly: its whole grids, threadgroup dispatches and indirect dispatches run that module at -//! their own threadgroup size, and a `dispatchThreads` grid that is not a whole number of -//! threadgroups runs its partial-threadgroups variant at its size, lowered with the state too. +//! plainly, and as its partial-threadgroups variant with the state: every direct dispatch, a +//! whole grid, threadgroups or a `dispatchThreads` grid that is not a whole number of +//! threadgroups, runs the variant at its own threadgroup size, a whole grid unflagged; its +//! indirect dispatches, and the direct ones where the content has no variant or the variant does +//! not take the threadgroup, run the plain module. //! Each `VkPipeline` is keyed by its module, threadgroup size and threadgroup memory lengths. //! //! What a state compiles is its content ([`KernelKey`]), never the state: the translator's key @@ -162,8 +164,10 @@ pub(crate) struct Compiled { core: Arc, /// Its number ([`CONTENTS`]). id: u64, - /// The module of whole grids, threadgroup dispatches and indirect dispatches; for a kernel - /// that does not observe its threadgroup partition, of every dispatch. + /// The module of indirect dispatches, and for a kernel that observes its threadgroup partition + /// of direct ones its partial-threadgroups variant does not run; for a kernel that does not + /// observe it, of every dispatch. Its interface numbers the state's samplers and answers + /// Metal's state. pub(crate) whole: Kernel, /// How a direct dispatch runs. pub(crate) form: Form, @@ -384,10 +388,9 @@ pub(crate) enum Form { /// `encoder::image_tile` over a deeper one, a tile deeper than `DEEP_THREADGROUP` as one row /// ([`Constant::Deep`]). Square, - /// The kernel observes its partition: a dispatch runs at its own threadgroup size, and a - /// `dispatchThreads` grid that is not a whole number of threadgroups on the - /// partial-threadgroups variant, lowered with the state where the content has a function to - /// lower it from. + /// The kernel observes its partition: a dispatch runs at its own threadgroup size, every + /// direct one on the partial-threadgroups variant, lowered with the state where the content + /// has a function to lower it from (`encoder::decide`). Sized { partial: Option> }, } @@ -807,9 +810,10 @@ impl crate::Device { /// kernel that does not observe its threadgroup partition as its partial-threadgroups /// variant, whose 256-wide tile's `VkPipeline`, or an image-only kernel's 16x16 workgroups' /// one, is made here too, and one that does as its plain module and its partial-threadgroups - /// variant, with the `VkPipeline`s a whole and a ragged grid run at the descriptor's - /// `requiredThreadsPerThreadgroup` where it sets one, or else at `[N, 1, 1]` where the state - /// declares a bound of threads (the descriptor's `maxTotalThreadsPerThreadgroup`, or the + /// variant, with the one `VkPipeline` its direct dispatches run, whole grids and ragged ones, + /// at the descriptor's `requiredThreadsPerThreadgroup` where it sets one, or else at + /// `[N, 1, 1]` where the state declares a bound of threads (the descriptor's + /// `maxTotalThreadsPerThreadgroup`, or the /// kernel's `[[max_total_threads_per_threadgroup(n)]]`), `N` the state's /// `maxTotalThreadsPerThreadgroup`, and the kernel takes no threadgroup memory length from /// the encoder. A kernel whose tier runs only whole SIMD groups has its @@ -1050,27 +1054,26 @@ impl Compiled { // where the state declares a bound of threads, `N` its `maxTotalThreadsPerThreadgroup`. // The bound is declared by the descriptor's `maxTotalThreadsPerThreadgroup`, or by the // kernel's `[[max_total_threads_per_threadgroup(n)]]`, which alone brings Metal's answer - // below its 1024; the device's own limit is no declared bound. The `VkPipeline`s a whole - // grid and a ragged one run are made, where the module takes no threadgroup memory length - // from the encoder. A make that fails here is made again by the dispatch that needs it, - // which reports its own error; a make refused for the stage bound refuses the state's, - // which the consumer's helper then fills. + // below its 1024; the device's own limit is no declared bound. The `VkPipeline` its direct + // dispatches of that size run is made, where the module takes no threadgroup memory length + // from the encoder: the partial-threadgroups variant's, which runs whole grids and ragged + // ones alike, or the whole module's where the content has no such variant. A make that + // fails here is made again by the dispatch that needs it, which reports its own error; a + // make refused for the stage bound refuses the state's, which the consumer's helper then + // fills. let interface = &compiled.whole.interface; let declared = descriptor.max_total_threads_per_threadgroup.is_some() || interface.metal_max_total_threads < METAL_MAX_THREADS; let bound = declared.then_some([interface.max_total_threads, 1, 1]); if let (Form::Sized { .. }, Some(size)) = (&compiled.form, required.or(bound)) { - let ragged = size.map(|axis| axis.saturating_add(1)); - for dispatch in [Dispatch::Threadgroups([1; 3]), Dispatch::Threads(ragged)] { - let found = compiled.dispatched(dispatch, size, &[]); - if let Some((kernel, module, local, lengths)) = found { - if kernel.interface.threadgroups.is_empty() { - let variant = compiled.dispatch_key(module, local, lengths); - let made = kernel.make_ahead(&compiled.core, variant, VariantStart::State); - match made { - Err(error) if past_stage_bound(&error) => return Err(error), - Ok(_) | Err(_) => {} - } + let found = compiled.dispatched(Dispatch::Threadgroups([1; 3]), size, &[]); + if let Some((kernel, module, local, lengths)) = found { + if kernel.interface.threadgroups.is_empty() { + let variant = compiled.dispatch_key(module, local, lengths); + let made = kernel.make_ahead(&compiled.core, variant, VariantStart::State); + match made { + Err(error) if past_stage_bound(&error) => return Err(error), + Ok(_) | Err(_) => {} } } } diff --git a/crates/metal2vulkan-runtime/src/tests/translate.rs b/crates/metal2vulkan-runtime/src/tests/translate.rs index 02e7bbde6..6a5dda726 100644 --- a/crates/metal2vulkan-runtime/src/tests/translate.rs +++ b/crates/metal2vulkan-runtime/src/tests/translate.rs @@ -628,14 +628,15 @@ mod on_device { } /// `ComputePipelineState::prepare_dispatch` makes what a dispatch of `icb_replay_grid_template`, - /// an observing kernel, then finds made: for a whole `dispatchThreads` grid, a ragged one (its - /// partial-threadgroups variant, lowered with the state), a `dispatchThreadgroups` grid and an - /// indirect dispatch, each prepared with the dispatch's own arguments, the call makes exactly - /// one pipeline and lowers nothing, and the dispatch adds no first-use wait and makes nothing. A `dispatchThreadgroups` grid whose threads saturate on an axis, - /// and a `dispatchThreads` grid of exactly `u32::MAX` threads on a size that does not divide - /// it, prepared alone, make what a small grid of the same call and size then runs with no - /// wait: the whole module and the partial-threadgroups variant. A second call with the same - /// arguments makes nothing. A call for what the dispatch records nothing for (a grid with no + /// an observing kernel, then finds made: for a whole `dispatchThreads` grid, a + /// `dispatchThreadgroups` grid and an indirect dispatch, each prepared with the dispatch's own + /// arguments, the call makes exactly one pipeline and lowers nothing, and the dispatch adds no + /// first-use wait and makes nothing; a ragged grid of the whole grid's size finds the whole + /// grid's made, since every direct dispatch runs the partial-threadgroups variant, lowered with + /// the state. A `dispatchThreadgroups` grid whose threads saturate on an axis, prepared alone, + /// makes what a small grid of the same call and size then runs with no wait, and a + /// `dispatchThreads` grid of exactly `u32::MAX` threads on that size, which does not divide + /// it, finds that made. A second call with the same arguments makes nothing. A call for what the dispatch records nothing for (a grid with no /// threads) or refuses (a threadgroup past Metal's, threadgroup memory past the device's) /// makes nothing, and the dispatch answers as it does unprepared. Each answer is Metal's. #[test] @@ -657,36 +658,42 @@ mod on_device { let state = state(name, &device, &descriptor); let counters = device.counters(); let length = |size: [u32; 3]| (4 * size.iter().product::()).next_multiple_of(16); - // What is prepared, at what size; the dispatch that then runs. + // What is prepared, at what size; the dispatch that then runs; the pipelines the call + // makes. let cases = [ ( Dispatch::Threads([8, 4, 1]), [4, 2, 1], ([8, 4, 1], How::Threads), + 1, ), ( Dispatch::Threads([10, 3, 1]), [4, 2, 1], ([10, 3, 1], How::Threads), + 0, ), ( Dispatch::Threadgroups([3, 2, 1]), [2, 2, 1], ([3, 2, 1], How::Groups), + 1, ), - (Dispatch::Indirect, [1, 1, 4], ([1, 1, 2], How::Indirect)), + (Dispatch::Indirect, [1, 1, 4], ([1, 1, 2], How::Indirect), 1), ( Dispatch::Threadgroups([u32::MAX, 1, 1]), [2, 1, 1], ([3, 1, 1], How::Groups), + 1, ), ( Dispatch::Threads([u32::MAX, 1, 1]), [2, 1, 1], ([3, 1, 1], How::Threads), + 0, ), ]; - for (prepared, size, (grid, how)) in cases { + for (prepared, size, (grid, how), made) in cases { let lengths = [(0, length(size))]; let before = counters.metrics(); state @@ -699,7 +706,7 @@ mod on_device { ahead.pipelines_created - before.pipelines_created, ahead.translations - before.translations, ), - (1, 1, 0), + (made, made, 0), "{name}: {prepared:?} by {size:?}: what the call made" ); state @@ -900,9 +907,10 @@ mod on_device { /// `ComputePipelineState::dispatch_variant`'s keys name exactly the `VkPipeline`s the /// dispatches run: over a table of dispatches of `icb_replay_grid_template`, an observing /// kernel with a threadgroup memory argument, two keys are equal exactly when the entries run - /// one module at one size and threadgroup memory length. Grids that differ only in their - /// extent or their call share a key, the ragged ones on the partial-threadgroups variant - /// another, and a different threadgroup size or length gives another. `prepare_dispatch` on + /// one module at one size and threadgroup memory length. Direct grids that differ only in their + /// extent, their call or whether they are whole threadgroups share a key, all on the + /// partial-threadgroups variant; an indirect dispatch, on the whole module, another; and a + /// different threadgroup size or length gives another. `prepare_dispatch` on /// each entry in turn makes one pipeline for each key not met before and none for one met. A /// second state of the same content answers the same keys and finds their pipelines made; a /// state of another content (the same function with a required threadgroup size) answers @@ -930,28 +938,28 @@ mod on_device { // Each entry: the dispatch, its size, its threadgroup memory length, and the pipeline // it runs. let table = [ - (Dispatch::Threads([8, 4, 1]), size, length(size), "whole"), - (Dispatch::Threads([16, 8, 1]), size, length(size), "whole"), + (Dispatch::Threads([8, 4, 1]), size, length(size), "direct"), + (Dispatch::Threads([16, 8, 1]), size, length(size), "direct"), ( Dispatch::Threadgroups([3, 2, 1]), size, length(size), - "whole", + "direct", ), - (Dispatch::Indirect, size, length(size), "whole"), - (Dispatch::Threads([10, 3, 1]), size, length(size), "partial"), - (Dispatch::Threads([9, 5, 1]), size, length(size), "partial"), + (Dispatch::Indirect, size, length(size), "indirect"), + (Dispatch::Threads([10, 3, 1]), size, length(size), "direct"), + (Dispatch::Threads([9, 5, 1]), size, length(size), "direct"), ( Dispatch::Threadgroups([3, 2, 1]), small, length(small), - "whole, small", + "direct, small", ), ( Dispatch::Threads([8, 4, 1]), size, 2 * length(size), - "whole, longer", + "direct, longer", ), ]; let first = state(name, &device, &descriptor); @@ -2864,18 +2872,19 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t /// threadgroup partition through `threadgroup_barrier(mem_flags::mem_device)`: each thread /// stores `((y << 16) | x) + 1` to its cell, then, past the barrier, copies the word of its /// column in row `y ^ 1` where that row is in the grid. The state lowers both modules and makes - /// no pipeline, and its grids run at their own size: 512 x 4 in 32 x 2 on the plain module, - /// 512 x 3 in 32 x 2 and 96 x 5 in 32 x 4 on the partial-threadgroups variant, each its own - /// pipeline made by a first-use wait, with no translation. A state of the descriptor with - /// `requiredThreadsPerThreadgroup` 32 x 2 makes, with the state, the pipelines a whole and a + /// no pipeline, and its grids run at their own size on the partial-threadgroups variant: 512 x + /// 4 in 32 x 2, whole, makes its pipeline by a first-use wait with no translation, 512 x 3 in + /// 32 x 2, ragged, finds it made, and 96 x 5 in 32 x 4 makes its own; the whole grid counts in + /// `Metrics::partial_variant_whole_grids`. A state of the descriptor with + /// `requiredThreadsPerThreadgroup` 32 x 2 makes, with the state, the one pipeline a whole and a /// ragged grid run at that size, so its 512 x 4 and 512 x 3 grids make nothing and wait for /// nothing. Every read sees the other row's word, Metal's answer: the probe /// `metal2vulkan-conformance/probes/row_barrier_reads.m` ran the same source over the same /// grids on an Apple M3 Max under macOS 27.0.1, and no word of `seen` differed from it. A /// state whose descriptor declares `maxTotalThreadsPerThreadgroup` 64 and no required size - /// makes, with the state, the pipelines a whole and a ragged grid run at 64 x 1, so its first - /// 512 x 1 and 500 x 1 grids in 64 x 1 make nothing and wait for nothing; one row reads no - /// other row's word. + /// makes, with the state, the one pipeline a whole and a ragged grid run at 64 x 1, so its + /// first 512 x 1 and 500 x 1 grids in 64 x 1 make nothing and wait for nothing; one row reads + /// no other row's word. #[test] fn every_device_keeps_a_kernel_that_reads_across_rows_after_a_barrier_at_its_own_size() { let Some(descriptor) = harness_kernel("row_barrier") else { @@ -2929,13 +2938,13 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t "{name}: {grid:?} in {size:?}" ); }; - // Each grid at a size its state has not run: one pipeline, made by a first-use wait, - // and no translation. + // Each grid on the partial variant at its own size: one pipeline, made by a first-use + // wait, where the state has not run the size, and no translation. let mut before = created; - for (grid, size, what) in [ - ([512, 4, 1], [32, 2, 1], "the plain module at 32 x 2"), - ([512, 3, 1], [32, 2, 1], "the partial variant at 32 x 2"), - ([96, 5, 1], [32, 4, 1], "the partial variant at 32 x 4"), + for (grid, size, made, whole, what) in [ + ([512, 4, 1], [32, 2, 1], 1, 1, "a whole grid at 32 x 2"), + ([512, 3, 1], [32, 2, 1], 0, 0, "a ragged grid at 32 x 2"), + ([96, 5, 1], [32, 4, 1], 1, 0, "a ragged grid at 32 x 4"), ] { run(&state, grid, size); let after = device.counters().metrics(); @@ -2944,13 +2953,14 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t after.pipelines_created - before.pipelines_created, after.first_use_waits - before.first_use_waits, after.translations - before.translations, + after.partial_variant_whole_grids - before.partial_variant_whole_grids, ), - (1, 1, 0), - "{name}: pipelines, first-use waits and translations of {what}" + (made, made, 0, whole), + "{name}: pipelines, first-use waits, translations and whole grids of {what}" ); before = after; } - // At the descriptor's required size, the make makes what both grids run. + // At the descriptor's required size, the make makes what both grids run: one pipeline. let mut required = descriptor.clone(); required.required_threads_per_threadgroup = Some([32, 2, 1]); let sized = self::state(name, &device, &required); @@ -2960,8 +2970,8 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t made.pipelines_created - before.pipelines_created, made.translations - before.translations, ), - (2, 2), - "{name}: the required size's state: its whole and ragged grids' pipelines" + (1, 2), + "{name}: the required size's state: its whole and ragged grids' pipeline" ); run(&sized, [512, 4, 1], [32, 2, 1]); run(&sized, [512, 3, 1], [32, 2, 1]); @@ -2976,7 +2986,7 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t "{name}: the required size's grids made nothing and waited for nothing" ); // At a declared bound and no required size, the make makes what a whole and a ragged - // grid run at the bound's row. + // grid run at the bound's row: one pipeline. let mut bounded = descriptor.clone(); bounded.max_total_threads_per_threadgroup = Some(64); let before = device.counters().metrics(); @@ -2985,8 +2995,8 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t assert_eq!(bound.max_total_threads_per_threadgroup(), 64, "{name}"); assert_eq!( made.pipelines_created - before.pipelines_created, - 2, - "{name}: the declared bound's state: its whole and ragged grids' pipelines" + 1, + "{name}: the declared bound's state: its whole and ragged grids' pipeline" ); run(&bound, [512, 1, 1], [64, 1, 1]); run(&bound, [500, 1, 1], [64, 1, 1]); @@ -3001,17 +3011,19 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t ); println!( "{name}: row barrier reads checked in 512 x 4 and 512 x 3 in 32 x 2 and 96 x 5 in \ - 32 x 4, at their own size; declared bound 64: whole and ragged grids made with \ - the state" + 32 x 4, at their own size on the partial variant; declared bound 64: whole and \ + ragged grids' pipeline made with the state" ); }); } /// `compute_thread_limit_64` declares `[[max_total_threads_per_threadgroup(64)]]` and observes - /// its threadgroups, with no required size: its state makes the pipelines a whole and a ragged - /// grid run at 64 x 1 x 1, so its first 128 x 1 and 100 x 1 grids in 64 x 1 make nothing and - /// wait for nothing, and each grid runs each of its threads once in threadgroups of 64. The - /// same kernel without the attribute, bounded only by the device, makes none with its state. + /// its threadgroups, with no required size: its state makes the one pipeline a whole and a + /// ragged grid run at 64 x 1 x 1, the partial-threadgroups variant's, so its first 128 x 1 and + /// 100 x 1 grids in 64 x 1 make nothing and wait for nothing, the whole one counted in + /// `Metrics::partial_variant_whole_grids`, and each grid runs each of its threads once in + /// threadgroups of 64. The same kernel without the attribute, bounded only by the device, + /// makes none with its state. #[test] fn every_device_makes_a_declared_bound_s_grids_with_the_state() { let (Some(bounded), Some(plain)) = ( @@ -3036,8 +3048,8 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t assert_eq!(state.max_total_threads_per_threadgroup(), 64, "{name}"); assert_eq!( created.pipelines_created - made.pipelines_created, - 2, - "{name}: the whole and ragged grids' pipelines, made with the state" + 1, + "{name}: the whole and ragged grids' pipeline, made with the state" ); let queue = device.new_command_queue(4).expect("a queue"); for threads in [128, 100] { @@ -3067,12 +3079,15 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t ( after.pipelines_created - created.pipelines_created, after.first_use_waits - created.first_use_waits, + after.partial_variant_whole_grids - created.partial_variant_whole_grids, ), - (0, 0), - "{name}: the declared bound's grids made nothing and waited for nothing" + (0, 0, 1), + "{name}: the declared bound's grids made nothing and waited for nothing, the \ + whole one on the partial variant" ); println!( - "{name}: declared bound 64: 128 x 1 and 100 x 1 in 64 x 1 made with the state" + "{name}: declared bound 64: 128 x 1 and 100 x 1 in 64 x 1 made with the state, \ + both on the partial variant" ); }); } diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index 084b080d8..53586ff8f 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -3052,6 +3052,10 @@ impl Backend for NewRuntime { ), ("made_ahead_variants", metrics.made_ahead_variants), ("made_ahead_variant_nanos", metrics.made_ahead_variant_nanos), + ( + "partial_variant_whole_grids", + metrics.partial_variant_whole_grids, + ), ("blocks_made", metrics.blocks_made), ("block_bytes_made", metrics.block_bytes_made), ("shared_long_reuses", metrics.shared_long_reuses), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 89e26d70b..d1eca1a8c 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1573,6 +1573,7 @@ field metal2vulkan-runtime::Metrics.over_copy_bytes_render_reach pub over_copy_b field metal2vulkan-runtime::Metrics.over_copy_bytes_spilled pub over_copy_bytes_spilled: u64 field metal2vulkan-runtime::Metrics.over_copy_bytes_surface pub over_copy_bytes_surface: u64 field metal2vulkan-runtime::Metrics.overlapping_copies_refused pub overlapping_copies_refused: u64 +field metal2vulkan-runtime::Metrics.partial_variant_whole_grids pub partial_variant_whole_grids: u64 field metal2vulkan-runtime::Metrics.passes pub passes: u64 field metal2vulkan-runtime::Metrics.pipeline_cache_lock_wait_max_nanos pub pipeline_cache_lock_wait_max_nanos: u64 field metal2vulkan-runtime::Metrics.pipeline_cache_lock_wait_nanos pub pipeline_cache_lock_wait_nanos: u64 diff --git a/docs/API.md b/docs/API.md index 6a7becaa6..353cf654b 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1440,9 +1440,9 @@ that plan a grid of whole threadgroups, of `Threads` or `Threadgroups`, takes th unflagged (each `MultiRegion` 1, every thread live), so one `VkPipeline` of a threadgroup size runs all of a kernel's direct dispatches. The runtime lowers a kernel that does not observe its partition once, as that variant, and runs every direct dispatch of it in its tiles; a kernel that -observes its partition runs its whole grids, threadgroup dispatches and indirect dispatches on its -plain module at their own threadgroup size, and a `dispatchThreads` grid that is not a whole -number of threadgroups on its partial-threadgroups variant. A kernel +observes its partition runs every direct dispatch, a whole grid, threadgroups or a +`dispatchThreads` grid that is not a whole number of threadgroups, on its partial-threadgroups +variant at its own threadgroup size, and its indirect dispatches on its plain module. A kernel that steps a stage-in attribute by thread position, or reads `[[grid_origin]]` or `[[grid_size]]`, needs the encoder's stage-in region: