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

Swiftコンパイラ超入門+async関数の仕組み

 Swiftコンパイラ超入門+async関数の仕組み

2025/1/17 わいわいswiftc

[email protected]

January 18, 2025
Tweet

Other Decks in Programming

Transcript

  1. ίϯύΠϥ͕ߦ͏ओͳॲཧ • ࣈ۟ղੳ: ιʔείʔυΛτʔΫϯʹ෼ղ͠ɺߏจతʹղੳ͠΍͘͢͢Δ • ߏจղੳ: τʔΫϯΛجʹߏจنଇΛνΣοΫ͠ɺߏ଄ΛղੳɻASTΛੜ ੒ • ҙຯղੳ:

    ม਺ͷܕ΍είʔϓΛ֬ೝ͠ɺҙຯతʹਖ਼͍͔͠νΣοΫ •IRੜ੒: ίϯύΠϥ͕࠷దԽ͠΍͘͢ΔͨΊͷίʔυΛੜ੒ • ࠷దԽ: ੑೳΛ޲্ͤ͞ΔͨΊʹɺIRΛΑΓૣ͘(খ͘͞)ม׵͢Δ • ίʔυੜ੒: ػցޠΛੜ੒͠ɺCPU͕࣮ߦՄೳͳܗࣜʹม׵ 💡 ίϯύΠϥʹΑͬͯ͸ߦ͏͜ͱ͸มΘΔ
  2. ίϯύΠϥ͕ߦ͏ओͳॲཧ •ࣈ۟ղੳ: ιʔείʔυΛτʔΫϯʹ෼ղ͠ɺίʔυΛߏจతʹղੳ͠΍͘͢ •ߏจղੳ: τʔΫϯΛجʹߏจنଇΛνΣοΫ͠ɺϓϩάϥϜͷߏ଄ΛղੳɻASTΛੜ੒ •ҙຯղੳ: ม਺ͷܕ΍είʔϓΛ֬ೝ͠ɺϓϩάϥϜ͕ҙຯతʹਖ਼͍͔͠νΣοΫ •IRੜ੒: ίϯύΠϥ͕࠷దԽ͠΍͘͢ΔͨΊͷίʔυΛੜ੒ •࠷దԽ:

    ੑೳΛ޲্ͤ͞ΔͨΊʹɺෆཁͳܭࢉͷ࡟আ΍ޮ཰ͷྑ͍ΞϧΰϦζϜ΁ม׵ •ίʔυੜ੒: ػցޠΛੜ੒͠ɺCPU͕࣮ߦՄೳͳܗࣜʹม׵ •ίʔυϦϯΫ: ιʔεϑΝΠϧ΍ϥΠϒϥϦΛ݁߹͠ɺ࠷ऴతͳ࣮ߦՄೳϑΝΠϧΛੜ੒ 💡 શͯΛඞͣߦ͏ͱ͍͏Θ͚Ͱ͸ͳ͍
  3. IRͱ͸ʁ • Intermediate Representation /தؒදݱ • ϓϩάϥϛϯάݴޠͷίʔυΛػցޠʹม׵͢ΔաఔͰੜ੒͢Δ தؒతͳίʔυ •ͳͥඞཁʁ •

    ߏ଄͕γϯϓϧͰίʔυΛ࠷దԽ͠΍͍͢ • ৭ʑͳλʔήοτͷίʔυΛੜ੒(e.g. Apple SiliconͷMacOS༻) • ίϯύΠϥϓϩηεͷ෼཭ʹΑΔ࠶ར༻ੑͱ։ൃޮ཰޲্
  4. SwiftίϯύΠϥͷओͳॲཧ 1. Parse(ࣈ۟ɾߏจղੳ) 2. Sema(ҙຯղੳ) 3. SILGen(IRੜ੒) 4.SILOptimizer(࠷దԽ) 5. IRGen(IRੜ੒)

    6.LLVM(࠷దԽɺίʔυੜ੒) https://qiita.com/rintaro/items/3ad640e3938207218c20 ❶ ❷ ❸ ❹ ❺ ❻
  5. ίʔϧελοΫ func1 func2 εϨου async தஅ࣌ʹऔΓग़͢ඞཁ͕͋Δ asyncݺͼग़͠ await ίʔϧελοΫ func1

    func2 εϨου async async ফ͑Δ… ଴͍ͬͯΔؒʹ ଞͷॲཧΛ͍ͤͨ͞
  6. ίʔϧελοΫ func1 func2 εϨου async ࠶։࣌ʹ·ͨඞཁ asyncݺͼग़͠ ίʔϧελοΫ func1 func2

    εϨου async ίʔϧελοΫ func1 func2 func3 εϨου ࠶։࣌ async Ͳ͏͠Α͏… await ⚠ಉ͡εϨουͱ ͸ݶΒͳ͍
  7. ώʔϓϝϞϦ Task TaskAllocator ϝϞϦྖҬ async ϝϞϦྖҬ ଍Γͳ͚Ε͹௥Ճ Stack discipline +

    Bump pointer allocation Slab ࿈ଓͨ͠ϝϞϦྖҬ ޮ཰తʹϝϞϦΛׂΓ౰ͯΔͨΊͷ࢓૊Έ StackAllocatorͷ࣮૷(TaskAllocator͸typealiasΈ͍ͨͳ΋ͷ)
  8. ؔ਺෼ׂ͸LLVM͕ߦ͍ͬͯΔ 1. Parse(ࣈ۟ɾߏจղੳ) 2. Sema(ҙຯղੳ) 3. SILGen(IRੜ੒) 4. SILOptimizer(࠷దԽ) 5.

    IRGen(IRੜ੒) 6.LLVM(࠷దԽɺίʔυੜ੒) https://qiita.com/rintaro/items/3ad640e3938207218c20 ͜͜
  9. Swi f ༻ͷ࣮૷͸ͳͥඞཁͩͬͨʁ •ΦʔόʔϔουΛ࠷খݶʹ཈͑ΔͨΊ • ෆཁͳॲཧͷলུ എܠ: ݩʑ͸C++ʹద࣮ͨ͠૷ & SwiftͰͷ࢖ΘΕํ͕ҟͳΔ(ྫ:

    forϧʔϓ) • Swiftଆʹ͓͚Δߴ౓ͳ࠷దԽΛॏࢹ • Swiftಠࣗͷݺͼग़͠ن໿ʹରԠ • কདྷతͳݴޠػೳͷ֦ுʹରԠ • ݺͼग़͠ଆʹΑΔॊೈͳϝϞϦ؅ཧ(Task͕࢘ྩౝ)
  10. Function Function Module ͜ΜͳΠϝʔδ if … else BasicBlock Instruction Instruction

    … BasicBlock Instruction Instruction … if … else BasicBlock Instruction Instruction … BasicBlock Instruction Instruction …
  11. •2ͭͷ BasicBlock A ͱ B ͕༩͑ΒΕͨ৔߹ʹɺA ͔Β B ΁ͷύε͕தஅϙΠϯτ(await)Λ௨ա͢ Δ͔Ͳ͏͔ΛνΣοΫ(தஅInstructionؚ͕·Ε͍ͯΔ͔Ͳ͏

    ͔) SuspendCrossingInfoΫϥε https://github.com/swiftlang/llvm-project/blob/next/llvm/include/llvm/Transforms/Coroutines/SuspendCrossingInfo.h LLVM
  12. BasicBlock Value ԿΛ͍ͯ͠Δʁ UseList BasicBlock Value BasicBlock Value BasicBlock Value

    … LLVM SuspendCrossingInfo ؔ਺෼ׂޙʹ ͕BasicBlockΛൺֱ ௒͍͑ͯΔʁ
  13. BasicBlockΛ෼ׂ Function BasicBlock … தஅInstruction … BasicBlock … BasicBlock தஅInstruction

    … ✂ https://github.com/swiftlang/llvm-project/blob/next/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
  14. • Swift ίϯύΠϥͷΞʔΩςΫνϟ • SwiftίϯύΠϥ։ൃೖ໳ • ͖ͭͶ͞ΜͰ΋Θ͔ΔLLVM ~ίϯύΠϥΛࣗ࡞͢ΔͨΊͷΨΠυϒοΫ~ • SwiftͷύϑΥʔϚϯεৄࡉ

    • Swiftฒߦॲཧ: ෣୆ཪ • SwiftϦϙδτϦ • LLVMϦϙδτϦ(Apple͕ϑΥʔΫ͍ͯ͠Δόʔδϣϯ) • Async lowering • ෆཁͳasyncϑϨʔϜ͕࡞੒͞Εͯ͠·͏ͱ͍͏issue ࢀߟࢿྉ