The Swift Compiler is a mysterious multi-headed hydra. It takes your code through a many-step journey from hello world
to machine code:
First, it parses your code into tokens on an abstract syntax tree. It performs semantic analysis on this data structure in real-time, checking types and emitting compiler warnings.
Next, it generates SIL, Swift Intermediate Language, and optimises the result.
Finally, it’s converted into LLVM Intermediate Representation; which allows it to be handed off to the LLVM toolchain. This performs more optimisations and allows the resulting to machine code to be compiled for any chip architecture.
The Swift Intermediate Language generation step, and subsequent optimisations, makes Swift special, and goes a long way towards its eponymous speed.
The SIL optimisations might include:
Performing devirtualisation — this replaces table dispatch with faster static dispatch.
Specialisation — Generating specific versions of generic functions to further avoid dynamic dispatch.
Dead code elimination to prevent code that’s never executed from bloating the binary.
Paid subscribers get full access to this article, plus my full Quick Hacks library. You’ll also get all my long-form content 3 weeks before anyone else.
Keep reading with a 7-day free trial
Subscribe to Jacob’s Tech Tavern to keep reading this post and get 7 days of free access to the full post archives.