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)
This commit is contained in:
Anees Iqbal committed 2026-11-03 07:56:21 -08:00
1 parent e23bd582ca
commit 0e7e09a064
12 files changed
+218 -24

No files matched your search

+16
View File
@@ -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**