SIL for the first time
Yusuke Kita
@kitasuke
SIL
Swift Intermediate
Language
Why SIL?
Better understanding of how
optimizations work
What is SIL?
SIL is an SSA-form IR
with high-level
semantic information
designed to implement
the Swift programming
language
Swift Programming
Language
Swift Compiler
Where all the magic happens !
How it works?
Swift Compiler
Swift Compiler
• Front end
Swift Compiler
• Front end
• Middle end
Swift Compiler
• Front end
• Middle end
• Back end
Swift Compiler
• Front end
• Middle end
• Back end
Parsing ➡ AST
Semantic analysis
➡ type checked AST
SIL generation ➡ raw SIL
SIL guaranteed transformations
➡ canonical SIL
Swift Compiler
• Front end
• Middle end
• Back end
LLVM IR Generation
➡ LLVM IR
Swift Compiler
• Front end
• Middle end
• Back end
LLVM ➡ .o
Recap
Swift Compiler
source ➡ executable
How SIL works?
SIL Stage
SIL Stage
• Raw SIL
SIL Stage
• Raw SIL
• Canonical SIL
Raw SIL
• May not have a fully-constructed SSA graph
• May contain dataflow errors
• Should not be used for native code generation or
distribution
Canonical SIL
• Dataflow errors must be eliminated
• Certain instructions must be canonicalized to simpler forms.
• Performance optimization and native code generation are
derived from this form
• A module can be distributed containing SIL in this (or later)
forms.
Optimizations
Optimizations
• Guaranteed Optimization Passes
and Diagnostic Passes
Optimizations
• Guaranteed Optimization Passes
and Diagnostic Passes
• General Optimization Passes
(not running at -Onone)
Guaranteed Optimization Passes
and Diagnostic Passes
• Mandatory inlining
• Memory promotion
• Constant propagation
• Return analysis
• Critical edge splitting
General Optimization Passes
• Generic Specialization
• Witness and VTable Devirtualization
• Performance Inlining
• Reference Counting Optimizations
• Memory Promotion/Optimizations
• High-level domain specific optimizations
Lots of optimizations
happening here !
SIL Gen
SIL Gen
• Raw SIL
$swiftc -emit-silgen sample.swift
SIL Gen
• Raw SIL
$swiftc -emit-silgen sample.swift
• Canonical SIL
$swiftc -emit-sil sample.swift
How .sil file looks like?
sample.swift
func number() -> Int {
let x: Int
x = 1
return x
}
sample.sil
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// main
...
// number()
sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int {
bb0:
%0 = alloc_stack $Int, let, name "x" // users: %3, %4
%1 = integer_literal $Builtin.Int64, 1 // user: %2
%2 = struct $Int (%1 : $Builtin.Int64) // users: %5, %3
store %2 to %0 : $*Int // id: %3
dealloc_stack %0 : $*Int // id: %4
return %2 : $Int // id: %5
} // end sil function '_T06sample6numberSiyF'
// Int.init(_builtinIntegerLiteral:)
...
Let's take a look
from the top on down
SIL Stage
sil_stage canonical
SIL files declare the processing stage of the included SIL with
one of the declarations
sil_stage raw or sil_stage canonical at top level.
Import
import Builtin
import Swift
import SwiftShims
SIL is reliant on Swift's type system and declarations. In a .sil
file, there are no implicit imports. The swift and/or Builtin
standard modules must be imported explicitly if used.
Legal SIL Types
• A loadable legal SIL type, $T
• The address of a legal SIL type, $*T
Linkage
sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int
hidden definitions are unique and visible only within the
current Swift module. In LLVM IR, they will be emitted with
external linkage and hidden visibility.
Name Mangling
sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int
The name @_T06sample6numberSiyF is the mangled name of
the number Swift function.
Calling Convention
How Swift functions are emitted in SIL
• @convention(swift)
• @convention(method)
• @convention(witness_method)
• @convention(c)
• @convention(objc_method)
Type Lowering
sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int
A formal type is the type of a value in Swift, such as an
expression result.Type lowering is the process of turning a
formal type into its lowered type.
The lowered type of a declaration will usually be thin.
Basic Blocks
bb0:
In SIL, basic blocks take arguments, which are used as an
alternative to LLVM's phi nodes.
alloc_stack
%0 = alloc_stack $Int, let, name "x"
Allocates uninitialized memory that is sufficiently aligned on
the stack to contain a value of type Int.
integer_literal
%1 = integer_literal $Builtin.Int64, 1
Creates an integer literal value. The result will be of type
Builtin.Int64, which must be a builtin integer type. The literal
value is specified using Swift’s integer literal syntax.
struct
%2 = struct $Int (%1 : $Builtin.Int64)
Creates a value of a loadable struct type by aggregating
multiple loadable values.
store
store %2 to %0 : $*Int
Stores the value %2 to memory at address %0. The type of %0
is *Int and the type of %2 is Int.
dealloc_stack
dealloc_stack %0 : $*Int
Deallocates memory previously allocated by alloc_stack.
The allocated value in memory must be uninitialized or
destroyed prior to being deallocated.
return
return %2 : $Int
Exits the current function and returns control to the calling
function.
sample.sil
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// main
...
// number()
sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int {
bb0:
%0 = alloc_stack $Int, let, name "x" // users: %3, %4
%1 = integer_literal $Builtin.Int64, 1 // user: %2
%2 = struct $Int (%1 : $Builtin.Int64) // users: %5, %3
store %2 to %0 : $*Int // id: %3
dealloc_stack %0 : $*Int // id: %4
return %2 : $Int // id: %5
} // end sil function '_T06sample6numberSiyF'
// Int.init(_builtinIntegerLiteral:)
...
SIL with optimizations
swiftc -emit-sil sample.swift -O
Compile with optimizations
sample.swift
func number() -> Int {
let x: Int
x = 1
return x
}
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// main
...
// number()
sil hidden @_T06sample6numberSiyF : $@convention(thin) () -> Int {
bb0:
%0 = integer_literal $Builtin.Int64, 1 // user: %1
%1 = struct $Int (%0 : $Builtin.Int64) // user: %2
return %1 : $Int // id: %2
} // end sil function '_T06sample6numberSiyF'
Summary
• Not so difficult than expected
• Good to know how Swift Compiler works as Swift developer
• Especially Swift optimizations
References
• swift/docs/SIL.rst
• Swiftコンパイラのアーキテクチャ
• Swiftコンパイラの構造と基盤テクニック
Thank you

More Related Content

PDF
Advanced Internationalization with Rails
PDF
Php Crash Course - Macq Electronique 2010
PPTX
Flying under the radar
PDF
A Deeper Deep Dive into Swift Literal
PDF
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any La...
KEY
Source Filters in Perl 2010
PPTX
SADI in Perl - Protege Plugin Tutorial (fixed Aug 24, 2011)
PDF
A Quick Taste of C
Advanced Internationalization with Rails
Php Crash Course - Macq Electronique 2010
Flying under the radar
A Deeper Deep Dive into Swift Literal
MongoDB World 2019: BSON Transpilers: Transpiling from Any Language to Any La...
Source Filters in Perl 2010
SADI in Perl - Protege Plugin Tutorial (fixed Aug 24, 2011)
A Quick Taste of C

What's hot (20)

PDF
Hello World on Slim Framework 3.x
PDF
PHP 良好實踐 (Best Practice)
PDF
perl course-in-mumbai
PDF
Zend Server: Not just a PHP stack
PDF
PHP 7.0 new features (and new interpreter)
PPTX
Understand prototype
PDF
Pioc
PDF
Beyond design patterns phpnw14
PDF
Don't be STUPID, Grasp SOLID - North East PHP
PDF
IoC&Laravel
PPTX
Migrating to PHP 7
PPT
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
PDF
Don't Be STUPID, Grasp SOLID - ConFoo Edition
PDF
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
PPTX
Creating your own framework on top of Symfony2 Components
PPTX
iOS,From Development to Distribution
PDF
Swift Reversing by Ryan Stortz
PDF
Crafting Quality PHP Applications (PHP Benelux 2018)
PDF
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
PDF
Crafting Quality PHP Applications (ConFoo YVR 2017)
Hello World on Slim Framework 3.x
PHP 良好實踐 (Best Practice)
perl course-in-mumbai
Zend Server: Not just a PHP stack
PHP 7.0 new features (and new interpreter)
Understand prototype
Pioc
Beyond design patterns phpnw14
Don't be STUPID, Grasp SOLID - North East PHP
IoC&Laravel
Migrating to PHP 7
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
Don't Be STUPID, Grasp SOLID - ConFoo Edition
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
Creating your own framework on top of Symfony2 Components
iOS,From Development to Distribution
Swift Reversing by Ryan Stortz
Crafting Quality PHP Applications (PHP Benelux 2018)
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Crafting Quality PHP Applications (ConFoo YVR 2017)
Ad

Similar to SIL for the first time (20)

PDF
Swift - Under the Hood
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
PDF
Open Source Swift Under the Hood
PDF
Compiler2016 by abcdabcd987
PDF
Rider - Taking ReSharper out of Process
PDF
Swift 2 Under the Hood - Gotober 2015
PDF
掀起 Swift 的面紗
PPTX
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
PPTX
02 - Introduction to the cdecl ABI and the x86 stack
PPT
PPTX
04 - I love my OS, he protects me (sometimes, in specific circumstances)
PDF
Linux Shell Scripting Craftsmanship
PPTX
07 140430-ipp-languages used in llvm during compilation
PDF
Connect.Tech- Swift Memory Management
PDF
Silex and Twig (PHP Dorset talk)
PPTX
05 - Bypassing DEP, or why ASLR matters
PDF
Lec-1c.pdf
PDF
BlueHat Seattle 2019 || Modern Binary Analysis with ILs
PDF
Tutorial for developing SILOptimizer Pass
PPTX
Introduction to Sightly and Sling Models
Swift - Under the Hood
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Open Source Swift Under the Hood
Compiler2016 by abcdabcd987
Rider - Taking ReSharper out of Process
Swift 2 Under the Hood - Gotober 2015
掀起 Swift 的面紗
Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks
02 - Introduction to the cdecl ABI and the x86 stack
04 - I love my OS, he protects me (sometimes, in specific circumstances)
Linux Shell Scripting Craftsmanship
07 140430-ipp-languages used in llvm during compilation
Connect.Tech- Swift Memory Management
Silex and Twig (PHP Dorset talk)
05 - Bypassing DEP, or why ASLR matters
Lec-1c.pdf
BlueHat Seattle 2019 || Modern Binary Analysis with ILs
Tutorial for developing SILOptimizer Pass
Introduction to Sightly and Sling Models
Ad

More from Yusuke Kita (20)

PDF
Integrating libSyntax into the compiler pipeline
PDF
Making your own tool using SwiftSyntax
PDF
[Deprecated] Integrating libSyntax into the compiler pipeline
PDF
Creating your own Bitrise step
PDF
Introducing swift-format
PDF
Unidirectional Data Flow Through SwiftUI
PDF
Open Source Swift Workshop
PDF
Contributing to Swift Compiler
PDF
Writing a compiler in go
PDF
Writing an interpreter in swift
PDF
SIL Optimizations - AllocBoxToStack
PDF
SIL for First Time Learners
PDF
var, let in SIL
PDF
SIL for First Time Leaners LT
PDF
How to try! Swift
PDF
Introducing protobuf in Swift
PDF
Type-safe Web APIs with Protocol Buffers in Swift at AltConf
PDF
Type-safe Web APIs with Protocol Buffers in Swift at iOSCon
PDF
Swift core
PDF
SwiftCoreとFoundationを読んでみた
Integrating libSyntax into the compiler pipeline
Making your own tool using SwiftSyntax
[Deprecated] Integrating libSyntax into the compiler pipeline
Creating your own Bitrise step
Introducing swift-format
Unidirectional Data Flow Through SwiftUI
Open Source Swift Workshop
Contributing to Swift Compiler
Writing a compiler in go
Writing an interpreter in swift
SIL Optimizations - AllocBoxToStack
SIL for First Time Learners
var, let in SIL
SIL for First Time Leaners LT
How to try! Swift
Introducing protobuf in Swift
Type-safe Web APIs with Protocol Buffers in Swift at AltConf
Type-safe Web APIs with Protocol Buffers in Swift at iOSCon
Swift core
SwiftCoreとFoundationを読んでみた

Recently uploaded (20)

PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
STKI Israel Market Study 2025 version august
PDF
Architecture types and enterprise applications.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Hindi spoken digit analysis for native and non-native speakers
PPT
What is a Computer? Input Devices /output devices
PPTX
Chapter 5: Probability Theory and Statistics
DOCX
search engine optimization ppt fir known well about this
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Benefits of Physical activity for teenagers.pptx
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Unlock new opportunities with location data.pdf
PDF
CloudStack 4.21: First Look Webinar slides
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
WOOl fibre morphology and structure.pdf for textiles
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
STKI Israel Market Study 2025 version august
Architecture types and enterprise applications.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
DP Operators-handbook-extract for the Mautical Institute
Getting started with AI Agents and Multi-Agent Systems
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Hindi spoken digit analysis for native and non-native speakers
What is a Computer? Input Devices /output devices
Chapter 5: Probability Theory and Statistics
search engine optimization ppt fir known well about this
Module 1.ppt Iot fundamentals and Architecture
1 - Historical Antecedents, Social Consideration.pdf
Benefits of Physical activity for teenagers.pptx
Final SEM Unit 1 for mit wpu at pune .pptx
Group 1 Presentation -Planning and Decision Making .pptx
Unlock new opportunities with location data.pdf
CloudStack 4.21: First Look Webinar slides
NewMind AI Weekly Chronicles – August ’25 Week III

SIL for the first time