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
66
社内版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
190
Deep Dive into "any" and "some"
freddi
3
1.3k
挑戦!ISUCON de Server-side Swift 〜タイムゾーンには気をつけろ〜
freddi
0
1.3k
意外と知られてないXcode13の新しい参照カウンタ最適化オプションの挙動
freddi
2
97
How to develop SIL Optimizer in Swift Language
freddi
0
280
SwiftコンパイラがSwiftで開発できるようになる話
freddi
3
550
知ろう! ShazamKit
freddi
1
160
Swift Optimizing at Compiler World
freddi
1
730
Recap Pointfree Vol. 1~3
freddi
1
760
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
How GitHub (no longer) Works
holman
310
140k
We Have a Design System, Now What?
morganepeng
50
7.2k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Visualization
eitanlees
145
15k
GraphQLとの向き合い方2022年版
quramy
43
13k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Code Review Best Practice
trishagee
64
17k
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ͯ͠ΈΑ͏