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)