Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

twitter: @ukitaka_ github: @ukitaka Yuki Takahashi

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

ษڧ͕ͯΒ࡞ͬͨ΋ͷ • SwiftͰܕγεςϜ / ܕਪ࿦Λॻ͍ͯΈͨΓ • ukitaka/TypeSystems • ukitaka/AlgorithmW.swift • SILOptimizerΛࣗ࡞ͯ͠ΈͨΓ • ukitaka/sil-opt-scala

Slide 6

Slide 6 text

contribute͸…

Slide 7

Slide 7 text

ࣗݾ঺հऴΘΓ

Slide 8

Slide 8 text

ࠓ೔ͷ࿩

Slide 9

Slide 9 text

swift/lib/Sema

Slide 10

Slide 10 text

swift/lib/Sema • Sema = Semantic Analysis (ҙຯղੳ) • ओʹܕਪ࿦ɾܕݕࠪΛ୲౰͢ΔϞδϡʔϧ • ίʔυྔҰ൪ଟ͍ • ΊͪΌΊͪΌෳࡶ

Slide 11

Slide 11 text

ܕͷ࿩ָͦ͠͏…Ͱ΋ Ͳ͔͜ΒखΛ෇͚Α͏…

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

ଘࡏܕͷղઆ͸QiitaͰ…

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Generics in Swift • δΣωϦΫεपΓͷ࣮૷͸ͱʹ͔͘೉͍͠… • ͔͠͠(੍໿Λؚ·ͳ͍)δΣωϦοΫͳؔ਺ͩ ͚ͳΒܕਪ࿦ؚΊཧ࿦΋࣮૷΋ͦΜͳʹ೉͘͠ ͳ͍ • δΣωϦοΫͳؔ਺ͱͦͷܕਪ࿦ΛݟΔ͜ͱͰɺ SemaϞδϡʔϧΛ၆ᛌ͢Δ

Slide 17

Slide 17 text

δΣωϦοΫͳؔ਺

Slide 18

Slide 18 text

= ଟ૬ͳؔ਺

Slide 19

Slide 19 text

ଟ૬ੑͱ͸…

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

ܕਪ࿦ͷେ·͔ͳྲྀΕ • ܕ͕ܾ·͍ͬͯͳ͍ͱ͜ΖΛܕม਺Ͱ͓͘ Type Variables: #0 = $T0 [inout allowed] #1 = $T1 [inout allowed] $ swift -frontend -typecheck -debug-constraints test.swift $T1 $T0

Slide 27

Slide 27 text

• ੍໿(Constraint)Λੜ੒͢Δ ܕਪ࿦ͷେ·͔ͳྲྀΕ $T0 literal conforms to ExpressibleByIntegerLiteral $T0 conv $T1 $ swift -frontend -typecheck -debug-constraints test.swift $T0 ͸੔਺Ϧςϥϧ $T0 ͸$T1ʹม׵Ͱ͖Δ

Slide 28

Slide 28 text

ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ 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

Slide 29

Slide 29 text

ܕਪ࿦ͷେ·͔ͳྲྀΕ • ੍໿Λղ͘ ---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

Slide 30

Slide 30 text

δΣωϦοΫͳؔ਺ͷ৔߹

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

δΣωϦοΫͳؔ਺ͷܕਪ࿦ ಺෦తͳܕͷදݱ: <τ_0_0> τ_0_0 → τ_0_0 ݟͨ໨ͷܕ: A → A Aͱ͍͏໊લ͸ࣺͯΒΕͯdepthͱindexʹΑΔ໊ແ͠දݱʹͳ͍ͬͯΔ

Slide 34

Slide 34 text

δΣωϦοΫͳؔ਺ͷܕਪ࿦ ग़ݱͷͨͼʹτ_0_0ʹ৽͍͠ܕม਺ΛׂΓ౰ͯΔ Opened types: opens τ_0_0 -> $T1 opens τ_0_0 -> $T4 $ swift -frontend -typecheck -debug-constraints test.swift ͜Ε͕ଟ૬ੑΛ࣮ݱ͍ͯ͠ΔϙΠϯτ $T1 -> $T1 $T4 -> $T4

Slide 35

Slide 35 text

δΣωϦοΫͳؔ਺ͷܕਪ࿦ 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

Slide 36

Slide 36 text

δΣωϦοΫͳؔ਺ͷܕਪ࿦ • ͋ͱ͸ಉ͡Α͏ʹ੍໿ੜ੒ͯ͠ղ͚ͩ͘ ---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

Slide 37

Slide 37 text

δΣωϦοΫͳؔ਺ͷܕਪ࿦ • ग़ݱͷͨͼʹ৽͍͠ܕม਺ΛׂΓ౰্ͯͨͰɺ ܕΛਪ࿦͢Δ͜ͱʹΑͬͯଟ૬ੑΛ࣮ݱͯ͠ ͍Δɻ • ͜ΕΛletଟ૬ͱݺͿ

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

΋͏গ࣮͠૷دΓͷ࿩

Slide 40

Slide 40 text

ܕνΣοΫؔ࿈ͷओཁͳΫϥε • ίʔυྔଟ͍͚ͲɺओͳΫϥε͸2ͭͷΈ • TypeChecker → ܕݕࠪ୲౰ • ConstraintSystem → ܕਪ࿦୲౰ • TypeChecker͕Expr͝ͱʹConstraintSystemΛ ΠϯελϯεԽͯ͠࢖͍ͬͯΔΠϝʔδ

Slide 41

Slide 41 text

TypeChecker / ConstraintSystem

Slide 42

Slide 42 text

ܕม਺ɺ੍໿ੜ੒ͷྫ

Slide 43

Slide 43 text

੍໿ͷ୯ҰԽ (solve)

Slide 44

Slide 44 text

੍໿ͷ୯ҰԽ (solve) • جຊతʹ͸ީิΛͲΜͲΜࢼ͍ͯ͘͠ • PotentialBindings ( e.g. ੔਺Ϧςϥϧ͔ͩΒIntͩΖ͏ ) • Overloadͷީิͨͪ • ConstraintGraphͱ͍͏(ϋΠύʔ)άϥϑͰ੍໿ Λ؅ཧ • େ·͔ʹ͸BindingΛ௥Ճ → άϥϑΛ੔ཧͷ܁Γฦ͠

Slide 45

Slide 45 text

ղͷద༻(apply)ͱASTॻ͖׵͑ • ExprRewriter͕ܕ෇͖ͷAST΁ͱॻ͖׵͑Δ

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

͓·͚: ੍໿෇͖ͷδΣωϦΫε͸ʁ • ݶఆܕ (Qualified type) • TaPLʹ͸໊લ͔͠ࡌͬͯͳ͍ • Jones, Mark P. Qualified Types: Theory and Practice. Cambridge University Press, 1994. • Haskellͷ Eq a => a -> a -> Bool • ༗քྔԽ(bounded qualification)Ͱ͸ͳ͍

Slide 50

Slide 50 text

αϒλΠϐϯάͱ ܕڧ੍ҙຯ࿦

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

T is subtype of Optional • ౴͑: αϒλΠϓɻ • αϒλΠϐϯάنଇ͸υΩϡϝϯτʹ͸ॻ͍ ͍ͯͳ͍͕ɺιʔείʔυதͷίϝϯτʹͪΐ ͪ͜ΐ͜ఆٛ͞Ε͍ͯΔɻ(͢΂͕ͯॻ͍ͯ͋ΔΘ͚ Ͱ͸ͳ͍) • lib/Sema/CSSimplify.cpp Λࢀর

Slide 53

Slide 53 text

T is subtype of Optional

Slide 54

Slide 54 text

αϒλΠϓؔ܎ • ͲΜͳαϒλΠϓؔ܎Λ࣋ͨͤΔ͔͸ͦͷݴޠͰ ͷܾΊͷ໰୊ • ྫ͑͹ Int < Float ͱ͔ Int < String ͱ͔࣋ͨͤͯ ΋(੔߹ੑ͕औΕΔͷͳΒ) ྑ͍ • Swift͸OptionalΛָʹѻ͏࢓૊Έͱͯ͠ ɺ T < OptionalΛ࠾༻͍ͯ͠Δ

Slide 55

Slide 55 text

ଞʹ͸ͲΜͳ αϒλΠϓؔ܎͕͋Δʁ

Slide 56

Slide 56 text

Optional-to-optional

Slide 57

Slide 57 text

Metatype

Slide 58

Slide 58 text

Existential

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

ྫ: Existential

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

͜ͷ໰୊ʹର͢ΔΞϓϩʔν αϒλΠϐϯάͷ࣮૷ํࣜ͸େ͖͘෼͚ͯ2ͭ 1. inclusiveͳ࣮૷ • ڞ௨ͷ࣮ߦ࣌ͷදݱΛ࢖͏ • JavaͳͲ 2.coerciveͳ࣮૷ • ม׵ॲཧ(ܕڧ੍)ΛڬΈࠐΉ ← Swift͸ͬͪ͜

Slide 63

Slide 63 text

ܕڧ੍ҙຯ࿦(coercion semantics) • ؆୯ʹݴ͏ͱɺ̍ͭͷαϒλΠϐϯάنଇʹ͍ͭͯɺ1 ͭͷม׵ॲཧ(ܕڧ੍)͕ଘࡏ͢Δɻ • TypeChecker͸ɺͲͷαϒλΠϓؔ܎Λ࢖ͬͯܕνΣο ΫΛ௨͔ͨ͠Λه࿥͓͍ͯͯ͠ɺ͋ͱͰ࣮ࡍͷม׵ॲཧ Λૠೖ͢Δɻ • lib/Sema/CSApply.cppͷExprRewriterͱ͍͏Ϋϥε Λࢀߟɻ

Slide 64

Slide 64 text

ྫ: Optionalͷ৔߹

Slide 65

Slide 65 text

ྫ: Optionalͷ৔߹

Slide 66

Slide 66 text

ྫ: Optionalͷ৔߹ Constraint restrictions: Int to Optional is [value-to-optional] $ swift -frontend -typecheck -debug-constraints test.swift

Slide 67

Slide 67 text

ྫ: Optionalͷ৔߹ $ swiftc -dump-ast test.swift (inject_into_optional implicit type=‘Optional’ … (declref_expr type=‘Int' … ) )

Slide 68

Slide 68 text

ྫ: Optionalͷ৔߹ $ swiftc -emit-sil test.swift %9 = load %3 : $*Int %10 = enum $Optional, #Optional.some!enumelt.1, %9 : $Int store %10 to %8 : $*Optional %12 = integer_literal $Builtin.Int32, 0 %13 = struct $Int32 (%12 : $Builtin.Int32) ͭ·ΓରԠ͢Δܕڧ੍͸ʮ.someͰแΉʯ

Slide 69

Slide 69 text

Classͷ৔߹

Slide 70

Slide 70 text

ྫ: Classͷ৔߹

Slide 71

Slide 71 text

ྫ: Classͷ৔߹ Constraint restrictions: Dog to Animal is [superclass] $ swift -frontend -typecheck -debug-constraints test.swift

Slide 72

Slide 72 text

ྫ: Classͷ৔߹ $ swiftc -dump-ast test.swift (derived_to_base_expr implicit type=‘Animal’ … (declref_expr type=‘Dog' … ) )

Slide 73

Slide 73 text

ྫ: 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) ͭ·ΓରԠ͢Δܕڧ੍͸ʮΞοϓΩϟετʯ

Slide 74

Slide 74 text

ྫ:Existentialͷ৔߹ աఔ͸লུ (erasure_expr implicit type=‘Animal’ … (declref_expr type=‘Dog' … ) ) ରԠ͢Δܕڧ੍͸ʮଘࡏܕ΁ͷύοέʔδԽʯ ( = type erase)

Slide 75

Slide 75 text

Swiftʹ͓͚Δʮ҉໧ม׵ʯ • Α͘ʮ҉໧ม׵ʯͳͲͱݺ͹ΕΔΞϨͷਖ਼ମ ͸΄΅΄΅αϒλΠϓؔ܎ʹج͍ͮͨܕڧ੍ • ҉໧ม׵͋Δͱ͜ΖʹαϒλΠϐϯά͋Γ

Slide 76

Slide 76 text

࣮૷Λগ͚ͩ͠ݟΔ

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

͓ΘΓ