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.

  1. What are the stages from Swift source to machine code, and why does Swift have SIL? 5.1.1 · L3
    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…
  2. What does the compiler guarantee through its mandatory SIL passes? 5.1.2 · L5
    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.

  1. How do you look at what the compiler produced at each stage? 5.1.3 · L3
    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…
  2. How do you read SIL to tell static dispatch from dynamic dispatch? 5.1.4 · L5
    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