5.1 · From source to machine code, and how to look inside
Most Swift questions about speed or errors are really questions about which compiler stage decides something. This section is the map, and the commands that let you check it yourself.
Mental model. Swift compiles through SIL, a Swift-specific intermediate language, before LLVM ever sees it. Many diagnostics and almost all Swift-specific optimisations happen there.
The stages
What runs in order, and which errors belong to which stage.
- What are the stages from Swift source to machine code, and why does Swift have SIL?
The compiler works in stages. Parsing turns text into a syntax tree. Semantic analysis (Sema) resolves names and type-checks, using a constraint solver for… - What does the compiler guarantee through its mandatory SIL passes?
Mandatory passes run in every build, including -Onone, because they enforce language rules rather than speed. They include: definite initialisation (every let…
Looking inside
The flags that show you each stage, and how to read SIL.
- How do you look at what the compiler produced at each stage?
Ask the compiler to stop and print. swiftc -dump-ast shows the type-checked tree. -emit-silgen shows raw SIL, -emit-sil SIL after the mandatory passes, and… - How do you read SIL to tell static dispatch from dynamic dispatch?
Look at the instruction that precedes the apply (the call). function_ref names one specific function: a direct call, which can be inlined. class_method looks…
More in Compiler & language internals
- 5.2 · Devirtualisation, whole-module optimisation, inlining, exclusivity
- 5.3 · Specialisation, witness tables, and opaque vs existential in the compiled code
- 5.4 · Library evolution, module interfaces, and the ABI boundary
- 5.5 · How Swift macros work, and the roles they play
- 5.6 · Why it compiles slowly, and what rebuilds
- 5.7 · Swift 6 language mode and upcoming features
- 5.8 · Symbol names, linkage, lazy globals and reflection metadata: what is in the binary
- 5.9 · Optimisation levels, closure contexts, ownership conventions and value layout
- 5.10 · What property wrappers, result builders, async and throws compile to
- 5.11 · Package access, SPI, internal imports, strict memory safety and pre-specialisation