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コンパイラにContributeするまで
Search
freddi(Yuki Aki)
October 09, 2020
0
91
社内版SwiftコンパイラにContributeするまで
freddi(Yuki Aki)
October 09, 2020
Tweet
Share
More Decks by freddi(Yuki Aki)
See All by freddi(Yuki Aki)
輝け俺のViewController 〜海外iOSカンファレンス登壇編〜
freddi
4
240
Deep Dive into "any" and "some"
freddi
3
1.4k
挑戦!ISUCON de Server-side Swift 〜タイムゾーンには気をつけろ〜
freddi
0
1.5k
意外と知られてないXcode13の新しい参照カウンタ最適化オプションの挙動
freddi
2
130
How to develop SIL Optimizer in Swift Language
freddi
0
320
SwiftコンパイラがSwiftで開発できるようになる話
freddi
3
600
知ろう! ShazamKit
freddi
1
180
Swift Optimizing at Compiler World
freddi
2
800
Recap Pointfree Vol. 1~3
freddi
1
790
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
22
2.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
28
1.6k
YesSQL, Process and Tooling at Scale
rocio
172
14k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
12
610
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.3k
How to Ace a Technical Interview
jacobian
276
23k
Transcript
SwiftίϯύΠϥʹContribute͢Δ·Ͱ freddi (LINE Fukuoka) Engineer Casual Meetup - Oct. 9
2020
Agenda • Contributeͨ͠༰ ͷͳ͠ • Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • Swift
Intermediate Language ͷͳ͠ • SILOptimizer ʹ͍ͭͯ • (͋ΒͨΊͯ) Contributeͨ͠༰ ͷͳ͠
Agenda • Contributeͨ͠༰ ͷͳ͠ • Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • Swift
Intermediate Language ͷͳ͠ • SILOptimizer ʹ͍ͭͯ • (͋ΒͨΊͯ) Contributeͨ͠༰ ͷͳ͠ ͷೖϑϩʔ
Contributeͨ͠༰ ͷͳ͠ • ̎ͭ Contribute ͨ͠ • https://github.com/apple/swift/pull/33851 • ࠷దԽϩδοΫͷϛε
• https://github.com/apple/swift/pull/31847 • Docs ͷ BNF Ͱॻ͔Εͨ Grammer ͷϛε
Contributeͨ͠༰ ͷͳ͠ • https://github.com/apple/swift/pull/33851 • ࠷దԽϩδοΫͷϛε • ʮ෭࡞༻͕͋Δ͔Εͳ͍ʯϩδοΫͷݕग़ϛε • Ұ෦ڧ੍Ϋϥογϡ͕ʮ෭࡞༻ͳ͠ʯʹͳΔϛε
• ࠷దԽϩδοΫ͕ҙਤͨ͠ͷͰͳ͘ͳΔՄೳੑ
Contributeͨ͠༰ ͷͳ͠
Contributeͨ͠༰ ͷͳ͠ • https://github.com/apple/swift/pull/31847 • υΩϡϝϯτͰղઆ͍ͯ͠Δ Grammer ͷϛε • ͕ࣗಡΜͩ
SIL ͷίʔυ ͱҧ͏͔Βؾ͍ͮͨ • SIL ͷυΩϡϝϯτͨ·ʹΨόΨό
Contributeͨ͠༰ ͷͳ͠ • https://github.com/apple/swift/pull/33851 • ࠷దԽϩδοΫͷϛε ← ࠓͬͪ͜ͷ • ͜ͷ
มߋͷཧղΛ௨ͯ͡SwiftίϯύΠϥʹೖ͢Δ
Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • Swift ͜ΜͳϑϩʔͰίϯύΠϧ͞ΕΔ • Swift ίϯύΠϥΔͱͷإΑΓݟΔ͜ͱʹͳΔ
Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • օ͞Μ͓ͳ͡Έ Swift ίʔυ
Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • AST (நߏจ) • ϓϩάϥϜΛจ๏ͷߏʹ฿ͬͨߏʹͨ͠ͷ
Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • AST (நߏจ) • ԋࢉࢠͷ༏ઌͱ͔ߏจͷߏͰܾ·ͬͨΓ
Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • SIL (Swift Intermediate Language) • Swift
ಛ༗ͷநදݱ
Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • SIL (Swift Intermediate Language) • ͜͜ͰSwiftಛ༗ͷ
࠷దԽ͕ߦΘΕΔ • ࠓͷTopic
Swift ͷίϯύΠϧϑϩʔ ͷͳ͠ • LLVM IR => … • SIL
Λ LLVM IR ʹͯ͠LLVM ʹ͛Δ • ͦͷ͋ͱϦϯΧʹ͛ΒΕͨΓ • όΠφϦʹͳΔ
Swift Intermediate Language ͷͳ͠ • Swift ಛ༗ͷதؒදݱ • ͜͜Ͱ Swiftಛ༗ͷ࠷దԽ͕ߦΘΕΔ
Swift Intermediate Language ͷͳ͠ • ͲͪΒखݩʹ swiftc ίϚϯυ͕͋ΕੜՄೳ
Swift Intermediate Language ͷͳ͠ • ࢼ͠ʹ hello world! Λ SIL
Ͱ ੜͯ͠ΈΔ • ࠓճ͕࣌ؒͳ͍ͷͰ raw SIL ͚ͩ
Swift Intermediate Language ͷͳ͠ • ͍ɾಡΈͮΒ͍ʢʁʣ
Swift Intermediate Language ͷͳ͠ • SIL͕swiftίʔυΑΓ͘ͳΔཧ༝ • ؔʢԾʣςʔϒϧͷใ • ࡉ͔͍ϝϞϦͷ֬อϦϑΝϨϯεΧϯτͷ໋ྩ
• Main ؔͳͲͷల։
Swift Intermediate Language ͷͳ͠ • SIL ͷߏΛେ͖͍ॱʹ • SILModule (ιʔείʔυશମ)
• SILFunction (֤ؔ) • SILBasicBlock (͍ΘΏΔείʔϓ) • SILInstruction (SILίʔυҰจҰจ)
Swift Intermediate Language ͷͳ͠ • ͍͍ͩͨ͜ΕΒΛཧղ͢ΔͱSILͬ͘͟ΓಡΊΔ • SIL͕swiftίʔυΑΓ͘ͳΔཧ༝ • SIL
ͷߏͷॱং
Swift Intermediate Language ͷͳ͠ • SILModule
SILFunction
SILBasicBlock
SILBasicBlock
SILBasicBlock Α͘ΈͨΒ bb0: ͬͯͳ͍ͬͯΔ -> basic block ͷུͰ͢ ݅ذͰBasic Block૿͑Δ
SILInstruction
Swift Intermediate Language ͷͳ͠ • ͜ͷߏΛ͍ͬͯΔͱίϯύΠϥͷίʔυͷҰ෦͕ ಡΊΔΑ͏ʹͳΔ • ͦΕ͕ SILOptimizer
ͷίʔυ
SILOptimizer ʹ͍ͭͯ • SIL (Swift Intermediate Language) • ͜͜ͰSwiftಛ༗ͷ ࠷దԽ͕ߦΘΕΔ
SILOptimizer ʹ͍ͭͯ
SILOptimizer ʹ͍ͭͯ
SILOptimizer ʹ͍ͭͯ • raw SIL ੜޙɺSILOptimizer ʹΑͬͯ࠷దԽ͞ΕΔ • SILOptimizerதͷͨ͘͞Μ͋ΔPassͱ͍͏ϞδϡʔϧͰ ͦΕͧΕͷతͷ࠷దԽ͕ߦΘΕΔ
• ࠓͦͷPassͷίʔυΛಡΉ • Passͷίʔυͷجૅ͕٧·͍ͬͯΔ
SILOptimizer ʹ͍ͭͯ • ࠓ AssumeSingleThreaded ΛಡΉ • γϯάϧεϨου͚ͩͰಈ͔͞ΕΔͷʹಛԽ͢ΔΑ͏ʹ ͢Δ࠷దԽ •
ΦϓγϣϯͰൃಈ͞ΕΔ • PassͷதͰଟҰ൪͍
SILOptimizer ʹ͍ͭͯ
SILOptimizer ʹ͍ͭͯ SILFunctionTransform Λܧঝ -> FuncitonΛࠪ͢ΔPassʹͳΔ
SILOptimizer ʹ͍ͭͯ Ծؔ run Λ࣮ -> Pass ͷ࠷దԽϩδοΫ͜͜ʹॻ͘
SILOptimizer ʹ͍ͭͯ ίϯύΠϥͷυϥΠόʢίϚϯυʣʹ͞ΕͨΦϓγϣϯΛΈΔ
SILOptimizer ʹ͍ͭͯ ࠓݟ͍ͯΔؔΛSILFunctionܕͷ σʔλͱͯ͠ड͚औΔ
SILOptimizer ʹ͍ͭͯ SILFunctionSILBasicBlockͷCollection ͱͯ͠ѻ͑ΔʢͳͷͰBBʣ
SILOptimizer ʹ͍ͭͯ SILBasicBlockSILInstructionͷCollection ͱͯ͠ѻ͑ΔʢͳͷͰIʣ
SILOptimizer ʹ͍ͭͯ ͳͷͰɺ͜͜Ͱؔͷ InstructionΛ͍ࠪͯ͠Δ
SILOptimizer ʹ͍ͭͯ ֤InstructionܕͰදݱ͞Ε͍ͯΔ ΩϟετͰಛఆͷInstruction͔ௐΔ
SILOptimizer ʹ͍ͭͯ ϦϑΝϨϯεΧϯλؔͷInscrutionͳΒ NonAtomicʹ͢Δ
SILOptimizer ʹ͍ͭͯ • AssumeSingleThreaded ʹ͍ͭͯ • FunctionͷInstructionΛࠪ • ϦϑΝϨϯεΧϯλؔͩͬͨΒnon Atomicʹ͢Δ
SILOptimizer ʹ͍ͭͯ • SILOptimizer ͷίʔυΛಡΉͱ͖ͷTips • ུޠ͕ଟ͍ͷͰίϯςΫετͰஅ͢Δ • ෳࡶͳϩδοΫ C++
Λཧղ͔ͯ͠ΒಡΉ
Contributeͨ͠༰ ͷͳ͠ • ࠓճͨ͠ͷɺmayHaveSideEffect ͱ͍͏ؔ • ʮ෭࡞༻͕͋Δ͔Εͳ͍ʯInstructionΛݟΔ • SILInstruction ܕͷ
ϝϯόม • Dead Code EliminationʹΘΕΔ
Contributeͨ͠༰ ͷͳ͠ • ͜ͷ݅ͷ̍൪͕ϛε͍ͬͯͨ(mayTrap ؔ)
Contributeͨ͠༰ ͷͳ͠
Contributeͨ͠༰ ͷͳ͠ • ίϯύΠϥͷInstructionͷछྨͷදݱํ๏ • ઌఔਃ্͛ͨ͠ܕʹΑΔදݱʢΩϟετʹΑΔผʣ • Enum ʹΑΔදݱʢswitch ͳͲ
ʹΑΔผʣ • मਖ਼Օॴͬͪ͜Ͱදݱ͞Ε͍ͯΔ
Contributeͨ͠༰ ͷͳ͠ • ͪΐͬͱ̎൪Ίͷհ
Contributeͨ͠༰ ͷͳ͠ • ϝϞϦΞΫηε͕ى͜Γͦ͏ͳInstructionͷݕํ๏ • SILInstruction ͷଐੑΛݟΔ • LLVMͷInstruction ͷଐੑΛݟΔ
• apply Instruction ͳΒݺͼग़͠ઌͷؔͷଐੑΛݟΔ • ͜͜ΒΜͷॲཧͯͯ͝͝ͷXϚΫϩ͕ೖͬͨॲཧͳͷ ͰׂѪɻڵຯ͕͋ͬͨΒ࣭Ͱฉ͍͍ͯͩ͘͞
Contributeͨ͠༰ ͷͳ͠
Contributeͨ͠༰ ͷͳ͠
Contributeͨ͠༰ ͷͳ͠ • ͏গ͠Γ͍ͨํ Θ͍Θ͍swiftc #20 ͷͷࢿྉΛݟ͍ͯͩ͘͞
• https://speakerdeck.com/freddi/mayhavesideeffect
Contributeͨ͠༰ ͷͳ͠ • ͜ͷमਖ਼ՕॴͳΜͰݟ͚ͭͨͷʁ • झຯͰίϯύΠϥίʔυಡΜͰ͍ͨΒۮવΈ͚ͭͨ • bug.swift.orgͰόάνέοτʹͳ͔ͬͨ
Contributeͨ͠༰ ͷͳ͠ • PRग़ͨ͠ΒɺTest ॻ͍ͨΒϚʔδ͢ΔͱݴΘΕͨ • SILͷςετ LLVMͷlitΛ͍ͬͯΔΒ͍͠ • LLVM
Integrated Tester ͷུ • ͦΜͳ͜ΜͳͰϚʔδ͞Εͨ • ͭʹόάνέοτΛΔඞཁͳ͍Β͍͠
Contributeͨ͠༰ ͷͳ͠ • SIL.rst ͱ͍͏υΩϡϝϯτ͕SILʹ͍ͭͯৄ͘͠هࡌ • https://github.com/apple/swift/blob/main/docs/SIL.rst • ͨͩ͠ɺSIL.rst ΨόΨόͳͷͰContributeνϟϯε͕
ͨ͘͞Μ͋Δ • ͜͜मਖ਼Λ͛ͨ͜ͱ͕͋Δ
·ͱΊ • SILΛಡΈ͡ΊΔͱίϯύΠϥೖΓ͍͢ • SIL͕ಡΊΔͱ࠷దԽͷίʔυ͕ಡΊΔΑ͏ʹͳΔ • Contributeʹؔͯ͠ݫ͍͠ϧʔϧͳ͍ͷͰ ؾܰʹͰ͖Δ • ಛʹυΩϡϝϯτΨόΨόͳͷͰContributeͯ͠ΈΑ͏