diff --git a/crates/metal2vulkan-devtools/src/half_simd_reductions.rs b/crates/metal2vulkan-devtools/src/half_simd_reductions.rs new file mode 100644 index 000000000..2a00faddf --- /dev/null +++ b/crates/metal2vulkan-devtools/src/half_simd_reductions.rs @@ -0,0 +1,289 @@ +//! The authored kernels (inline AIR text, for `llvm-as`) of Metal's `half` simdgroup sums and +//! products, and what Metal answers: the product's CPU-interpreter tests and the runtime's device +//! test both run them ([`module`]). +//! +//! Metal's `simd_sum` and `simd_product` of `half` operands are a tree of `half` operations: in +//! round `d` (1, 2, 4, 8, 16) each aligned block of `2d` lanes holds its lower and upper halves' +//! results combined and rounded to `half`, and a lane that takes no part counts as the operation's +//! identity. Measured on an M3 Max (macOS 27.0.1), fast math on and off alike, with our own MSL: +//! [`SUMS`] (potluck job m3-max-80839), [`PRODUCTS`] (m3-max-80879), and [`MASKED_SUMS`] and +//! [`MASKED_PRODUCTS`] (m3-max-80890). Each answer is that tree's on every vector; the `float` +//! result rounded once differs on five of the sums (`0x3c10` for `0x3c0f` on the first), four of +//! the products and ten of the 48 masked results, and the tree over the active lanes packed +//! together differs on three masked sums. +//! +//! - [`FULL`] (`half_simd_reductions`): one threadgroup of [`THREADS`] threads, eight +//! simdgroups. Thread `t` takes [`SUM_OPERANDS`] and [`PRODUCT_OPERANDS`] of its simdgroup +//! `t / 32` at its lane `t % 32` and writes `simd_sum`'s bits in the low half of its word and +//! `simd_product`'s in the high half ([`full`]). +//! - [`MASKED`] (`half_simd_reductions_masked`): one threadgroup of `3 * THREADS` threads. Thread +//! `t` takes [`SUM_OPERANDS`]'s value of thread `t % 256` and, where bit `lane` of +//! [`MASKS`]`[t / 256]` is set, writes the sum and the product of that operand over those lanes +//! as [`FULL`] does; every other thread writes `0xffff_ffff` ([`masked`]). + +/// The full kernel. +pub const FULL: &str = "half_simd_reductions"; +/// The masked kernel. +pub const MASKED: &str = "half_simd_reductions_masked"; +/// The threads of [`FULL`]'s threadgroup: eight simdgroups. +pub const THREADS: u32 = 256; + +/// The `half` bits each simdgroup sums: a 1 among `2^-11`s (first and last lane), a cancellation, +/// `2048` and `-2048` among ones, and four vectors of mixed signs and exponents. +pub const SUM_OPERANDS: [[u16; 32]; 8] = [ + [ + 0x3c00, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, + 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, + 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, + ], + [ + 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, + 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, + 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x3c00, + ], + [ + 0x63d0, 0xe3d0, 0x63d0, 0xe3d0, 0x63d0, 0xe3d0, 0x63d1, 0xe3d0, 0x63d1, 0xe3d0, 0x63d1, + 0xe3d0, 0x63d2, 0xe3d0, 0x63d2, 0xe3d0, 0x63d2, 0xe3d0, 0x63d2, 0xe3d0, 0x63d2, 0xe3d0, + 0x63d3, 0xe3d0, 0x63d3, 0xe3d0, 0x63d3, 0xe3d0, 0x63d4, 0xe3d0, 0x63d4, 0xe3d0, + ], + [ + 0x6800, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, + 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0xe800, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, + 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, 0x3c00, + ], + [ + 0x4f70, 0xc411, 0x3994, 0xceaa, 0x307c, 0xceb6, 0x3477, 0xcd6b, 0x476a, 0xc795, 0x42b3, + 0xb86f, 0x487b, 0xcd7d, 0x3c4e, 0xb9c2, 0x39e4, 0xc1b3, 0x47f8, 0xc841, 0x3fff, 0xc864, + 0x42bd, 0xc983, 0x3076, 0xc013, 0x4eb4, 0xb6d3, 0x3476, 0xc80b, 0x48ae, 0xb519, + ], + [ + 0x45e3, 0xbdcc, 0x30e8, 0xc7cb, 0x4b7f, 0xc580, 0x4ebb, 0xc6df, 0x3c21, 0xbd66, 0x3b48, + 0xb3ba, 0x40e5, 0xc444, 0x372d, 0xb120, 0x3257, 0xbad7, 0x3f33, 0xc225, 0x34a7, 0xc045, + 0x3bfe, 0xc019, 0x4ad9, 0xb989, 0x44bc, 0xcf7f, 0x4cf3, 0xc3e4, 0x40d7, 0xcff5, + ], + [ + 0x3d2e, 0xb651, 0x48a3, 0xbf2b, 0x4026, 0xbdf0, 0x4511, 0xbecf, 0x340a, 0xb578, 0x325b, + 0xc97d, 0x3acc, 0xbfa8, 0x4db7, 0xcab2, 0x4b53, 0xb0b5, 0x3605, 0xb9b9, 0x4da5, 0xbabc, + 0x3382, 0xbb9b, 0x421e, 0xb130, 0x3e73, 0xc412, 0x479d, 0xbb4c, 0x38da, 0xc575, + ], + [ + 0x40b2, 0xd980, 0x4f25, 0xc3ab, 0x47d1, 0xc1e6, 0x4bda, 0xc21a, 0x5a83, 0xdbad, 0x564e, + 0xcc98, 0x3f90, 0xc18b, 0x504c, 0xcf58, 0x4c37, 0xd72d, 0x5ace, 0xbddd, 0x5057, 0xbda9, + 0x55a8, 0xbce9, 0x45a3, 0xd6e9, 0x4238, 0xcb6c, 0x49d3, 0xbc21, 0x3d18, 0xca78, + ], +]; + +/// Metal's `simd_sum` of each vector of [`SUM_OPERANDS`]. +pub const SUMS: [u16; 8] = [ + 0x3c0f, 0x3c0f, 0x4b80, 0x4f40, 0xd238, 0xc57c, 0x51eb, 0x5124, +]; + +/// The `half` bits each simdgroup multiplies: `1 + 2^-10` in every lane, that and `1 - 2^-11` in +/// turn, three vectors of `1 + k 2^-10`, and three of magnitudes in `[0.75, 1.5)` of mixed signs. +pub const PRODUCT_OPERANDS: [[u16; 32]; 8] = [ + [ + 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, + 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, + 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, 0x3c01, + ], + [ + 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, + 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, + 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, 0x3c01, 0x3bff, + ], + [ + 0x3c01, 0x3bfa, 0x3bf2, 0x3bf4, 0x3bf6, 0x3bfa, 0x3bf8, 0x3bf2, 0x3bf2, 0x3bfa, 0x3bf2, + 0x3c05, 0x3bf4, 0x3c03, 0x3bf0, 0x3bf2, 0x3bf8, 0x3c01, 0x3c02, 0x3bf4, 0x3c06, 0x3c01, + 0x3c07, 0x3c01, 0x3c08, 0x3c02, 0x3bf0, 0x3c04, 0x3bf6, 0x3c04, 0x3bf4, 0x3bfe, + ], + [ + 0x3c06, 0x3c08, 0x3c04, 0x3c06, 0x3c07, 0x3c02, 0x3bfa, 0x3bfa, 0x3bf4, 0x3c06, 0x3c05, + 0x3bf6, 0x3bf8, 0x3c04, 0x3bf6, 0x3bfe, 0x3c02, 0x3bfc, 0x3bf4, 0x3c07, 0x3bf6, 0x3c05, + 0x3bfa, 0x3c05, 0x3c01, 0x3bf0, 0x3bf6, 0x3bf2, 0x3bf6, 0x3c07, 0x3c05, 0x3c05, + ], + [ + 0x3c07, 0x3bf8, 0x3bfa, 0x3bf8, 0x3c00, 0x3c06, 0x3bf2, 0x3c06, 0x3bf6, 0x3bf0, 0x3c00, + 0x3bfc, 0x3bf2, 0x3bf0, 0x3c07, 0x3c05, 0x3bf0, 0x3c02, 0x3c00, 0x3bfa, 0x3bf8, 0x3bf4, + 0x3c06, 0x3bf2, 0x3c08, 0x3c06, 0x3bfa, 0x3bf6, 0x3c00, 0x3bfc, 0x3c08, 0x3c03, + ], + [ + 0x3bb8, 0x3bd0, 0xbd43, 0xbccd, 0xba35, 0x3de2, 0xbc89, 0x3a23, 0xbaab, 0xbcbb, 0xba56, + 0xbaf0, 0xbdcb, 0x3b88, 0x3ae4, 0x3c3a, 0xbcfd, 0xba91, 0x3c90, 0x3b0d, 0x3cc9, 0xbc15, + 0x3ccf, 0xbda9, 0x3b6e, 0x3c3a, 0x3a1a, 0x3d68, 0x3b24, 0xbc11, 0xbd67, 0xbcb0, + ], + [ + 0x3d3a, 0x3c7f, 0xbc34, 0xbc71, 0xbb70, 0x3c79, 0xbc35, 0x3cc6, 0xbb25, 0xbb95, 0xba13, + 0xbc58, 0xbac1, 0x3d14, 0x3bcc, 0x3d04, 0xbb3f, 0xba28, 0x3b0b, 0x3b76, 0x3cc5, 0xba53, + 0x3abc, 0xbca9, 0x3c95, 0x3a2d, 0x3d15, 0x3b33, 0x3b67, 0xbc07, 0xbd33, 0xbc98, + ], + [ + 0x3c7b, 0x3dd1, 0xbc18, 0xbdd7, 0xba1b, 0x3c7e, 0xbd30, 0x3dd5, 0xbcac, 0xbc07, 0xbc65, + 0xbdbd, 0xbd48, 0x3ac6, 0x3dd4, 0x3d0c, 0xbd58, 0xbb80, 0x3b84, 0x3d76, 0x3ced, 0xba1f, + 0x3dd0, 0xbd0a, 0x3c04, 0x3cc6, 0x3c6d, 0x3b26, 0x3d1a, 0xbce0, 0xbc8d, 0xbc41, + ], +]; + +/// Metal's `simd_product` of each vector of [`PRODUCT_OPERANDS`]. +pub const PRODUCTS: [u16; 8] = [ + 0x3c20, 0x3c00, 0x3b8e, 0x3c11, 0x3bd3, 0x43b1, 0x3e11, 0x55cb, +]; + +/// The active lanes of [`MASKED`]'s three cases: the odd lanes, lanes 5 to 27 but 16, and lanes 0, +/// 15, 16 and 31. +pub const MASKS: [u32; 3] = [0xaaaa_aaaa, 0x0ffe_ffe0, 0x8001_8001]; + +/// Metal's `simd_sum` of each vector of [`SUM_OPERANDS`] over the lanes of each of [`MASKS`]. +pub const MASKED_SUMS: [[u16; 8]; 3] = [ + [ + 0x2000, 0x3c07, 0xf3d0, 0x4c00, 0xd8c1, 0xd663, 0xd1a8, 0xe1c8, + ], + [ + 0x2180, 0x2180, 0xe7c6, 0x4d80, 0xd272, 0xc3d4, 0x4d19, 0x5933, + ], + [ + 0x3c01, 0x3c01, 0x3c00, 0x3c00, 0x4f5d, 0xce7a, 0xc1d0, 0xcdc7, + ], +]; + +/// Metal's `simd_product` of each vector of [`SUM_OPERANDS`] over the lanes of each of [`MASKS`]. +pub const MASKED_PRODUCTS: [[u16; 8]; 3] = [ + [ + 0x0000, 0x0000, 0x7c00, 0x3c00, 0x7c00, 0x7c00, 0x51f1, 0x7c00, + ], + [ + 0x0000, 0x0000, 0x7c00, 0x3c00, 0x7c00, 0x7c00, 0x611c, 0x7c00, + ], + [ + 0x0000, 0x0000, 0x7c00, 0xfc00, 0x4506, 0x45f2, 0x656a, 0x7358, + ], +]; + +/// What thread `t` of [`FULL`] writes. +pub fn full(t: u32) -> u32 { + let v = (t / 32) as usize; + u32::from(SUMS[v]) | (u32::from(PRODUCTS[v]) << 16) +} + +/// What thread `t` of [`MASKED`] writes. +pub fn masked(t: u32) -> u32 { + let (case, v, lane) = ( + (t / THREADS) as usize, + ((t % THREADS) / 32) as usize, + t % 32, + ); + if (MASKS[case] >> lane) & 1 == 0 { + return 0xffff_ffff; + } + u32::from(MASKED_SUMS[case][v]) | (u32::from(MASKED_PRODUCTS[case][v]) << 16) +} + +/// A table of `half` bits as an AIR constant array. +fn halves(table: &[[u16; 32]; 8]) -> String { + let cells: Vec = table + .iter() + .flatten() + .map(|h| format!("half 0xH{h:04X}")) + .collect(); + format!("[256 x half] [{}]", cells.join(", ")) +} + +/// The module holding [`FULL`] and [`MASKED`]. +pub fn module() -> String { + let masks: Vec = MASKS.iter().map(|m| format!("i32 {}", *m as i32)).collect(); + format!( + r#"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" + +@sum_operands = internal addrspace(2) constant {sums}, align 2 +@product_operands = internal addrspace(2) constant {products}, align 2 +@masks = internal addrspace(2) constant [3 x i32] [{masks}], align 4 + +define void @{FULL}(ptr addrspace(1) noundef "air-buffer-no-alias" %out, i32 noundef %t) #0 {{ + %i = zext i32 %t to i64 + %px = getelementptr inbounds [256 x half], ptr addrspace(2) @sum_operands, i64 0, i64 %i + %x = load half, ptr addrspace(2) %px, align 2 + %py = getelementptr inbounds [256 x half], ptr addrspace(2) @product_operands, i64 0, i64 %i + %y = load half, ptr addrspace(2) %py, align 2 + %s = tail call half @air.simd_sum.f16(half %x) #1 + %p = tail call half @air.simd_product.f16(half %y) #1 + %sb = bitcast half %s to i16 + %pb = bitcast half %p to i16 + %sw = zext i16 %sb to i32 + %pw = zext i16 %pb to i32 + %ph = shl i32 %pw, 16 + %w = or i32 %sw, %ph + %o = getelementptr inbounds i32, ptr addrspace(1) %out, i64 %i + store i32 %w, ptr addrspace(1) %o, align 4 + ret void +}} + +define void @{MASKED}(ptr addrspace(1) noundef "air-buffer-no-alias" %out, i32 noundef %t, i32 noundef %lane) #0 {{ +entry: + %i = zext i32 %t to i64 + %v = and i32 %t, 255 + %vi = zext i32 %v to i64 + %px = getelementptr inbounds [256 x half], ptr addrspace(2) @sum_operands, i64 0, i64 %vi + %x = load half, ptr addrspace(2) %px, align 2 + %case = lshr i32 %t, 8 + %ci = zext i32 %case to i64 + %pm = getelementptr inbounds [3 x i32], ptr addrspace(2) @masks, i64 0, i64 %ci + %m = load i32, ptr addrspace(2) %pm, align 4 + %shifted = lshr i32 %m, %lane + %bit = and i32 %shifted, 1 + %on = icmp ne i32 %bit, 0 + br i1 %on, label %active, label %done + +active: + %s = tail call half @air.simd_sum.f16(half %x) #1 + %p = tail call half @air.simd_product.f16(half %x) #1 + %sb = bitcast half %s to i16 + %pb = bitcast half %p to i16 + %sw = zext i16 %sb to i32 + %pw = zext i16 %pb to i32 + %ph = shl i32 %pw, 16 + %w = or i32 %sw, %ph + br label %done + +done: + %r = phi i32 [ %w, %active ], [ -1, %entry ] + %o = getelementptr inbounds i32, ptr addrspace(1) %out, i64 %i + store i32 %r, ptr addrspace(1) %o, align 4 + ret void +}} + +declare half @air.simd_sum.f16(half) #1 +declare half @air.simd_product.f16(half) #1 + +attributes #0 = {{ convergent mustprogress nounwind willreturn }} +attributes #1 = {{ convergent mustprogress nounwind willreturn }} + +!llvm.module.flags = !{{!0, !1, !2, !3, !4, !5, !6, !7, !8}} +!air.kernel = !{{!9, !21}} +!air.version = !{{!19}} +!air.language_version = !{{!20}} + +!0 = !{{i32 2, !"SDK Version", [2 x i32] [i32 26, i32 5]}} +!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 @{FULL}, !10, !11}} +!10 = !{{}} +!11 = !{{!12, !13}} +!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_index_in_threadgroup", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"}} +!14 = !{{i32 2, !"air.thread_index_in_simdgroup", !"air.arg_type_name", !"uint", !"air.arg_name", !"lane"}} +!19 = !{{i32 2, i32 8, i32 0}} +!20 = !{{!"Metal", i32 4, i32 0, i32 0}} +!21 = !{{ptr @{MASKED}, !10, !22}} +!22 = !{{!12, !13, !14}} +"#, + sums = halves(&SUM_OPERANDS), + products = halves(&PRODUCT_OPERANDS), + masks = masks.join(", "), + ) +} diff --git a/crates/metal2vulkan-devtools/src/lib.rs b/crates/metal2vulkan-devtools/src/lib.rs index cb6cea35e..897832f4e 100644 --- a/crates/metal2vulkan-devtools/src/lib.rs +++ b/crates/metal2vulkan-devtools/src/lib.rs @@ -10,7 +10,9 @@ //! both run ([`loop_kernels`]), the inputs and answer of the vector word-phase fixture the product's //! interpreter test, the runtime's device test and the conformance suite all hold //! ([`vector_word_phases`]), the dispatches and answer of the threadgroup-memory fixture the -//! runtime's device test and the conformance suite both hold ([`threadgroup_start`]), and the +//! runtime's device test and the conformance suite both hold ([`threadgroup_start`]), the `half` +//! simdgroup sums and products and Metal's answers to them, which the product's interpreter tests +//! and the runtime's device test both run ([`half_simd_reductions`]), and the //! counter of a thread's user-space instructions the matrix's //! instructions-per-draw records are measured with ([`instructions`]). //! @@ -32,6 +34,7 @@ use std::path::{Path, PathBuf}; use std::process::Stdio; pub mod fuzz; +pub mod half_simd_reductions; pub mod instructions; pub mod linked_signatures; pub mod litmus; diff --git a/crates/metal2vulkan-runtime/src/tests/half_simd_reductions.rs b/crates/metal2vulkan-runtime/src/tests/half_simd_reductions.rs new file mode 100644 index 000000000..b33b12871 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/half_simd_reductions.rs @@ -0,0 +1,91 @@ +//! Metal's `half` simdgroup sums and products, under a device cfg on every device of the cfg's +//! kind: the kernels of `metal2vulkan_devtools::half_simd_reductions` (our own AIR, with Metal's +//! answers measured on an M3 Max) dispatched as one threadgroup each, eight simdgroups summing and +//! multiplying their lanes, then the same in blocks only a mask of lanes executes. Every word is +//! Metal's: each sum and product the tree of rounded `half` operations Metal's is. The device's +//! kernel subgroup sizes, which decide the tier, are printed with the pipelines' precision notes. +//! +//! `llvm-as` assembles the module; without it the test skips visibly. + +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod on_device { + use crate::tests::command::on_device::{each_device, finished}; + use crate::{ + ComputePipelineDescriptor, DispatchType, FunctionDescriptor, Library, MemoryClass, + StageSource, + }; + use metal2vulkan_devtools::half_simd_reductions::{self as h, FULL, MASKED, THREADS}; + use metal2vulkan_devtools::{llvm_assemble_text, AssembleError}; + + /// Each kernel's threadgroup gives Metal's words on every device. + #[test] + fn every_device_sums_and_multiplies_half_simdgroups_as_metal() { + let bitcode = match llvm_assemble_text(&h::module()) { + Ok(bitcode) => bitcode, + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + return; + } + Err(error) => panic!("half_simd_reductions: {error:?}"), + }; + let library = Library::from_air(&bitcode).expect("the library"); + let cases: [(&str, u32, fn(u32) -> u32); 2] = + [(FULL, THREADS, h::full), (MASKED, 3 * THREADS, h::masked)]; + each_device("half_simd_reductions", |name, _, device| { + let kernel = device.core().translator.profile().subgroups().kernel; + for (entry, n, want) in cases { + let function = library.function(entry).expect("the kernel"); + let descriptor = ComputePipelineDescriptor::new(StageSource::Function( + FunctionDescriptor::new(function), + )); + let state = device + .new_compute_pipeline_state(&descriptor, crate::tests::no_constexpr_samplers) + .unwrap_or_else(|error| panic!("{name} {entry}: {error}")); + let out_len = u64::from(n * 4); + let block = device + .take_block(MemoryClass::Shared, out_len) + .expect("a Shared block"); + let output = device + .new_buffer(block.id, 0, out_len, Some(0)) + .expect("the output"); + let queue = device.new_command_queue(4).expect("a queue"); + 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"); + encoder.set_buffer(Some(&output), 0, 0).expect("a buffer"); + encoder + .dispatch_threads([n, 1, 1], [n, 1, 1]) + .unwrap_or_else(|error| panic!("{name} {entry}: the dispatch: {error}")); + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished(name, &command_buffer); + assert_eq!(command_buffer.error(), None, "{name} {entry}"); + let contents = output.contents().expect("a Shared buffer"); + let mut wrong = Vec::new(); + for t in 0..n { + // SAFETY: the word lies in the buffer, which is alive, and its work completed. + let got = unsafe { + (contents.as_ptr().cast::()) + .add(t as usize) + .read_unaligned() + }; + if got != want(t) { + wrong.push(format!("{t}: {got:#010x} Metal {:#010x}", want(t))); + } + } + println!( + "{name} {entry}: kernel subgroup sizes {}..={} (pinnable {}); notes {:?}; \ + {} of {n} words differ from Metal's", + kernel.sizes.min, + kernel.sizes.max, + kernel.pinnable, + state.precision_notes(), + wrong.len() + ); + assert!(wrong.is_empty(), "{name} {entry}: words differ: {wrong:?}"); + } + }); + } +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index a70bbaa36..5c7176486 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -52,6 +52,7 @@ mod float_depth; mod floor; mod function_tables; mod gpu_span; +mod half_simd_reductions; mod hazard; mod heap; mod heap_alias; diff --git a/crates/metal2vulkan/src/tests/engine_simd.rs b/crates/metal2vulkan/src/tests/engine_simd.rs index f59dfa059..e5f83f9d6 100644 --- a/crates/metal2vulkan/src/tests/engine_simd.rs +++ b/crates/metal2vulkan/src/tests/engine_simd.rs @@ -26,8 +26,8 @@ use crate::pipeline::facts::RuntimeFact; use crate::pipeline::interface::PushLoc; use crate::pipeline::sym::{PushField, ResourceKind, ResourceSlot, SpecKey, Stage}; use crate::pipeline::testing::{ - compile, compile_kept, engine_dir, link_and_validate, llvm_assemble, profile, typed_module, - AssembleError, Scratch, + compile, compile_kept, engine_dir, link_and_validate, llvm_assemble, llvm_assemble_text, + profile, typed_module, AssembleError, Scratch, }; use crate::spirv::interp::{run, Buffer, Config, Order, Resources, SubgroupSize}; use crate::spirv::ir::TypedModule; @@ -721,6 +721,42 @@ fn float_sums_record_their_precision() { assert!(!a.precision.contains(&PrecisionNote::ReductionOrder)); } +/// Metal's `half` simdgroup sums and products (`metal2vulkan_devtools::half_simd_reductions`, +/// measured on an M3 Max) are a tree of rounded `half` operations, in a block every lane executes +/// and over a mask of lanes, and each subgroup tier computes them: the `Direct` tier at 32 lanes and +/// the `Cluster32` tier at 64, on the CPU interpreter. +#[test] +fn half_simd_sums_and_products_are_metals_trees() { + use metal2vulkan_devtools::half_simd_reductions::{self as h, FULL, MASKED, THREADS}; + let bc = match llvm_assemble_text(&h::module()) { + Ok(b) => b, + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + return; + } + Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the half reductions: {why}"), + }; + let cases: [(&str, u32, fn(u32) -> u32); 2] = + [(FULL, THREADS, h::full), (MASKED, 3 * THREADS, h::masked)]; + for (p, lanes) in [ + (moltenvk(), SubgroupSize::W32), + (wave64(), SubgroupSize::W64), + ] { + for (kernel, n, want) in cases { + let (plan, m) = typed_module(&bc, kernel, &p).expect("typed module"); + let got = run_all(&plan, &m, [n, 1, 1], lanes); + for (t, w) in (0u32..).zip(&got) { + assert_eq!( + *w, + want(t), + "{kernel} thread {t} at {lanes:?}: {w:#010x}, Metal {:#010x}", + want(t) + ); + } + } + } +} + /// `gsm2_vertex_quad_permutes`' quad broadcasts and shuffle-xors by a constant lane or mask in /// vertex functions are shuffles through the ballot of the live lanes on every committed profile, /// since a vertex quad may lack a lane past the draw's vertex count: each function compiles, diff --git a/crates/metal2vulkan/src/tests/engine_simd_emulated.rs b/crates/metal2vulkan/src/tests/engine_simd_emulated.rs index ee3132d1c..b651fd06f 100644 --- a/crates/metal2vulkan/src/tests/engine_simd_emulated.rs +++ b/crates/metal2vulkan/src/tests/engine_simd_emulated.rs @@ -791,6 +791,37 @@ fn validate(stem: &str, bc: &[u8], kernel: &str) { assert_eq!(a.subgroup, SubgroupRequirement::NONE, "{stem} {kernel}"); } +/// The emulated tier's `half` simdgroup sums and products are Metal's trees of rounded `half` +/// operations (`metal2vulkan_devtools::half_simd_reductions`, whose kernels and M3 Max readings +/// these are), in a block every lane executes and over a mask of lanes, at every subgroup width. +#[test] +fn half_simd_sums_and_products_are_metals_trees_emulated() { + use metal2vulkan_devtools::half_simd_reductions::{self as h, FULL, MASKED, THREADS}; + let Some(bc) = assemble_text("half-reductions", &h::module()) else { + return; + }; + let cases: [(&str, u32, fn(u32) -> u32); 2] = + [(FULL, THREADS, h::full), (MASKED, 3 * THREADS, h::masked)]; + for stem in PROFILES { + let p = profile(stem, DevicePolicy::RESIDENT_TABLES).expect("profile"); + for (kernel, n, want) in cases { + let (plan, m) = typed_module(&bc, kernel, &p).expect("typed module"); + check_emulated(stem, kernel, &plan, None); + for lanes in WIDTHS { + let got = run_all(&plan, &m, [n, 1, 1], lanes, None); + for (t, w) in (0u32..).zip(&got) { + assert_eq!( + *w, + want(t), + "{stem} {kernel} thread {t} at {lanes:?}: {w:#010x}, Metal {:#010x}", + want(t) + ); + } + } + } + } +} + /// The workgroup-uniform kernels of `simd_ops` compute Metal's results on emulated simdgroups at /// every subgroup width, in full and partially occupied threadgroups. #[test]