Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift compiler 101 & How async function works

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
Tweet

Other Decks in Programming

Transcript

  1. 2025/01/17 w a iw a i swiftc shiz Swift compiler

    101 How async function works &
  2. Contents •Swift compiler 101 •Two features of Swift async function

    (overview) •Two features of Swift async function (detail)
  3. Contents •Swift compiler 101 •Two features of Swift async function

    (overview) •Two features of Swift async function (detail)
  4. Compiler •A program that translate source code from a high-level

    programming language to a low- level programming language
  5. 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….
  6. 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….
  7. IR •Intermediate Representation •Intermediate code generated in the process of

    converting programming language code to machine language
  8. 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
  9. 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 ❶ ❷ ❸ ❹ ❺ ❻
  10. 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)
  11. 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
  12. IRGen •Generate LLVM IR •IR for LLVM generated to facilitate

    optimization in the process of generating the fi nal machine language
  13. 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
  14. LLVM •Not Swift •Compiler backend •Generate machine languages for various

    targets from LLVM IR •Optimization according to the target •Used universally(C++ɺRustɺ ActionScript…)
  15. Contents •Swift compiler 101 •Two features of Swift async function

    (overview) •Two features of Swift async function (detail)
  16. Contents •Swift compiler 101 •Two features of Swift async function

    (overview) •Two features of Swift async function (detail)
  17. Two features of Swift async function •Keep local state on

    a special stack •Split functions into partial functions that run between suspensions
  18. Two features of Swift async function •Keep local state on

    a special stack •Split functions into partial functions that run between suspensions
  19. Stack memory Sync function Call stack(※) on stack memory Call

    stack func1 func2 func3 Thread ※ A data structure that stores info about active functions
  20. 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
  21. Thread Sync function Start Finish Call stack func1 func2 foo

    Call stack func1 func2 Thread foo Pop Push disappear
  22. 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
  23. 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
  24. So

  25. async Heap memory Stack memory Store to another stack Thread

    func1 func2 async Call stack Stack Stack on heap memory
  26. async Stack Call stack async Async frame Information without any

    suspend points in- between Information across suspend points
  27. Stack memory Return to call stack on resume Stack Thread

    ⚠might be a di ff erent thread func4 func5 async Call stack Heap memory
  28. Heap memory Hold memory areas Task TaskAllocator Memory 💡It is

    done when creating a Task Task TaskAllocator Memory
  29. 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
  30. Two features of Swift async function •Keep local state on

    a special stack •Split functions into partial functions that run between suspensions
  31. Two features of Swift async function •Keep local state on

    a special stack •Split functions into partial functions that run between suspensions
  32. Appendix: ramp & resume function •ramp function: By the fi

    rst await •resume function: After await
  33. 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
  34. hoge1 Call when complete the process await await hoge2 async

    frame Process on resume Function Pointer call
  35. hoge1 await await hoge2 async frame Process on resume Function

    Pointer call Tail call 💡No need to use stack(Memory optimization)
  36. Task Entire task info async frame async frame async frame

    One async function info AsyncContext
  37. hoge1 await hoge2 async frame Process on resume AsyncContext pointer

    Get info from AsyncContext via pointer AsyncContext pointer AsyncContext pointer
  38. 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
  39. 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)
  40. Contents •Swift compiler 101 •Two features of Swift async function

    (overview) •Two features of Swift async function (detail)
  41. Contents •Swift compiler 101 •Two features of Swift async function

    (overview) •Two features of Swift async function (detail)
  42. 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
  43. Function Function Module Almost like this if … else BasicBlock

    Instruction Instruction … BasicBlock Instruction Instruction … if … else BasicBlock Instruction Instruction … BasicBlock Instruction Instruction …
  44. Two features of Swift async function •Keep local state on

    a special stack •Split functions into partial functions that run between suspensions
  45. •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
  46. •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
  47. BasicBlock Value What does it do? UseList BasicBlock Value BasicBlock

    Value BasicBlock Value … LLVM Across suspend point? SuspendCrossingInfo compares BasicBlocks after splitting functions
  48. Unnecessary async frame… Nothing across suspend point Async frame is

    created but https://github.com/swiftlang/swift/issues/72289
  49. 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
  50. •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
  51. BTW

  52. Two features of Swift async function •Keep local state on

    a special stack •Split functions into partial functions that run between suspensions
  53. Functions split is one of them ม׵લ LLVM IR Pass

    Pass ม׵ޙ LLVM IR … Function split
  54. Split BasicBlocks Function BasicBlock … Suspend Instruction … BasicBlock …

    BasicBlock Suspend Instruction … ✂ https://github.com/swiftlang/llvm-project/blob/next/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
  55. Contents •Swift compiler 101 •Two features of Swift async function

    (overview) •Two features of Swift async function (detail)
  56. •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
  57. • 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