Adds a Zig DSL for Metal kernels over the AIR dialect, and moves the Metal paged attention kernels onto it. - `mlir/dialects/air`: a `thread` address space. Each op has an `llvmBuilder` that calls its `air.*` builtin with Apple's attributes, and the dialect's translation interface builds the `!air.kernel` metadata, so lowering to LLVM IR is MLIR's own translation. The verifier rejects what Apple's compiler can't take: bool, scalable and n-D vectors, buffers of non-Metal scalars, scalar f32 converts, and `air.kernel` on a declaration. - `dsl/metal`: an MSL-shaped builder. It emits `air` plus the `arith`/`cf`/`vector`/`llvm` IR that clang produces for MSL, and covers MSL's math, SIMD-group, barrier and `simdgroup_matrix` functions. - `zml.kernel.metal` / `ops.metal`: wrap a kernel into a `__gpu$xla.gpu.air` custom call. XLA lowers it, compiles it to a metallib and launches it. |
History
|
|
|---|---|---|
| .. | ||
| air.zig | ||
| AIRAttributes.cpp | ||
| AIRAttributes.h | ||
| AIRAttributes.td | ||
| AIRDialect.cpp | ||
| AIRDialect.h | ||
| AIRDialect.td | ||
| AIREnums.h | ||
| AIREnums.td | ||
| AIROps.cpp | ||
| AIROps.h | ||
| AIROps.td | ||
| BUILD.bazel | ||
| capi.cc | ||
| capi.h | ||
| README.md | ||
| test.zig | ||
Metal AIR dialect
The Metal-specific part of AIR, the LLVM IR Apple's Metal compiler emits (not
AMD's asynchronous AIR dialect). Namespace air, C++ namespace
mlir::metal::air, C API prefix mlirMetalAIR. dsl/metal emits it, and
XLA's __gpu$xla.gpu.air custom call lowers it, from a vendored copy, to a
metallib.
Operations
Each operation is an air.* intrinsic, named as Apple's compiler names it
minus the type suffix its operand and result types determine:
| Operation | Intrinsic |
|---|---|
air.fast_exp %x : vector<2xf32> |
@air.fast_exp.v2f32 |
air.max.u %a, %b : i32 |
@air.max.u.i32 |
air.simd_shuffle_xor %x, %lane : f16 |
@air.simd_shuffle_xor.f16 |
air.convert.s %x : i32 to f32 |
@air.convert.f.f32.s.i32 |
air.simdgroup_matrix_8x8_load %p, ... : !llvm.ptr<3> -> vector<64xbf16> |
@air.simdgroup_matrix_8x8_load.v64bf16.p3bf16 |
air.wg.barrier <threadgroup> |
@air.wg.barrier(i32 2, i32 1) |
- Float math:
exp,exp2,log,log2,tanh,sqrt,rsqrt,fabs,ceil,floor,round,trunc,sin,cos,fmax,fmin,pow,fmod,fma,clampanddoton f16 and f32. Thefast_variants are f32 only: under fast math (MSL's default)exp(x)isfast_exp, andmetal::precise::expisexp. - Integer math:
max,min,clampandabs, as.sand.uoverloads. - SIMD-group functions:
simd_sum,simd_max,simd_min,simd_prefix_exclusive_sum,simd_shuffle,simd_shuffle_xor,simd_shuffle_downandsimd_broadcaston floats, the integer ones as.sand.u, andsimd_any,simd_all. There are no bfloat overloads. convertbetween float types, andconvert.s,convert.ubetween floats and integers. Apple's compiler uses LLVM casts for scalar f32 to and from f16 or bf16, and between integers.wg.barrierandsimdgroup.barrier(threadgroup_barrier,simdgroup_barrier), with#air.mem_flags.simdgroup_matrix_8x8_load,_storeand_multiply_accumulate. Asimdgroup_matrix<T, 8, 8>is avector<64xT>, andthread_elements()its elements 0 and 1.
Float operations carry fastmath flags, which become the call's flags (Apple's
compiler marks every float call fast under fast math). SIMD-group, barrier
and matrix operations have unknown effects, so they are not speculated or
moved across divergent control flow. Arithmetic, memory and control flow use
the arith, cf, scf, vector and llvm dialects, with pointers as !llvm.ptr in
AIR's address spaces: 0 thread, 1 device, 2 constant, 3 threadgroup (a buffer
is never in thread memory).
Kernels
A kernel is a void func.func with air.kernel. Each argument carries one
!air.kernel descriptor:
air.builtin = #air.builtin<threadgroup_position_in_grid>: the position and size builtins areuintorushortscalars or 2- and 3-element vectors, the index builtins (thread_index_in_simdgroup, ...) scalars.air.buffer = #air.buffer<...>on an!llvm.ptr: the[[buffer(n)]]or[[threadgroup(n)]]index, the address space, the access (constpointers areread), the pointee type with its Metal name, size and alignment, the size of a constant reference,noaliasfor__restrict__, and the fields of a constant struct (air.struct_type_info). Device and constant buffers share indices; threadgroup buffers have their own.air.arg_name, optionally, the argument's name.
air.fast_math_enable on the module records that it was compiled with fast
math (air.compile.fast_math_enable).
Evidence
Names, signatures, argument descriptors and flags follow what Apple's Metal
compiler 32023.830 (the @metal_toolchain repository) emits for MSL probes and
for XLA's Metal kernels, with metal -std=metal4.0 -S -emit-llvm (fast math,
-O2, as XLA compiles them). Only the overloads it emits are accepted.
Build and test
bazel build //mlir/dialects/air //mlir/dialects/air:capi
bazel test //mlir/dialects/air:test
The Zig library is :air, the C++ dialect :dialect and its C API :capi.
registerDialect(registry) registers AIR, which loads the LLVM dialect;
kernel builds a func.func, so callers also register func. Attributes are
built through the C API (BuiltinAttr, MemFlagsAttr, StructFieldAttr,
BufferAttr), kernel arguments through builtinArgumentAttr and
bufferArgumentAttr, and every operation has a builder named after it
(fast_exp, max_u, wg_barrier, simdgroup_matrix_8x8_load).