metalworking
GitHub (opens in a new tab)
/glossary/metal/compilation-pipeline

Compilation Pipeline

MSL source compiles to AIR (portable IR), AIR ships in a metallib, and the GPU binary is, by default, generated when you build a pipeline state on the user's machine.

CUDA equivalent, stage by stage:

CUDA Metal Notes
CUDA C++ MSL
nvcc metal frontend (xcrun metal) or runtime compile
PTX AIR (Apple IR, LLVM-bitcode-based) portable across GPU generations
cubin / SASS GPU binary inside a pipeline state built on-device by default; can ship precompiled per GPU in a binary archive
fatbin / JIT-from-PTX metallib → MTLLibrary → MTLComputePipelineState the on-device compile is the default path
-arch=sm_90 matrix none needed AIR finalizes per-device at pipeline creation
what ships to the user build machine user's device CUDA CUDA C++ nvcc PTX SASS (cubin) machine code shipped, or PTX for the driver to JIT per-arch fatbins: sm_80, sm_90, ... METAL MSL metal frontend or runtime string compile AIR metallib makeComputePipelineState AIR → G-series ISA, built here at pipeline creation GPU binary function constants bind here folded as literals: dead code deleted pre-GPU consequences: · pipeline creation is the expensive step: cache pipeline states · runtime string compile uses the same backend (measured bit-identical) · no cuobjdump: inspecting the ISA is community tooling (applegpu) · no -arch matrix: AIR finalizes per-device at pipeline creation
The dashed line is the ship boundary. CUDA crosses it with machine code; Metal crosses it with portable IR and finishes compiling on the user's device, which is what lets function constants participate in the final compile.

The practical differences from CUDA-land:

The default path to machine code runs on the device, but it is not the only one. You compile MSL → metallib ahead of time (and should, for app startup), and by default the last mile, AIR → the actual G-series ISA (opens in a new tab), happens on-device, inside makeComputePipelineState. Consequences: pipeline creation is the expensive step (cache pipeline states the way you'd cache cuBLAS handles), and function constants get to participate in that final compile, which is what makes them real dead-code elimination rather than runtime branching. Apple also documents an offline route: the Metal translator, metal-tt, compiles AIR plus a JSON pipeline description into a binary archive (opens in a new tab) holding one precompiled slice per GPU generation (metal-lipo -archs lists them; an M1 Pro reports applegpu_g13g), which an app ships and loads through MTLBinaryArchive. Each slice is keyed to a GPU and can be invalidated by an OS update, in which case Metal recompiles from the AIR kept in the archive, so an archive is a shipped cache, not a portable cubin. The rest of the toolchain: metal-ar bundles AIR files, metal-lipo -thin extracts one slice, and metal-source unpacks an archive.

Runtime string compilation is a first-class workflow. device.makeLibrary(source:options:) compiles an MSL string at runtime through the same backend as the offline compiler, with measured bit-identical performance (m5-gemm's README (opens in a new tab) tested -O3 -ffast-math offline vs runtime: no difference, which the spec predicts, since runtime compilation already defaults to fast math and the documented optimization levels are -O2, the default, and -Os). This is why metal-flash-attention generates kernels as Swift strings, why MLX JIT-compiles specialized kernels, and why Python harnesses need no Xcode at all. If you compile offline and want Xcode's per-line shader profiling, add -frecord-sources to the xcrun metal command (or -frecord-sources=flat for a separate .metallibsym file), because the shader editor needs the source.

Inspecting what the compiler actually emitted is community tooling, not vendor tooling. There is no cuobjdump. The reverse-engineered applegpu (opens in a new tab) disassembler is the closest thing to Godbolt here (compiler_explorer.py: MSL in, ISA out), and the standard move when a kernel underperforms and you suspect spills or missed unrolling. Expect to find one surprise every time you look.

Compile-time configuration arrives through three channels, in decreasing rigidity: C++ templates (fixed at MSL→AIR time; how steel specializes shapes), preprocessor -D defines (same stage; how m5-gemm sets tile sizes), and function constants (AIR→binary time; specialization without recompiling the front half).