Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Swiftのリンク時最適化プロジェクト
Search
Yuta Saito
June 18, 2020
Programming
430
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swiftのリンク時最適化プロジェクト
わいわいswiftc #21
https://iosdiscord.connpass.com/event/177407/
Yuta Saito
June 18, 2020
More Decks by Yuta Saito
See All by Yuta Saito
Swift at Scale: Where Performance Really Comes From
kateinoigakukun
0
290
The Evolution of the CRuby Build System
kateinoigakukun
3
1.1k
Running Swift on WebAssembly Platforms
kateinoigakukun
0
250
What you can do with Ruby on WebAssembly
kateinoigakukun
1
2k
RubyGems on ruby.wasm
kateinoigakukun
1
6.2k
Building a Smaller App Binary
kateinoigakukun
2
730
The new linker in Xcode 15
kateinoigakukun
4
4.5k
Optimizing your Swift code
kateinoigakukun
0
2.7k
Ruby & WebAssembly Introduction
kateinoigakukun
0
3.2k
Other Decks in Programming
See All in Programming
Simple Storage Service(S3) is not simple
iwatsukayura
0
250
Embedded Swiftで作る自作USBデバイス によるiOSデバイスの自動テスト / iOSDC Japan 2026 glassfiber
glassfiber
0
330
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
240
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
130
Java 27新機能 / Java 27 new features
kishida
2
220
SREの越境 / SRE Collaboration
y0hgi
2
300
Workers Cache を知る
syumai
0
350
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
330
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
360
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
370
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
Featured
See All Featured
Fireside Chat
paigeccino
43
4.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
WENDY [Excerpt]
tessaabrams
14
40k
Tell your own story through comics
letsgokoyo
1
1.1k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
450
YesSQL, Process and Tooling at Scale
rocio
174
15k
Making Projects Easy
brettharned
120
6.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Bash Introduction
62gerente
615
220k
RailsConf 2023
tenderlove
30
1.6k
Transcript
SwiftͷϦϯΫ࣌࠷దԽϓϩδΣΫτ Θ͍Θ͍swiftc #21 @kateinoigakukun 1
Swiftͷ࠷దԽϨϕϧ • ϑΝΠϧ୯Ґ ( -O ) • Ϟδϡʔϧ୯Ґ ( -whole-module-optimization
) http://llvm.org/devmtg/2016-10/slides/GroffLattner-SILHighLevelIR.pdf 2
Swiftͷ࠷దԽϨϕϧ • ϑΝΠϧ୯Ґ ( -O ) • Ϟδϡʔϧ୯Ґ ( -whole-module-optimization
) • (New ➕ ) ϓϩάϥϜશମ at LLVMϨϕϧ • (New ➕ ) ϓϩάϥϜશମ at SILϨϕϧ 3
LTO (Link Time Optimization) ίϯύΠϥͰͳ͘ɼϦϯΧ͕࣮ߦ͢Δ࠷దԽɽϓϩάϥϜશ ମΛҰͭͷ୯Ґͱͯ͠࠷దԽ͢Δɽ 4
LTO (Link Time Optimization) ϝϦοτ • όΠφϦͷґଘͯ͢ͷใΛ͑Δ • ϦϯΧ෦ͷγϯϘϧςʔϒϧΛ࠶ར༻Ͱ͖Δ 5
LLVM Ϩϕϧ LTO ΦϒδΣΫτϑΝΠϧͰͳ͘ɼLLVMϏοτίʔυΛೖྗϑΝ Πϧͱͯ͠ड͚͚Δɽ 6
LLVM Ϩϕϧ LTO ಛ • நͷߴ͍࠷దԽ • LLVMͷ࠷దԽύεΛ࠶ར༻ 7
ྫ͑ // LibX.swift public func unusedPublicFunc() {} public func publicFunc()
{} // main.swift import LibX publicFunc() 8
// LibX.ll define swiftcc void @"$s4LibX16unusedPublicFuncyyF"() #0 { entry: ret
void } define swiftcc void @"$s4LibX10publicFuncyyF"() #0 { entry: ret void } // main.ll define i32 @main(i32 %0, i8** %1) #0 { entry: %2 = bitcast i8** %1 to i8* call swiftcc void @"$s4LibX10publicFuncyyF"() ret i32 0 } 9
// LibX.ll define swiftcc void @"$s4LibX16unusedPublicFuncyyF"() #0 { entry: ret
void } define swiftcc void @"$s4LibX10publicFuncyyF"() #0 { entry: ret void } // main.ll define i32 @main(i32 %0, i8** %1) #0 { entry: %2 = bitcast i8** %1 to i8* call swiftcc void @"$s4LibX10publicFuncyyF"() ret i32 0 } 9
LLVM Ϩϕϧͩͱ͍͠࠷దԽ • ݴޠʹґଘͨ͠ϝλσʔλͷऔΓѻ͍ • ܕύϥϝʔλͷεϖγϟϥΠζ • ؔςʔϒϧʹࡌͬͨؔͷDFE (Dead Function
Elimination) 10
ྫ͑ // LibX.swift public protocol P { func foo() func
unusedMethod() } public struct S: P { public func foo() {} public func unusedMethod() {} } // main.swift import LibX func useP<T: P>(_ value: T) { value.foo() } useP(S()) 11
// LibX.ll @"$s4LibX1SVAA1PAAWP" = constant [3 x i8*] [ ...,
i8* bitcast (void (%T4LibX1SV*, %swift.type*, i8**)* @"$s4LibX1SVAA1PA2aDP3fooyyFTW" to i8*), i8* bitcast (void (%T4LibX1SV*, %swift.type*, i8**)* @"$s4LibX1SVAA1PA2aDP12unusedMethodyyFTW" to i8*) ], align 8 define linkonce_odr hidden swiftcc void @"$s4LibX1SVAA1PA2aDP3fooyyFTW"(...) { ... } define linkonce_odr hidden swiftcc void @"$s4LibX1SVAA1PA2aDP12unusedMethodyyFTW"(...) { ... } // main.ll define i32 @main(i32 %0, i8** %1) #0 { entry: ... call swiftcc void @"$s4main4usePyyx4LibX1PRzlF"( %swift.opaque* noalias nocapture undef, %swift.type* @"$s4LibX1SVN", i8** @"$s4LibX1SVAA1PAAWP" ) ret i32 0 } define hidden swiftcc void @"$s4main4usePyyx4LibX1PRzlF"(%swift.opaque* noalias nocapture %0, %swift.type* %T, i8** %T.P) { entry: %1 = getelementptr inbounds i8*, i8** %T.P, i32 1 %2 = load i8*, i8** %1, align 8, !invariant.load !14 %3 = bitcast i8* %2 to void (%swift.opaque*, %swift.type*, i8**)* call swiftcc void %3(%swift.opaque* noalias nocapture swiftself %0, %swift.type* %T, i8** %T.P) ret void } 12
// LibX.ll @"$s4LibX1SVAA1PAAWP" = constant [3 x i8*] [ ...,
i8* bitcast (void (%T4LibX1SV*, %swift.type*, i8**)* @"$s4LibX1SVAA1PA2aDP3fooyyFTW" to i8*), i8* bitcast (void (%T4LibX1SV*, %swift.type*, i8**)* @"$s4LibX1SVAA1PA2aDP12unusedMethodyyFTW" to i8*) ], align 8 define linkonce_odr hidden swiftcc void @"$s4LibX1SVAA1PA2aDP3fooyyFTW"(...) { ... } define linkonce_odr hidden swiftcc void @"$s4LibX1SVAA1PA2aDP12unusedMethodyyFTW"(...) { ... } // main.ll define i32 @main(i32 %0, i8** %1) #0 { entry: ... call swiftcc void @"$s4main4usePyyx4LibX1PRzlF"( %swift.opaque* noalias nocapture undef, %swift.type* @"$s4LibX1SVN", i8** @"$s4LibX1SVAA1PAAWP" ) ret i32 0 } define hidden swiftcc void @"$s4main4usePyyx4LibX1PRzlF"(%swift.opaque* noalias nocapture %0, %swift.type* %T, i8** %T.P) { entry: %1 = getelementptr inbounds i8*, i8** %T.P, i32 1 %2 = load i8*, i8** %1, align 8, !invariant.load !14 %3 = bitcast i8* %2 to void (%swift.opaque*, %swift.type*, i8**)* call swiftcc void %3(%swift.opaque* noalias nocapture swiftself %0, %swift.type* %T, i8** %T.P) ret void } 12
// LibX.ll @"$s4LibX1SVAA1PAAWP" = constant [3 x i8*] [ ...,
i8* bitcast (void (%T4LibX1SV*, %swift.type*, i8**)* @"$s4LibX1SVAA1PA2aDP3fooyyFTW" to i8*), i8* bitcast (void (%T4LibX1SV*, %swift.type*, i8**)* @"$s4LibX1SVAA1PA2aDP12unusedMethodyyFTW" to i8*) ], align 8 define linkonce_odr hidden swiftcc void @"$s4LibX1SVAA1PA2aDP3fooyyFTW"(...) { ... } define linkonce_odr hidden swiftcc void @"$s4LibX1SVAA1PA2aDP12unusedMethodyyFTW"(...) { ... } // main.ll define i32 @main(i32 %0, i8** %1) #0 { entry: ... call swiftcc void @"$s4main4usePyyx4LibX1PRzlF"( %swift.opaque* noalias nocapture undef, %swift.type* @"$s4LibX1SVN", i8** @"$s4LibX1SVAA1PAAWP" ) ret i32 0 } define hidden swiftcc void @"$s4main4usePyyx4LibX1PRzlF"(%swift.opaque* noalias nocapture %0, %swift.type* %T, i8** %T.P) { entry: %1 = getelementptr inbounds i8*, i8** %T.P, i32 1 %2 = load i8*, i8** %1, align 8, !invariant.load !14 %3 = bitcast i8* %2 to void (%swift.opaque*, %swift.type*, i8**)* call swiftcc void %3(%swift.opaque* noalias nocapture swiftself %0, %swift.type* %T, i8** %T.P) ret void } 12
SIL Ϩϕϧ LTO 13
SIL Ϩϕϧ LTO 14
SIL Ϩϕϧ LTO ͷ Ϟνϕʔγϣϯ • LLVM IRΑΓநͷߴ͍࠷దԽ • SwiftͷηϚϯςΟΫεΛҡ࣋ͨ͠··LTO͍ͨ͠
• Ͳ͏ͤ੩తϦϯΫ͢ΔͳΒɺABI resiliencyΛແࢹͨ͠࠷దԽ Λ͍ͨ͠ɻ • ࣮ߦ࣌ύϑΥʔϚϯεͷվળͱόΠφϦαΠζͷॖখΛظ 15
࠷దԽϙΠϯτ (ະ࣮) 16
1. Cross Module Devirtualization 17
https://speakerdeck.com/kateinoigakukun/konpairakaraniu-jie-kuswift-method-dispatch-1?slide=39 18
https://speakerdeck.com/kateinoigakukun/konpairakaraniu-jie-kuswift-method-dispatch-1?slide=55 19
20
Cross Module Devirtualization • ͯ͢ͷϞδϡʔϧ͕LTOରͳΒɺϝιου͕Φʔόʔϥ Πυ͞Ε͍ͯΔ͔ඞ͔ͣΔ • ࣮࣭finalೝఆͰ͖Δݺͼग़͕͠૿͑Δ • ABI
resiliencyͷͨΊͷςʔϒϧܦ༝ݺͼग़͠Λ੩తݺͼग़͠ ʹͰ͖Δɻ 21
// LibX.swift open class A { public func foo1() {}
open func foo2() {} } public func invokeFoo1InModule(_ a: A) { // ͜Εݱঢ়ͷ࠷దԽύεͰdevirtͰ͖Δ a.foo1() } // main.swift import LibX func invokeFoo1(_ a: A) { // [LTO] ֎෦Ϟδϡʔϧͷ͕ؔͩɺABI resiliencyແࢹͰdevirtͰ͖Δɻ a.foo1() // [LTO] ֎෦Ϟδϡʔϧͷ͕ؔͩɺ୭override͍ͯ͠ͳ͍ͷͰdevirtͰ͖Δɻ a.foo2() } 22
2. Cross Module Specialization 23
Cross Module Specialization • ϞδϡʔϧΛ·͍ͨͩεϖγϟϥΠζ • ABI resiliencyΛແࢹͯ͠ɺશͯʹ@inlinable ͕͍͍ͭͯΔ Πϝʔδ
• όΠφϦαΠζͷංେԽ͕৺ 24
// LibX.swift public protocol Animal { func bark() } public
struct Cat: Animal { func bark() { } } // main.swift import LibX public callBark<T: Animal>(_ animal: T) { animal.bark() } 25
3. Dead Table Elimination 26
Dead Table Elimination • ௨ৗɺpublicͳVTableProtocol Witness Tableৗʹग़ྗ͞ ΕΔ • LTOͷ߹ɺΫϩεϞδϡʔϧͰΘΕ͍ͯΔ߹͚ͩग़ྗ
͢Ε͍͍ɻ • devirtͱεϖγϟϥΠζͰςʔϒϧͷࢀর͕ফ͑Δͱɺ͜ͷ ύε͕ΑΓޮՌతʹͳΔ 27
4. Dead public Function Elimination 28
Dead public Function Elimination • ௨ৗͷDFEinternalͳ͕ؔର • LTOͷ߹ɺpublicؔফͤΔ • ࣮ࡍʹDead
Table Eliminationͱಉ࣌ʹΔ • main͔ؔΒґଘ͢ΔؔΛ෯༏ઌ୳ࡧͰϚʔΫ͍ͯ͘͠ 29
࡞ۀਐḿ िใΛϑΥʔϥϜʹॻ͍ͯΔ https://forums.swift.org/t/gsoc-lto-support-progress-report/ 37149 30
LTO͕ಛʹخ͍͠໘ 31
stdlibΛؙ͝ͱ੩తϦϯΫ͢ΔͨΊɺ͍ͬͯͳ͍ػೳόΠφ Ϧʹೖͬͯ͠·͏ɻ Hello, worldͰ͢Β10MBɻ·͞ʹLTOͷग़൪ 32