Share
Embed
Swift compiler 101 & How async function works
The introduction of the Swift compiler and an explanation of how it implements asynchronous functions
[email protected]
January 23, 2025
Transcript
109 slides
-
2025/01/17 w a iw a i swiftc shiz Swift compiler
101 How async function works & -
Self-introduction •shiz(sizu) X: @stzn3 GitHub: @stzn •iOS engineer in Fukuoka,
Japan .ZCPPLT +BQBOFTFUSBOTMBUJPOPG4XJGUHVJEFT -
Contents •Swift compiler 101 •Two features of Swift async function
(overview) •Two features of Swift async function (detail) -
Contents •Swift compiler 101 •Two features of Swift async function
(overview) •Two features of Swift async function (detail) -
Compiler •A program that translate source code from a high-level
programming language to a low- level programming language -
Mainly compiler does •Parsing • Divide the source code into
the smallest meaningful units "tokens." •Lexical analysis • Check syntax rules based on tokens and analyze the structure of the program. Generate AST •Semantic analysis • Check the type and scope of the variable and check if the program is semantically correct •IR generation • Generate code to make the compiler optimized •Optimization • Transform IR into functionally equivalent but faster (or smaller) forms •Code generation • Translate transformed IR into machine language ⚠Depends on the compiler and so on…. -
The main pipelines of compiler • Parsing • Divide the
source code into the smallest meaningful units "tokens." • Lexical analysis • Check syntax rules based on tokens and analyze the structure of the program. Generate AST • Semantic analysis • Check the type and scope of the variable and check if the program is semantically correct •IR generation • Generate code to make the compiler optimized • Optimization • Transform IR into functionally equivalent but faster (or smaller) forms • Code generation • Translate transformed IR into machine language ⚠Depends on the compiler and so on…. -
IR •Intermediate Representation •Intermediate code generated in the process of
converting programming language code to machine language -
IR • Intermediate Representation • Intermediate code generated in the
process of converting programming language code to machine language •Why needed? • The structure is simple and the code is easy to optimize • Generate code for various targets (e.g. Apple Silicon's MacOS) • Development e ff i ciency by separating compiler processes -
The main pipelines of Swift compiler 1. Parse(Parsing, Lexical analysis)
2. Sema(Semantic Analysis) 3. SILGen(IR generation) 4.SILOptimizer(Optimization) 5. IRGen(IR generation) 6.LLVM(Optimization, Code generation) https://qiita.com/rintaro/items/3ad640e3938207218c20 ❶ ❷ ❸ ❹ ❺ ❻ -
The main pipelines of Swift compiler 1. Parse(Parsing, Lexical analysis)
2. Sema(Semantic Analysis) 3.SILGen(IR generation) 4. SILOptimizer(Optimization) 5.IRGen(IR generation) 6. LLVM(Optimization, Code generation) https://qiita.com/rintaro/items/3ad640e3938207218c20 ⚠More IRs are generated (e.g. AST) -
SILGen •Generate SIL •SIL: Swift Intermediate Language •High-level, Swift-speci fi
c intermediate language suitable for further analysis and optimization of Swift code TIPS: Documentation about SIL was updated recently -
IRGen •Generate LLVM IR •IR for LLVM generated to facilitate
optimization in the process of generating the fi nal machine language -
The main pipelines of Swift compiler 1. Parse(Parsing, Lexical analysis)
2. Sema(Semantic Analysis) 3. SILGen(IR generation) 4. SILOptimizer(Optimization) 5. IRGen(IR generation) 6.LLVM(Optimization, Code generation) https://qiita.com/rintaro/items/3ad640e3938207218c20 -
LLVM •Not Swift •Compiler backend •Generate machine languages for various
targets from LLVM IR •Optimization according to the target •Used universally(C++ɺRustɺ ActionScript…) -
SIL & LLVM IR Swift SIL LLVM IR string integer
Array i64 i32 ptr -
Contents •Swift compiler 101 •Two features of Swift async function
(overview) •Two features of Swift async function (detail) -
Contents •Swift compiler 101 •Two features of Swift async function
(overview) •Two features of Swift async function (detail) -
Two features of Swift async function •Keep local state on
a special stack •Split functions into partial functions that run between suspensions -
Two features of Swift async function •Keep local state on
a special stack •Split functions into partial functions that run between suspensions -
Stack memory Sync function Call stack(※) on stack memory Call
stack func1 func2 func3 Thread ※ A data structure that stores info about active functions -
Stack(data structure) Stack Element LIFO(Last-In, First-Out) Element Stack Element Element
Last-In First-Out push pop -
Stack(data structure) Stack Element LIFO(Last-In, First-Out) Element Stack Element Element
Last-In First-Out ⚠Stack can also be used in heap memory -
Call stack func1 func2 func3 Thread Stack frame Parameters Local
variables Return address …and so on -
Thread Sync function Start Finish Call stack func1 func2 foo
Call stack func1 func2 Thread foo Pop Push disappear -
Call stack func1 func2 Thread async Need to pop the
frame on suspends Await Call stack func1 func2 Thread async async Run other operations Start disappear -
Call stack func1 func2 Thread async Need the frame again
Call stack func1 func2 Thread async Call stack func1 func2 func3 Thread Resume async Hmm…? Await ⚠Might be a di ff erent thread Start -
async Heap memory Stack memory Store to another stack Thread
func1 func2 async Call stack Stack Stack on heap memory -
Only info across suspend points async Stack Call stack async
a b Suspend point -
async Stack Call stack async Async frame Information without any
suspend points in- between Information across suspend points -
Stack memory Return to call stack on resume Stack Thread
⚠might be a di ff erent thread func4 func5 async Call stack Heap memory -
Heap memory Hold memory areas Task TaskAllocator Memory 💡It is
done when creating a Task Task TaskAllocator Memory -
Heap memory Task TaskAllocator Memory async Memory Allocate a new
memory if needed -
Heap memory Task More detail TaskAllocator Memory async StackAllocator(TaskAllocator is
a kind of typealias) Slab Continuous memory Stack discipline + Bump pointer allocation Mechanism for e ff i cient memory allocation -
Two features of Swift async function •Keep local state on
a special stack •Split functions into partial functions that run between suspensions -
Two features of Swift async function •Keep local state on
a special stack •Split functions into partial functions that run between suspensions -
Split function between await Partial function hoge1 hoge2 Partial function
await = potential suspend point -
Appendix: ramp & resume function •ramp function: By the fi
rst await •resume function: After await -
hoge1 Call the process a ft er await on resume
await hoge2 https://github.com/swiftlang/swift/blob/main/docs/SIL/Types.md#async-functions async frame Process on resume -
hoge1 await hoge2 https://github.com/swiftlang/swift/blob/main/docs/SIL/Types.md#async-functions async frame Process on resume 💡Information
where to run on the Call stack Call the process a ft er await on resume -
hoge1 await await hoge2 async frame Process on resume Function
Pointer Pass address directly -
hoge1 Call when complete the process await await hoge2 async
frame Process on resume Function Pointer call -
hoge1 await await hoge2 async frame Process on resume Function
Pointer call Tail call 💡No need to use stack(Memory optimization) -
Task Entire task info async frame async frame async frame
One async function info AsyncContext -
hoge1 await hoge2 async frame Process on resume AsyncContext pointer
Get info from AsyncContext via pointer AsyncContext pointer AsyncContext pointer -
LLVM! 1. Parse(Parsing, Lexical analysis) 2. Sema(Semantic Analysis) 3. SILGen(IR
generation) 4. SILOptimizer(Optimization) 5. IRGen(IR generation) 6.LLVM(Optimization, Code generation) https://qiita.com/rintaro/items/3ad640e3938207218c20 -
Why needed? •Minimize overhead • Omit unnecessary process • Originally
suitable for C++ & not fi t for the usage of Swift(e.g. for loop) • Emphasis on advanced optimization on the Swift side • Compatible with Swift's unique calling conventions • Supports future language features • Flexible memory management by the caller(via Task) -
Contents •Swift compiler 101 •Two features of Swift async function
(overview) •Two features of Swift async function (detail) -
Contents •Swift compiler 101 •Two features of Swift async function
(overview) •Two features of Swift async function (detail) -
Module Structure of LLVM IR Function BasicBlock Instruction Manage the
entire program One program List of instructions executed in order Actual instruction to the machine -
Function Function Module Almost like this if … else BasicBlock
Instruction Instruction … BasicBlock Instruction Instruction … if … else BasicBlock Instruction Instruction … BasicBlock Instruction Instruction … -
Two features of Swift async function •Keep local state on
a special stack •Split functions into partial functions that run between suspensions -
•Common parent classes for all values (variables, constants, functions, instructions,
etc.) •def-use chain: Hold a list of other Values that use this value(UseList) Value class https://llvm.org/doxygen/classllvm_1_1Value.html LLVM -
•If two BasicBlocks A and B are given, check whether
the path from A to B passes through the suspend point (whether a suspend instruction is included) SuspendCrossingInfo class https://github.com/swiftlang/llvm-project/blob/next/llvm/include/llvm/Transforms/Coroutines/SuspendCrossingInfo.h LLVM -
BasicBlock Value What does it do? UseList BasicBlock Value BasicBlock
Value BasicBlock Value … LLVM Across suspend point? SuspendCrossingInfo compares BasicBlocks after splitting functions -
Unnecessary async frame… Nothing across suspend point Async frame is
created but https://github.com/swiftlang/swift/issues/72289 -
Investigation Need a memory if a value is included in
Spills Is it because swift_task_alloc(※) is called? ※ Called when AsyncContext requires a dynamic size https://github.com/swiftlang/swift/issues/72289 -
All async functions calls it = AsyncContext size ❌static https://github.com/swiftlang/swift/blob/main/lib/IRGen/GenCall.cpp#L166
https://github.com/swiftlang/swift/issues/72289 Investigation -
•My understanding is correct? •How solve it? •If not across
suspend point, use a static AsyncContextSize? (Not sure how to judge) •Erase swift_task_alloc on SILOptimizer or LLVM? Hmm…🤔 https://github.com/swiftlang/swift/issues/72289 -
Two features of Swift async function •Keep local state on
a special stack •Split functions into partial functions that run between suspensions -
Optimization with multiple passes มલ LLVM IR Pass Pass มޙ
LLVM IR … Parse & Optimization -
Functions split is one of them มલ LLVM IR Pass
Pass มޙ LLVM IR … Function split -
Coroutine Transformation Passes •CoroEarly •CoroSplit •CoroAnnotationElide •CoroElide •CoroCleanup https://llvm.org/docs/Coroutines.html#coroutine-transformation-passes LLVM
Coro = Coroutine Mechanism to handle async process (etc.) in LLVM -
Split BasicBlocks Function BasicBlock … Suspend Instruction … BasicBlock …
BasicBlock Suspend Instruction … ✂ https://github.com/swiftlang/llvm-project/blob/next/llvm/lib/Transforms/Coroutines/CoroSplit.cpp -
Contents •Swift compiler 101 •Two features of Swift async function
(overview) •Two features of Swift async function (detail) -
•Even if you are not familiar with C++, it's okay
at fi rst •Debug and grasp the processing fl ow (it’s important to fi nd the entry point) •Get used to frequent words and abbreviations •Don't be misled by casting For those who are interested in Swift compilers -
• Swift ίϯύΠϥͷΞʔΩςΫνϟ(The architecture of Swift compiler) • Swift Compiler
• Awesome LLVM • SwiftίϯύΠϥ։ൃೖ(The introduction of Swift compiler dvelopment) • Explore Swift performance • Swift concurrency: Behind the scenes • Swift repository • LLVM repository(forked by Apple) • Async lowering • Issue about unnecessary async frame References