Runtime tests: a kernel's reaches outside its buffer stay off its neighbours in one block

pool_reach places its target between two 256-byte neighbours of one Shared block and reads and
writes word indices inside it, before its first byte and past its end (beyond any slack), at bind
offsets 0 and 16: every read outside reads zero, every write outside lands nowhere, the
neighbours keep their fills, and the dispatch counts in the form the device takes
(bound_dispatches where the translator binds a kernel's buffers, address_dispatches elsewhere).
The fixture's companions are regen-air-fixtures' (job m3-max-75033). Built only; the device runs
follow.
This commit is contained in:
Anees Iqbal committed 2026-11-26 05:04:54 -08:00
1 parent b81d9b1484
commit babb94cf0c
5 files changed
+302

No files matched your search

@@ -91,6 +91,7 @@ mod patch;
mod pixel_format_info;
mod pixel_interlock;
mod places;
mod pool_reach;
mod present;
mod private_arena;
mod private_sinks;
@@ -0,0 +1,168 @@
//! What a kernel's reads and writes outside its buffer reach when the buffer lies between two
//! neighbours in one Shared block, under a device cfg on every device of the cfg's kind. The
//! owned fixture `examples/pool_reach.ll` (its source `pool_reach.metal`) holds `pool_reach`:
//! each thread reads the word `at[i]` words from the bound place of slot 0 into `seen[i]` and
//! then writes its mark, `0xa5a5_0000 | i`, there.
//!
//! Five buffers of 256 bytes are consecutive in one block ([`Arena`]): the neighbour before, the
//! target, the neighbour after, then `seen` and `at`. Every place the indices reach outside the
//! target lies past the slack the runtime README's contract states for a bound buffer (a start
//! off 16 bytes and a length off 4; the target has neither), so on every device each such read
//! is zero and each such write lands nowhere:
//!
//! - bound at offset 0: words 0 and 63 are the target's own (read, then marked); words -1, -4 and
//! -64 lie in the neighbour before, 64, 68 and 127 in the neighbour after, and -2^20 and 2^20
//! far outside the block;
//! - bound at offset 16: words 0 and 59 are the target's 4th and last (read, then marked); word -5
//! is the neighbour before's last and word 60 the neighbour after's first.
//!
//! Each neighbour keeps its fill and each word of the target the indices do not reach keeps its
//! own. A device whose translator binds a kernel's buffers
//! (`DeviceProfile::binds_kernel_buffers`) runs both dispatches in that form, its pipeline's
//! robust buffer access 2 containing them (`Metrics::bound_dispatches`); any other runs them at
//! the target's checked device address (`Metrics::address_dispatches`). The test prints which.
//!
//! `llvm-as` assembles the fixture; without it the test skips visibly.
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
mod on_device {
use crate::tests::command::on_device::{each_device, Arena};
use crate::tests::draw::on_device::{library, run};
use crate::tests::no_constexpr_samplers;
use crate::{Buffer, ComputePipelineDescriptor, DispatchType, FunctionDescriptor, StageSource};
use metal2vulkan_devtools::fixtures_dir;
/// Words in each 256-byte buffer.
const WORDS: usize = 64;
/// The neighbours' fills, and the target's words (`TARGET | index`).
const BEFORE: u32 = 0x1111_1111;
const AFTER: u32 = 0x3333_3333;
const TARGET: u32 = 0x2222_0000;
/// A thread's mark.
const MARK: u32 = 0xa5a5_0000;
fn write(buffer: &Buffer, word: impl Fn(usize) -> u32) {
let contents = buffer.contents().expect("a Shared buffer").cast::<u32>();
for index in 0..WORDS {
// SAFETY: the buffer is alive, 256 bytes long, and no GPU work uses it.
unsafe { contents.as_ptr().add(index).write_volatile(word(index)) };
}
}
fn read(buffer: &Buffer) -> Vec<u32> {
let contents = buffer.contents().expect("a Shared buffer").cast::<u32>();
// SAFETY: the buffer is alive and 256 bytes long, and its work completed.
(0..WORDS)
.map(|index| unsafe { contents.as_ptr().add(index).read_volatile() })
.collect()
}
#[test]
fn every_device_keeps_a_kernel_s_reaches_outside_its_buffer_from_its_neighbours() {
let Some(library) = library(&fixtures_dir().join("examples/pool_reach.ll")) else {
return;
};
let function = library.function("pool_reach").expect("pool_reach");
let descriptor = ComputePipelineDescriptor::new(StageSource::Function(
FunctionDescriptor::new(function),
));
// Each dispatch: the target's bind offset, then each thread's word index and the target
// word it reaches (None outside the target).
let dispatches: [(u64, &[(i32, Option<usize>)]); 2] = [
(
0,
&[
(0, Some(0)),
(63, Some(63)),
(-1, None),
(-4, None),
(-64, None),
(64, None),
(68, None),
(127, None),
(-(1 << 20), None),
(1 << 20, None),
],
),
(16, &[(0, Some(4)), (59, Some(63)), (-5, None), (60, None)]),
];
each_device("pool_reach", |name, _, device| {
let state = (device.new_compute_pipeline_state(&descriptor, no_constexpr_samplers))
.unwrap_or_else(|error| panic!("{name}: {error}"));
let bound = device.translator().profile().binds_kernel_buffers();
let queue = device.new_command_queue(4).expect("a queue");
let mut arena = Arena::new(&device);
let (before, target, after, seen, at) = (
arena.buffer(),
arena.buffer(),
arena.buffer(),
arena.buffer(),
arena.buffer(),
);
for &(offset, reaches) in &dispatches {
let what = format!("{name}: bound at {offset}");
write(&before, |_| BEFORE);
write(&after, |_| AFTER);
write(&target, |index| TARGET | index as u32);
write(&seen, |_| u32::MAX);
write(&at, |index| {
reaches.get(index).map_or(0, |&(word, _)| word as u32)
});
let metrics = device.counters().metrics();
let command_buffer = queue.command_buffer().expect("a command buffer");
let mut encoder = command_buffer
.compute_command_encoder(DispatchType::Serial)
.expect("an encoder");
encoder.set_compute_pipeline_state(&state).expect("a state");
for (slot, (buffer, at_offset)) in [(&target, offset), (&seen, 0), (&at, 0)]
.into_iter()
.enumerate()
{
encoder
.set_buffer(Some(buffer), at_offset, slot as u32)
.expect("a buffer");
}
let threads = reaches.len() as u32;
encoder
.dispatch_threads([threads, 1, 1], [threads, 1, 1])
.unwrap_or_else(|error| panic!("{what}: {error}"));
encoder.end_encoding();
run(&what, &command_buffer);
let after_run = device.counters().metrics();
let grown = (
after_run.bound_dispatches - metrics.bound_dispatches,
after_run.address_dispatches - metrics.address_dispatches,
);
assert_eq!(
grown,
if bound { (1, 0) } else { (0, 1) },
"{what}: dispatches by form"
);
let mut want_target: Vec<u32> = (0..WORDS).map(|i| TARGET | i as u32).collect();
let mut want_seen = vec![u32::MAX; WORDS];
for (thread, &(_, reached)) in reaches.iter().enumerate() {
want_seen[thread] = reached.map_or(0, |index| TARGET | index as u32);
if let Some(index) = reached {
want_target[index] = MARK | thread as u32;
}
}
assert_eq!(read(&seen), want_seen, "{what}: the words read");
assert_eq!(read(&target), want_target, "{what}: the target");
assert_eq!(
read(&before),
[BEFORE; WORDS],
"{what}: the neighbour before"
);
assert_eq!(read(&after), [AFTER; WORDS], "{what}: the neighbour after");
}
let form = match bound {
true => "bound, robust buffer access 2",
false => "checked device address",
};
println!(
"{name}: reads outside a pooled buffer read zero and writes there land nowhere \
({form}), at bind offsets 0 and 16"
);
});
}
}
@@ -9,5 +9,6 @@ fixture dispatch_tiles: metal: Apple metal version 32023.922 (metalfe-32023.922.
fixture heap_draws: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture heap_samples: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture lost_writes: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture pool_reach: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture runtime_examples: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture texel_rows: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
@@ -0,0 +1,66 @@
; Generated by scripts/regen-air-fixtures from pool_reach.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: argmemonly mustprogress nofree norecurse nosync nounwind willreturn
define void @pool_reach(i32 addrspace(1)* nocapture noundef "air-buffer-no-alias" %0, i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %1, i32 addrspace(2)* nocapture noundef readonly "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #0 {
%5 = zext i32 %3 to i64
%6 = getelementptr inbounds i32, i32 addrspace(2)* %2, i64 %5
%7 = load i32, i32 addrspace(2)* %6, align 4, !tbaa !22, !alias.scope !26, !noalias !29
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %8
%10 = load i32, i32 addrspace(1)* %9, align 4, !tbaa !22, !alias.scope !32, !noalias !33
%11 = getelementptr inbounds i32, i32 addrspace(1)* %1, i64 %5
store i32 %10, i32 addrspace(1)* %11, align 4, !tbaa !22, !alias.scope !34, !noalias !35
%12 = or i32 %3, -1515913216
store i32 %12, i32 addrspace(1)* %9, align 4, !tbaa !22, !alias.scope !32, !noalias !33
ret void
}
attributes #0 = { argmemonly mustprogress nofree norecurse nosync nounwind willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "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" }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.kernel = !{!9}
!air.compile_options = !{!16, !17, !18}
!llvm.ident = !{!19}
!air.version = !{!20}
!air.language_version = !{!21}
!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)*, i32 addrspace(1)*, i32 addrspace(2)*, i32)* @pool_reach, !10, !11}
!10 = !{}
!11 = !{!12, !13, !14, !15}
!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", !"target"}
!13 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, 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", !"seen"}
!14 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"int", !"air.arg_name", !"at"}
!15 = !{i32 3, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"i"}
!16 = !{!"air.compile.denorms_disable"}
!17 = !{!"air.compile.fast_math_enable"}
!18 = !{!"air.compile.framebuffer_fetch_enable"}
!19 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!20 = !{i32 2, i32 8, i32 0}
!21 = !{!"Metal", i32 4, i32 0, i32 0}
!22 = !{!23, !23, i64 0}
!23 = !{!"int", !24, i64 0}
!24 = !{!"omnipotent char", !25, i64 0}
!25 = !{!"Simple C++ TBAA"}
!26 = !{!27}
!27 = distinct !{!27, !28, !"air-alias-scope-arg(2)"}
!28 = distinct !{!28, !"air-alias-scopes(pool_reach)"}
!29 = !{!30, !31}
!30 = distinct !{!30, !28, !"air-alias-scope-arg(0)"}
!31 = distinct !{!31, !28, !"air-alias-scope-arg(1)"}
!32 = !{!30}
!33 = !{!31, !27}
!34 = !{!31}
!35 = !{!30, !27}
@@ -0,0 +1,66 @@
; Generated by scripts/regen-air-fixtures from pool_reach.metal (llvm-dis of pool_reach.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: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
define void @pool_reach(ptr addrspace(1) noundef captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %1, ptr addrspace(2) noundef readonly captures(none) "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #0 {
%5 = zext i32 %3 to i64
%6 = getelementptr inbounds i32, ptr addrspace(2) %2, i64 %5
%7 = load i32, ptr addrspace(2) %6, align 4, !tbaa !22, !alias.scope !26, !noalias !29
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %8
%10 = load i32, ptr addrspace(1) %9, align 4, !tbaa !22, !alias.scope !32, !noalias !33
%11 = getelementptr inbounds i32, ptr addrspace(1) %1, i64 %5
store i32 %10, ptr addrspace(1) %11, align 4, !tbaa !22, !alias.scope !34, !noalias !35
%12 = or i32 %3, -1515913216
store i32 %12, ptr addrspace(1) %9, align 4, !tbaa !22, !alias.scope !32, !noalias !33
ret void
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "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" }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.kernel = !{!9}
!air.compile_options = !{!16, !17, !18}
!llvm.ident = !{!19}
!air.version = !{!20}
!air.language_version = !{!21}
!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 @pool_reach, !10, !11}
!10 = !{}
!11 = !{!12, !13, !14, !15}
!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", !"target"}
!13 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, 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", !"seen"}
!14 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"int", !"air.arg_name", !"at"}
!15 = !{i32 3, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"i"}
!16 = !{!"air.compile.denorms_disable"}
!17 = !{!"air.compile.fast_math_enable"}
!18 = !{!"air.compile.framebuffer_fetch_enable"}
!19 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!20 = !{i32 2, i32 8, i32 0}
!21 = !{!"Metal", i32 4, i32 0, i32 0}
!22 = !{!23, !23, i64 0}
!23 = !{!"int", !24, i64 0}
!24 = !{!"omnipotent char", !25, i64 0}
!25 = !{!"Simple C++ TBAA"}
!26 = !{!27}
!27 = distinct !{!27, !28, !"air-alias-scope-arg(2)"}
!28 = distinct !{!28, !"air-alias-scopes(pool_reach)"}
!29 = !{!30, !31}
!30 = distinct !{!30, !28, !"air-alias-scope-arg(0)"}
!31 = distinct !{!31, !28, !"air-alias-scope-arg(1)"}
!32 = !{!30}
!33 = !{!31, !27}
!34 = !{!31}
!35 = !{!30, !27}