From 0e7e09a064627ff5701eb64e2954fd9de486d85e Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 3 Nov 2026 06:47:51 -0800 Subject: [PATCH] Answer a required threadgroup size's threads as a state's maxTotalThreadsPerThreadgroup A compute or tile state whose descriptor states requiredThreadsPerThreadgroup now answers that size's threads as its maxTotalThreadsPerThreadgroup. The answer is lowered only where this device holds fewer threads for the kernel than the size. A state that requires no size keeps today's answer, the compiled pipeline's limit. Before this change every state answered the compiled limit (1024 on a full device) whatever size it required. Metal answers the size. Measured on the M3 Max under macOS 27.0.1. Each state came from a source compiled once and never before, with an integer baked into it: - Compute: exactly W*H*D. 1x1x1 gives 1, 3x1x1 gives 3, 3x3x1 gives 9, 7x3x1 gives 21, 8x8x2 gives 128, 1x1x64 gives 64, 32x32x1 and 1024x1x1 give 1024. No size gives 1024. [[max_total_threads_per_threadgroup(128)]] with 8x8x1 gives 64. - Tile (threadgroupSizeMatchesTileSize on or off): W*H*D rounded down to a multiple of 4. 1x1, 2x1 and 3x1 give 0, 3x3 gives 8, 7x3 gives 20, 2x2 gives 4, 6x6 gives 36, 8x8 gives 64, 16x16 gives 256, 32x32 gives 1024. A tile dispatch of the state's own size runs no thread where the threads are not a multiple of 4 (1x1, 2x1, 3x3, 7x3). The command buffer completes with no error. - Metal keeps a state it compiled and hands its limit to a later state of the same function that differs only in the required size. For compute this was seen in the same process; for tile, in the same process and in a later one. A compute state with 8x8x1, and one with no size, answered 16 after a 4x4x1 state of the same MTLFunction. An 8x8 tile state answered 16 after a 4x4 one, and its 8x8 dispatch ran no thread. The module doc's earlier "the state's maxTotalThreadsPerThreadgroup stays the kernel's own" reads like such a measurement: a function compiled first with no size. Rule 1 does not cover this: it is an API value Metal reports, not arithmetic. So the runtime answers Metal's first-compile rule and does not imitate its cache. Following the owner's decision, a tile state also answers the size's threads where Metal rounds them down, and its dispatch of that size runs. Metal runs no thread there, which is its own defect. docs/API.md's accepted differences records both and Metal's cache answer. How: - ComputePipelineState::max_total_threads_per_threadgroup answers pipeline::reported_max_total_threads(required size, compiled limit). A tile state's RenderPipelineState answer goes through its tile function's compute state, so it follows. - The compiled limit moves to pipeline_max_total_threads_per_threadgroup (crate-private). The replay and folded indirect-dispatch paths (gpu_execute, indirect_dispatch) read it, so no dispatch path changes. - Docs: the additions module's measurement, the tile module's answers, the render and compute accessors, the translator's descriptor fields and ComputePipelineMetalInfo, and docs/API.md's accepted differences. Tests: - tests::pipeline::a_required_size_is_the_states_thread_limit (CPU): the rule, the device's lower limit, no size, and axes whose product overflows. - runtime_lane::compute_threadgroups::a_compute_state_with_a_required_size_answers_its_threads and runtime_lane::tile_states::a_tile_state_with_a_required_size_answers_its_threads (--cfg m2v_gpu): states made through the runtime with and without a size. Verified: - cargo clippy -D warnings, plain (metal2vulkan, metal2vulkan-runtime, metal2vulkan-validation) and with --cfg m2v_gpu (metal2vulkan-validation); - RUSTDOCFLAGS=-D warnings cargo doc (metal2vulkan, metal2vulkan-runtime); - scripts/api-docs; - the metal2vulkan-runtime and metal2vulkan-air suites: 1715 passed, 4 skipped; - the device lanes on the M3 Max through MoltenVK (tile_states, compute_threadgroups, tile_threadgroups, indirect_dispatches, indirect_executions, function_tables): 124 passed. With the old answer put back, both new device tests fail ("left: 1024, right: 1" at 1x1x1). No version bump and no pin here: they come at stacking. (cherry picked from commit 94fbec600e2155fff4c05b88894155054569621e) (cherry picked from commit 09be67cab1968bbadcc698456fb245ec6e94f20a) --- .../metal2vulkan-runtime/src/gpu_execute.rs | 2 +- .../src/indirect_dispatch.rs | 4 +- .../src/pipeline/additions.rs | 25 +++++++-- .../metal2vulkan-runtime/src/pipeline/mod.rs | 32 +++++++++++- .../src/pipeline/render.rs | 4 +- .../metal2vulkan-runtime/src/pipeline/tile.rs | 9 ++-- .../src/tests/pipeline.rs | 49 +++++++++++++++-- .../src/runtime_lane/compute_threadgroups.rs | 52 ++++++++++++++++++- .../src/runtime_lane/tile_states.rs | 36 ++++++++++++- crates/metal2vulkan/src/pipeline/artifact.rs | 4 +- .../metal2vulkan/src/pipeline/descriptor.rs | 9 ++-- docs/API.md | 16 ++++++ 12 files changed, 218 insertions(+), 24 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/gpu_execute.rs b/crates/metal2vulkan-runtime/src/gpu_execute.rs index 2ead8791a..ef81f80ca 100644 --- a/crates/metal2vulkan-runtime/src/gpu_execute.rs +++ b/crates/metal2vulkan-runtime/src/gpu_execute.rs @@ -485,7 +485,7 @@ fn prepare( // for the state alone, since the kernel does not observe Metal's threadgroups. let threads = [ FLAT_THREADS - .min(pipeline.max_total_threads_per_threadgroup()) + .min(pipeline.pipeline_max_total_threads_per_threadgroup()) .max(1), 1, 1, diff --git a/crates/metal2vulkan-runtime/src/indirect_dispatch.rs b/crates/metal2vulkan-runtime/src/indirect_dispatch.rs index 8e44a07c9..f9893992d 100644 --- a/crates/metal2vulkan-runtime/src/indirect_dispatch.rs +++ b/crates/metal2vulkan-runtime/src/indirect_dispatch.rs @@ -389,7 +389,7 @@ impl FlatCall<'_> { FlatIndirect::new( arguments, form, - flat_local(pipeline.max_total_threads_per_threadgroup()), + flat_local(pipeline.pipeline_max_total_threads_per_threadgroup()), pipeline.metal_max_total_threads_per_threadgroup(), ) } @@ -405,7 +405,7 @@ impl FlatCall<'_> { return None; }; let arguments = self.buffer.device_address()?.checked_add(self.offset)?; - let max_threads = pipeline.max_total_threads_per_threadgroup(); + let max_threads = pipeline.pipeline_max_total_threads_per_threadgroup(); let threads: u64 = threads_per_group.iter().map(|&n| u64::from(n)).product(); if threads == 0 || threads > u64::from(max_threads) { return None; diff --git a/crates/metal2vulkan-runtime/src/pipeline/additions.rs b/crates/metal2vulkan-runtime/src/pipeline/additions.rs index 827583ec5..da07aeb74 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/additions.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/additions.rs @@ -6,17 +6,32 @@ //! - **Required threads per threadgroup.** Creation stops, with or without API validation, a //! size with an axis of 0, one of more threads than the device's limit, and one of more threads //! than the descriptor's `maxTotalThreadsPerThreadgroup`; it fails (an error, not a stop) where -//! that maximum is above the size's threads. The state's `maxTotalThreadsPerThreadgroup` stays -//! the kernel's own. API validation stops a direct, threads or indirect dispatch, and a tile -//! dispatch, whose threadgroup differs on any axis; without it Metal runs the size given, and -//! so does the runtime. An indirect command buffer's dispatch is not checked, and runs the size -//! it was encoded with. +//! that maximum is above the size's threads. API validation stops a direct, threads or indirect +//! dispatch, and a tile dispatch, whose threadgroup differs on any axis; without it Metal runs +//! the size given, and so does the runtime. An indirect command buffer's dispatch is not +//! checked, and runs the size it was encoded with. //! - **Binary functions, added.** Adding to a stage of a state made without its support flag //! (`supportAddingBinaryFunctions`, or the vertex and fragment forms) is an API validation stop; //! without validation Metal adds them. The added state has the base state's handles and one for //! each added function; an addition the stage already links keeps its handle, and adding //! nothing makes a state alike. //! +//! Measured on an M3 Max under macOS 27.0.1, each state of a source compiled once and never +//! before (a number baked into it): +//! - **A required size is the state's thread limit.** A compute state's +//! `maxTotalThreadsPerThreadgroup` is the size's threads: 1 for 1x1x1, 21 for 7x3x1, 128 for +//! 8x8x2, 64 for 1x1x64, 1024 for 32x32x1. A tile state's, with +//! `threadgroupSizeMatchesTileSize` on or off, is the threads rounded down to a multiple of 4: +//! 0 for 1x1, 2x1 and 3x1, 8 for 3x3, 20 for 7x3, 64 for 8x8. A tile dispatch of such a state's +//! own size runs no thread where the threads are not a multiple of 4, and the command buffer +//! completes with no error. With no size, both kinds answer 1024. Metal keeps a state it +//! compiled and hands its limit to a later state of the same function that differs only in the +//! required size, in the same process (compute and tile) and in a later one (tile). A +//! measurement of one function under several sizes therefore reads the first size's limit +//! throughout. The runtime answers each state's own size's threads +//! (`ComputePipelineState::max_total_threads_per_threadgroup`), and a tile state's dispatch of +//! a size that is not a multiple of 4 runs (docs/API.md, accepted differences). +//! //! Measured on an M3 Max under macOS 27.0.1 (`binary_function_linking.m`, of libraries compiled //! from source and of metallibs alike, plain and with `MTL_DEBUG_LAYER=1`): //! - **A binary function is a table's function alone.** No visible-function reference resolves to diff --git a/crates/metal2vulkan-runtime/src/pipeline/mod.rs b/crates/metal2vulkan-runtime/src/pipeline/mod.rs index 5d23ebbed..96954702a 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/mod.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/mod.rs @@ -138,6 +138,21 @@ use std::collections::{BTreeMap, HashSet, VecDeque}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, OnceLock, Weak}; +/// The `maxTotalThreadsPerThreadgroup` a compute or tile state answers. `required` is the size +/// its descriptor requires, `None` where it requires none (Metal's all-zero size), and `compiled` +/// is the compiled pipeline's answer on this device. Where a size is required, the answer is its +/// threads, within `compiled`; where none is, it is `compiled`. +pub(super) fn reported_max_total_threads(required: Option<[u32; 3]>, compiled: u32) -> u32 { + match required { + Some(required) => required + .iter() + .try_fold(1u32, |threads, &axis| threads.checked_mul(axis)) + .unwrap_or(u32::MAX) + .min(compiled), + None => compiled, + } +} + /// `imageblockMemoryLengthForDimensions:` for an explicit imageblock of `cell_bytes` a cell (the /// compile's), none without an imageblock or at a width or height of 0. Otherwise each side rounds /// up to a power of two and the depth is ignored, as Metal does on an M3 and an M4 (its API takes @@ -1009,8 +1024,23 @@ impl ComputePipelineState { } } - /// `maxTotalThreadsPerThreadgroup`. + /// `maxTotalThreadsPerThreadgroup`. Where the descriptor states a + /// `required_threads_per_threadgroup`, it is that size's threads, as Metal answers for a + /// compute state compiled for the first time (measured on an M3 Max, macOS 27.0.1). A tile + /// state answers the same, where Metal's rounds the threads down to a multiple of 4 + /// (docs/API.md, accepted differences). It is lowered only where this device holds fewer + /// threads than the size. Where the descriptor states no size, it is the compiled pipeline's + /// answer. pub fn max_total_threads_per_threadgroup(&self) -> u32 { + reported_max_total_threads( + self.required_threads(), + self.pipeline_max_total_threads_per_threadgroup(), + ) + } + + /// The compiled pipeline's `maxTotalThreadsPerThreadgroup` on this device, whatever size the + /// descriptor requires: the most threads a threadgroup of the pipeline holds here. + pub(crate) fn pipeline_max_total_threads_per_threadgroup(&self) -> u32 { self.0 .base .pipeline diff --git a/crates/metal2vulkan-runtime/src/pipeline/render.rs b/crates/metal2vulkan-runtime/src/pipeline/render.rs index 6ea66aba2..d4bdac624 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/render.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/render.rs @@ -1174,7 +1174,9 @@ impl RenderPipelineState { } /// `maxTotalThreadsPerThreadgroup`: 0 for a drawing state, as Metal answers (measured on an - /// M3 Max and an M4); a tile pipeline's compile's (`tile.rs`). + /// M3 Max and an M4). A tile state answers as its tile function's compute state does + /// (`ComputePipelineState::max_total_threads_per_threadgroup`, `tile.rs`): a required size's + /// threads, else the compiled pipeline's limit. pub fn max_total_threads_per_threadgroup(&self) -> u32 { match &self.0 { Kind::Graphics(_) => 0, diff --git a/crates/metal2vulkan-runtime/src/pipeline/tile.rs b/crates/metal2vulkan-runtime/src/pipeline/tile.rs index 01cc6457a..8e0a43e33 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/tile.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/tile.rs @@ -11,9 +11,12 @@ //! for render target 0", and a member whose type the attachment's format does not hold, which //! Metal's API validation stops (an M3 Max and an M4). //! - **Answers.** A tile pipeline's state answers its descriptor's -//! `threadgroup_size_matches_tile_size`, no indirect command buffers, and the compiled -//! pipeline's `max_total_threads_per_threadgroup`: Metal's 1024, or the descriptor's limit, where -//! the device runs a threadgroup of 1024 threads, else the device's lower limit. +//! `threadgroup_size_matches_tile_size`, no indirect command buffers, and a +//! `max_total_threads_per_threadgroup`. That is the threads of the descriptor's +//! `required_threads_per_threadgroup` where it requires a size, as a compute state's is +//! (`ComputePipelineState::max_total_threads_per_threadgroup`), and otherwise the compiled +//! pipeline's: Metal's 1024, or the descriptor's limit, where the device runs a threadgroup of +//! 1024 threads, else the device's lower limit. //! `imageblock_sample_length` is the aliased explicit imageblock's size, which the compile //! records (`RuntimeFact::AliasedImageblock`), else the attachments' texels packed as Metal packs //! them, after an explicit imageblock that stands alone as a fragment function's diff --git a/crates/metal2vulkan-runtime/src/tests/pipeline.rs b/crates/metal2vulkan-runtime/src/tests/pipeline.rs index 373f95bdf..289af9fbe 100644 --- a/crates/metal2vulkan-runtime/src/tests/pipeline.rs +++ b/crates/metal2vulkan-runtime/src/tests/pipeline.rs @@ -1,9 +1,10 @@ //! The checks a compute pipeline state's descriptor passes before its compile //! (`pipeline::duplicate_linked_name`, by the name each linked function links under), the //! imageblock length it answers -//! (`pipeline::imageblock_memory_length`), the specialization values a failed draw's -//! pipeline names (`pipeline::specialization_values`), and which form of a key a driver's facts -//! choose (`pipeline::views_read_at_bind`). +//! (`pipeline::imageblock_memory_length`), the thread limit a state whose descriptor requires a +//! threadgroup size answers (`pipeline::reported_max_total_threads`), the specialization values a +//! failed draw's pipeline names (`pipeline::specialization_values`), and which form of a key a +//! driver's facts choose (`pipeline::views_read_at_bind`). //! //! The check reads only the linked functions' names, so any library function stands in for a //! visible one. The runtime reaches only the public compile-and-plan layer (`api_surface`), its @@ -12,7 +13,8 @@ //! no tool runs and no `METAL2VULKAN_*` knob is read. use super::super::pipeline::{ - duplicate_linked_name, imageblock_memory_length, specialization_values, views_read_at_bind, + duplicate_linked_name, imageblock_memory_length, reported_max_total_threads, + specialization_values, views_read_at_bind, }; use super::super::values::Size; use metal2vulkan::cache::SpecializationKey; @@ -261,3 +263,42 @@ fn every_committed_driver_reads_its_uniform_views_at_their_residuals() { .collect() )); } + +/// A compute or tile state whose descriptor requires a threadgroup size answers that size's +/// threads as its `maxTotalThreadsPerThreadgroup`. Metal's compute state does the same where Metal +/// compiles it for the first time (an M3 Max, macOS 27.0.1, sources compiled once each): 1 for +/// 1x1x1, 21 for 7x3x1, 128 for 8x8x2, 64 for 1x1x64 and 1024 for 1024x1x1. Metal's tile state +/// rounds the threads down to a multiple of 4 (0 for 1x1, 20 for 7x3), and ours answers the +/// threads. Where the device holds fewer threads than the size, the device's own limit stays. A +/// state that requires no size keeps the compiled pipeline's answer, today's. +#[test] +fn a_required_size_is_the_states_thread_limit() { + for (required, threads) in [ + ([1, 1, 1], 1), + ([2, 2, 1], 4), + ([7, 3, 1], 21), + ([8, 8, 1], 64), + ([8, 8, 2], 128), + ([1, 1, 64], 64), + ([32, 32, 1], 1024), + ([1024, 1, 1], 1024), + ] { + assert_eq!( + reported_max_total_threads(Some(required), 1024), + threads, + "{required:?}" + ); + } + // The device's lower limit stays where the size is past it. + assert_eq!(reported_max_total_threads(Some([32, 32, 1]), 960), 960); + assert_eq!(reported_max_total_threads(Some([16, 16, 1]), 960), 256); + // No size: the compiled pipeline's answer, whatever it is. + for compiled in [1024, 960, 512, 64] { + assert_eq!(reported_max_total_threads(None, compiled), compiled); + } + // Axes whose product passes u32 do not wrap. + assert_eq!( + reported_max_total_threads(Some([u32::MAX, u32::MAX, 2]), 1024), + 1024 + ); +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/compute_threadgroups.rs b/crates/metal2vulkan-validation/src/runtime_lane/compute_threadgroups.rs index 6989944aa..4346b43a3 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/compute_threadgroups.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/compute_threadgroups.rs @@ -6,13 +6,20 @@ //! (lavapipe) holds 512 of its threads a threadgroup, where Metal holds 1024. Its state answers //! the device's number; a threadgroup past Metal's 1024 runs nothing, as plain Metal's does; one //! within Metal's limit but past the device's is the device's gap, refused typed in every dispatch -//! form, and never run as nothing. +//! form, and never run as nothing. A state whose descriptor requires a threadgroup size answers +//! that size's threads, within the device's number. use super::indirect_host_steps::state; use super::tile_states::on_device; use super::{buffer_with, read_buffer, WAIT}; +use crate::air_library::AirLibrary; use metal2vulkan::error::{BindError, Feature}; -use metal2vulkan_runtime::{CommandBufferStatus, ComputeCommandEncoder, Device, Error, Size}; +use metal2vulkan::pipeline::{ComputePipelineDescriptor, StageSource}; +use metal2vulkan_devtools::{engine_dir, llvm_assemble}; +use metal2vulkan_runtime::{ + CommandBufferStatus, ComputeCommandEncoder, ComputePipelineState, Device, Error, + PipelineOptions, Size, +}; /// Metal's threads a threadgroup. const METAL: u64 = 1024; @@ -149,3 +156,44 @@ fn a_compute_threadgroup_past_the_devices_limit_within_metals_is_refused_typed() } }); } + +/// The state of the owned fixture `stem`'s kernel, its descriptor requiring `required` threads per +/// threadgroup. +fn required_state(device: &Device, stem: &str, required: [u32; 3]) -> ComputePipelineState { + let bitcode = llvm_assemble(&engine_dir().join(format!("{stem}.ll"))).unwrap(); + let library = device.new_library_with_air(&bitcode).unwrap(); + let function = library.new_function_with_name(stem).unwrap(); + let mut descriptor = + ComputePipelineDescriptor::new(StageSource::Function(function.function_descriptor())); + descriptor.required_threads_per_threadgroup = Some(required); + device + .new_compute_pipeline_state(&descriptor, &PipelineOptions::default()) + .unwrap() +} + +/// A compute state whose descriptor requires a threadgroup size answers that size's threads as +/// its `maxTotalThreadsPerThreadgroup`, as Metal's compute state does where Metal compiles it for +/// the first time (an M3 Max, macOS 27.0.1: 1 for 1x1x1, 21 for 7x3x1, 128 for 8x8x2). Where the +/// device holds fewer threads than the size, the answer is the device's limit, which the state +/// with no size answers. +#[test] +fn a_compute_state_with_a_required_size_answers_its_threads() { + on_device(|device| { + let stem = "ttg_compute_simd_scratch"; + let unrequired = state(device, stem, false).max_total_threads_per_threadgroup(); + assert!((32..=1024).contains(&unrequired), "{unrequired}"); + for (required, threads) in [([1, 1, 1], 1), ([7, 3, 1], 21), ([8, 8, 2], 128)] { + let made = required_state(device, stem, required); + assert_eq!( + made.max_total_threads_per_threadgroup(), + threads.min(unrequired), + "{required:?}" + ); + let [width, height, depth] = required.map(u64::from); + assert_eq!( + made.required_threads_per_threadgroup(), + Size::new(width, height, depth) + ); + } + }); +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/tile_states.rs b/crates/metal2vulkan-validation/src/runtime_lane/tile_states.rs index ca6eae0c7..43f01658b 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/tile_states.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/tile_states.rs @@ -7,7 +7,8 @@ //! the attachments' texels packed, or the aliased layout's size whatever the formats pack to; its //! memory length for a tile is that times the tile's cells, unrounded; it supports no indirect //! command buffers and echoes the descriptor's `threadgroupSizeMatchesTileSize`. Its thread limit -//! is Metal's 1024 where the device runs it, lowered by the descriptor's. +//! is Metal's 1024 where the device runs it, lowered by the descriptor's. Where the descriptor +//! requires a threadgroup size, the limit is that size's threads. //! - Creation refuses what Metal's does: a function reaching an attachment the descriptor gives //! no format. //! - A binary archive holds a tile pipeline it was given (`add_tile_render_pipeline_functions`), @@ -136,6 +137,39 @@ fn a_tile_state_answers_metals_queries() { }); } +/// A tile state whose descriptor requires a threadgroup size answers that size's threads as its +/// `maxTotalThreadsPerThreadgroup`, as Metal's tile state does where Metal compiles it for the +/// first time (an M3 Max, macOS 27.0.1: 64 for 8x8 and 256 for 16x16). It answers 1 for 1x1, +/// where Metal rounds the threads down to a multiple of 4 and answers 0 (docs/API.md, accepted +/// differences). Where the device holds fewer threads than the size, the answer is the device's +/// limit. The same descriptor with no size keeps today's answer. +#[test] +fn a_tile_state_with_a_required_size_answers_its_threads() { + on_device(|device| { + let mut grid = descriptor(device, "k10ts_tile_grid", &GRID); + let unrequired = state(device, &grid) + .unwrap() + .max_total_threads_per_threadgroup(); + assert!((1..=1024).contains(&unrequired), "{unrequired}"); + for (required, threads) in [([1, 1, 1], 1), ([8, 8, 1], 64), ([16, 16, 1], 256)] { + grid.required_threads_per_threadgroup = Some(required); + let made = state(device, &grid).unwrap(); + assert_eq!( + made.max_total_threads_per_threadgroup(), + threads.min(unrequired), + "{required:?}" + ); + } + grid.required_threads_per_threadgroup = None; + assert_eq!( + state(device, &grid) + .unwrap() + .max_total_threads_per_threadgroup(), + unrequired + ); + }); +} + #[test] fn a_tile_state_is_refused_where_metal_refuses_it() { on_device(|device| { diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index c70133a58..6180afd51 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -546,7 +546,9 @@ pub struct ComputePipelineMetalInfo { /// simdgroups' scratch, by the function's own maximum and by the descriptor's (a compute or /// tile descriptor's `max_total_threads_per_threadgroup`). A tile pipeline's /// (`Device::compile_tile_pipeline`) is at most 1024, the 32x32 tile threadgroup, and is the - /// largest threadgroup of even sides within those limits, as Metal rounds it. + /// largest threadgroup of even sides within those limits, as Metal rounds it. A descriptor's + /// `required_threads_per_threadgroup` does not change it. Metal's state answers that size's + /// threads instead, and so does `metal2vulkan-runtime`'s. pub max_total_threads_per_threadgroup: u32, /// `maxTotalThreadsPerThreadgroup` on Apple's GPUs, before this device's limits: the least /// of 1024, the function's own maximum and the descriptor's (a compute pipeline answers a diff --git a/crates/metal2vulkan/src/pipeline/descriptor.rs b/crates/metal2vulkan/src/pipeline/descriptor.rs index 3377d03a6..90decafbf 100644 --- a/crates/metal2vulkan/src/pipeline/descriptor.rs +++ b/crates/metal2vulkan/src/pipeline/descriptor.rs @@ -660,8 +660,9 @@ pub struct ComputePipelineDescriptor { pub max_call_stack_depth: u32, /// `requiredThreadsPerThreadgroup`: `None` for Metal's zero size, no requirement. The runtime /// refuses a dispatch of another threadgroup size, as Metal's API validation stops one. The - /// pipeline does not depend on it (the state's `maxTotalThreadsPerThreadgroup` stays the - /// kernel's, as Metal's does), so it is not part of the key. + /// compiled pipeline does not depend on it, so it is not part of the key, and its + /// `metal_info` answers the kernel's limit. The runtime's state answers the size's threads as + /// its `maxTotalThreadsPerThreadgroup`, as Metal's state does. pub required_threads_per_threadgroup: Option<[u32; 3]>, /// `supportAddingBinaryFunctions`: binary functions may be added to a state made from this /// descriptor. Not part of the key. @@ -768,7 +769,9 @@ pub struct TileRenderPipelineDescriptor { pub max_call_stack_depth: u32, /// `requiredThreadsPerThreadgroup`: `None` for Metal's zero size, no requirement. The runtime /// refuses a tile dispatch of another threadgroup size, as Metal's API validation stops one. - /// Not part of the key. + /// Not part of the key. The runtime's state answers the size's threads as its + /// `maxTotalThreadsPerThreadgroup`, as a compute descriptor's + /// ([`ComputePipelineDescriptor::required_threads_per_threadgroup`]). pub required_threads_per_threadgroup: Option<[u32; 3]>, /// `supportAddingBinaryFunctions`: binary functions may be added to the tile stage of a /// state made from this descriptor. Not part of the key. diff --git a/docs/API.md b/docs/API.md index 42b7a8f30..b147ef4cf 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4010,6 +4010,22 @@ each item named states the rule in full. - **A simdgroup barrier in divergent code waits for its tangle**, as SPIR-V defines it. The Metal Shading Language requires every thread of the SIMD-group to execute a `simdgroup_barrier` that any of them executes in a conditional or a loop iteration, so Metal defines no answer there. +- **A state whose descriptor requires a threadgroup size answers that size's threads** as its + `maxTotalThreadsPerThreadgroup`, lowered only to the device's own limit + (`ComputePipelineState::max_total_threads_per_threadgroup`, and a tile state's + `RenderPipelineState::max_total_threads_per_threadgroup`). A compute state compiled by Metal + for the first time answers the same (M3 Max, macOS 27.0.1, each state of a source compiled + once: 1 for 1x1x1, 21 for 7x3x1, 128 for 8x8x2, 1024 for 32x32x1). The runtime differs in two + ways: + - A tile state answers the size's threads and runs a dispatch of its size. Metal's tile state + rounds the threads down to a multiple of 4 (0 for 1x1, 2x1 and 3x1, 8 for 3x3, 20 for 7x3), + and its dispatch of a size that is not a multiple of 4 runs no thread, with the command + buffer completed and no error. + - Each state answers its own size. Metal hands a state of a function it has already compiled + under another size the earlier state's limit, in the same process or a later one. An 8x8 + tile state then answered 16 after a 4x4 one, and its 8x8 dispatch ran no thread. A compute + state of the same `MTLFunction` answered 16 after a 4x4x1 state, with an 8x8x1 size and with + none, where the runtime's answers 64 and the compiled limit. **Process exit**