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
410
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
250
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
720
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
Claude Codeを組織的に動かして月400PRを実現した話
happy_ryo
0
270
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
0
150
[DroidKaigi 2026] Bring your own phones to Gradle Managed Devices
f2lk
0
110
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
230
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
110
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
240
Webの地図
yosuke_furukawa
PRO
6
4.1k
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
56
38k
20260828_品質と開発生産性を両立させる、AI時代のE2Eテストの考え方
magicpod
0
200
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
150
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
530
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
1.9k
Featured
See All Featured
Crafting Experiences
bethany
1
320
Building the Perfect Custom Keyboard
takai
2
870
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
202
76k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
690
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
320
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
520
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
660
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
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