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:
- Driver
- 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.
- Preprocessing
- 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.
- Parsing and Semantic
Analysis
- 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).
- Code Generation and
Optimization
- 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.
- 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.
- Linker
- 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.
- -E
- Run the preprocessor stage.
- -fsyntax-only
- Run the preprocessor, parser and type checking stages.
- -S
- Run the previous stages as well as LLVM generation and optimization stages
and target-specific code generation, producing an assembly file.
- -c
- Run all of the above, plus the assembler, generating a target
".o" object file.
- no stage selection
option
- 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.
- -x <language>
- Treat subsequent input files as having type language.
- -std=<standard>
- Specify the language standard to compile for.
Supported values for the Metal language are:
ios-metal1.0
ios-metal1.1
ios-metal1.2
ios-metal2.0
ios-metal2.1
ios-metal2.2
ios-metal2.3
ios-metal2.4
macos-metal1.1
macos-metal1.2
macos-metal2.0
macos-metal2.1
macos-metal2.2
macos-metal2.3
macos-metal2.4
metal3.0
metal3.1
The default Metal language standard depends on the chosen
deployment target for Apple platform:
iOS 8.0
iOS 9.0
tvOS 9.0
iOS 10.0
tvOS 10.0
watchOS 3.0
iOS 11.0
tvOS 11.0
watchOS 4.0
iOS 12.0
tvOS 12.0
watchOS 5.0
iOS 13.0
tvOS 13.0
watchOS 6.0
iOS 14.0
tvOS 14.0
watchOS 7.0
iOS 15.0
tvOS 15.0
watchOS 8.0
Mac OS X 10.11
Mac OS X 10.12
Mac OS X 10.13
Mac OS X 10.14
Mac OS X 10.15
macOS 11
macOS 12
iOS 16.0
tvOS 16.0
watchOS 9.0
macOS 13
iOS 17.0
tvOS 17.0
watchOS 10.0
macOS 14
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.
- -miphoneos-version-min
- When building for iPhone OS, specify the minimum version supported by your
application.
- -mtvos-version-min
- When building for Tv OS, specify the minimum version supported by your
application.
- -mwatchos-version-min
- When building for Watch OS, specify the minimum version supported by your
application.
- -march=<cpu>
- 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.
- -O0, -O1, -O2, -O3, -Ofast,
-Os, -Oz, -Og, -O, -O4
- 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
- -g, -gline-tables-only,
-gmodules
- 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.
- -fstandalone-debug
-fno-standalone-debug
- 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.
- -fcommon,
-fno-common
- This flag specifies that variables without initializers get common
linkage. It can be disabled with -fno-common.
- -ftls-model=<model>
- 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.
- --help
- Display available options.
- -Wa,<args>
- Pass the comma separated arguments in args to the assembler.
- -Wl,<args>
- Pass the comma separated arguments in args to the linker.
- -Wp,<args>
- Pass the comma separated arguments in args to the preprocessor.
- -print-libgcc-file-name
- Print the library path for the currently used compiler runtime library
("libgcc.a" or "libclang_rt.builtins.*.a").
- -integrated-as,
-no-integrated-as
- Used to enable and disable, respectively, the use of the integrated
assembler. Whether the integrated assembler is on by default is target
dependent.
- -time
- Time individual commands.
- -v
- Show commands to run and use verbose output.
- -D<macroname>=<value>
- Adds an implicit #define into the predefines buffer which is read before
the source file is preprocessed.
- -U<macroname>
- Adds an implicit #undef into the predefines buffer which is read before
the source file is preprocessed.
- -include
<filename>
- Adds an implicit #include into the predefines buffer which is read before
the source file is preprocessed.
- -I<directory>
- Add the specified directory to the search path for include files.
- -F<directory>
- Add the specified directory to the search path for framework include
files.
- -nostdinc
- Do not search the standard system directories or compiler builtin
directories for include files.
- -nostdlibinc
- Do not search the standard system directories for include files, but do
search compiler builtin include directories.
- -nobuiltininc
- Do not search metal's builtin directory for include files.
- -N <file>
- Set <file> as the native translator script.
- TMPDIR, TEMP,
TMP
- These environment variables are checked, in order, for the location to
write temporary files used during the compilation process.
- CPATH
- 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.
- MACOSX_DEPLOYMENT_TARGET
- If -mmacosx-version-min is unspecified, the default deployment
target is read from this environment variable. This option only affects
Darwin targets.
- IPHONEOS_DEPLOYMENT_TARGET
- If -miphoneos-version-min is unspecified, the default deployment
target is read from this environment variable. This option only affects
Darwin targets.
- TVOS_DEPLOYMENT_TARGET
- If -mtvos-version-min is unspecified, the default deployment target
is read from this environment variable. This option only affects Darwin
targets.
- WATCHOS_DEPLOYMENT_TARGET
- 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-2023, The Metal Team