From 0d063a8cec25e87062e2250de44a67cdc2bb083d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 08:48:14 -0800 Subject: [PATCH] generic_pointers fixture: generic_array, an array member of generic pointers passed as one aggregate The owned fixture and its probe gain `generic_array`: a `ptrs` struct whose three generic pointers are an array member, which Clang's AIR ABI passes to the noinline `read_ptrs` as one `[3 x ptr addrspace(8)]` aggregate (struct fields alone are flattened into scalars). Its words match `generic_fields`'s, so the lane test compares it with the same table; the engine test compiles it on the committed profiles. The companions (.ll, .air.ll) are regenerated on the M3 in the next commit. --- .../probes/generic_pointers.m | 39 ++++++++++++++++--- .../public/engine/generic_pointers.metal | 36 +++++++++++++++++ .../src/runtime_lane/generic_pointers.rs | 18 +++++---- .../src/tests/engine_generic_pointers.rs | 11 ++++-- 4 files changed, 87 insertions(+), 17 deletions(-) diff --git a/crates/metal2vulkan-conformance/probes/generic_pointers.m b/crates/metal2vulkan-conformance/probes/generic_pointers.m index f951722f2..fed509a37 100644 --- a/crates/metal2vulkan-conformance/probes/generic_pointers.m +++ b/crates/metal2vulkan-conformance/probes/generic_pointers.m @@ -3,15 +3,15 @@ // functions that take them, and asked which space they point into (`is_device_addrspace`, // `is_thread_addrspace`, `is_threadgroup_addrspace`). The owned fixture // `engine/generic_pointers.metal` holds the same source for `generic_spaces`, `generic_mixed`, -// `generic_null`, `generic_slots` and `generic_fields` (its header gives each word); -// `generic_null` asks which space a null generic pointer answers, where the run decides whether a -// pointer is null, and `generic_slots` and `generic_fields` read generic pointers a struct holds, -// by reference and by value. +// `generic_null`, `generic_slots`, `generic_fields` and `generic_array` (its header gives each +// word); `generic_null` asks which space a null generic pointer answers, where the run decides +// whether a pointer is null, and the last three read generic pointers a struct holds, by +// reference, by value and in an array member passed as one aggregate. // // Build (no cargo target needed): // clang -fobjc-arc -O1 -fmodules -o generic_pointers generic_pointers.m \ // -framework Foundation -framework Metal -// Run: ./generic_pointers spaces (or mixed, null, slots, fields), one case a process, +// Run: ./generic_pointers spaces (or mixed, null, slots, fields, array), one case a process, // MTL_DEBUG_LAYER unset. // // Each case compiles the source as MSL 4.1, runs its kernel over 8 threads in one threadgroup @@ -144,6 +144,34 @@ static const char *SOURCE = " r.n = t;\n" " read_fields(r, out + 4 * t, t);\n" " }\n" + "}\n" + "struct ptrs {\n" + " const float *p[3];\n" + " const float *q;\n" + " uint n;\n" + "};\n" + "__attribute__((noinline)) void read_ptrs(ptrs r, device float *o, uint t) {\n" + " o[0] = weigh(r.p[0], t);\n" + " o[1] = weigh(r.q, 1);\n" + " o[2] = space(r.p[2]);\n" + " o[3] = float(r.n);\n" + "}\n" + "kernel void generic_array(device float *out [[buffer(0)]],\n" + " device const float *in [[buffer(1)]],\n" + " uint t [[thread_position_in_grid]]) {\n" + " switch (t >> 31) {\n" + " float local[2];\n" + " ptrs r;\n" + " default:\n" + " local[0] = float(t);\n" + " local[1] = float(t) + 1.0f;\n" + " r.p[0] = in;\n" + " r.p[1] = in + 4;\n" + " r.p[2] = in[t] < 4.0f ? in + 8 : nullptr;\n" + " r.q = local;\n" + " r.n = t;\n" + " read_ptrs(r, out + 4 * t, t);\n" + " }\n" "}\n"; enum { THREADS = 8, WORDS = 8 }; @@ -168,6 +196,7 @@ int main(int argc, char **argv) { : strcmp(c, "null") == 0 ? @"generic_null" : strcmp(c, "slots") == 0 ? @"generic_slots" : strcmp(c, "fields") == 0 ? @"generic_fields" + : strcmp(c, "array") == 0 ? @"generic_array" : @"generic_spaces"; id f = [lib newFunctionWithName:name]; id ps = [D newComputePipelineStateWithFunction:f error:&e]; diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/generic_pointers.metal b/crates/metal2vulkan-validation/fixtures/public/engine/generic_pointers.metal index 05e84f644..1d5dc15c0 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/generic_pointers.metal +++ b/crates/metal2vulkan-validation/fixtures/public/engine/generic_pointers.metal @@ -33,6 +33,11 @@ // (`generic_fields`); each writes out[4t + j]: [0] weigh(r.a, t) = t + 100; [1] weigh(r.b, 1) = // t + 201; [2] space(r.c): 1 for t < 4, 4 for the rest; [3] r.n = t. // +// `generic_array`: as `generic_fields`, the struct a `ptrs` whose three device pointers (in, +// in + 4, and in + 8 or null) are an array member, which Clang passes by value as one aggregate +// of generic pointers: out[4t + j] is [0] weigh(r.p[0], t) = t + 100; [1] weigh(r.q, 1) = t + 201 +// (r.q = local); [2] space(r.p[2]): 1 for t < 4, 4 for the rest; [3] r.n = t. +// // The locals are declared ahead of the switch's only label (on t >> 31, zero for every thread), // so every path into the switch bypasses their declarations and Clang emits no lifetime markers // for them (as in ob2_sized_array.metal). Loop-free. @@ -168,3 +173,34 @@ kernel void generic_fields(device float *out [[buffer(0)]], read_fields(r, out + 4 * t, t); } } + +struct ptrs { + const float *p[3]; + const float *q; + uint n; +}; + +__attribute__((noinline)) void read_ptrs(ptrs r, device float *o, uint t) { + o[0] = weigh(r.p[0], t); + o[1] = weigh(r.q, 1); + o[2] = space(r.p[2]); + o[3] = float(r.n); +} + +kernel void generic_array(device float *out [[buffer(0)]], + device const float *in [[buffer(1)]], + uint t [[thread_position_in_grid]]) { + switch (t >> 31) { + float local[2]; + ptrs r; + default: + local[0] = float(t); + local[1] = float(t) + 1.0f; + r.p[0] = in; + r.p[1] = in + 4; + r.p[2] = in[t] < 4.0f ? in + 8 : nullptr; + r.q = local; + r.n = t; + read_ptrs(r, out + 4 * t, t); + } +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/generic_pointers.rs b/crates/metal2vulkan-validation/src/runtime_lane/generic_pointers.rs index 8533e8adf..37a20af46 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/generic_pointers.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/generic_pointers.rs @@ -2,8 +2,9 @@ //! fixture `generic_pointers`'s `generic_spaces` (generic pointers into the device, thread and //! threadgroup spaces, read, written and asked their space) and `generic_null` (generic pointers //! the run keeps or makes null, asked their space) answer what the M3 Max answers -//! (`generic_pointers.m`), and so do `generic_slots` and `generic_fields` (generic pointers a -//! struct holds, read back from the thread's struct by reference and passed by value). +//! (`generic_pointers.m`), and so do `generic_slots`, `generic_fields` and `generic_array` (generic +//! pointers a struct holds, read back from the thread's struct by reference, passed by value and +//! passed in an array member). //! //! Each kernel runs eight threads in one threadgroup over `in[i] = i` (16 words); the fixture's //! header says what each word is. Every answer is a small integer or a half, exact in `float`. @@ -120,9 +121,10 @@ fn generic_pointers_answer_as_metal() { layer_reported_nothing_at_all(&messages); } -/// What the M3 Max answers (macOS 27.0.1) for `generic_slots` and `generic_fields` alike, four -/// words a thread: a read through the device pointer and through the thread pointer a `refs` -/// holds, the space of the second device pointer it holds or of null (1, then 4), and its count. +/// What the M3 Max answers (macOS 27.0.1) for `generic_slots`, `generic_fields` and +/// `generic_array` alike, four words a thread: a read through a device pointer and through the +/// thread pointer the struct holds, the space of a second device pointer it holds or of null (1, +/// then 4), and its count. #[rustfmt::skip] const SLOTS: [[f32; 4]; THREADS] = [ [100.0, 201.0, 1.0, 0.0], @@ -136,15 +138,15 @@ const SLOTS: [[f32; 4]; THREADS] = [ ]; /// Generic pointers a struct holds, stored into the thread's struct and read back by reference -/// (`generic_slots`) or passed by value (`generic_fields`), read and answer their spaces as Metal -/// does. +/// (`generic_slots`), passed by value (`generic_fields`) or passed in an array member that is one +/// aggregate (`generic_array`), read and answer their spaces as Metal does. #[test] fn generic_pointers_a_struct_holds_answer_as_metal() { let messages = Messages::default(); let cache = ScratchCache::new().unwrap(); { let device = validated_device_with(TEST_DEVICE, &messages, cache.path(), |_| {}).unwrap(); - for entry in ["generic_slots", "generic_fields"] { + for entry in ["generic_slots", "generic_fields", "generic_array"] { let got = dispatched(&device, entry, 4 * THREADS); for (t, (got, want)) in got.chunks(4).zip(SLOTS.iter()).enumerate() { assert_eq!(got, want, "{entry}: thread {t}"); diff --git a/crates/metal2vulkan/src/tests/engine_generic_pointers.rs b/crates/metal2vulkan/src/tests/engine_generic_pointers.rs index ca7a401c8..027d99204 100644 --- a/crates/metal2vulkan/src/tests/engine_generic_pointers.rs +++ b/crates/metal2vulkan/src/tests/engine_generic_pointers.rs @@ -1,9 +1,11 @@ //! MSL 4.1's generic pointers, as compute pipelines of the owned fixture `engine/generic_pointers` //! on committed profiles: `generic_spaces` (generic pointers into the device, thread and -//! threadgroup spaces, each of one space, read, written and asked their space) and `generic_null` -//! (generic pointers the run may make null) compile; `generic_mixed` (one generic pointer into the -//! thread or the device space as the run decides) is refused as a generic pointer of two spaces -//! (`GenericPointer::TwoSpaces`). +//! threadgroup spaces, each of one space, read, written and asked their space), `generic_null` +//! (generic pointers the run may make null), and `generic_slots`, `generic_fields` and +//! `generic_array` (generic pointers a struct holds: in the thread's struct read by reference, its +//! fields passed by value, and an array member passed as one aggregate) compile; `generic_mixed` +//! (one generic pointer into the thread or the device space as the run decides) is refused as a +//! generic pointer of two spaces (`GenericPointer::TwoSpaces`). use crate::air::ir::print::print; use crate::budget::Budget; @@ -64,6 +66,7 @@ fn generic_pointers_of_one_space_compile() { "generic_null", "generic_slots", "generic_fields", + "generic_array", ] { if let Err(e) = compiled(&library, stem, entry) { panic!("{stem} {entry}: {e}\n{}", specialized(&bc, stem, entry));