Give each tile threadgroup size a Metal pipeline of its own: ORACLE_ABI v49

The tile oracle now sets the tile pipeline descriptor's maxTotalThreadsPerThreadgroup to the
dispatch's threads, beside the requiredThreadsPerThreadgroup it already set.

Why: Metal keeps a tile pipeline it has compiled. For a later descriptor of the same function
that differs only in requiredThreadsPerThreadgroup, it hands back that pipeline and its
maxTotalThreadsPerThreadgroup, in the same process and in later ones (measured on the M3 Max,
macOS 27.0.1, with salted from-scratch kernels). A case's outcome therefore followed whichever
case of its fixture ran first:
- In the reference re-measure, the 8x8 case of k10ts_tile_order (81c239f3…) ran after its 4x4
  case and got 16: "Metal tile dispatch asks for 64 threads a threadgroup ([8, 8, 1]), but the
  pipeline permits 16".
- The 1x1 case qualified only when a larger size's pipeline was handed back.
- Such a pipeline also runs nothing: an 8x8 state that reports 16 runs no thread of its 8x8
  dispatch.

The descriptor's own limit separates the pipelines. With it set to the size's threads, each size
reports its own limit and runs all of its threads (16, 64 and 256 for 4x4, 8x8 and 16x16), in
the same process and in a later one. A first compile of a required size reports that limit
anyway, and Metal refuses a descriptor limit above it ("must match"), so the limit adds no
constraint of its own. Metal still answers what it answers without a cache: the size's threads
rounded down to a multiple of 4.

ORACLE_ABI moves from v48 to v49: what the oracle compiles changed. Every Metal row recorded under
v48 stops being current evidence until it is re-qualified, which the commits that follow do.

Verified with the release corpus-metal of this tree, reference environment, single runs:
- e307478f… (2x2), 5e61d5c4… (4x4) and 81c239f3… (8x8) qualify, each with its committed output.
- b879f660… (1x1) is refused: "Metal tile dispatch asks for 1 threads a threadgroup ([1, 1, 1]),
  but the pipeline permits 0".

(cherry picked from commit 193875e7393810840c517a9d9d5ea127a0e317ec)
(cherry picked from commit ac29781634d83f9d1b814f0156b3d01ad320d84b)
This commit is contained in:
Anees Iqbal committed 2026-11-03 09:25:28 -08:00
1 parent 3cbc39b51c
commit 4def63989b
3 files changed
+28 -9

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@ use std::sync::Arc;
/// change no recorded row can observe (a case kind no earlier oracle ran, or one it refused) needs
/// no bump; its commit says why. The commit that bumps it says what changed; this comment keeps no
/// history.
pub const ORACLE_ABI: &str = "metal-literal-resources-v48";
pub const ORACLE_ABI: &str = "metal-literal-resources-v49";
/// The case's own module: the AIR the oracle compiles for it and how it bounds its execution.
#[derive(Clone)]
@@ -1230,12 +1230,7 @@ pub(super) fn threads_per_threadgroup_within_limit(
limit: usize,
what: &str,
) -> Result<(), String> {
let total = threads_per_threadgroup
.iter()
.try_fold(1usize, |total, extent| total.checked_mul(*extent as usize))
.ok_or_else(|| {
format!("{what} threadgroup {threads_per_threadgroup:?} overflows a thread count")
})?;
let total = threads_per_threadgroup_total(threads_per_threadgroup, what)?;
if total > limit {
return Err(format!(
"{what} dispatch asks for {total} threads a threadgroup ({threads_per_threadgroup:?}), but the pipeline permits {limit}"
@@ -1244,6 +1239,19 @@ pub(super) fn threads_per_threadgroup_within_limit(
Ok(())
}
/// The threads of a threadgroup of `threads_per_threadgroup`, refused where they overflow a count.
pub(super) fn threads_per_threadgroup_total(
threads_per_threadgroup: [u32; 3],
what: &str,
) -> Result<usize, String> {
threads_per_threadgroup
.iter()
.try_fold(1usize, |total, extent| total.checked_mul(*extent as usize))
.ok_or_else(|| {
format!("{what} threadgroup {threads_per_threadgroup:?} overflows a thread count")
})
}
fn mtl_size(size: [u32; 3]) -> MTLSize {
MTLSize {
width: size[0] as usize,
@@ -9,8 +9,8 @@ use super::{
make_acceleration_structures, make_argument_buffer_buffers, make_argument_buffer_textures,
make_buffers, make_function, make_indirect_commands, make_render_function_tables,
make_render_targets, make_samplers, make_texture_arrays, make_textures, mtl_size,
selected_output, threads_per_threadgroup_within_limit, MetalContext, MetalOutputResources,
NativeBuffers, NativePipeline, ShaderFacts, Texture,
selected_output, threads_per_threadgroup_total, threads_per_threadgroup_within_limit,
MetalContext, MetalOutputResources, NativeBuffers, NativePipeline, ShaderFacts, Texture,
};
use crate::case::{AuthoredCase, ImplicitImageblockCoverage};
use crate::gpu::compile;
@@ -81,6 +81,17 @@ pub(super) fn execute_tile(
pipeline_descriptor.setThreadgroupSizeMatchesTileSize(false);
pipeline_descriptor
.setRequiredThreadsPerThreadgroup(mtl_size(dispatch.threads_per_threadgroup));
// The limit is the required size's threads, the limit Metal answers for such a pipeline it
// compiles for the first time. Metal keeps a tile pipeline it has compiled and returns it for
// a later descriptor of the same function that differs only in `requiredThreadsPerThreadgroup`,
// in this process or a later one, with the first pipeline's limit (measured on an M3 Max,
// macOS 27.0.1). A case's limit would then follow whichever case of its function ran first:
// the 8x8 case of a fixture got 16 after its 4x4 case. The descriptor's own limit separates
// the pipelines, so each threadgroup size gets a pipeline of its own.
pipeline_descriptor.setMaxTotalThreadsPerThreadgroup(threads_per_threadgroup_total(
dispatch.threads_per_threadgroup,
"Metal tile",
)?);
if let Some(linked) = &linked.descriptor {
pipeline_descriptor.setLinkedFunctions(Some(linked));
}