Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Swiftコンパイラ超入門+async関数の仕組み
Search
[email protected]
January 18, 2025
Programming
0
150
Swiftコンパイラ超入門+async関数の仕組み
2025/1/17 わいわいswiftc
[email protected]
January 18, 2025
Tweet
Share
More Decks by
[email protected]
See All by
[email protected]
複雑さに立ち向かうための ソフトウェア開発入門
shiz
3
1.3k
座談会 「Strict ConcurrencyとSwift 6が開く新時代: 私たちはどう生きるか?」
shiz
5
13k
(非公開スライド追加)座談会 「Strict ConcurrencyとSwift 6が開く新時代: 私たちはどう生きるか?」
shiz
1
420
複雑さに立ち向かうためのコードリーディング入門
shiz
35
13k
Swift Observation
shiz
5
780
Swift Reflection
shiz
1
190
Swift Concurrency Next Step
shiz
7
7.8k
そろそろSwift Concurrency
shiz
1
470
Hello, Swift Concurrency world.の振り返り / Looking back on Hello, Swift Concurrency world.
shiz
0
130
Other Decks in Programming
See All in Programming
rails newと同時に型を書く
aki19035vc
5
690
GitHubで育つ コラボレーション文化 : ニフティでのインナーソース挑戦事例 - 2024-12-16 GitHub Universe 2024 Recap in ZOZO
niftycorp
PRO
0
1.3k
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
920
Alba: Why, How and What's So Interesting
okuramasafumi
0
110
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
290
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
230
Amazon Nova Reelの可能性
hideg
0
150
良いユニットテストを書こう
mototakatsu
11
3.5k
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
1.3k
Androidアプリの One Experience リリース
nein37
0
990
2025.01.17_Sansan × DMM.swift
riofujimon
2
390
はてなにおけるfujiwara-wareの活用やecspressoのCI/CD構成 / Fujiwara Tech Conference 2025
cohalz
0
440
Featured
See All Featured
Gamification - CAS2011
davidbonilla
80
5.1k
A designer walks into a library…
pauljervisheath
205
24k
How STYLIGHT went responsive
nonsquared
96
5.3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
GitHub's CSS Performance
jonrohan
1030
460k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.7k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
560
Optimizing for Happiness
mojombo
376
70k
Site-Speed That Sticks
csswizardry
2
240
Transcript
2025/01/17 Θ͍Θ͍swiftc shiz SwiftίϯύΠϥೖ asyncؔͷΈ +
ࣗݾհ •shiz(ͣ͠) X: @stzn3 GitHub: @stzn •Ԭࡏॅ iOSΤϯδχΞ ຊͷࣥච 4XJGUݴޠΨΠυͷ༁
৽ץग़͠·͢ •λΠτϧ: ετϦʔτίʔμʔ •20252݄ൃച༧ఆ •ݱ(ετϦʔτ)Ͱੜ͖ൈͨ͘Ίͷٕࣝज़ ͕٧·͍ͬͯΔ https://amzn.asia/d/dfD9BxU
ຊͷ༰ •SwiftίϯύΠϥೖ •Swift asyncؔͷ2ͭͷಛ(֓ཁ) •Swift asyncؔͷ2ͭͷಛ(ৄࡉ)
༻ͨ͠swiftlang/swiftͷtag •swift-DEVELOPMENT-SNAPSHOT-2024-12-22-a •Swift6.2
ຊͷ༰ •SwiftίϯύΠϥೖ •Swift asyncؔͷ2ͭͷಛ(֓ཁ) •Swift asyncؔͷ2ͭͷಛ(ৄࡉ)
ίϯύΠϥͱʁ
ίϯύΠϥ •ϓϩάϥϛϯάݴޠͰॻ͔ΕͨίʔυΛɺʮί ϯϐϡʔλ͕࣮ࡍʹ࣮ߦͰ͖Δܗʯʹม͢Δ ϓϩάϥϜ
ίϯύΠϥ͕ߦ͏ओͳॲཧ •ࣈ۟ղੳ: ιʔείʔυΛτʔΫϯʹղ͠ɺߏจతʹղੳ͘͢͢͠Δ •ߏจղੳ: τʔΫϯΛجʹߏจنଇΛνΣοΫ͠ɺߏΛղੳɻASTΛੜ •ҙຯղੳ: มͷܕείʔϓΛ֬ೝ͠ɺҙຯతʹਖ਼͍͔͠νΣοΫ •IRੜ: ίϯύΠϥ͕࠷దԽ͘͢͠ΔͨΊͷίʔυΛੜ •࠷దԽ:
ੑೳΛ্ͤ͞ΔͨΊʹɺIRΛΑΓૣ͘(খ͘͞)ม͢Δ •ίʔυੜ: ػցޠΛੜ͠ɺCPU͕࣮ߦՄೳͳܗࣜʹม 💡 ίϯύΠϥʹΑͬͯߦ͏͜ͱมΘΔ
ίϯύΠϥ͕ߦ͏ओͳॲཧ • ࣈ۟ղੳ: ιʔείʔυΛτʔΫϯʹղ͠ɺߏจతʹղੳ͘͢͢͠Δ • ߏจղੳ: τʔΫϯΛجʹߏจنଇΛνΣοΫ͠ɺߏΛղੳɻASTΛੜ • ҙຯղੳ:
มͷܕείʔϓΛ֬ೝ͠ɺҙຯతʹਖ਼͍͔͠νΣοΫ •IRੜ: ίϯύΠϥ͕࠷దԽ͘͢͠ΔͨΊͷίʔυΛੜ • ࠷దԽ: ੑೳΛ্ͤ͞ΔͨΊʹɺIRΛΑΓૣ͘(খ͘͞)ม͢Δ • ίʔυੜ: ػցޠΛੜ͠ɺCPU͕࣮ߦՄೳͳܗࣜʹม 💡 ίϯύΠϥʹΑͬͯߦ͏͜ͱมΘΔ
ίϯύΠϥ͕ߦ͏ओͳॲཧ •ࣈ۟ղੳ: ιʔείʔυΛτʔΫϯʹղ͠ɺίʔυΛߏจతʹղੳ͘͢͠ •ߏจղੳ: τʔΫϯΛجʹߏจنଇΛνΣοΫ͠ɺϓϩάϥϜͷߏΛղੳɻASTΛੜ •ҙຯղੳ: มͷܕείʔϓΛ֬ೝ͠ɺϓϩάϥϜ͕ҙຯతʹਖ਼͍͔͠νΣοΫ •IRੜ: ίϯύΠϥ͕࠷దԽ͘͢͠ΔͨΊͷίʔυΛੜ •࠷దԽ:
ੑೳΛ্ͤ͞ΔͨΊʹɺෆཁͳܭࢉͷআޮͷྑ͍ΞϧΰϦζϜม •ίʔυੜ: ػցޠΛੜ͠ɺCPU͕࣮ߦՄೳͳܗࣜʹม •ίʔυϦϯΫ: ιʔεϑΝΠϧϥΠϒϥϦΛ݁߹͠ɺ࠷ऴతͳ࣮ߦՄೳϑΝΠϧΛੜ 💡 શͯΛඞͣߦ͏ͱ͍͏Θ͚Ͱͳ͍
IRͱʁ •Intermediate Representation /தؒදݱ •ϓϩάϥϛϯάݴޠͷίʔυΛػցޠʹม͢ ΔաఔͰੜ͢Δதؒతͳίʔυ
IRͱʁ • Intermediate Representation /தؒදݱ • ϓϩάϥϛϯάݴޠͷίʔυΛػցޠʹม͢ΔաఔͰੜ͢Δ தؒతͳίʔυ •ͳͥඞཁʁ •
ߏ͕γϯϓϧͰίʔυΛ࠷దԽ͍͢͠ • ৭ʑͳλʔήοτͷίʔυΛੜ(e.g. Apple SiliconͷMacOS༻) • ίϯύΠϥϓϩηεͷʹΑΔ࠶ར༻ੑͱ։ൃޮ্
Swi f ίϯύΠϥʁ
SwiftίϯύΠϥͷओͳॲཧ 1. Parse(ࣈ۟ɾߏจղੳ) 2. Sema(ҙຯղੳ) 3. SILGen(IRੜ) 4.SILOptimizer(࠷దԽ) 5. IRGen(IRੜ)
6.LLVM(࠷దԽɺίʔυੜ) https://qiita.com/rintaro/items/3ad640e3938207218c20 ❶ ❷ ❸ ❹ ❺ ❻
SwiftίϯύΠϥʹ͍ͭͯΓ͚ͨΕ https://qiita.com/rintaro/items/3ad640e3938207218c20
͍͔ͭ͘ิ
SwiftίϯύΠϥͷओͳύΠϓϥΠϯ 1. Parse(ࣈ۟ɾߏจղੳ) 2. Sema(ҙຯղੳ) 3.SILGen(IRੜ) 4.SILOptimizer(࠷దԽ) 5.IRGen(IRੜ) 6.LLVM(࠷దԽɺίʔυੜ) https://qiita.com/rintaro/items/3ad640e3938207218c20
💡࣮ࡍASTͳͲͬͱଟ͘ͷIRΛੜ͍ͯ͠Δ
SILGen •SILΛੜ͢Δ •SIL: Swift Intermediate Language •SwiftͷݴޠߏΛөͨ͠IR •ܕνΣοΫSwiftಛ༗ͷ࠷దԽΛߦ͏ ϓνใ: ࠷ۙSILʹ͍ͭͯͷυΩϡϝϯτ͕ཧ͞Εͨ
IRGen •LLVM IRΛੜ͢Δ •࠷ऴతͳػցޠΛੜ͢ΔաఔͰ࠷దԽ͠ ͘͢͢ΔͨΊʹੜ͞ΕΔLLVM༻ͷIR
LLVMʁ
SwiftίϯύΠϥͷओͳύΠϓϥΠϯ 1. Parse(ࣈ۟ɾߏจղੳ) 2. Sema(ҙຯղੳ) 3. SILGen(IRੜ) 4.SILOptimizer(࠷దԽ) 5. IRGen(IRੜ)
6.LLVM(࠷దԽɺίʔυੜ) https://qiita.com/rintaro/items/3ad640e3938207218c20
LLVM •SwiftͰͳ͍ •ίϯύΠϥͷόοΫΤϯυͱݺΕΔ෦ •LLVM IR͔Β͞·͟·ͳλʔήοτͷػցޠΛੜ •λʔήοτʹԠͨ͡࠷దԽΛͯ͘͠ΕΔ •൚༻తʹΘΕ͍ͯΔ(C++ɺRustɺAdobe…)
LLVMʹ͍ͭͯΓ͚ͨΕ 💡ͪΐͬͱݹ͍ใ͋Δ
LLVM IR •࠷ऴతͳػցޠΛੜ͢ΔաఔͰ࠷దԽ͢͠ ͘͢ΔͨΊʹੜ͞ΕΔLLVM༻ͷIR
SILͱLLVM IR Swift SIL LLVM IR
SILͱLLVM IR Swift SIL LLVM IR string integer Array i64
i32 ptr
ຊͷ༰ •SwiftίϯύΠϥೖ •Swift asyncؔͷ2ͭͷಛ(֓ཁ) •Swift asyncؔͷ2ͭͷಛ(ৄࡉ)
ຊͷ༰ •SwiftίϯύΠϥೖ •Swift asyncؔͷ2ͭͷಛ(֓ཁ) •Swift asyncؔͷ2ͭͷಛ(ৄࡉ)
SwiftͷύϑΥʔϚϯεৄࡉ Swift asyncؔͷ2ͭͷಛ
ಛผͳελοΫʹϩʔΧϧঢ়ଶΛอ࣋ தஅϙΠϯτ(await)Ͱෳͷؔʹׂ Swift asyncؔͷ2ͭͷಛ SwiftͷύϑΥʔϚϯεৄࡉ
Swift asyncؔͷ2ͭͷಛ •ಛผͳελοΫʹϩʔΧϧঢ়ଶΛอ࣋ •தஅϙΠϯτ(await)Ͱෳͷؔʹׂ
Swift asyncؔͷ2ͭͷಛ •ಛผͳελοΫʹϩʔΧϧঢ়ଶΛอ࣋ •தஅϙΠϯτ(await)Ͱෳͷؔʹׂ
ελοΫϝϞϦ ಉظ(sync)ؔͷΈ ελοΫϝϞϦ্ͷ ίʔϧελοΫ(※) ίʔϧελοΫ func1 func2 func3 εϨου ※
ؔͷݺͼग़͠ΓΛ ཧ͢ΔελοΫ(σʔλߏ)
ิ: ελοΫ(σʔλߏ) ελοΫ ཁૉ LIFO(Last-In, First-OutʗޙೖΕઌग़͠)ͷಛੑΛ࣋ͭσʔλߏ ཁૉ ελοΫ ཁૉ ཁૉ
ޙೖΕ(Last-In) ઌग़͠(First-Out)
ิ: ελοΫ(σʔλߏ) ελοΫ ཁૉ LIFO(Last-In, First-OutʗޙೖΕઌग़͠)ͷಛੑΛ࣋ͭσʔλߏ ཁૉ ελοΫ ཁૉ ཁૉ
ޙೖΕ(Last-In) ઌग़͠(First-Out) ⚠ώʔϓϝϞϦͰελοΫ(σʔλߏ)༻Մೳ
ίʔϧελοΫ func1 func2 func3 εϨου ελοΫϑϨʔϜ Ҿ ϩʔΧϧม ؔͷΓΞυϨε ݺͼग़͠ݩͷࢀর
εϨου ಉظ(sync)ؔ syncݺͼग़͠ syncऴྃ ίʔϧελοΫ func1 func2 sync ίʔϧελοΫ func1
func2 εϨου sync ऴΘͬͨΒ ফ͑Δ
asyncؔͰߟ͑ͯΈΔ
ݺͼग़࣌͠ಉ͡ asyncݺͼग़͠ ίʔϧελοΫ func1 func2 εϨου async
ίʔϧελοΫ func1 func2 εϨου async தஅ࣌ʹऔΓग़͢ඞཁ͕͋Δ asyncݺͼग़͠ await ίʔϧελοΫ func1
func2 εϨου async async ফ͑Δ… ͍ͬͯΔؒʹ ଞͷॲཧΛ͍ͤͨ͞
ίʔϧελοΫ func1 func2 εϨου async ࠶։࣌ʹ·ͨඞཁ asyncݺͼग़͠ ίʔϧελοΫ func1 func2
εϨου async ίʔϧελοΫ func1 func2 func3 εϨου ࠶։࣌ async Ͳ͏͠Α͏… await ⚠ಉ͡εϨουͱ ݶΒͳ͍
ͦ͜Ͱ
ಛผͳελοΫʹ ϩʔΧϧঢ়ଶΛอ࣋ 💡͜͜Ͱݴ͏ελοΫσʔλߏͷ
async ώʔϓϝϞϦ ελοΫϝϞϦ Ұ෦ΛผͷελοΫʹୀආ εϨου func1 func2 async ίʔϧελοΫ ελοΫ
ώʔϓϝϞϦ্ͷ ελοΫ
࠶։ޙʹඞཁͳใͷΈ async ελοΫ ίʔϧελοΫ async a b
async ελοΫ ίʔϧελοΫ async asyncϑϨʔϜ தஅ(await)લ·Ͱ͔͠ Θͳ͍ใ ࠶։ޙʹඞཁͳใ
ελοΫϝϞϦ ࠶։࣌ʹίʔϧελοΫʹ͢ ελοΫ εϨου ⚠ผεϨου ͷՄೳੑ͋Γ ώʔϓϝϞϦ func4 func5 async
ίʔϧελοΫ
Task Task͕ඇಉظॲཧΛཧ
Task TaskAllocatorͰϝϞϦཧ TaskAllocator
ώʔϓϝϞϦ ใΛ֨ೲ͢ΔϝϞϦΛ֬อ Task TaskAllocator ϝϞϦྖҬ 💡Taskͷ࡞࣌ʹߦ͍ͬͯΔ Task TaskAllocator ϝϞϦྖҬ
ώʔϓϝϞϦ Task TaskAllocator ϝϞϦྖҬ Task TaskAllocator ϝϞϦྖҬ async ඞཁʹԠͯ͡ϝϞϦׂΓͯ
ώʔϓϝϞϦ Task TaskAllocator ϝϞϦྖҬ async ϝϞϦྖҬ Γͳ͚ΕՃ
ώʔϓϝϞϦ Task TaskAllocator ϝϞϦྖҬ async ϝϞϦྖҬ Γͳ͚ΕՃ Stack discipline +
Bump pointer allocation Slab ࿈ଓͨ͠ϝϞϦྖҬ ޮతʹϝϞϦΛׂΓͯΔͨΊͷΈ StackAllocatorͷ࣮(TaskAllocatortypealiasΈ͍ͨͳͷ)
ώʔϓϝϞϦ Task ޮԽͷ TaskAllocator ϝϞϦྖҬ async StackAllocatorͷ࣮(TaskAllocatortypealiasΈ͍ͨͳͷ) Slab ࿈ଓͨ͠ϝϞϦྖҬ Stack
discipline + Bump pointer allocation ޮతʹϝϞϦΛׂΓͯΔͨΊͷΈ
Swift asyncؔͷ2ͭͷಛ •ಛผͳελοΫʹϩʔΧϧঢ়ଶΛอ࣋ •தஅϙΠϯτ(await)Ͱෳͷؔʹׂ
Swift asyncؔͷ2ͭͷಛ •ಛผͳελοΫʹϩʔΧϧঢ়ଶΛอ࣋ •தஅϙΠϯτ(await)Ͱෳͷؔʹׂ
ؔΛׂʁʁʁ Ͳ͏͍͏͜ͱʁ
awaitͰؔΛׂͤ͞Δ 💡ͦΕͧΕΛ෦ؔͱݺͿ hoge1 hoge2
ิ: rampؔ&resumeؔ •rampؔ: ࠷ॳͷawait·Ͱ࣮ߦ •resumeؔ: awaitҎ߱ͷॲཧΛ࣮ߦ
hoge1 hoge2 resumeؔawait͝ͱʹ࡞ hoge3
hoge1 ࠶։࣌ʹawaitҎ߱ͷॲཧΛݺͼग़͢ await hoge2 https://github.com/swiftlang/swift/blob/main/docs/SIL/Types.md#async-functions asyncϑϨʔϜ ࠶։ޙͷॲཧ
hoge1 ࠶։࣌ʹawaitҎ߱ͷॲཧΛݺͼग़͢ await hoge2 https://github.com/swiftlang/swift/blob/main/docs/SIL/Types.md#async-functions asyncϑϨʔϜ ࠶։ޙͷॲཧ 💡͜ͷதஅͯ͠࠶։͢ΔΈΛ ContinuationͱݺͿ 💡ίʔϧελοΫ্Ͱ
Ͳ͔͜Β࣮ߦ͢ΕΑ͍͔ͷใ
͞Βʹ2ͭͷಛ •ඌ࠶ؼ(tail call) •AsyncContext
͞Βʹ2ͭͷಛ •ඌ࠶ؼ(tail call) •AsyncContext
hoge1 await await hoge2 asyncϑϨʔϜ ࠶։ޙͷॲཧ ؔϙΠϯλ ΞυϨεΛ͢
hoge1 ॲཧ͕ऴΘͬͨΒݺͼग़͢ await await hoge2 asyncϑϨʔϜ ࠶։ޙͷॲཧ 💡ඌ࠶ؼ(tail call)ͱ͍͏ख๏Λ͍ͬͯΔ ؔϙΠϯλ
ݺͼग़͠
hoge1 ඌ࠶ؼ(tail call) await await hoge2 asyncϑϨʔϜ ࠶։ޙͷॲཧ 💡ελοΫΛΘͳͯ͘ࡁΉ(ϝϞϦ࠷దԽ) ؔϙΠϯλ
ݺͼग़͠
͞Βʹ2ͭͷಛ •ඌ࠶ؼ(tail call) •AsyncContext
Task Taskશମͷใ asyncϑϨʔϜ asyncϑϨʔϜ asyncϑϨʔϜ 1ͭͷඇಉظؔͷใ AsyncContext
hoge1 await hoge2 asyncϑϨʔϜ ࠶։ޙͷॲཧ AsyncContext ϙΠϯλ AsyncContextͷϙΠϯλΛ࣋ͪճΔ ϙΠϯλܦ༝Ͱ ඞཁͳใΛऔಘ
AsyncContext ϙΠϯλ AsyncContext ϙΠϯλ
ׂؔ Ͳ͜ͰߦͬͯΔͷ͔ʁ
ׂؔLLVM͕ߦ͍ͬͯΔ 1. Parse(ࣈ۟ɾߏจղੳ) 2. Sema(ҙຯղੳ) 3. SILGen(IRੜ) 4. SILOptimizer(࠷దԽ) 5.
IRGen(IRੜ) 6.LLVM(࠷దԽɺίʔυੜ) https://qiita.com/rintaro/items/3ad640e3938207218c20 ͜͜
Async lowering https://llvm.org/docs/Coroutines.html#async-lowering
ݩʑSwi f ͷͨΊʹ Ճ࣮ͨ͠
Swi f ༻ͷ࣮ͳͥඞཁͩͬͨʁ •ΦʔόʔϔουΛ࠷খݶʹ͑ΔͨΊ • ෆཁͳॲཧͷলུ എܠ: ݩʑC++ʹద࣮ͨ͠ & SwiftͰͷΘΕํ͕ҟͳΔ(ྫ:
forϧʔϓ) • Swiftଆʹ͓͚Δߴͳ࠷దԽΛॏࢹ • Swiftಠࣗͷݺͼग़͠نʹରԠ • কདྷతͳݴޠػೳͷ֦ுʹରԠ • ݺͼग़͠ଆʹΑΔॊೈͳϝϞϦཧ(Task͕࢘ྩౝ)
Coroutine Representations and ABIs in LLVM
⚠ Returned-Continuation Lowering → Async Lowering LLVMͷϓϩϙʔβϧ
ຊͷ༰ •SwiftίϯύΠϥೖ •Swift asyncؔͷ2ͭͷಛ(֓ཁ) •Swift asyncؔͷ2ͭͷಛ(ৄࡉ)
ຊͷ༰ •SwiftίϯύΠϥೖ •Swift asyncؔͷ2ͭͷಛ(֓ཁ) •Swift asyncؔͷ2ͭͷಛ(ৄࡉ)
͍͍ͩͨLLVMͷ
LLVM IRೖ
Module LLVM IRͷߏ Function BasicBlock(BB) Instruction ϓϩάϥϜશମΛཧ͢Δίϯςφ 1ͭͷϓϩάϥϜɻ͍ΘΏΔؔ ॱ൪ʹ࣮ߦ͢Δ໋ྩͷϦετ Ϛγϯͷ࣮ࡍͷ໋ྩ
Function Function Module ͜ΜͳΠϝʔδ if … else BasicBlock Instruction Instruction
… BasicBlock Instruction Instruction … if … else BasicBlock Instruction Instruction … BasicBlock Instruction Instruction …
Swift asyncؔͷ2ͭͷಛ •ಛผͳελοΫʹϩʔΧϧঢ়ଶΛอ࣋ •தஅϙΠϯτ(await)Ͱෳͷؔʹׂ
࠶։ޙʹඞཁͳใͷΈอ࣋ ώʔϓϝϞϦ ελοΫ async value
Ͳ͏ͬͯඞཁ͔Ͳ͏͔Λ அ͍ͯ͠Δʁ
•ͯ͢ͷʢมɺఆɺؔɺ໋ྩͳͲʣͷ ڞ௨ͷΫϥε •͜ͷΛ༻͍ͯ͠ΔଞͷValueͷϦετ (UseList)Λอ࣋ = def-use chain Value Ϋϥε https://llvm.org/doxygen/classllvm_1_1Value.html
LLVM
•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
BasicBlock Value ԿΛ͍ͯ͠Δʁ UseList BasicBlock Value BasicBlock Value BasicBlock Value
… LLVM SuspendCrossingInfo ׂؔޙʹ ͕BasicBlockΛൺֱ ͍͑ͯΔʁ
͜ΕͰෆཁͳϝϞϦΛ Θͳͯ͘ࡁΉ
…͚ͣͩͲ
όά͕͋ΔΒ͍͠ https://github.com/swiftlang/swift/issues/72289
ṖͷasyncϑϨʔϜ… ԿதஅϙΠϯτΛ͑ͳ͍ asyncϑϨʔϜ͕࡞ΒΕ͍ͯΔ ͷʹ https://github.com/swiftlang/swift/issues/72289
ݪҼߟ 💡Spillsʹؚ͕·Ε͍ͯΔͱ ϝϞϦΛ֬อ͠ͳΕͳΒͳ͍ swift_task_alloc(※)͕ݺΕ͍ͯΔ͜ͱ͕ݪҼʁ ※ AsyncContextʹಈతͳαΠζ͕ඞཁͳ߹ʹݺΕΔ https://github.com/swiftlang/swift/issues/72289
ݪҼߟ asyncؔશ෦͜ΕΛ ݺͿΑ͏ʹͳ͍ͬͯΔ = AsyncContextͷsize͕ staticʹܾ·Βͳ͍ https://github.com/swiftlang/swift/blob/main/lib/IRGen/GenCall.cpp#L166 https://github.com/swiftlang/swift/issues/72289
•͜ͷೝࣝͰ߹͍ͬͯΔͷ͔ʁ •Ͳ͏ղܾ͢Δ͔ʁ •தஅϙΠϯτΛ͑ͳ͍߹staticͳ AsyncContextSizeΛ͢ʁ(Ͳ͏ఆ͢Δʁ) •LLVMଆͰswift_task_allocΛফ͢ʁ Μʙ… https://github.com/swiftlang/swift/issues/72289
ͦΕ͓͖ͯ͞
Swift asyncؔͷ2ͭͷಛ •ಛผͳελοΫʹϩʔΧϧঢ়ଶΛอ࣋ •தஅϙΠϯτ(await)Ͱෳͷؔʹׂ
LLVMʹ͓͚Δίʔυͷ࠷దԽ มલ LLVM IR มޙ LLVM IR
ෳͷPassʹΑΔ࠷దԽ มલ LLVM IR Pass Pass มޙ LLVM IR …
ղੳ/ ࠷దԽ
ׂؔͦͷ1ͭ มલ LLVM IR Pass Pass มޙ LLVM IR …
ؔ ׂ
Coroutine Transformation Passes •CoroEarly •CoroSplit •CoroAnnotationElide •CoroElide •CoroCleanup https://llvm.org/docs/Coroutines.html#coroutine-transformation-passes LLVM
💡LLVMͰඇಉظॲཧ(ͳͲ)Λ ѻ͏ΈΛCoroutineͱݺͿ
ׂؔCoroSplitͰߦ͏ •CoroEarly •CoroSplit •CoroAnnotationElide •CoroElide •CoroCleanup https://llvm.org/docs/Coroutines.html#coroutine-transformation-passes LLVM
BasicBlockΛׂ Function BasicBlock … தஅInstruction … BasicBlock … BasicBlock தஅInstruction
… ✂ https://github.com/swiftlang/llvm-project/blob/next/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
ຊͷ༰ •SwiftίϯύΠϥೖ •Swift asyncؔͷ2ͭͷಛ(֓ཁ) •Swift asyncؔͷ2ͭͷಛ(ৄࡉ)
৭ʑ͠·͕ͨ͠
ࣗͰ৮ͬͯΈΔͷ͕Ұ൪ʂ
https://zenn.dev/omochimetaru/articles/8a7f35c5e23a24
σόοάํ๏ https://github.com/swiftlang/swift/blob/main/docs/DebuggingTheCompiler.md
•C++Θ͔Βͳͯ͘࠷ॳେৎ •σόοάͯ͠ॲཧͷྲྀΕΛ͔ͭΉ(ೖΓޱΛݟ͚ͭΔ) •සग़ϫʔυͱུޠʹ׳ΕΔ •castʹΘ͞Εͳ͍ SwiftίϯύΠϥʹڵຯΛ࣋ͬͨํ
ݸਓతʹࢥ͍ͬͯΔ͜ͱ •SwiftશવΘ͔ΒΜʂָ͍͠ʂ •Swiftʹߩݙֶͯ͠Ϳʂ •ࠔͬͨΒॿ͚͍ͯͩ͘͞🙏
• Swift ίϯύΠϥͷΞʔΩςΫνϟ • SwiftίϯύΠϥ։ൃೖ • ͖ͭͶ͞ΜͰΘ͔ΔLLVM ~ίϯύΠϥΛࣗ࡞͢ΔͨΊͷΨΠυϒοΫ~ • SwiftͷύϑΥʔϚϯεৄࡉ
• Swiftฒߦॲཧ: ཪ • SwiftϦϙδτϦ • LLVMϦϙδτϦ(Apple͕ϑΥʔΫ͍ͯ͠Δόʔδϣϯ) • Async lowering • ෆཁͳasyncϑϨʔϜ͕࡞͞Εͯ͠·͏ͱ͍͏issue ࢀߟࢿྉ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ