rt: the unit-axes test runs unit_axes_walk, whose emulated walk states the relief

simd_divergent's trip_k (a trip count from the thread's index) states no unit-axes relief on any
device: the legs (m3-max-42278) ran its sums right and found neither its plain module nor its
emulated variant stating UnitAxes, so the test did not show that a module stating it ran. The
relief follows a position along an axis, so the test now runs the owned fixture unit_axes_walk:
a loop of (y & 3) + 1 iterations at grid row y around a simdgroup sum, in one threadgroup of
8 x 8 and one of 8 x 10 threads.

The fixture's companions are regenerated by scripts/regen-air-fixtures on the M3 Max
(m3-max-42286), where Metal (macOS 27.0.1) answers every thread of both threadgroups as the test
expects: the sum over its iterations i of i + 1 times the threads of its simdgroup whose own trip
count exceeds i.
This commit is contained in:
Anees Iqbal committed 2026-11-17 13:50:21 -08:00
1 parent 1bb19ba769
commit 069e4753ac
5 files changed
+167 -17

No files matched your search

+19 -17
View File
@@ -791,21 +791,21 @@ mod on_device {
});
}
/// `trip_k` of `simd_divergent`: a loop whose trip count is the thread's, each iteration a
/// SIMD-group sum over the threads still in it, in threadgroups of 64 threads and of 80 (a
/// partial SIMD group of 16 last). Where its SIMD groups are emulated its walk is predicated,
/// and the translator states the unit axes that would make it less divergent
/// (`RuntimeFact::UnitAxes` with no axes): the module is the general one, and every device
/// creates the state and runs both sizes, on the module whose walk states it where its tier
/// emulates SIMD groups, and on the emulated module at 80 where it runs only whole ones. Every
/// answer is Metal's: thread `t` adds, at each iteration `i` below `t & 7`, `i + 1` times the
/// threads of its SIMD group still in the loop.
/// `unit_axes_walk`: a loop of `(y & 3) + 1` iterations at row `y`, each a SIMD-group sum over
/// the threads still in it, in one threadgroup of 8 x 8 threads and one of 8 x 10 (a partial
/// SIMD group of 16 last). Where its SIMD groups are emulated its walk is predicated, and the
/// translator states the unit axes that would make it less divergent (`RuntimeFact::UnitAxes`
/// with no axes): the module is the general one, and every device creates the state and runs
/// both sizes, on the module whose walk states it where its tier emulates SIMD groups, and on
/// the emulated module at 8 x 10 where it runs only whole ones. Every answer is Metal's (M3 Max,
/// macOS 27.0.1): thread `t` adds, at each iteration `i` of its own, `i + 1` times the threads
/// of its SIMD group whose own trip count exceeds `i`.
#[test]
fn every_device_runs_a_kernel_whose_walk_unit_axes_could_relieve() {
use crate::pipeline::Lowered;
use metal2vulkan::pipeline::RuntimeFact;
let Some(descriptor) = descriptor_of("simd_divergent", "trip_k") else {
let Some(descriptor) = descriptor("unit_axes_walk") else {
return;
};
let StageSource::Function(function) = &descriptor.kernel else {
@@ -814,7 +814,8 @@ mod on_device {
each_device("compute_unit_axes", |name, _, device| {
let state = state(name, &device, &descriptor);
let queue = device.new_command_queue(4).expect("a queue");
for size in [64, 80] {
for rows in [8, 10] {
let size = 8 * rows;
let mut buffers = Buffers::new(&device, 1 << 16);
let out = buffers.buffer(u64::from(size) * 4);
let command_buffer = queue.command_buffer().expect("a command buffer");
@@ -824,19 +825,20 @@ mod on_device {
encoder.set_compute_pipeline_state(&state).expect("a state");
encoder.set_buffer(Some(&out), 0, 0).expect("a buffer");
encoder
.dispatch_threadgroups([1, 1, 1], [size, 1, 1])
.unwrap_or_else(|error| panic!("{name}: a threadgroup of {size}: {error}"));
.dispatch_threadgroups([1, 1, 1], [8, rows, 1])
.unwrap_or_else(|error| panic!("{name}: a threadgroup of 8 x {rows}: {error}"));
encoder.end_encoding();
command_buffer.commit().expect("a commit");
finished(name, &command_buffer);
assert_eq!(command_buffer.error(), None, "{name}");
let trip = |u: u32| ((u / 8) & 3) + 1;
let want: Vec<u32> = (0..size)
.map(|t| {
let base = t - t % 32;
let simdgroup = base..(base + 32).min(size);
(0..(t & 7))
(0..trip(t))
.map(|i| {
let still = simdgroup.clone().filter(|&u| (u & 7) > i).count();
let still = simdgroup.clone().filter(|&u| trip(u) > i).count();
(i + 1) * still as u32
})
.sum()
@@ -845,7 +847,7 @@ mod on_device {
assert_eq!(
words(&out, 0, want.len()),
want,
"{name}: the sums in a threadgroup of {size}"
"{name}: the sums in a threadgroup of 8 x {rows}"
);
}
// Which modules state the relief: the plain one, and the emulated variant.
@@ -884,7 +886,7 @@ mod on_device {
);
println!(
"{name}: unit axes with no axes stated by the {} module, which ran; sums checked \
in threadgroups of 64 and 80",
in threadgroups of 8 x 8 and 8 x 10",
if plain { "plain" } else { "emulated" }
);
});
@@ -288,6 +288,7 @@ depth buffer 2 holds, bounded by its stack); an authored case that executes a lo
| `fold_static_init_widen` | `k` | a program-scope `uint4` built from a `uchar4` function constant (`air.convert.u.v4i32.u.v4i8` in the static initializer) and a `uint` from a `uchar` one; thread i writes `kWide + kSelWide + i` | specialized, it compiles, and another constant value is other words (`tests::engine_specialized_folds`) |
| `fold_config_kernel` | `fold_config` | a kernel in `fold_config_struct`'s shape: the configuration struct (two layers' present flags, selectors and `uchar4` opcodes widened to `uint4`, a layer count) filled by the static initializer, or with `kGeneric` read from buffer 1 instead; two layers of a five-way selector and four opcodes of a 16-case integer switch (one arm a `float` multiply and add, exact for every input), in loops of constant trip count; one read at a run-time index | Metal's words for each of six cases, four specializations and two of the generic path |
| `pad_simd_shape` | `simd_shape` | each thread writes its simdgroup index, its lane, the threads of its simdgroup (`simd_sum(1u)`) and its index in the threadgroup | Metal (M3 Max, macOS 27.0.1) puts thread `i` in lane `i % 32` of simdgroup `i / 32`, of `min(32, n - 32 (i / 32))` threads, at threadgroups of 80, 1, 32, 64 and 40: the runtime's device test `every_device_lays_out_simdgroups_as_metal_does` holds every module that runs a size to it |
| `unit_axes_walk` | `unit_axes_walk` | a loop of `(y & 3) + 1` iterations at grid row `y`, each adding `i + 1` times `simd_sum(1u)`, the threads of the simdgroup still in it; `out[t]` the total | Metal (M3 Max, macOS 27.0.1) answers each thread's sum over its simdgroup's threads still in the loop in threadgroups of 8 x 8 and 8 x 10; emulated, its walk is predicated and states `RuntimeFact::UnitAxes` with no axes: the runtime's device test `every_device_runs_a_kernel_whose_walk_unit_axes_could_relieve` runs it on every device |
The `a3_*` fixtures, the tessellation probes (`k7_*`, sixteen cases), the imageblock fixtures (`k10ex_*`, ten cases), the
pipeline-state fixtures (`k14_*`, one case each), the strategy-coverage
@@ -395,6 +395,7 @@ fixture ttg_compute_simd_scratch: metal: Apple metal version 32023.921 (metalfe-
fixture ttg_tile_simd_blur: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture ubo_bind_offsets: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture uni_argument_tables: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture unit_axes_walk: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture unk_atomic_lanes: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture unk_atomic_ops: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture unk_calculate_lod: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
@@ -0,0 +1,73 @@
; Generated by scripts/regen-air-fixtures from unit_axes_walk.metal (typed pointers; xcrun metal-as reassembles it). Do not edit.
; ModuleID = '-'
source_filename = "-"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
target triple = "air64_v28-apple-macosx26.0.0"
; Function Attrs: convergent mustprogress nounwind
define void @unit_axes_walk(i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %0, <2 x i32> noundef %1, i32 noundef %2) local_unnamed_addr #0 {
%4 = extractelement <2 x i32> %1, i64 1
%5 = and i32 %4, 3
br label %9
6: ; preds = %9
%7 = zext i32 %2 to i64
%8 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %7
store i32 %15, i32 addrspace(1)* %8, align 4, !tbaa !21, !alias.scope !25
ret void
9: ; preds = %3, %9
%10 = phi i32 [ 0, %3 ], [ %13, %9 ]
%11 = phi i32 [ 0, %3 ], [ %15, %9 ]
%12 = tail call i32 @air.simd_sum.u.i32(i32 1) #2
%13 = add nuw nsw i32 %10, 1
%14 = mul i32 %12, %13
%15 = add i32 %14, %11
%16 = icmp eq i32 %10, %5
br i1 %16, label %6, label %9, !llvm.loop !28
}
; Function Attrs: convergent mustprogress nounwind willreturn
declare i32 @air.simd_sum.u.i32(i32) local_unnamed_addr #1
attributes #0 = { convergent mustprogress nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="64" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #1 = { convergent mustprogress nounwind willreturn }
attributes #2 = { convergent nounwind willreturn }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.kernel = !{!9}
!air.compile_options = !{!15, !16, !17}
!llvm.ident = !{!18}
!air.version = !{!19}
!air.language_version = !{!20}
!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{i32 7, !"air.max_device_buffers", i32 31}
!4 = !{i32 7, !"air.max_constant_buffers", i32 31}
!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31}
!6 = !{i32 7, !"air.max_textures", i32 128}
!7 = !{i32 7, !"air.max_read_write_textures", i32 8}
!8 = !{i32 7, !"air.max_samplers", i32 16}
!9 = !{void (i32 addrspace(1)*, <2 x i32>, i32)* @unit_axes_walk, !10, !11}
!10 = !{}
!11 = !{!12, !13, !14}
!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"out"}
!13 = !{i32 1, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint2", !"air.arg_name", !"gid"}
!14 = !{i32 2, !"air.thread_index_in_threadgroup", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"}
!15 = !{!"air.compile.denorms_disable"}
!16 = !{!"air.compile.fast_math_enable"}
!17 = !{!"air.compile.framebuffer_fetch_enable"}
!18 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!19 = !{i32 2, i32 8, i32 0}
!20 = !{!"Metal", i32 4, i32 0, i32 0}
!21 = !{!22, !22, i64 0}
!22 = !{!"int", !23, i64 0}
!23 = !{!"omnipotent char", !24, i64 0}
!24 = !{!"Simple C++ TBAA"}
!25 = !{!26}
!26 = distinct !{!26, !27, !"air-alias-scope-arg(0)"}
!27 = distinct !{!27, !"air-alias-scopes(unit_axes_walk)"}
!28 = distinct !{!28, !29}
!29 = !{!"llvm.loop.mustprogress"}
@@ -0,0 +1,73 @@
; Generated by scripts/regen-air-fixtures from unit_axes_walk.metal (llvm-dis of unit_axes_walk.air). Do not edit.
; ModuleID = '<stdin>'
source_filename = "-"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
target triple = "air64_v28-apple-macosx26.0.0"
; Function Attrs: convergent mustprogress nounwind
define void @unit_axes_walk(ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %0, <2 x i32> noundef %1, i32 noundef %2) local_unnamed_addr #0 {
%4 = extractelement <2 x i32> %1, i64 1
%5 = and i32 %4, 3
br label %9
6: ; preds = %9
%7 = zext i32 %2 to i64
%8 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %7
store i32 %15, ptr addrspace(1) %8, align 4, !tbaa !21, !alias.scope !25
ret void
9: ; preds = %9, %3
%10 = phi i32 [ 0, %3 ], [ %13, %9 ]
%11 = phi i32 [ 0, %3 ], [ %15, %9 ]
%12 = tail call i32 @air.simd_sum.u.i32(i32 1) #2
%13 = add nuw nsw i32 %10, 1
%14 = mul i32 %12, %13
%15 = add i32 %14, %11
%16 = icmp eq i32 %10, %5
br i1 %16, label %6, label %9, !llvm.loop !28
}
; Function Attrs: convergent mustprogress nounwind willreturn
declare i32 @air.simd_sum.u.i32(i32) local_unnamed_addr #1
attributes #0 = { convergent mustprogress nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="64" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #1 = { convergent mustprogress nounwind willreturn }
attributes #2 = { convergent nounwind willreturn }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.kernel = !{!9}
!air.compile_options = !{!15, !16, !17}
!llvm.ident = !{!18}
!air.version = !{!19}
!air.language_version = !{!20}
!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{i32 7, !"air.max_device_buffers", i32 31}
!4 = !{i32 7, !"air.max_constant_buffers", i32 31}
!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31}
!6 = !{i32 7, !"air.max_textures", i32 128}
!7 = !{i32 7, !"air.max_read_write_textures", i32 8}
!8 = !{i32 7, !"air.max_samplers", i32 16}
!9 = !{ptr @unit_axes_walk, !10, !11}
!10 = !{}
!11 = !{!12, !13, !14}
!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"out"}
!13 = !{i32 1, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint2", !"air.arg_name", !"gid"}
!14 = !{i32 2, !"air.thread_index_in_threadgroup", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"}
!15 = !{!"air.compile.denorms_disable"}
!16 = !{!"air.compile.fast_math_enable"}
!17 = !{!"air.compile.framebuffer_fetch_enable"}
!18 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!19 = !{i32 2, i32 8, i32 0}
!20 = !{!"Metal", i32 4, i32 0, i32 0}
!21 = !{!22, !22, i64 0}
!22 = !{!"int", !23, i64 0}
!23 = !{!"omnipotent char", !24, i64 0}
!24 = !{!"Simple C++ TBAA"}
!25 = !{!26}
!26 = distinct !{!26, !27, !"air-alias-scope-arg(0)"}
!27 = distinct !{!27, !"air-alias-scopes(unit_axes_walk)"}
!28 = distinct !{!28, !29}
!29 = !{!"llvm.loop.mustprogress"}