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

社内版SwiftコンパイラにContributeするまで

1405a601755e5fcbfdc93a2560368bb1?s=47 freddi(Yuki Aki)
October 09, 2020
28

 社内版SwiftコンパイラにContributeするまで

1405a601755e5fcbfdc93a2560368bb1?s=128

freddi(Yuki Aki)

October 09, 2020
Tweet

Transcript

  1. SwiftίϯύΠϥʹContribute͢Δ·Ͱ freddi (LINE Fukuoka) Engineer Casual Meetup - Oct. 9

    2020
  2. Agenda • Contributeͨ͠಺༰ ͷ͸ͳ͠ • Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • Swift

    Intermediate Language ͷ͸ͳ͠ • SILOptimizer ʹ͍ͭͯ • (͋ΒͨΊͯ) Contributeͨ͠಺༰ ͷ͸ͳ͠
  3. Agenda • Contributeͨ͠಺༰ ͷ͸ͳ͠ • Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • Swift

    Intermediate Language ͷ͸ͳ͠ • SILOptimizer ʹ͍ͭͯ • (͋ΒͨΊͯ) Contributeͨ͠಺༰ ͷ͸ͳ͠ ๻ͷೖ໳ϑϩʔ
  4. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ̎ͭ Contribute ͨ͠ • https://github.com/apple/swift/pull/33851 • ࠷దԽϩδοΫͷϛε

    • https://github.com/apple/swift/pull/31847 • Docs ͷ BNF Ͱॻ͔Εͨ Grammer ͷϛε
  5. Contributeͨ͠಺༰ ͷ͸ͳ͠ • https://github.com/apple/swift/pull/33851 • ࠷దԽϩδοΫͷϛε • ʮ෭࡞༻͕͋Δ͔΋஌Εͳ͍ʯϩδοΫͷݕग़ϛε • Ұ෦ڧ੍Ϋϥογϡ͕ʮ෭࡞༻ͳ͠ʯʹͳΔϛε

    • ࠷దԽϩδοΫ͕ҙਤͨ͠ͷͰ͸ͳ͘ͳΔՄೳੑ
  6. Contributeͨ͠಺༰ ͷ͸ͳ͠

  7. Contributeͨ͠಺༰ ͷ͸ͳ͠ • https://github.com/apple/swift/pull/31847 • υΩϡϝϯτͰղઆ͍ͯ͠Δ Grammer ͷϛε • ࣗ෼͕ಡΜͩ

    SIL ͷίʔυ ͱҧ͏͔Βؾ͍ͮͨ • SIL ͷυΩϡϝϯτ͸ͨ·ʹΨόΨό
  8. Contributeͨ͠಺༰ ͷ͸ͳ͠ • https://github.com/apple/swift/pull/33851 • ࠷దԽϩδοΫͷϛε ← ࠓ೔͸ͬͪ͜ͷ࿩ • ͜ͷ

    มߋͷཧղΛ௨ͯ͡SwiftίϯύΠϥʹೖ໳͢Δ
  9. Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • Swift ͸͜ΜͳϑϩʔͰίϯύΠϧ͞ΕΔ • Swift ίϯύΠϥ΍Δͱ਌ͷإΑΓݟΔ͜ͱʹͳΔ

  10. Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • օ͞Μ͓ͳ͡Έ Swift ίʔυ

  11. Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • AST (ந৅ߏจ໦) • ϓϩάϥϜΛจ๏ͷߏ଄ʹ฿ͬͨ໦ߏ଄ʹͨ͠΋ͷ

  12. Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • AST (ந৅ߏจ໦) • ԋࢉࢠͷ༏ઌ౓ͱ͔΋ߏจ໦ͷߏ଄Ͱܾ·ͬͨΓ

  13. Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • SIL (Swift Intermediate Language) • Swift

    ಛ༗ͷந৅දݱ
  14. Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • SIL (Swift Intermediate Language) • ͜͜ͰSwiftಛ༗ͷ

    ࠷దԽ౳͕ߦΘΕΔ • ࠓ೔ͷTopic
  15. Swift ͷίϯύΠϧϑϩʔ ͷ͸ͳ͠ • LLVM IR => … • SIL

    Λ LLVM IR ʹ௚ͯ͠LLVM ʹ౤͛Δ • ͦͷ͋ͱϦϯΧʹ౤͛ΒΕͨΓ • όΠφϦ౳ʹͳΔ
  16. Swift Intermediate Language ͷ͸ͳ͠ • Swift ಛ༗ͷதؒදݱ • ͜͜Ͱ Swiftಛ༗ͷ࠷దԽ͕ߦΘΕΔ

  17. Swift Intermediate Language ͷ͸ͳ͠ • ͲͪΒ΋खݩʹ swiftc ίϚϯυ͕͋Ε͹ੜ੒Մೳ

  18. Swift Intermediate Language ͷ͸ͳ͠ • ࢼ͠ʹ hello world! Λ SIL

    Ͱ ੜ੒ͯ͠ΈΔ • ࠓճ͸͕࣌ؒͳ͍ͷͰ raw SIL ͚ͩ
  19. Swift Intermediate Language ͷ͸ͳ͠ • ௕͍ɾಡΈͮΒ͍ʢʁʣ

  20. Swift Intermediate Language ͷ͸ͳ͠ • SIL͕swiftίʔυΑΓ௕͘ͳΔཧ༝ • ؔ਺ʢԾ૝ʣςʔϒϧͷ৘ใ • ࡉ͔͍ϝϞϦͷ֬อ΍ϦϑΝϨϯεΧ΢ϯτͷ໋ྩ

    • Main ؔ਺ͳͲͷల։
  21. Swift Intermediate Language ͷ͸ͳ͠ • SIL ͷߏ଄Λେ͖͍ॱʹ • SILModule (ιʔείʔυશମ)

    • SILFunction (֤ؔ਺) • SILBasicBlock (͍ΘΏΔείʔϓ) • SILInstruction (SILίʔυҰจҰจ)
  22. Swift Intermediate Language ͷ͸ͳ͠ • ͍͍ͩͨ͜ΕΒΛཧղ͢ΔͱSIL͸ͬ͘͟ΓಡΊΔ • SIL͕swiftίʔυΑΓ௕͘ͳΔཧ༝ • SIL

    ͷߏ଄ͷॱং
  23. Swift Intermediate Language ͷ͸ͳ͠ • SILModule

  24. SILFunction

  25. SILBasicBlock

  26. SILBasicBlock

  27. SILBasicBlock Α͘ΈͨΒ bb0: ͬͯͳ͍ͬͯΔ -> basic block ͷུͰ͢ ৚݅෼ذ౳ͰBasic Block͸૿͑Δ

  28. SILInstruction

  29. Swift Intermediate Language ͷ͸ͳ͠ • ͜ͷߏ଄Λ஌͍ͬͯΔͱίϯύΠϥͷίʔυͷҰ෦͕
 ಡΊΔΑ͏ʹͳΔ • ͦΕ͕ SILOptimizer

    ͷίʔυ
  30. SILOptimizer ʹ͍ͭͯ • SIL (Swift Intermediate Language) • ͜͜ͰSwiftಛ༗ͷ ࠷దԽ౳͕ߦΘΕΔ

  31. SILOptimizer ʹ͍ͭͯ

  32. SILOptimizer ʹ͍ͭͯ

  33. SILOptimizer ʹ͍ͭͯ • raw SIL ੜ੒ޙɺSILOptimizer ʹΑͬͯ࠷దԽ͞ΕΔ • SILOptimizerதͷͨ͘͞Μ͋ΔPassͱ͍͏ϞδϡʔϧͰ ͦΕͧΕͷ໨తͷ࠷దԽ͕ߦΘΕΔ

    • ࠓ೔͸ͦͷPassͷίʔυΛಡΉ • Passͷίʔυͷجૅ͕٧·͍ͬͯΔ
  34. SILOptimizer ʹ͍ͭͯ • ࠓ೔͸ AssumeSingleThreaded ΛಡΉ • γϯάϧεϨου͚ͩͰಈ͔͞ΕΔͷʹಛԽ͢ΔΑ͏ʹ ͢Δ࠷దԽ •

    ΦϓγϣϯͰൃಈ͞ΕΔ • PassͷதͰଟ෼Ұ൪୹͍
  35. SILOptimizer ʹ͍ͭͯ

  36. SILOptimizer ʹ͍ͭͯ SILFunctionTransform Λܧঝ -> FuncitonΛ૸ࠪ͢ΔPassʹͳΔ

  37. SILOptimizer ʹ͍ͭͯ Ծ૝ؔ਺ run Λ࣮૷ -> Pass ͷ࠷దԽϩδοΫ͸͜͜ʹॻ͘

  38. SILOptimizer ʹ͍ͭͯ ίϯύΠϥͷυϥΠόʢίϚϯυʣʹ౉͞ΕͨΦϓγϣϯΛΈΔ

  39. SILOptimizer ʹ͍ͭͯ ࠓݟ͍ͯΔؔ਺ΛSILFunctionܕͷ σʔλͱͯ͠ड͚औΔ

  40. SILOptimizer ʹ͍ͭͯ SILFunction͸SILBasicBlockͷCollection ͱͯ͠ѻ͑ΔʢͳͷͰBBʣ

  41. SILOptimizer ʹ͍ͭͯ SILBasicBlock͸SILInstructionͷCollection ͱͯ͠ѻ͑ΔʢͳͷͰIʣ

  42. SILOptimizer ʹ͍ͭͯ ͳͷͰɺ͜͜Ͱ͸ؔ਺಺ͷ InstructionΛ૸͍ࠪͯ͠Δ

  43. SILOptimizer ʹ͍ͭͯ ֤Instruction͸ܕͰදݱ͞Ε͍ͯΔ ΩϟετͰಛఆͷInstruction͔ௐ΂Δ

  44. SILOptimizer ʹ͍ͭͯ ϦϑΝϨϯεΧ΢ϯλؔ܎ͷInscrutionͳΒ NonAtomicʹ͢Δ

  45. SILOptimizer ʹ͍ͭͯ • AssumeSingleThreaded ʹ͍ͭͯ • Function಺ͷInstructionΛ૸ࠪ • ϦϑΝϨϯεΧ΢ϯλؔ܎ͩͬͨΒnon Atomicʹ͢Δ

  46. SILOptimizer ʹ͍ͭͯ • SILOptimizer ͷίʔυΛಡΉͱ͖ͷTips • ུޠ͕ଟ͍ͷͰίϯςΫετͰ൑அ͢Δ • ෳࡶͳϩδοΫ͸ C++

    Λཧղ͔ͯ͠ΒಡΉ
  47. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ࠓճ௚ͨ͠ͷ͸ɺmayHaveSideEffect ͱ͍͏ؔ਺ • ʮ෭࡞༻͕͋Δ͔΋஌Εͳ͍ʯInstructionΛݟΔ • SILInstruction ܕͷ

    ϝϯόม਺ • Dead Code Elimination౳ʹ࢖ΘΕΔ
  48. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ͜ͷ৚݅ͷ̍൪໨͕ϛε͍ͬͯͨ(mayTrap ؔ਺)

  49. Contributeͨ͠಺༰ ͷ͸ͳ͠

  50. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ίϯύΠϥ಺ͷInstructionͷछྨͷදݱํ๏ • ઌఔਃ্͛ͨ͠ܕʹΑΔදݱʢΩϟετʹΑΔ൑ผʣ • Enum ʹΑΔදݱʢswitch ͳͲ

    ʹΑΔ൑ผʣ • मਖ਼Օॴ͸ͬͪ͜Ͱදݱ͞Ε͍ͯΔ
  51. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ͪΐͬͱ̎൪Ίͷ঺հ

  52. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ϝϞϦΞΫηε͕ى͜Γͦ͏ͳInstructionͷݕ஌ํ๏ • SILInstruction ͷଐੑΛݟΔ • LLVMͷInstruction ͷଐੑΛݟΔ

    • apply Instruction ͳΒݺͼग़͠ઌͷؔ਺ͷଐੑΛݟΔ • ͜͜Β΁Μͷॲཧ͸ͯͯ͝͝ͷXϚΫϩ͕ೖͬͨॲཧͳͷ ͰׂѪɻڵຯ͕͋ͬͨΒ࣭໰Ͱฉ͍͍ͯͩ͘͞
  53. Contributeͨ͠಺༰ ͷ͸ͳ͠

  54. Contributeͨ͠಺༰ ͷ͸ͳ͠

  55. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ΋͏গ͠஌Γ͍ͨํ͸ Θ͍Θ͍swiftc #20 ͷ๻ͷࢿྉΛݟ͍ͯͩ͘͞ 
 
 


    
 
 
 
 • 
 
 
 https://speakerdeck.com/freddi/mayhavesideeffect
  56. Contributeͨ͠಺༰ ͷ͸ͳ͠ • ͜ͷमਖ਼ՕॴͳΜͰݟ͚ͭͨͷʁ • झຯͰίϯύΠϥίʔυಡΜͰ͍ͨΒۮવΈ͚ͭͨ • bug.swift.orgͰόάνέοτʹ͸ͳ͔ͬͨ

  57. Contributeͨ͠಺༰ ͷ͸ͳ͠ • PRग़ͨ͠ΒɺTest ॻ͍ͨΒϚʔδ͢ΔͱݴΘΕͨ • SILͷςετ΋ LLVMͷlitΛ࢖͍ͬͯΔΒ͍͠ • LLVM

    Integrated Tester ͷུ • ͦΜͳ͜ΜͳͰϚʔδ͞Εͨ • ΂ͭʹόάνέοτΛ੾Δඞཁ͸ͳ͍Β͍͠
  58. Contributeͨ͠಺༰ ͷ͸ͳ͠ • SIL.rst ͱ͍͏υΩϡϝϯτ͕SILʹ͍ͭͯৄ͘͠هࡌ • https://github.com/apple/swift/blob/main/docs/SIL.rst • ͨͩ͠ɺSIL.rst ͸ΨόΨόͳͷͰContributeνϟϯε͕


    ͨ͘͞Μ͋Δ • ͜͜΋मਖ਼Λ౤͛ͨ͜ͱ͕͋Δ
  59. ·ͱΊ • SILΛಡΈ͸͡ΊΔͱίϯύΠϥೖ໳΍Γ΍͍͢ • SIL͕ಡΊΔͱ࠷దԽͷίʔυ͕ಡΊΔΑ͏ʹͳΔ • Contributeʹؔͯ͠΋ݫ͍͠ϧʔϧ͸ͳ͍ͷͰ
 ؾܰʹͰ͖Δ • ಛʹυΩϡϝϯτΨόΨόͳͷͰContributeͯ͠ΈΑ͏