SlideShare a Scribd company logo
Tutorial for developing SILOptimizer Pass
freddi - Yuki.Aki
at SwiftcKaigi #1 (16th.Nov)
About Me
- I’m freddi
- iOS Engineer @ Fukuoka
- twitter: _ _ _freddi_ _ _
Before Start
- This presentation is written in English
- This session will be talked in Japanese
Before Start
- This session is mainly for Beginner
- Better to listen previous SIL session
- Better to be familiar with C++ grammar
Today’s Aim
- Understand What SILOptimizer and Pass are.
- Understand How Pass optmize SIL Code
- Understand How Can we Play developing Pass
Flow of Compiling Swift Code,
with SILOptimizer
Flow of Compiling Swift Code
Flow of Compiling Swift Code
SILGen generates SIL from type-checked AST
SIL
- "Swift Intermediate Language” = SIL
- between type-checked AST and LLVM IR
- SIL Code is used for optimization of program
Between SILGen and IRGen
canonical
SIL
raw SIL SILOptimizer
raw SIL
- raw SIL code is generated from SILGen
- raw SIL has not been optimized and diagnosed
- optimized and diagnosed on SILOptimizer phase
raw SIL
canonical SIL
- Optimized and diagnosed SIL code is canonical SIL
- on SILOptimizer phase
canonical
SIL
canonical
SIL
raw SIL
Flow of raw SIL to canonical SIL
SILOptimizer
SILOptimizer
- SILOptimizer is one of phase in swiftc
- raw SIL is optimized and diagnosed on this phase
- It is consist of many module which called Pass
SILOptimizer
canonical
SIL
raw SIL SILOptimizer
Flow from raw SIL to canonical SIL
SILOptimizer
canonical
SIL
raw SIL Pass Pass Pass Pass
Flow from raw SIL to canonical SIL
= “PipeLine of Pass”
Pass in SILOptimizer
- Pass diagnoses and optimizes received ras SIL
- Some of Passes are called definitely
Pass
Pass Pipeline
- Pass Pipeline has info which Passes must be called
- Pipeline is created before starting optimizing
- Pass in Pipeline is decided by …
- swiftc Option (e.g.-O,-Onone) etc …
Pass in SILOptimizer
- Example of Optimizer Pass
- Memory Promotion
- Assuming Single Thread (Optional Optimizer)
Pass
Pass in SILOptimizer
- Example of Diagnostic Pass
- Overflow checking of Numeric Type
Pass
Pass in SILOptimizer
- Example of Diagnostic Pass
- Overflow checking of Numeric Type
Let's check it by Demo!
by using -Xllvm -sil-print-pass-name option
Pass
Pass in SILOptimizer
-Xllvm -sil-print-pass-name
SIL
- SIL is generated from our Swift Code
- via type-checked AST
- raw SIL and canonical SIL
- canonical SIL is optimized and diagnosed SIL
SILOptimizer and Pass
- SILOptimizer is phase of optimizing and diagnosing
- SILOptimizer is consist of many Pass
- raw SIL is optimized and diagnosed by Pass
How can we add optimize Pass to compiler?
To add original Pass to swiftc
1. Create your C++ file (because Pass is C++ class)
@ lib/SILOptimizer/Transforms/*.cpp
To add original Pass to swiftc
2. Add location of C++ source for Pass Class
to lib/SILOptimizer/Transforms/CMakeLists.txt
3. Generate swiftc Xcode Project
※ You must do 2.before generating Xcode Project
4. Register Pass info
@ include/swift/SILOptimizer/PassManager/Passes.def
5. Rebuild on Xcode
6. Add code which adding Pass to Pipeline
@ lib/SILOptimizer/PassManager/PassPipeline.cpp
To add original Pass to swiftc
7. Write Pass Class and Creating Pass object func
@ on your C++ file which is added on 1.
To add original Pass to swiftc
Demo
Get Swift Compiler Code
$ mkdir swift-source
$ cd swift-source
$ git clone https://github.com/apple/swift
Create your C++ file (because Pass is C++ class)
Add location of C++ source for Pass Class
Generate swiftc Xcode Project
$ cd swift-source
$ utils/update-checkout --clone
$ utils/build-script 
—debug 
—xcode --skip-build-benchmarks
Register Pass info and Rebuild on Xcode
Add code which adding Pass to Pipeline
Write Pass Class and Creating Pass object func
Demo
Tutorial for developing SILOptimizer Pass
Understand structural unit of SIL Code
treated by SILOptimizer Pass
SIL Component
- SILModule
- SILGlobalVariable-s
- SILFunction-s
- SILBasicBlock-s
- SILInstruction-s
- etc …
Demo again :)
SILModule
SILFunction-sSILModule
SILBasicBlock-sSILFunctions
SILBasicBlock-s SILInstruction-s
Basic Structure of SILInstruction
What instruction is using it (or Instruction’s id)
Instruction’s id
(Some instruction has no id on left of =)
Kind of SILInstruction
Integer Literal Instruction
Function Ref Instruction
Apply Literal Instruction
Understand structural unit of SIL Code
treated by SILOptimizer Pass
Understand structural unit of SIL Code
treated by SILOptimizer Pass
==
We can use these unit as C++ Class
on writing Pass
Demo time XD
AssumeSingleThreaded
SILFunctionTransform
Check Option
getFunction = Collection of BasicBlock
Basic Block = Collection of Instruction
Checking the Instruction is kind of Ref Count
Set Instruction non atomic
Invalidate Analysis
Now,we can read easy Pass!
Let’s play Pass by Demo!

More Related Content

KEY
Future of PHP
PDF
BDD API Tests with Gherkin and Behat
KEY
Testing Adhearsion Applications
PPSX
Community Tech Days C# 4.0
PPTX
轉轉轉好運旺來一起來之雲端轉檔大作戰!
PDF
Improving DroidBox
PDF
Building a chatbot – step by step
PDF
Eugene PHP June 2015 - Let's Talk Laravel
Future of PHP
BDD API Tests with Gherkin and Behat
Testing Adhearsion Applications
Community Tech Days C# 4.0
轉轉轉好運旺來一起來之雲端轉檔大作戰!
Improving DroidBox
Building a chatbot – step by step
Eugene PHP June 2015 - Let's Talk Laravel

What's hot (20)

PDF
The Paved PaaS to Microservices at Netflix (IAS2017 Nanjing)
PDF
How to generate JAR file dynamically compiling Java code in Kotlin
PPTX
Java 8 concurrency abstractions
PDF
Refactoring a go project
PDF
Testing Automaton - CFSummit 2016
PDF
Slaying Monoliths with Node and Docker
PDF
Killer Docker Workflows for Development
PDF
2021laravelconftwslides6
PDF
vienna.js - Automatic testing of (RESTful) API documentation
PDF
Elixir – Peeking into Elixir's Processes, OTP and Supervisors
PDF
Introduction to Dynamic Analysis of Android Application
PPTX
Aspect j introduction for non-programmers
PDF
Using PHP Functions! (Not those functions, Google Cloud Functions)
PDF
NLP using JavaScript Natural Library
PPTX
Microservices for building an IDE – The innards of JetBrains Rider - TechDays...
PPTX
Introduction to Scala language
PDF
B 2 line game cloud - our personal ec2
PDF
Lecture 12
PPT
Building scalable and language independent java services using apache thrift
PPT
Stuff we noticed while building "Asterisk in the cloud"
The Paved PaaS to Microservices at Netflix (IAS2017 Nanjing)
How to generate JAR file dynamically compiling Java code in Kotlin
Java 8 concurrency abstractions
Refactoring a go project
Testing Automaton - CFSummit 2016
Slaying Monoliths with Node and Docker
Killer Docker Workflows for Development
2021laravelconftwslides6
vienna.js - Automatic testing of (RESTful) API documentation
Elixir – Peeking into Elixir's Processes, OTP and Supervisors
Introduction to Dynamic Analysis of Android Application
Aspect j introduction for non-programmers
Using PHP Functions! (Not those functions, Google Cloud Functions)
NLP using JavaScript Natural Library
Microservices for building an IDE – The innards of JetBrains Rider - TechDays...
Introduction to Scala language
B 2 line game cloud - our personal ec2
Lecture 12
Building scalable and language independent java services using apache thrift
Stuff we noticed while building "Asterisk in the cloud"
Ad

More from 秋 勇紀 (7)

PDF
For Developing URL Routing of SwiftUI App
PDF
A Deeper Deep Dive into Swift Literal
PDF
What Swifty is, from Enum
PDF
Deep Dive into Swift Literal
PDF
SILOptimizerのCode Reading入門
PDF
自作コンパイラのお話
PDF
ディープラーニングでポプテピピック
For Developing URL Routing of SwiftUI App
A Deeper Deep Dive into Swift Literal
What Swifty is, from Enum
Deep Dive into Swift Literal
SILOptimizerのCode Reading入門
自作コンパイラのお話
ディープラーニングでポプテピピック
Ad

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Review of recent advances in non-invasive hemoglobin estimation
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm

Tutorial for developing SILOptimizer Pass