Save 37% off PRO during our Black Friday Sale! »

理論から入門するswift/lib/Sema - わいわいswiftc #1

0cf23fed58f84e729aa59ba5d8d0baaf?s=47 ukitaka
March 30, 2018

理論から入門するswift/lib/Sema - わいわいswiftc #1

https://iosdiscord.connpass.com/event/82436/
での発表資料です。

0cf23fed58f84e729aa59ba5d8d0baaf?s=128

ukitaka

March 30, 2018
Tweet

Transcript

  1. ཧ࿦͔Βೖ໳͢Δ swift/lib/Sema Θ͍Θ͍swiftc #1 @ukitaka

  2. twitter: @ukitaka_ github: @ukitaka Yuki Takahashi

  3. ࣗݾ঺հ ❤ ؔ਺ܕϓϩάϥϛϯάɺDDD ✏ ܕγεςϜɺίϯύΠϥ ௿ϨΠϠʔ

  4. ϒϩάॻ͍ͯΔ https://blog.waft.me

  5. ษڧ͕ͯΒ࡞ͬͨ΋ͷ • SwiftͰܕγεςϜ / ܕਪ࿦Λॻ͍ͯΈͨΓ • ukitaka/TypeSystems • ukitaka/AlgorithmW.swift •

    SILOptimizerΛࣗ࡞ͯ͠ΈͨΓ • ukitaka/sil-opt-scala
  6. contribute͸…

  7. ࣗݾ঺հऴΘΓ

  8. ࠓ೔ͷ࿩

  9. swift/lib/Sema

  10. swift/lib/Sema • Sema = Semantic Analysis (ҙຯղੳ) • ओʹܕਪ࿦ɾܕݕࠪΛ୲౰͢ΔϞδϡʔϧ •

    ίʔυྔҰ൪ଟ͍ • ΊͪΌΊͪΌෳࡶ
  11. ܕͷ࿩ָͦ͠͏…Ͱ΋ Ͳ͔͜ΒखΛ෇͚Α͏…

  12. swift/lib/Sema • ෳࡶʹབྷΈ߹ͬͯ͸͍Δ͕ɺ͍͔ͭ͘ͷτϐο Ϋͷू·Γɻͦͯ͠୯ମͰݟΔͱཧ࿦௨Γ࣮ ૷͞Ε͍ͯΔ৔߹͕ଟ͍ɻ • ֓೦Λ஌͍ͬͯΔ͚ͩͰ΋͍ͩͿೖΓ΍͍͢ • ͳͷͰ… ʮཧ࿦͔Βೖ໳͢Δʯ

  13. ࠓ೔ѻ͏τϐοΫ • δΣωϦοΫͳؔ਺ (letଟ૬)ͱܕਪ࿦ • αϒλΠϐϯάͱܕڧ੍ҙຯ࿦ • ଘࡏܕ (࣌ؒͷ౎߹Ͱলུ)

  14. ଘࡏܕͷղઆ͸QiitaͰ…

  15. δΣωϦοΫͳؔ਺(letଟ૬) ͱܕਪ࿦

  16. Generics in Swift • δΣωϦΫεपΓͷ࣮૷͸ͱʹ͔͘೉͍͠… • ͔͠͠(੍໿Λؚ·ͳ͍)δΣωϦοΫͳؔ਺ͩ ͚ͳΒܕਪ࿦ؚΊཧ࿦΋࣮૷΋ͦΜͳʹ೉͘͠ ͳ͍ •

    δΣωϦοΫͳؔ਺ͱͦͷܕਪ࿦ΛݟΔ͜ͱͰɺ SemaϞδϡʔϧΛ၆ᛌ͢Δ
  17. δΣωϦοΫͳؔ਺

  18. = ଟ૬ͳؔ਺

  19. ଟ૬ੑͱ͸…

  20. ଟ૬ੑ(polymorphism) • ϓϩάϥϜͷҰ෦෼ΛɺҟͳΔจ຺Ͱ͸ҟͳ Δܕͱͯ͠༻͍Δ͜ͱΛՄೳʹ͢Δ֤छݴޠ ػೳ

  21. ଟ૬ੑ(polymorphism) Bool → Bool ಉ͡idؔ਺Ͱ΋ɺҟͳΔจ຺Ͱ͸ҟͳΔܕ Int → Int

  22. ଟ૬ͳؔ਺͸SwiftͰ͸ Ͳ͏࣮ݱ͞Ε͍ͯΔʁ

  23. ଟ૬ͳؔ਺ͱܕਪ࿦ • ࣮͸ଟ૬ͳؔ਺ͱܕਪ࿦͸͋ΔҙຯηοτͰ ఏڙ͞Ε͍ͯΔػೳ • ܕਪ࿦Λ࢖ͬͯϙϦϞʔϑΟζϜΛ࣮ݱ • δΣωϦοΫͳؔ਺Λ஌Δʹ͸ܕਪ࿦΋஌Δ ඞཁ͕͋Δ

  24. Swiftʹ͓͚Δ (ϩʔΧϧ)ܕਪ࿦

  25. ܕਪ࿦ͷେ·͔ͳྲྀΕ 1. ܕ͕໌ࣔ͞Ε͍ͯͳ͍ͱ͜ΖΛܕม਺(Type Variable)Ͱ͓͘ 2. ܕม਺ͷ੍໿(Constraint)Λੜ੒͢Δ 3. ੍໿Λղ͘

  26. ܕਪ࿦ͷେ·͔ͳྲྀΕ • ܕ͕ܾ·͍ͬͯͳ͍ͱ͜ΖΛܕม਺Ͱ͓͘ Type Variables: #0 = $T0 [inout allowed]

    #1 = $T1 [inout allowed] $ swift -frontend -typecheck -debug-constraints test.swift $T1 $T0
  27. • ੍໿(Constraint)Λੜ੒͢Δ ܕਪ࿦ͷେ·͔ͳྲྀΕ $T0 literal conforms to ExpressibleByIntegerLiteral $T0 conv

    $T1 $ swift -frontend -typecheck -debug-constraints test.swift $T0 ͸੔਺Ϧςϥϧ $T0 ͸$T1ʹม׵Ͱ͖Δ
  28. ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ Active bindings: $T0 := Int (trying $T0

    := Int ($T1 bindings=(supertypes of) Int) Active bindings: $T1 := Int (trying $T1 := Int (found solution 0 0 0 0 0 0 0 0 0 0 0 0 0) ) ) $ swift -frontend -typecheck -debug-constraints test.swift
  29. ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ ---Solution--- Fixed score: 0 0 0 0

    0 0 0 0 0 0 0 0 0 Type variables: $T1 as Int $T0 as Int $ swift -frontend -typecheck -debug-constraints test.swift Int Int
  30. δΣωϦοΫͳؔ਺ͷ৔߹

  31. ܕਪ࿦ͷେ·͔ͳྲྀΕ(࠶ܝ) 1. ܕ͕໌ࣔ͞Ε͍ͯͳ͍ͱ͜ΖΛܕม਺(Type Variable)Ͱ͓͘ ←ଟ૬ͳؔ਺ͷ࣮ݱʹ͸͜͜ʹϙΠϯτ 2. ܕม਺ͷ੍໿(Constraint)Λੜ੒͢Δ 3. ੍໿Λղ͘

  32. δΣωϦοΫͳؔ਺ͷܕਪ࿦

  33. δΣωϦοΫͳؔ਺ͷܕਪ࿦ ಺෦తͳܕͷදݱ: <τ_0_0> τ_0_0 → τ_0_0 ݟͨ໨ͷܕ: <A> A →

    A Aͱ͍͏໊લ͸ࣺͯΒΕͯdepthͱindexʹΑΔ໊ແ͠දݱʹͳ͍ͬͯΔ
  34. δΣωϦοΫͳؔ਺ͷܕਪ࿦ ग़ݱͷͨͼʹτ_0_0ʹ৽͍͠ܕม਺ΛׂΓ౰ͯΔ Opened types: opens τ_0_0 -> $T1 opens τ_0_0

    -> $T4 $ swift -frontend -typecheck -debug-constraints test.swift ͜Ε͕ଟ૬ੑΛ࣮ݱ͍ͯ͠ΔϙΠϯτ $T1 -> $T1 $T4 -> $T4
  35. δΣωϦοΫͳؔ਺ͷܕਪ࿦ Type Variables: #0 = $T0 as ($T1) -> $T1

    #1 = $T1 #2 = $T2 #3 = $T3 as ($T4) -> $T4 #4 = $T4 #5 = $T5 #6 = $T6 $ swift -frontend -typecheck -debug-constraints test.swift
  36. δΣωϦοΫͳؔ਺ͷܕਪ࿦ • ͋ͱ͸ಉ͡Α͏ʹ੍໿ੜ੒ͯ͠ղ͚ͩ͘ ---Solution--- Fixed score: 0 0 0 0

    0 0 0 0 0 0 0 0 0 Type variables: $T6 as (Int, Bool) $T3 as (Bool) -> Bool $T4 as Bool $T0 as (Int) -> Int $T1 as Int $T5 as Bool $T2 as Int $ swift -frontend -typecheck -debug-constraints test.swift
  37. δΣωϦοΫͳؔ਺ͷܕਪ࿦ • ग़ݱͷͨͼʹ৽͍͠ܕม਺ΛׂΓ౰্ͯͨͰɺ ܕΛਪ࿦͢Δ͜ͱʹΑͬͯଟ૬ੑΛ࣮ݱͯ͠ ͍Δɻ • ͜ΕΛletଟ૬ͱݺͿ

  38. δΣωϦοΫͳؔ਺ͱܕਪ࿦ The secret life of types in Swift https://medium.com/@slavapestov/the-secret-life-of-types-in-swift-ff83c3c000a5

  39. ΋͏গ࣮͠૷دΓͷ࿩

  40. ܕνΣοΫؔ࿈ͷओཁͳΫϥε • ίʔυྔଟ͍͚ͲɺओͳΫϥε͸2ͭͷΈ • TypeChecker → ܕݕࠪ୲౰ • ConstraintSystem →

    ܕਪ࿦୲౰ • TypeChecker͕Expr͝ͱʹConstraintSystemΛ ΠϯελϯεԽͯ͠࢖͍ͬͯΔΠϝʔδ
  41. TypeChecker / ConstraintSystem

  42. ܕม਺ɺ੍໿ੜ੒ͷྫ

  43. ੍໿ͷ୯ҰԽ (solve)

  44. ੍໿ͷ୯ҰԽ (solve) • جຊతʹ͸ީิΛͲΜͲΜࢼ͍ͯ͘͠ • PotentialBindings ( e.g. ੔਺Ϧςϥϧ͔ͩΒIntͩΖ͏ )

    • Overloadͷީิͨͪ • ConstraintGraphͱ͍͏(ϋΠύʔ)άϥϑͰ੍໿ Λ؅ཧ • େ·͔ʹ͸BindingΛ௥Ճ → άϥϑΛ੔ཧͷ܁Γฦ͠
  45. ղͷద༻(apply)ͱASTॻ͖׵͑ • ExprRewriter͕ܕ෇͖ͷAST΁ͱॻ͖׵͑Δ

  46. letଟ૬Λ࣮ݱ͍ͯ͠Δ෦෼ δΣωϦοΫͳؔ਺ͳΒ ܕύϥϝʔλʹ ϑϨογϡͳܕม਺Λ ׂΓ౰ͯΔͱ͍͏ ཧ࿦ͦͷ··ͷ࣮૷ɻ

  47. ·ͱΊ • ଟ૬ͳؔ਺͕ܕਪ࿦ʹΑ࣮ͬͯݱ͞Ε͍ͯΔ ༷ࢠΛݟͨ • ConstraintSystem͕ܕਪ࿦Λ͢Δͷେ·͔ͳ ྲྀΕΛΈͨ

  48. ͓·͚: ੍໿෇͖ͷδΣωϦΫε͸ʁ • ࣮૷ͱͯ͠͸جຊతʹ͸ಉ͡ • TʹׂΓ౰ͯΒΕͨܕม਺ʹ $T1 conforms to EquatableͷΑ͏ͳ੍໿͕ՃΘΔ͘Β͍

    • ͔͠͠എܠʹ͋Δཧ࿦͸·ͨผ…
  49. ͓·͚: ੍໿෇͖ͷδΣωϦΫε͸ʁ • ݶఆܕ (Qualified type) • TaPLʹ͸໊લ͔͠ࡌͬͯͳ͍ • Jones,

    Mark P. Qualified Types: Theory and Practice. Cambridge University Press, 1994. • Haskellͷ Eq a => a -> a -> Bool • ༗քྔԽ(bounded qualification)Ͱ͸ͳ͍
  50. αϒλΠϐϯάͱ ܕڧ੍ҙຯ࿦

  51. ΫΠζ • Swiftʹ͓͍ͯɺT͸ Optional<T>ͷαϒλΠ ϓ͔ʁʁ

  52. T is subtype of Optional<T> • ౴͑: αϒλΠϓɻ • αϒλΠϐϯάنଇ͸υΩϡϝϯτʹ͸ॻ͍

    ͍ͯͳ͍͕ɺιʔείʔυதͷίϝϯτʹͪΐ ͪ͜ΐ͜ఆٛ͞Ε͍ͯΔɻ(͢΂͕ͯॻ͍ͯ͋ΔΘ͚ Ͱ͸ͳ͍) • lib/Sema/CSSimplify.cpp Λࢀর
  53. T is subtype of Optional<T>

  54. αϒλΠϓؔ܎ • ͲΜͳαϒλΠϓؔ܎Λ࣋ͨͤΔ͔͸ͦͷݴޠͰ ͷܾΊͷ໰୊ • ྫ͑͹ Int < Float ͱ͔

    Int < String ͱ͔࣋ͨͤͯ ΋(੔߹ੑ͕औΕΔͷͳΒ) ྑ͍ • Swift͸OptionalΛָʹѻ͏࢓૊Έͱͯ͠ ɺ T < Optional<T>Λ࠾༻͍ͯ͠Δ
  55. ଞʹ͸ͲΜͳ αϒλΠϓؔ܎͕͋Δʁ

  56. Optional-to-optional

  57. Metatype

  58. Existential

  59. αϒλΠϓ͔ͩΒ ͳΜͰ΋OKʁ

  60. ྫ: Existential

  61. ࣮ߦ࣌ͷϝϞϦ্ͷදݱ͕ ҧ͏Մೳੑ͕͋Δ

  62. ͜ͷ໰୊ʹର͢ΔΞϓϩʔν αϒλΠϐϯάͷ࣮૷ํࣜ͸େ͖͘෼͚ͯ2ͭ 1. inclusiveͳ࣮૷ • ڞ௨ͷ࣮ߦ࣌ͷදݱΛ࢖͏ • JavaͳͲ 2.coerciveͳ࣮૷ •

    ม׵ॲཧ(ܕڧ੍)ΛڬΈࠐΉ ← Swift͸ͬͪ͜
  63. ܕڧ੍ҙຯ࿦(coercion semantics) • ؆୯ʹݴ͏ͱɺ̍ͭͷαϒλΠϐϯάنଇʹ͍ͭͯɺ1 ͭͷม׵ॲཧ(ܕڧ੍)͕ଘࡏ͢Δɻ • TypeChecker͸ɺͲͷαϒλΠϓؔ܎Λ࢖ͬͯܕνΣο ΫΛ௨͔ͨ͠Λه࿥͓͍ͯͯ͠ɺ͋ͱͰ࣮ࡍͷม׵ॲཧ Λૠೖ͢Δɻ •

    lib/Sema/CSApply.cppͷExprRewriterͱ͍͏Ϋϥε Λࢀߟɻ
  64. ྫ: Optionalͷ৔߹

  65. ྫ: Optionalͷ৔߹

  66. ྫ: Optionalͷ৔߹ Constraint restrictions: Int to Optional<Int> is [value-to-optional] $

    swift -frontend -typecheck -debug-constraints test.swift
  67. ྫ: Optionalͷ৔߹ $ swiftc -dump-ast test.swift (inject_into_optional implicit type=‘Optional<Int>’ …

    (declref_expr type=‘Int' … ) )
  68. ྫ: Optionalͷ৔߹ $ swiftc -emit-sil test.swift %9 = load %3

    : $*Int %10 = enum $Optional<Int>, #Optional.some!enumelt.1, %9 : $Int store %10 to %8 : $*Optional<Int> %12 = integer_literal $Builtin.Int32, 0 %13 = struct $Int32 (%12 : $Builtin.Int32) ͭ·ΓରԠ͢Δܕڧ੍͸ʮ.someͰแΉʯ
  69. Classͷ৔߹

  70. ྫ: Classͷ৔߹

  71. ྫ: Classͷ৔߹ Constraint restrictions: Dog to Animal is [superclass] $

    swift -frontend -typecheck -debug-constraints test.swift
  72. ྫ: Classͷ৔߹ $ swiftc -dump-ast test.swift (derived_to_base_expr implicit type=‘Animal’ …

    (declref_expr type=‘Dog' … ) )
  73. ྫ: Classͷ৔߹ $ swiftc -emit-sil test.swift … %10 = load

    %3 : $*Dog strong_retain %10 : $Dog %12 = upcast %10 : $Dog to $Animal store %12 to %9 : $*Animal %14 = integer_literal $Builtin.Int32, 0 %15 = struct $Int32 (%14 : $Builtin.Int32) ͭ·ΓରԠ͢Δܕڧ੍͸ʮΞοϓΩϟετʯ
  74. ྫ:Existentialͷ৔߹ աఔ͸লུ (erasure_expr implicit type=‘Animal’ … (declref_expr type=‘Dog' … )

    ) ରԠ͢Δܕڧ੍͸ʮଘࡏܕ΁ͷύοέʔδԽʯ ( = type erase)
  75. Swiftʹ͓͚Δʮ҉໧ม׵ʯ • Α͘ʮ҉໧ม׵ʯͳͲͱݺ͹ΕΔΞϨͷਖ਼ମ ͸΄΅΄΅αϒλΠϓؔ܎ʹج͍ͮͨܕڧ੍ • ҉໧ม׵͋Δͱ͜ΖʹαϒλΠϐϯά͋Γ

  76. ࣮૷Λগ͚ͩ͠ݟΔ

  77. αϒλΠϐϯάʙܕڧ੍ • ConstraintSystemͷConstraintRestrictionsʹ ࢖ͬͨαϒλΠϓؔ܎౳͕ه࿥͞ΕΔ • ExprRewriter::coerceToTypeͰܕڧ੍

  78. ExprRewriter::coerceToType ܕڧ੍(coercion)ͷॲཧΛ͍ͯ͠Δؔ਺

  79. ExprRewriter::coerceToType ConversionRestrictionKindʹԠͨ͡ॲཧ ͨͱ͑͹ValueToOptionalͳΒInjectIntoOptionalͱ͍͏ ܕڧ੍Λૠೖ

  80. αϒλΠϐϯά·ͱΊ • αϒλΠϐϯά͸نଇʹΑͬͯఆٛ͞Ε͍ͯΔ • ͦΕͧΕنଇʹରԠ͢Δܕڧ੍͕ଘࡏ͢Δ • TypeChecker/ConstraintSystem͸ɺͲͷαϒ λΠϓؔ܎Λ࢖ͬͯܕνΣοΫΛ௨͔ͨ͠Λ֮ ͓͍͑ͯͯɺ͋ͱͰ࣮ࡍͷม׵ॲཧΛૠೖ͢Δɻ

  81. ͓ΘΓ