METAL(1) Metal METAL(1)

metal - the Metal compiler

metal [options] filename ...

metal is a Metal compiler which encompasses preprocessing, parsing, optimization, code generation, assembly, and linking. Depending on which high-level mode setting is passed, Metal will stop before doing a full link. While Metal is highly integrated, it is important to understand the stages of compilation, to understand how to invoke it. These stages are:

The metal executable is actually a small driver which controls the overall execution of other tools such as the compiler, assembler and linker. Typically you do not need to interact with the driver, but you transparently use it to run the other tools.
This stage handles tokenization of the input source file, macro expansion, #include expansion and handling of other preprocessor directives. The output of this stage is typically called a ".metali" (for Metal) file.
This stage parses the input file, translating preprocessor tokens into a parse tree. Once in the form of a parse tree, it applies semantic analysis to compute types for expressions as well and determine whether the code is well formed. This stage is responsible for generating most of the compiler warnings as well as parse errors. The output of this stage is an "Abstract Syntax Tree" (AST).
This stage translates an AST into low-level intermediate code (known as "LLVM IR") and ultimately to machine code. This phase is responsible for optimizing the generated code and handling target-specific code generation. The output of this stage is typically called a ".s" file or "assembly" file.

Metal also supports the use of an integrated assembler, in which the code generator produces object files directly. This avoids the overhead of generating the ".s" file and of calling the target assembler.

This stage runs the target assembler to translate the output of the compiler into a target object file. The output of this stage is typically called a ".o" file or "object" file.
This stage runs the target linker to merge multiple object files into an executable or dynamic library. The output of this stage is typically called an "a.out", ".dylib", ".so", or ".metallib" file.

By default metal targets the air64 virtual architecture. Final translation to native code is performed at runtime.

metal supports targeting native architectures using air64 outputs as intermediate steps. Translation from an air64 output to the native output is performed inside the "Code Generation and Optimization", "Assembler", or "Linker" stages by a native translator tool.

A translation script can be fed to a native translator to convey extra information useful for the compilation process, such as a render pipeline description.

Run the preprocessor stage.

Run the preprocessor, parser and type checking stages.

Run the previous stages as well as LLVM generation and optimization stages and target-specific code generation, producing an assembly file.

Run all of the above, plus the assembler, generating a target ".o" object file.

If no stage selection option is specified, all stages above are run, and the linker is run to combine the results into an executable or shared library.

Treat subsequent input files as having type language.

Specify the language standard to compile for.

Supported values for the Metal language are:

ios-metal1.0

iOS Metal 1.0
ios-metal1.1

iOS Metal 1.1


ios-metal1.2

iOS Metal 1.2
ios-metal2.0

iOS Metal 2.0


ios-metal2.1

iOS Metal 2.1
ios-metal2.2

iOS Metal 2.2


ios-metal2.3

iOS Metal 2.3
ios-metal2.4

iOS Metal 2.4


macos-metal1.1

macOS Metal 1.1
macos-metal1.2

macOS Metal 1.2


macos-metal2.0

macOS Metal 2.0
macos-metal2.1

macOS Metal 2.1


macos-metal2.2

macOS Metal 2.2
macos-metal2.3

macOS Metal 2.3


macos-metal2.4

macOS Metal 2.4
metal3.0

Metal 3.0


metal3.1

Metal 3.1

The default Metal language standard depends on the chosen deployment target for Apple platform:

iOS 8.0

iOS Metal 1.0


iOS 9.0
tvOS 9.0

iOS Metal 1.1


iOS 10.0
tvOS 10.0
watchOS 3.0

iOS Metal 1.2


iOS 11.0
tvOS 11.0
watchOS 4.0

iOS Metal 2.0


iOS 12.0
tvOS 12.0
watchOS 5.0

iOS Metal 2.1


iOS 13.0
tvOS 13.0
watchOS 6.0

iOS Metal 2.2


iOS 14.0
tvOS 14.0
watchOS 7.0

iOS Metal 2.3


iOS 15.0
tvOS 15.0
watchOS 8.0

iOS Metal 2.4


Mac OS X 10.11

macOS Metal 1.1
Mac OS X 10.12

macOS Metal 1.2


Mac OS X 10.13

macOS Metal 2.0
Mac OS X 10.14

macOS Metal 2.1


Mac OS X 10.15

macOS Metal 2.2
macOS 11

macOS Metal 2.3


macOS 12

macOS Metal 2.4
iOS 16.0
tvOS 16.0
watchOS 9.0
macOS 13

Metal 3.0


iOS 17.0
tvOS 17.0
watchOS 10.0
macOS 14

Metal 3.1





Metal fully supports cross compilation as an inherent part of its design. Depending on how your version of Metal is configured, it may have support for a number of cross compilers, or may only support a native target.

Specify the architecture to build for.

When building for Mac OS X, specify the minimum version supported by your application.

When building for iPhone OS, specify the minimum version supported by your application.

When building for Tv OS, specify the minimum version supported by your application.

When building for Watch OS, specify the minimum version supported by your application.

Specify that Metal should generate code for a specific processor family member and later. For example, if you specify -march=versionX, the compiler is allowed to generate instructions that are valid on versionX and later processors, but which may not exist on earlier ones.

Specify which optimization level to use:
-O0 Means "no optimization": this level compiles the fastest and generates the most debuggable code.

-O1 Somewhere between -O0 and -O2.

-O2 Moderate level of optimization which enables most optimizations.

-O3 Like -O2, except that it enables optimizations that take longer to perform or that may generate larger code (in an attempt to make the program run faster).

-Ofast Enables all the optimizations from -O3 along with other aggressive optimizations that may violate strict compliance with language standards.

-Os Like -O2 with extra optimizations to reduce code size.

-Oz Like -Os (and thus -O2), but reduces code size further.

-Og Like -O1. In future versions, this option might disable different optimizations in order to improve debuggability.

-O Equivalent to -O2.

-O4 and higher

Currently equivalent to -O3






Control debug information output. Note that Metal debug information works best at -O0. When more than one option starting with -g is specified, the last one wins:
-g Generate debug information.

-gline-tables-only Generate only line table debug information. This allows for symbolicated backtraces with inlining information, but does not include any information about variables, their locations or types.

-gmodules Generate debug information that contains external references to types defined in Metal modules or precompiled headers instead of emitting redundant debug type information into every object file. This option transparently switches the Metal module format to object file containers that hold the Metal module together with the debug information. When compiling a program that uses Metal modules or precompiled headers, this option produces complete debug information with faster compile times and much smaller object files.

This option should not be used when building static libraries for distribution to other machines because the debug info will contain references to the module cache on the machine the object files in the library were built on.




Metal supports a number of optimizations to reduce the size of debug information in the binary. They work based on the assumption that the debug type information can be spread out over multiple compilation units. For instance, Metal will not emit type definitions for types that are not needed by a module and could be replaced with a forward declaration. Further, Metal will only emit type info for a dynamic C++ class in the module that contains the vtable for the class.

The -fstandalone-debug option turns off these optimizations. This is useful when working with 3rd-party libraries that don't come with debug information. This is the default on Darwin. Note that Metal will never emit type information for types that are not referenced at all by the program.


This flag sets the default visibility level.

This flag specifies that variables without initializers get common linkage. It can be disabled with -fno-common.

Set the default thread-local storage (TLS) model to use for thread-local variables. Valid values are: "global-dynamic", "local-dynamic", "initial-exec" and "local-exec". The default is "global-dynamic". The default model can be overridden with the tls_model attribute. The compiler will try to choose a more efficient model if possible.

-###
Print (but do not run) the commands to run for this compilation.

Display available options.

Do not emit any warnings for unused driver arguments.

Pass the comma separated arguments in args to the assembler.

Pass the comma separated arguments in args to the linker.

Pass the comma separated arguments in args to the preprocessor.

Pass arg to the static analyzer.

Pass arg to the assembler.

Pass arg to the linker.

Pass arg to the preprocessor.

Write output to file.

Print the full library path of file.

Print the library path for the currently used compiler runtime library ("libgcc.a" or "libclang_rt.builtins.*.a").

Print the full program path of name.

Print the paths used for finding libraries and programs.

Save intermediate compilation results.

Save internal code generation (LLVM) statistics to a file in the current directory (-save-stats/"-save-stats=cwd") or the directory of the output file ("-save-state=obj").

Used to enable and disable, respectively, the use of the integrated assembler. Whether the integrated assembler is on by default is target dependent.

Time individual commands.

Print timing summary of each stage of compilation.

Show commands to run and use verbose output.

Adds an implicit #define into the predefines buffer which is read before the source file is preprocessed.

Adds an implicit #undef into the predefines buffer which is read before the source file is preprocessed.

Adds an implicit #include into the predefines buffer which is read before the source file is preprocessed.

Add the specified directory to the search path for include files.

Add the specified directory to the search path for framework include files.

Do not search the standard system directories or compiler builtin directories for include files.

Do not search the standard system directories for include files, but do search compiler builtin include directories.

Do not search metal's builtin directory for include files.

Set <file> as the native translator script.

These environment variables are checked, in order, for the location to write temporary files used during the compilation process.

If this environment variable is present, it is treated as a delimited list of paths to be added to the default system include path list. The delimiter is the platform dependent delimiter, as used in the PATH environment variable.

Empty components in the environment variable are ignored.


These environment variables specify additional paths, as for CPATH, which are only used when processing the appropriate language.

If -mmacosx-version-min is unspecified, the default deployment target is read from this environment variable. This option only affects Darwin targets.

If -miphoneos-version-min is unspecified, the default deployment target is read from this environment variable. This option only affects Darwin targets.

If -mtvos-version-min is unspecified, the default deployment target is read from this environment variable. This option only affects Darwin targets.

If -mwatchos-version-min is unspecified, the default deployment target is read from this environment variable. This option only affects Darwin targets.

To compile a bunch of ".metal" files for the air64 architecture:

$ metal -arch air64 -c foo.metal bar.metal


To link a bunch of ".air" files for the air64 architecture:

$ metal -arch air64 foo.air bar.air


To link a bunch of ".air" files and translate the linked output to native code:

$ metal -arch amdgpu_gfx600 -N gfx600.mtlp-json foo.air bar.air -o default-gfx600.metallib


Where gfx600.metal-pipelines is a native translator script.

To report bugs, please visit <https://developer.apple.com/bug-reporting/>. Most bug reports should include preprocessed source files (use the -E option) and the full output of the compiler, along with information to reproduce.

metal-arch(1), metal-config(1), metal-libtool(1), metal-nt(1), metal-pipelines-script(5), metal-tt(1)

Metal Shading Language Specification: <https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf>

2014-2024, The Metal Team

July 10, 2024 32023