diff --git a/crates/metal2vulkan-air/src/intrinsics/llvm.rs b/crates/metal2vulkan-air/src/intrinsics/llvm.rs index e9848bed0..209c9886b 100644 --- a/crates/metal2vulkan-air/src/intrinsics/llvm.rs +++ b/crates/metal2vulkan-air/src/intrinsics/llvm.rs @@ -93,11 +93,15 @@ impl AgxSpace { /// `agx_emask` and `matrix` lowerings): the `agx3` lane-masked accesses and their mask builder /// (lane `i` of an emask access is active exactly when bit `i` of the first `i16` mask is set; the /// second `i16` is a constant of the shape; the third, the element stride, has no effect), the -/// `agx2` cluster number, and the `agx2` distributed-register matrix multiply-adds. +/// `agx2` cluster number, the `agx2` distributed-register matrix multiply-adds, and the `agx2` +/// bit interleave (measured on an M3 Max over every pair of operands). #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum AgxOp { /// `llvm.agx2.cluster.num`: `i32 ()`, the physical GPU cluster the threadgroup runs on. ClusterNum, + /// `llvm.agx2.shfl`: `i32 (i16 a, i16 b)`, the bit interleave: bit `2i` of the result is bit + /// `i` of `a`, and bit `2i + 1` is bit `i` of `b`. + Shfl, /// `llvm.agx2.{f16,f32}matmad{4x4,8x8}.v2{f16,f32}`: `<2 x T> (<2 x T> a, <2 x T> b, /// <2 x T> c)`, `c + a·b` over the 8x8 tile (or over each of its four 4x4 quadrants) whose /// cells are distributed two per lane over the 32 lanes of the simdgroup. `elem` is `F16` @@ -140,11 +144,13 @@ impl AgxOp { } } - /// Parse an `agx2` name: the cluster number, and the matrix multiply-adds, whose overload - /// suffix must spell their element (`v2f16` or `v2f32`). + /// Parse an `agx2` name: the cluster number, the bit interleave, and the matrix + /// multiply-adds, whose overload suffix must spell their element (`v2f16` or `v2f32`). fn parse2(rest: &str) -> Option { - if rest == "cluster.num" { - return Some(AgxOp::ClusterNum); + match rest { + "cluster.num" => return Some(AgxOp::ClusterNum), + "shfl" => return Some(AgxOp::Shfl), + _ => {} } let (op, suffix) = rest.split_once('.')?; let (elem, dim) = match op { @@ -187,6 +193,7 @@ impl AgxOp { let i32s = Operand::Num(NumTy::scalar(Elem::I32)); Some(match *self { AgxOp::ClusterNum => Signature::new(i32s, &[]), + AgxOp::Shfl => Signature::new(i32s, &[i16s, i16s]), AgxOp::MatMad { elem, .. } => { let v2 = Operand::Num(NumTy { elem, lanes: 2 }); Signature::new(v2, &[v2, v2, v2]) diff --git a/crates/metal2vulkan-air/src/intrinsics/tests.rs b/crates/metal2vulkan-air/src/intrinsics/tests.rs index 9bf1d9dc6..d7d120eae 100644 --- a/crates/metal2vulkan-air/src/intrinsics/tests.rs +++ b/crates/metal2vulkan-air/src/intrinsics/tests.rs @@ -939,6 +939,7 @@ fn llvm_bases_decode_against_their_declarations() { "llvm.agx2.f64matmad8x8.v2f64", "llvm.agx2.f32matmad16x16.v2f32", "llvm.agx3.igemm.v8i32.i64.i64.v8i32", + "llvm.agx3.shfl", ] { assert_eq!( llvm(name, v4f32, &[p1]), @@ -998,6 +999,15 @@ fn llvm_bases_decode_against_their_declarations() { llvm("llvm.agx2.cluster.num", i16s, &[]), DeclClass::Mismatch ); + // The `agx2` bit interleave. + assert_eq!( + llvm("llvm.agx2.shfl", i32s, &[i16s, i16s]), + DeclClass::Llvm(LlvmIntrinsic::Agx(AgxOp::Shfl)) + ); + assert_eq!( + llvm("llvm.agx2.shfl", i32s, &[i32s, i32s]), + DeclClass::Mismatch + ); for (name, elem, dim) in [ ("llvm.agx2.f16matmad4x4.v2f16", Elem::F16, 4), ("llvm.agx2.f16matmad8x8.v2f16", Elem::F16, 8), diff --git a/crates/metal2vulkan/src/lower/simd/agx.rs b/crates/metal2vulkan/src/lower/simd/agx.rs index 5cd476d23..37bf78730 100644 --- a/crates/metal2vulkan/src/lower/simd/agx.rs +++ b/crates/metal2vulkan/src/lower/simd/agx.rs @@ -1,7 +1,8 @@ //! The AGX lane intrinsics `llvm.agx{2,3}.*`, ported from the old engine's device-measured //! lowerings (`passes/air_calls/agx_emask.rs`, `passes/air_calls/matrix.rs`, -//! `native/emitter/ops/air_calls.rs`). Every other AGX operation (`Agx(Unmodeled)`: the `wide` -//! emask forms, `igemm`, …) is refused as `Unsupported{AgxUnmodeled, NotImplemented}`; +//! `native/emitter/ops/air_calls.rs`), and the bit interleave measured on an M3 Max. Every other +//! AGX operation (`Agx(Unmodeled)`: the `wide` emask forms, `igemm`, …) is refused as +//! `Unsupported{AgxUnmodeled, NotImplemented}`, by the memory analysis where it takes a pointer; //! `llvm.agx3.yield`, a delay with no data effect, never reaches lowering: decode drops it //! (`air::intrinsics::Dropped::AgxYield`). //! @@ -20,6 +21,12 @@ //! over the threadgroup and chosen by the hardware (0..=36 measured on an M3 Max). Vulkan has //! no cluster; the constant 0 is a value the hardware itself gives (one cluster), and it keeps //! the value uniform. +//! - **`llvm.agx2.shfl(a, b)`** interleaves the bits of its two `i16` operands into an `i32`: bit +//! `2i` is bit `i` of `a`, bit `2i + 1` bit `i` of `b` (measured on an M3 Max over all 2^32 +//! pairs; MSL documents the same contract for `interleave(ushort, ushort)`, which compiles to +//! another intrinsic). Each operand, held zero-extended in its container, is spread to the even +//! bits by four mask-and-shift steps, and `b`'s is shifted up by one. Per-thread arithmetic, +//! exact on every device, no subgroup operation. //! - **`llvm.agx2.{f16,f32}matmad{4x4,8x8}.v2{f16,f32}(a, b, c)`** is `c + a·b` over an 8x8 //! tile distributed two cells per lane over the 32 lanes of the simdgroup (device-measured //! storage mapping: lane `L` owns row `((L >> 1) & 3) | ((L >> 4) << 2)`, columns @@ -70,6 +77,8 @@ pub enum AgxStrategy { EdgeCheck(Form), /// `llvm.agx2.cluster.num`: the constant 0 in its `i32` form. ClusterNum(Form), + /// `llvm.agx2.shfl`: the bit interleave of two `i16`s. + Shfl, /// `llvm.agx2.*matmad*`. MatMad(MatMad), /// `llvm.agx3.{load,store}.with.emask.*`. @@ -107,6 +116,7 @@ impl AgxStrategy { match self { AgxStrategy::EdgeCheck(_) => "agx.edgecheck".into(), AgxStrategy::ClusterNum(_) => "agx.cluster_num".into(), + AgxStrategy::Shfl => "agx.shfl".into(), AgxStrategy::MatMad(MatMad { dim: 4, .. }) => "agx.matmad4x4".into(), AgxStrategy::MatMad(_) => "agx.matmad8x8".into(), AgxStrategy::Emask(e) if e.value.is_some() => { @@ -124,6 +134,7 @@ impl AgxStrategy { match self { AgxStrategy::EdgeCheck(_) => 4 * 48 + 32, AgxStrategy::ClusterNum(_) => 12, + AgxStrategy::Shfl => 26 * 6 + 32, AgxStrategy::MatMad(m) => { let round = narrow::round_words(m.form); 96 + u32::from(m.dim) * (72 + 2 * round) @@ -166,15 +177,15 @@ pub(super) fn select( } check_operands(cx, &call.args)?; match *op { - AgxOp::EdgeCheck | AgxOp::ClusterNum => { + AgxOp::EdgeCheck | AgxOp::ClusterNum | AgxOp::Shfl => { let form = form_on(cx.entry, cx.profile, inst.ty)?; if !form.is_int() || form.lanes != 1 || form.container() != 32 { return Err(cx.invariant("an AGX integer result outside its signature")); } - Ok(if *op == AgxOp::EdgeCheck { - AgxStrategy::EdgeCheck(form) - } else { - AgxStrategy::ClusterNum(form) + Ok(match op { + AgxOp::EdgeCheck => AgxStrategy::EdgeCheck(form), + AgxOp::ClusterNum => AgxStrategy::ClusterNum(form), + _ => AgxStrategy::Shfl, }) } AgxOp::MatMad { elem, dim } => select_matmad(cx, at, inst, call, elem, dim), @@ -299,6 +310,13 @@ pub(super) fn emit<'c>( edgecheck(fb, nth(&a, 0)?, nth(&a, 1)?, nth(&a, 2)?)? } AgxStrategy::ClusterNum(form) => ops::splat(fb, *form, 0)?, + AgxStrategy::Shfl => { + let a = args(cx, fb, at)?; + let even = spread(fb, nth(&a, 0)?)?; + let odd = spread(fb, nth(&a, 1)?)?; + let odd = bin_k(fb, BinOp::ShiftLeftLogical, odd, 1)?; + fb.binary(BinOp::BitwiseOr, even, odd)? + } AgxStrategy::MatMad(m) => { let a = args(cx, fb, at)?; matmad(cx, fb, at, m, &a)? @@ -347,6 +365,23 @@ fn edgecheck( Ok(acc) } +/// The 16 bits of `x` (an `i16` held zero-extended) spread to the even bits of a word, bit `i` to +/// bit `2i`: each step moves the upper half of every group of bits up by the step, and the mask +/// keeps the moved halves and the lower ones. +fn spread(fb: &mut FunctionBuilder<'_>, mut x: SValue) -> BResult { + for (step, mask) in [ + (8, 0x00FF_00FF), + (4, 0x0F0F_0F0F), + (2, 0x3333_3333), + (1, 0x5555_5555), + ] { + let up = bin_k(fb, BinOp::ShiftLeftLogical, x, step)?; + let both = fb.binary(BinOp::BitwiseOr, x, up)?; + x = bin_k(fb, BinOp::BitwiseAnd, both, mask)?; + } + Ok(x) +} + /// `c + a·b` of the distributed tile ([`super::matrix::tile_mad`]), each step rounded to the /// cell format. fn matmad( diff --git a/crates/metal2vulkan/src/memory/provenance.rs b/crates/metal2vulkan/src/memory/provenance.rs index a21eeb8d5..9e38a2e75 100644 --- a/crates/metal2vulkan/src/memory/provenance.rs +++ b/crates/metal2vulkan/src/memory/provenance.rs @@ -3223,6 +3223,15 @@ impl Walk<'_, '_> { }; let Some(sig) = sig else { if call.args.iter().any(|a| self.is_ptr(*a)) || self.is_ptr(Value::Inst(id)) { + // An AGX operation has no signature only where its contract is not modeled: the + // refusal names that, not the access it cannot follow. + if let Callee::Llvm { + op: crate::air::intrinsics::LlvmIntrinsic::Agx(_), + .. + } = call.callee + { + return Err(crate::lower::simd::agx::unmodeled()); + } return Err(not_lowered()); } return Ok(()); diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index cfa6e641e..0666a10fc 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -7599,6 +7599,7 @@ variant metal2vulkan-air::intrinsics::AgxOp::ClusterNum ClusterNum variant metal2vulkan-air::intrinsics::AgxOp::EdgeCheck EdgeCheck variant metal2vulkan-air::intrinsics::AgxOp::LoadEmask LoadEmask { space: AgxSpace, ty: NumTy } variant metal2vulkan-air::intrinsics::AgxOp::MatMad MatMad { elem: Elem, dim: u8 } +variant metal2vulkan-air::intrinsics::AgxOp::Shfl Shfl variant metal2vulkan-air::intrinsics::AgxOp::StoreEmask StoreEmask { space: AgxSpace, ty: NumTy } variant metal2vulkan-air::intrinsics::AgxOp::Unmodeled Unmodeled(Sym) variant metal2vulkan-air::intrinsics::AgxSpace::Global Global