From f090d99c9ac43bebba1817d532f879774aa37cdb Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 05:00:09 -0800 Subject: [PATCH] rt: an observing kernel's indirect dispatches run its partial-threadgroups variant too decide() moved an observer's whole grids and threadgroup dispatches onto the partial-threadgroups variant but left its indirect dispatches on the plain module, so a state dispatched both ways made a second VkPipeline of the same size on the first indirect dispatch. By the translator's dispatch regions (lower::dispatch), an indirect dispatch states region[1].xyz = 0 and takes the single-region form, and the variant's partial answers sit only under region[1].w NONUNIFORM, which the encoder's indirect region never sets. So the variant runs it unflagged, every invocation live, as it runs a whole grid. decide() now drops the direct-only condition: one module and one VkPipeline a size run every dispatch of an observing kernel where the variant takes the threadgroup. Metrics::partial_variant_whole_grids counts the indirect ones with the whole grids. No key, version or translator file changes. Tests: the observing kernel's run table expects its indirect dispatch on the variant (one first-use wait fewer where it shares a size with a direct dispatch, and one more count); prepare_dispatch's table gains an indirect dispatch at the whole grid's size that finds the variant made (0 makes; 1 before this change); the variant-key table groups the indirect dispatch with the direct ones. The conformance case compute-indirect-threadgroup-grid dispatches region_k, an observer, indirectly; its dispatch-forms line is the check that the replay ran it on the variant. Checks to run on this commit: tests::translate and the conformance replay on a card per vendor. --- crates/metal2vulkan-runtime/README.md | 15 +++--- crates/metal2vulkan-runtime/src/counters.rs | 9 ++-- crates/metal2vulkan-runtime/src/encoder.rs | 20 ++++---- crates/metal2vulkan-runtime/src/pipeline.rs | 38 +++++++------- .../src/tests/translate.rs | 50 +++++++++---------- docs/API.md | 7 +-- 6 files changed, 71 insertions(+), 68 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index d974c4052..23ceaac1c 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -2567,12 +2567,13 @@ while the driver's exit handler waited on a lock, with every runtime thread alre 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 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 + through the cache directory: every dispatch, a whole `dispatch_threads` grid, + `dispatch_threadgroups`, `dispatch_threadgroups_with_indirect_buffer` or a grid that is not a + whole number of threadgroups, runs the variant at its own size, every region but a ragged grid's + unflagged, so one `VkPipeline` of a size runs them all (`Metrics::partial_variant_whole_grids` + counts the dispatches of whole threadgroups). A dispatch runs the plain module 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. @@ -3057,7 +3058,7 @@ 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 its direct dispatches run, and the + observing kernel's partial-threadgroups variant, which its 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 diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index 0c9d0c67f..3aa0a50bb 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -2259,10 +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. + /// Dispatches of whole threadgroups, `dispatchThreads` of a whole grid, `dispatchThreadgroups` + /// or `dispatchThreadgroupsWithIndirectBuffer`, that ran a kernel's partial-threadgroups + /// variant: a kernel that observes its threadgroup partition runs every 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 diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 42ffcbe89..9e3702711 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -333,14 +333,16 @@ pub(crate) struct Choice { /// How a state of `form` runs a dispatch of threadgroup size `size`, direct over `direct` or /// indirect, the same on every device: -/// - indirect: the whole module at its own size; +/// - indirect: the whole module at its own size ([`decide`] runs an observing kernel's on its +/// partial-threadgroups variant); /// - an image-only kernel ([`Form::Square`]): the whole module at `MultiRegion` 0 in workgroups /// of [`SQUARE`] over a grid one deep, and of its [`image_tile`] over a deeper one, the region /// carrying them, flagged where they do not divide the grid; /// - 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 ([`decide`] runs its whole grids there too). +/// partial-threadgroups variant ([`decide`] runs its whole grids and indirect dispatches 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 @@ -1286,10 +1288,12 @@ 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. An observer's direct -/// dispatch of a whole grid, or of threadgroups, that its whole module would run runs on the +/// partial-threadgroups variant has none), each lowered with the state. An observer's dispatch of +/// a whole grid, of threadgroups, or indirect, 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. +/// threadgroup: one module, and one `VkPipeline` a size, runs every dispatch of it. An indirect +/// dispatch states no grid in its region, so the variant runs it in the single-region form, every +/// invocation live, as the translator's dispatch regions say (`lower::dispatch`). pub(crate) fn decide( compiled: &Compiled, direct: Option, @@ -1314,11 +1318,7 @@ pub(crate) fn decide( .copied() }; let observes = matches!(compiled.form, Form::Sized { .. }); - if observes - && choice.module == Module::Whole - && choice.direct.is_some() - && short(&compiled.whole).is_none() - { + if observes && choice.module == Module::Whole && short(&compiled.whole).is_none() { if let Ok(partial) = compiled.variant_kernel(Which::Partial, partial_threadgroups) { if short(partial).is_none() { let choice = Choice { diff --git a/crates/metal2vulkan-runtime/src/pipeline.rs b/crates/metal2vulkan-runtime/src/pipeline.rs index 5f2e64d41..48b264368 100644 --- a/crates/metal2vulkan-runtime/src/pipeline.rs +++ b/crates/metal2vulkan-runtime/src/pipeline.rs @@ -12,11 +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, 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. +//! plainly, and as its partial-threadgroups variant with the state: every dispatch, a whole +//! grid, threadgroups, an indirect dispatch or a `dispatchThreads` grid that is not a whole number +//! of threadgroups, runs the variant at its own threadgroup size, all but the last unflagged; the +//! dispatches 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 @@ -164,10 +164,10 @@ pub(crate) struct Compiled { core: Arc, /// Its number ([`CONTENTS`]). id: u64, - /// 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. + /// For a kernel that observes its threadgroup partition, the module of the dispatches its + /// partial-threadgroups variant does not run; for a kernel that does not observe it, the module + /// of indirect dispatches, and of every dispatch where it is the only one. 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, @@ -388,9 +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, every - /// direct one on the partial-threadgroups variant, lowered with the state where the content - /// has a function to lower it from (`encoder::decide`). + /// The kernel observes its partition: a dispatch runs at its own threadgroup size, every one, + /// direct or indirect, on the partial-threadgroups variant, lowered with the state where the + /// content has a function to lower it from (`encoder::decide`). Sized { partial: Option> }, } @@ -810,8 +810,8 @@ 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 one `VkPipeline` its direct dispatches run, whole grids and ragged ones, - /// at the descriptor's `requiredThreadsPerThreadgroup` where it sets one, or else at + /// variant, with the one `VkPipeline` its dispatches run, whole grids, ragged and indirect + /// 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 @@ -1054,12 +1054,12 @@ 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` its direct + // below its 1024; the device's own limit is no declared bound. The `VkPipeline` its // 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 + // from the encoder: the partial-threadgroups variant's, which runs whole grids, ragged and + // indirect 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() diff --git a/crates/metal2vulkan-runtime/src/tests/translate.rs b/crates/metal2vulkan-runtime/src/tests/translate.rs index c108d9307..83027eed2 100644 --- a/crates/metal2vulkan-runtime/src/tests/translate.rs +++ b/crates/metal2vulkan-runtime/src/tests/translate.rs @@ -413,9 +413,9 @@ mod on_device { /// `icb_replay_grid_template`, which observes its threadgroup partition (threadgroup memory, /// a barrier, and every builtin of the partition), translated at creation, plainly and as its - /// partial-threadgroups variant: every direct dispatch, a whole `dispatchThreads` grid, a - /// ragged one or threadgroup counts, runs the variant at the call's size, and an indirect one - /// the plain module, with no translation at any dispatch, and a + /// partial-threadgroups variant: every dispatch, a whole `dispatchThreads` grid, a ragged one, + /// threadgroup counts or an indirect one, runs the variant at the call's size, with no + /// translation at any dispatch, and a /// threadgroup count past the device's limit on an axis /// runs split, each piece reading Metal's whole grid. Threadgroups deeper than 64, 1 by 1 by /// 320 and 2 by 3 by 100, run as one row of their threads on every device, whole, ragged and @@ -546,8 +546,8 @@ mod on_device { "{name}: no translation at a dispatch, the first ragged grid's included" ); // One pipeline for each module and size the runs take: a ragged grid the variant; a - // threadgroup of whole SIMD groups of the device's tier the variant where direct and - // the plain module where indirect; any other the emulated variant. + // threadgroup of whole SIMD groups of the device's tier the variant, direct or + // indirect; any other the emulated variant. let simd = &state.inner.compiled.whole.interface.multiples; let whole_simd = |size: [u32; 3]| { let threads = size.iter().product::(); @@ -559,11 +559,10 @@ mod on_device { for &(grid, size, how) in &runs { let ragged = how == How::Threads && (0..3).any(|axis| !grid[axis].is_multiple_of(size[axis])); - let module = match (how, ragged, whole_simd(size)) { - (_, true, _) => "variant", - (_, false, false) => "emulated", - (How::Indirect, false, true) => "plain", - (How::Threads | How::Groups, false, true) => { + let module = match (ragged, whole_simd(size)) { + (true, _) => "variant", + (false, false) => "emulated", + (false, true) => { on_variant += 1; "variant" } @@ -578,7 +577,7 @@ mod on_device { assert_eq!( after.partial_variant_whole_grids - created.partial_variant_whole_grids, on_variant, - "{name}: whole grids on the partial-threadgroups variant" + "{name}: whole threadgroups, direct and indirect, on the partial-threadgroups variant" ); println!( "{name}: observing kernel checked over {} runs{}; creation {creation} ns, one lowering {lowering} ns; precision notes {}", @@ -658,9 +657,9 @@ mod on_device { /// 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, + /// first-use wait and makes nothing; a ragged grid and an indirect dispatch of the whole grid's + /// size find the whole grid's made, since every 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 @@ -700,6 +699,7 @@ mod on_device { ([10, 3, 1], How::Threads), 0, ), + (Dispatch::Indirect, [4, 2, 1], ([2, 2, 1], How::Indirect), 0), ( Dispatch::Threadgroups([3, 2, 1]), [2, 2, 1], @@ -935,9 +935,9 @@ mod on_device { /// 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. 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 + /// extent, their call or whether they are whole threadgroups, and an indirect dispatch, share a + /// key, all on the partial-threadgroups variant; 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 @@ -965,28 +965,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), "direct"), - (Dispatch::Threads([16, 8, 1]), size, length(size), "direct"), + (Dispatch::Threads([8, 4, 1]), size, length(size), "size"), + (Dispatch::Threads([16, 8, 1]), size, length(size), "size"), ( Dispatch::Threadgroups([3, 2, 1]), size, length(size), - "direct", + "size", ), - (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::Indirect, size, length(size), "size"), + (Dispatch::Threads([10, 3, 1]), size, length(size), "size"), + (Dispatch::Threads([9, 5, 1]), size, length(size), "size"), ( Dispatch::Threadgroups([3, 2, 1]), small, length(small), - "direct, small", + "small", ), ( Dispatch::Threads([8, 4, 1]), size, 2 * length(size), - "direct, longer", + "longer", ), ]; let first = state(name, &device, &descriptor); diff --git a/docs/API.md b/docs/API.md index 353cf654b..3f36366af 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1440,9 +1440,10 @@ 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 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 +observes its partition runs every dispatch, a whole grid, threadgroups, an indirect dispatch or +a `dispatchThreads` grid that is not a whole number of threadgroups, on its partial-threadgroups +variant at its own threadgroup size; an indirect dispatch states no grid in its region and takes +the single-region form, unflagged. 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: