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/lib/Sema - わいわいswiftc #1
Search
ukitaka
March 30, 2018
Programming
5
1.6k
理論から入門するswift/lib/Sema - わいわいswiftc #1
https://iosdiscord.connpass.com/event/82436/
での発表資料です。
ukitaka
March 30, 2018
Tweet
Share
More Decks by ukitaka
See All by ukitaka
CSの生産性改善を支える分析環境 Mercari CS/CRE Tech Talk #1
ukitaka
1
2.6k
switchのexhaustiveness/redundancy チェック 理論と実装 わいわいswiftc #8 @ukitaka
ukitaka
0
180
SwiftのDemanglerを書く @ わいわいswiftc番外編
ukitaka
0
410
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
5.7k
Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift
ukitaka
6
1.3k
Realmの処理を再利用可能かつ合成可能にする
ukitaka
0
880
マルチスレッドRxSwift @ 社内RxSwift勉強会
ukitaka
5
1.2k
今日こそ理解するHot / Cold @社内RxSwift勉強会
ukitaka
14
2.6k
RxSwift コードリーディングの勘所@社内RxSwift勉強会
ukitaka
3
990
Other Decks in Programming
See All in Programming
Java ジェネリクス入門 2024
nagise
0
600
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
350
Pinia Colada が実現するスマートな非同期処理
naokihaba
2
150
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
960
色々なIaCツールを実際に触って比較してみる
iriikeita
0
260
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.2k
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
270
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
170
EventSourcingの理想と現実
wenas
6
2.1k
CSC509 Lecture 09
javiergs
PRO
0
100
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.5k
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
Featured
See All Featured
Side Projects
sachag
452
42k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
32
1.8k
Designing for humans not robots
tammielis
249
25k
Six Lessons from altMBA
skipperchong
26
3.5k
Done Done
chrislema
181
16k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
680
GraphQLとの向き合い方2022年版
quramy
43
13k
Designing for Performance
lara
604
68k
Scaling GitHub
holman
458
140k
Optimizing for Happiness
mojombo
376
69k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
Transcript
ཧ͔Βೖ͢Δ swift/lib/Sema Θ͍Θ͍swiftc #1 @ukitaka
twitter: @ukitaka_ github: @ukitaka Yuki Takahashi
ࣗݾհ ❤ ؔܕϓϩάϥϛϯάɺDDD ✏ ܕγεςϜɺίϯύΠϥ ϨΠϠʔ
ϒϩάॻ͍ͯΔ https://blog.waft.me
ษڧ͕ͯΒ࡞ͬͨͷ • SwiftͰܕγεςϜ / ܕਪΛॻ͍ͯΈͨΓ • ukitaka/TypeSystems • ukitaka/AlgorithmW.swift •
SILOptimizerΛࣗ࡞ͯ͠ΈͨΓ • ukitaka/sil-opt-scala
contribute…
ࣗݾհऴΘΓ
ࠓͷ
swift/lib/Sema
swift/lib/Sema • Sema = Semantic Analysis (ҙຯղੳ) • ओʹܕਪɾܕݕࠪΛ୲͢ΔϞδϡʔϧ •
ίʔυྔҰ൪ଟ͍ • ΊͪΌΊͪΌෳࡶ
ܕͷָͦ͠͏…Ͱ Ͳ͔͜ΒखΛ͚Α͏…
swift/lib/Sema • ෳࡶʹབྷΈ߹͍ͬͯΔ͕ɺ͍͔ͭ͘ͷτϐο Ϋͷू·Γɻͦͯ͠୯ମͰݟΔͱཧ௨Γ࣮ ͞Ε͍ͯΔ߹͕ଟ͍ɻ • ֓೦Λ͍ͬͯΔ͚ͩͰ͍ͩͿೖΓ͍͢ • ͳͷͰ… ʮཧ͔Βೖ͢Δʯ
ࠓѻ͏τϐοΫ • δΣωϦοΫͳؔ (letଟ૬)ͱܕਪ • αϒλΠϐϯάͱܕڧ੍ҙຯ • ଘࡏܕ (࣌ؒͷ߹Ͱলུ)
ଘࡏܕͷղઆQiitaͰ…
δΣωϦοΫͳؔ(letଟ૬) ͱܕਪ
Generics in Swift • δΣωϦΫεपΓͷ࣮ͱʹ͔͍͘͠… • ͔͠͠(੍Λؚ·ͳ͍)δΣωϦοΫͳؔͩ ͚ͳΒܕਪؚΊཧ࣮ͦΜͳʹ͘͠ ͳ͍ •
δΣωϦοΫͳؔͱͦͷܕਪΛݟΔ͜ͱͰɺ SemaϞδϡʔϧΛ၆ᛌ͢Δ
δΣωϦοΫͳؔ
= ଟ૬ͳؔ
ଟ૬ੑͱ…
ଟ૬ੑ(polymorphism) • ϓϩάϥϜͷҰ෦ΛɺҟͳΔจ຺Ͱҟͳ Δܕͱͯ͠༻͍Δ͜ͱΛՄೳʹ͢Δ֤छݴޠ ػೳ
ଟ૬ੑ(polymorphism) Bool → Bool ಉ͡idؔͰɺҟͳΔจ຺ͰҟͳΔܕ Int → Int
ଟ૬ͳؔSwiftͰ Ͳ͏࣮ݱ͞Ε͍ͯΔʁ
ଟ૬ͳؔͱܕਪ • ࣮ଟ૬ͳؔͱܕਪ͋ΔҙຯηοτͰ ఏڙ͞Ε͍ͯΔػೳ • ܕਪΛͬͯϙϦϞʔϑΟζϜΛ࣮ݱ • δΣωϦοΫͳؔΛΔʹܕਪΔ ඞཁ͕͋Δ
Swiftʹ͓͚Δ (ϩʔΧϧ)ܕਪ
ܕਪͷେ·͔ͳྲྀΕ 1. ܕ͕໌ࣔ͞Ε͍ͯͳ͍ͱ͜ΖΛܕม(Type Variable)Ͱ͓͘ 2. ܕมͷ੍(Constraint)Λੜ͢Δ 3. ੍Λղ͘
ܕਪͷେ·͔ͳྲྀΕ • ܕ͕ܾ·͍ͬͯͳ͍ͱ͜ΖΛܕมͰ͓͘ Type Variables: #0 = $T0 [inout allowed]
#1 = $T1 [inout allowed] $ swift -frontend -typecheck -debug-constraints test.swift $T1 $T0
• ੍(Constraint)Λੜ͢Δ ܕਪͷେ·͔ͳྲྀΕ $T0 literal conforms to ExpressibleByIntegerLiteral $T0 conv
$T1 $ swift -frontend -typecheck -debug-constraints test.swift $T0 Ϧςϥϧ $T0 $T1ʹมͰ͖Δ
ܕਪͷେ·͔ͳྲྀΕ • ੍Λղ͘ 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
ܕਪͷେ·͔ͳྲྀΕ • ੍Λղ͘ ---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
δΣωϦοΫͳؔͷ߹
ܕਪͷେ·͔ͳྲྀΕ(࠶ܝ) 1. ܕ͕໌ࣔ͞Ε͍ͯͳ͍ͱ͜ΖΛܕม(Type Variable)Ͱ͓͘ ←ଟ૬ͳؔͷ࣮ݱʹ͜͜ʹϙΠϯτ 2. ܕมͷ੍(Constraint)Λੜ͢Δ 3. ੍Λղ͘
δΣωϦοΫͳؔͷܕਪ
δΣωϦοΫͳؔͷܕਪ ෦తͳܕͷදݱ: <τ_0_0> τ_0_0 → τ_0_0 ݟͨͷܕ: <A> A →
A Aͱ͍͏໊લࣺͯΒΕͯdepthͱindexʹΑΔ໊ແ͠දݱʹͳ͍ͬͯΔ
δΣωϦοΫͳؔͷܕਪ ग़ݱͷͨͼʹτ_0_0ʹ৽͍͠ܕมΛׂΓͯΔ Opened types: opens τ_0_0 -> $T1 opens τ_0_0
-> $T4 $ swift -frontend -typecheck -debug-constraints test.swift ͜Ε͕ଟ૬ੑΛ࣮ݱ͍ͯ͠ΔϙΠϯτ $T1 -> $T1 $T4 -> $T4
δΣωϦοΫͳؔͷܕਪ 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
δΣωϦοΫͳؔͷܕਪ • ͋ͱಉ͡Α͏ʹ੍ੜͯ͠ղ͚ͩ͘ ---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
δΣωϦοΫͳؔͷܕਪ • ग़ݱͷͨͼʹ৽͍͠ܕมΛׂΓ্ͯͨͰɺ ܕΛਪ͢Δ͜ͱʹΑͬͯଟ૬ੑΛ࣮ݱͯ͠ ͍Δɻ • ͜ΕΛletଟ૬ͱݺͿ
δΣωϦοΫͳؔͱܕਪ The secret life of types in Swift https://medium.com/@slavapestov/the-secret-life-of-types-in-swift-ff83c3c000a5
͏গ࣮͠دΓͷ
ܕνΣοΫؔ࿈ͷओཁͳΫϥε • ίʔυྔଟ͍͚ͲɺओͳΫϥε2ͭͷΈ • TypeChecker → ܕݕࠪ୲ • ConstraintSystem →
ܕਪ୲ • TypeChecker͕Expr͝ͱʹConstraintSystemΛ ΠϯελϯεԽ͍ͯͬͯ͠ΔΠϝʔδ
TypeChecker / ConstraintSystem
ܕมɺ੍ੜͷྫ
੍ͷ୯ҰԽ (solve)
੍ͷ୯ҰԽ (solve) • جຊతʹީิΛͲΜͲΜࢼ͍ͯ͘͠ • PotentialBindings ( e.g. Ϧςϥϧ͔ͩΒIntͩΖ͏ )
• Overloadͷީิͨͪ • ConstraintGraphͱ͍͏(ϋΠύʔ)άϥϑͰ੍ Λཧ • େ·͔ʹBindingΛՃ → άϥϑΛཧͷ܁Γฦ͠
ղͷద༻(apply)ͱASTॻ͖͑ • ExprRewriter͕ܕ͖ͷASTͱॻ͖͑Δ
letଟ૬Λ࣮ݱ͍ͯ͠Δ෦ δΣωϦοΫͳؔͳΒ ܕύϥϝʔλʹ ϑϨογϡͳܕมΛ ׂΓͯΔͱ͍͏ ཧͦͷ··ͷ࣮ɻ
·ͱΊ • ଟ૬ͳ͕ؔܕਪʹΑ࣮ͬͯݱ͞Ε͍ͯΔ ༷ࢠΛݟͨ • ConstraintSystem͕ܕਪΛ͢Δͷେ·͔ͳ ྲྀΕΛΈͨ
͓·͚: ੍͖ͷδΣωϦΫεʁ • ࣮ͱͯ͠جຊతʹಉ͡ • TʹׂΓͯΒΕͨܕมʹ $T1 conforms to EquatableͷΑ͏ͳ੍͕ՃΘΔ͘Β͍
• ͔͠͠എܠʹ͋Δཧ·ͨผ…
͓·͚: ੍͖ͷδΣωϦΫεʁ • ݶఆܕ (Qualified type) • TaPLʹ໊લ͔͠ࡌͬͯͳ͍ • Jones,
Mark P. Qualified Types: Theory and Practice. Cambridge University Press, 1994. • Haskellͷ Eq a => a -> a -> Bool • ༗քྔԽ(bounded qualification)Ͱͳ͍
αϒλΠϐϯάͱ ܕڧ੍ҙຯ
ΫΠζ • Swiftʹ͓͍ͯɺT Optional<T>ͷαϒλΠ ϓ͔ʁʁ
T is subtype of Optional<T> • ͑: αϒλΠϓɻ • αϒλΠϐϯάنଇυΩϡϝϯτʹॻ͍
͍ͯͳ͍͕ɺιʔείʔυதͷίϝϯτʹͪΐ ͪ͜ΐ͜ఆٛ͞Ε͍ͯΔɻ(͕ͯ͢ॻ͍ͯ͋ΔΘ͚ Ͱͳ͍) • lib/Sema/CSSimplify.cpp Λࢀর
T is subtype of Optional<T>
αϒλΠϓؔ • ͲΜͳαϒλΠϓؔΛ࣋ͨͤΔ͔ͦͷݴޠͰ ͷܾΊͷ • ྫ͑ Int < Float ͱ͔
Int < String ͱ͔࣋ͨͤͯ (߹ੑ͕औΕΔͷͳΒ) ྑ͍ • SwiftOptionalΛָʹѻ͏Έͱͯ͠ ɺ T < Optional<T>Λ࠾༻͍ͯ͠Δ
ଞʹͲΜͳ αϒλΠϓ͕ؔ͋Δʁ
Optional-to-optional
Metatype
Existential
αϒλΠϓ͔ͩΒ ͳΜͰOKʁ
ྫ: Existential
࣮ߦ࣌ͷϝϞϦ্ͷදݱ͕ ҧ͏Մೳੑ͕͋Δ
͜ͷʹର͢ΔΞϓϩʔν αϒλΠϐϯάͷ࣮ํࣜେ͖͚ͯ͘2ͭ 1. inclusiveͳ࣮ • ڞ௨ͷ࣮ߦ࣌ͷදݱΛ͏ • JavaͳͲ 2.coerciveͳ࣮ •
มॲཧ(ܕڧ੍)ΛڬΈࠐΉ ← Swiftͬͪ͜
ܕڧ੍ҙຯ(coercion semantics) • ؆୯ʹݴ͏ͱɺ̍ͭͷαϒλΠϐϯάنଇʹ͍ͭͯɺ1 ͭͷมॲཧ(ܕڧ੍)͕ଘࡏ͢Δɻ • TypeCheckerɺͲͷαϒλΠϓؔΛͬͯܕνΣο ΫΛ௨͔ͨ͠Λه͓͍ͯͯ͠ɺ͋ͱͰ࣮ࡍͷมॲཧ Λૠೖ͢Δɻ •
lib/Sema/CSApply.cppͷExprRewriterͱ͍͏Ϋϥε Λࢀߟɻ
ྫ: Optionalͷ߹
ྫ: Optionalͷ߹
ྫ: Optionalͷ߹ Constraint restrictions: Int to Optional<Int> is [value-to-optional] $
swift -frontend -typecheck -debug-constraints test.swift
ྫ: Optionalͷ߹ $ swiftc -dump-ast test.swift (inject_into_optional implicit type=‘Optional<Int>’ …
(declref_expr type=‘Int' … ) )
ྫ: 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ͰแΉʯ
Classͷ߹
ྫ: Classͷ߹
ྫ: Classͷ߹ Constraint restrictions: Dog to Animal is [superclass] $
swift -frontend -typecheck -debug-constraints test.swift
ྫ: Classͷ߹ $ swiftc -dump-ast test.swift (derived_to_base_expr implicit type=‘Animal’ …
(declref_expr type=‘Dog' … ) )
ྫ: 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) ͭ·ΓରԠ͢Δܕڧ੍ʮΞοϓΩϟετʯ
ྫ:Existentialͷ߹ աఔলུ (erasure_expr implicit type=‘Animal’ … (declref_expr type=‘Dog' … )
) ରԠ͢Δܕڧ੍ʮଘࡏܕͷύοέʔδԽʯ ( = type erase)
Swiftʹ͓͚Δʮ҉มʯ • Α͘ʮ҉มʯͳͲͱݺΕΔΞϨͷਖ਼ମ ΄΅΄΅αϒλΠϓؔʹج͍ͮͨܕڧ੍ • ҉ม͋Δͱ͜ΖʹαϒλΠϐϯά͋Γ
࣮Λগ͚ͩ͠ݟΔ
αϒλΠϐϯάʙܕڧ੍ • ConstraintSystemͷConstraintRestrictionsʹ ͬͨαϒλΠϓ͕ؔه͞ΕΔ • ExprRewriter::coerceToTypeͰܕڧ੍
ExprRewriter::coerceToType ܕڧ੍(coercion)ͷॲཧΛ͍ͯ͠Δؔ
ExprRewriter::coerceToType ConversionRestrictionKindʹԠͨ͡ॲཧ ͨͱ͑ValueToOptionalͳΒInjectIntoOptionalͱ͍͏ ܕڧ੍Λૠೖ
αϒλΠϐϯά·ͱΊ • αϒλΠϐϯάنଇʹΑͬͯఆٛ͞Ε͍ͯΔ • ͦΕͧΕنଇʹରԠ͢Δܕڧ੍͕ଘࡏ͢Δ • TypeChecker/ConstraintSystemɺͲͷαϒ λΠϓؔΛͬͯܕνΣοΫΛ௨͔ͨ͠Λ֮ ͓͍͑ͯͯɺ͋ͱͰ࣮ࡍͷมॲཧΛૠೖ͢Δɻ
͓ΘΓ