what’s new in llvm · 2016. 7. 8. · these are confidential sessions–please refrain from...
TRANSCRIPT
![Page 1: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/1.jpg)
These are confidential sessions–please refrain from streaming, blogging, or taking pictures
#WWDC14
What’s New in LLVM
Session 417 Jim Grosbach Manager, LLVM CPU Compiler Team
Tools
![Page 2: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/2.jpg)
Apple LLVM Compiler
![Page 3: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/3.jpg)
Apple LLVM Compiler
Xcode Static Analyzer
Xcode Code Completion
Swift
Metal
OpenGL
JavaScript FTL
Xcode Objective-C Modernization
LLDB
CoreImage
![Page 4: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/4.jpg)
Tools for Modernization and Performance
![Page 5: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/5.jpg)
New in LLVM
64-bit iOS support
Objective-C modernization tool
User-defined modules
Profile-guided optimization
Vectorizer advancements
JavaScript Fourth Tier LLVM
![Page 6: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/6.jpg)
64-Bit iOS Support
![Page 7: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/7.jpg)
Building for 64-Bit iOS
Included in standard architectures: armv7, arm64
Just rebuild with Xcode 6
Can continue to deploy back to iOS 4.3
iOS Simulator fully supports 64-bit development
Static libraries must be built for arm64, including third-party code
![Page 8: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/8.jpg)
Migration Hints
Function type checking
Objective-C BOOL type
Type size independence
![Page 9: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/9.jpg)
Missing Prototypes
All functions must have a prototype for ARM64
int my_function(int val) { return other_function(val); }
![Page 10: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/10.jpg)
Missing Prototypes
error: implicit declaration of function 'other_function' is invalid in C99 [-Werror,-Wimplicit-function-declaration] return other_function(val); ^
All functions must have a prototype for ARM64
int my_function(int val) { return other_function(val); }
![Page 11: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/11.jpg)
Strict Checking of objc_msgSend
objc_msgSend without a typecast is usually an error
Recommended build setting for strict checking
![Page 12: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/12.jpg)
#include <objc/message.h> void foo(void *object) { !
!
!
objc_msgSend Example
objc_msgSend(object, sel_getUid("foo:"), 5);}
![Page 13: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/13.jpg)
#include <objc/message.h> void foo(void *object) { !
!
!
objc_msgSend Example
error: too many arguments to function call, expected 0, have 3 objc_msgSend(object, sel_getUid("foo:"), 5); ~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
objc_msgSend(object, sel_getUid("foo:"), 5);}
![Page 14: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/14.jpg)
#include <objc/message.h> void foo(void *object) { !
!
!
typedef void (*send_type)(void *, SEL, int); send_type func = (send_type)objc_msgSend; func(object, sel_getUid("foo:"), 5); }
objc_msgSend Example
![Page 15: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/15.jpg)
Objective-C Boolean Type
BOOL is a _Bool in 64-bit iOS and signed char in 32-bit iOS !
int foo(BOOL b) { return b + 1; } !
int value = foo(-1); // 0 in 32-bit code, 2 in 64-bit code
![Page 16: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/16.jpg)
Pointer Casting
Pointer size change can expose latent bugs
int is 32 bits, but pointers are 64 bits
void *foo(int i) { return (void*)i; }
![Page 17: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/17.jpg)
Pointer Casting
Pointer size change can expose latent bugs
int is 32 bits, but pointers are 64 bits
void *foo(int i) { return (void*)i; }
warning: cast to 'void *' from smaller integer type 'int' [-Wint-to-void-pointer-cast] return (void*)i; ^
![Page 18: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/18.jpg)
Size Safe Types for Pointers
intptr_t and uintptr_t for saving pointer values to integers
size_t for array indices
ptrdiff_t for pointer differences
void *foo(intptr_t i) { return (void*)i; }
![Page 19: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/19.jpg)
Structure Layouts
Object sizes can change between 32-bit and 64-bit iOS
For example: On-disk formats, network protocols, etc.
!
struct foo { long a; // 4 bytes in 32-bit iOS, 8 bytes in 64-bit iOS. int b; // 4 bytes always. };
![Page 20: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/20.jpg)
Summary: Building for 64-Bit iOS
Enabled by default
Easy to adopt
Compiler assistance to find and resolve issues
![Page 21: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/21.jpg)
Objective-C Modernization Tool
![Page 22: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/22.jpg)
Modernizations
Property attribute annotations
Designated initializers
instancetype
Objective-C literals
Objective-C subscripting
Enumeration macros
![Page 23: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/23.jpg)
DemoXcode Objective-C modernization tool
![Page 24: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/24.jpg)
Summary: Objective-C Modernization Tool
Many modernizations to the Objective-C language
Xcode modernization tool makes it easy to adopt best practices
![Page 25: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/25.jpg)
User-Defined Modules
Bob Wilson Manager, LLVM Core Team
![Page 26: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/26.jpg)
Precompiled headers speed up compilation, but are not flexible
Textual includes have problems • Multiple inclusion
• Fragile headers
Modules: BackgroundModern alternative to precompiled headers
![Page 27: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/27.jpg)
Precompiled headers speed up compilation, but are not flexible
Textual includes have problems • Multiple inclusion
• Fragile headers
Modules: BackgroundModern alternative to precompiled headers
#define count 100#import <Foundation/Foundation.h>
![Page 28: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/28.jpg)
Precompiled headers speed up compilation, but are not flexible
Textual includes have problems • Multiple inclusion
• Fragile headers
Modules: BackgroundModern alternative to precompiled headers
#define count 100. . . . @interface NSArray : NSObject - (NSUInteger) - (id)objectAtIndex:(NSUInteger)index; @end . . . .
count;
![Page 29: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/29.jpg)
Precompiled headers speed up compilation, but are not flexible
Textual includes have problems • Multiple inclusion
• Fragile headers
Modules: BackgroundModern alternative to precompiled headers
#define count 100. . . . @interface NSArray : NSObject - (NSUInteger) - (id)objectAtIndex:(NSUInteger)index; @end . . . .
100;
![Page 30: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/30.jpg)
Now your C and Objective-C frameworks can define new modules • Import your frameworks into Swift code
• Speed up compilation
• Avoid problems of textual inclusion
Not Just for System Frameworks
![Page 31: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/31.jpg)
Defining a Module
Provide an umbrella header that includes all the framework API
!
!
!
!
!
Custom module maps can describe more complex modules
For more information: http://clang.llvm.org/docs/Modules.html
![Page 32: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/32.jpg)
Importing a Module
Guidelines
• Use @import when importing your framework in another target
• Use #import in the implementation to textually include the framework headers
@import MyFramework; // import the module #import <MyFramework/MyFramework.h> // implicit modular import
![Page 33: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/33.jpg)
Do not expose nonmodular headers in the framework API
Module Rules
@import Cocoa; // OK! !
#import “Postgres.h” // Only in the implementation, not the API
![Page 34: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/34.jpg)
Do not expose nonmodular headers in the framework API
Module Rules
@import Cocoa; // OK! !
#import “Postgres.h” // Only in the implementation, not the API
![Page 35: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/35.jpg)
Modules can change the semantics
Do not expose nonmodular headers in the framework API
Module Rules
#define DEBUG 1 @import MyFramework; // DEBUG ignored inside the framework
@import Cocoa; // OK! !
#import “Postgres.h” // Only in the implementation, not the API
Use macro definitions on the command line (-DDEBUG=1) if necessary
![Page 36: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/36.jpg)
Summary: User-Defined Modules
Modules are the modern alternative to precompiled headers • Fast compilation
• Clear semantics
• Swift interoperability
Define modules for your own frameworks!
![Page 37: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/37.jpg)
Profile Guided Optimization (PGO)
![Page 38: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/38.jpg)
Better Optimization via Profiling
Problem: Compiler has to assume all code paths are equally likely
AppLLVMSourceCode
![Page 39: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/39.jpg)
Better Optimization via Profiling
Problem: Compiler has to assume all code paths are equally likely
Solution: Use profile information to optimize for the common case
AppLLVMSourceCode
Profile
![Page 40: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/40.jpg)
Instrumented App
LLVM
Better Optimization via Profiling
Problem: Compiler has to assume all code paths are equally likely
Solution: Use profile information to optimize for the common case
AppLLVMSourceCode
Profile
![Page 41: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/41.jpg)
How Does Profiling Help?
Inline more aggressively for “hot” functions
Lay out most common code paths contiguously
Better register allocation
![Page 42: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/42.jpg)
void UpdatePosition(ColoredObject *Obj) { if (Obj->Color == Red) { // Red objects just move in a horizontal line. Obj->XCoord += 1; } else if (Obj->Color == Blue) { // Lots of complicated code here. } }
for (ColoredObject *Obj : Objects) UpdatePosition(Obj);
Example: PGO Optimizations
![Page 43: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/43.jpg)
void UpdatePosition(ColoredObject *Obj) { if (Obj->Color == Red) { // Red objects just move in a horizontal line. Obj->XCoord += 1; } else if (Obj->Color == Blue) { // Lots of complicated code here. } }
for (ColoredObject *Obj : Objects) UpdatePosition(Obj);
Example: PGO Optimizations
![Page 44: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/44.jpg)
void UpdatePosition(ColoredObject *Obj) { if (Obj->Color == Red) { // Red objects just move in a horizontal line. Obj->XCoord += 1; } else if (Obj->Color == Blue) { // Lots of complicated code here. } }
for (ColoredObject *Obj : Objects) UpdatePosition(Obj);
Example: PGO Optimizations
![Page 45: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/45.jpg)
void UpdatePosition(ColoredObject *Obj) { if (Obj->Color == Red) { // Red objects just move in a horizontal line. Obj->XCoord += 1; } else if (Obj->Color == Blue) { // Lots of complicated code here. } }
for (ColoredObject *Obj : Objects) UpdatePosition(Obj);
Example: PGO Optimizations
![Page 46: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/46.jpg)
Example: PGO Optimizations
Original codelayout
![Page 47: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/47.jpg)
Example: PGO Optimizations
Original codelayout
Inlining the “hot” function
![Page 48: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/48.jpg)
Example: PGO Optimizations
Original codelayout
Inlining the “hot” function
![Page 49: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/49.jpg)
Example: PGO Optimizations
Original codelayout
Inlining the “hot” function
Optimized codelayout
![Page 50: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/50.jpg)
When Should You Use PGO?
Compiler already does a good job by default
PGO can provide even more improvements • Requires some extra effort to collect a good profile
• Not all apps will benefit
• Worth the effort for very performance-sensitive code!
![Page 51: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/51.jpg)
PGO Performance
![Page 52: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/52.jpg)
PGO Performance
LLVM
SQLite
Perl
Gzip
Percent Speedup
0% 4% 8% 12% 16% 20%
![Page 53: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/53.jpg)
PGO Performance
LLVM
SQLite
Perl
Gzip
Percent Speedup
0% 4% 8% 12% 16% 20%
3.6%
8.8%
16.3%
18.0%
![Page 54: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/54.jpg)
How to Use PGO in Xcode
Step 1: Collect a profile
Step 2: Build with PGO
![Page 55: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/55.jpg)
Compiler warns when the profile needs to be regenerated
How to Use PGO in Xcode
Step 1: Collect a profile
Step 2: Build with PGO
![Page 56: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/56.jpg)
Generating the Optimization Profile
Run Xcode’s “Generate Optimization Profile” command
Xcode will build and run an instrumented version of your app
Important that you exercise all the performance-sensitive code
![Page 57: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/57.jpg)
Profiling with Tests
Can use performance tests to drive the profiling • Reproducible results
• Requires good test coverage
• Tests should combine to reflect overall usage
Easy to evaluate the benefits of PGO
![Page 58: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/58.jpg)
DemoProfile Guided Optimization
![Page 59: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/59.jpg)
Summary: Profile Guided Optimization
Profile data enables better optimization
Be careful to collect good profiles and keep them updated
PGO can provide an extra boost in performance for many apps
![Page 60: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/60.jpg)
Vectorization in LLVM
Nadav Rotem Manager, LLVM Performance Team
![Page 61: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/61.jpg)
Loop Vectorization Overview
Optimization for accelerating loops using vector instructions
A [ ]
temp4
sum
for (int i = 0; i < 256; i++) sum += A[i];
![Page 62: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/62.jpg)
Loop Vectorization Overview
Optimization for accelerating loops using vector instructions
A [ ]
temp4
sum
for (int i = 0; i < 256; i++) sum += A[i];
![Page 63: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/63.jpg)
Loop Vectorization Overview
Optimization for accelerating loops using vector instructions
A [ ]
temp4
sum
for (int i = 0; i < 256; i++) sum += A[i];
![Page 64: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/64.jpg)
Loop Vectorization Overview
Optimization for accelerating loops using vector instructions
A [ ]
temp4
sum
for (int i = 0; i < 256; i++) sum += A[i];
![Page 65: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/65.jpg)
What’s New in the Loop Vectorizer
Better analysis of loops
PGO integration
Improved ARM64 and X86 code generation
Specialization of loop values
![Page 66: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/66.jpg)
for (int i = 0; i < 256; i++) sum += A[i * Step];
Specialization of Loop Values
Most variables computed and known only at runtime
Vectorization often requires constant values
Nonconsecutive
memory access
![Page 67: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/67.jpg)
Loop Specialization
if (Step == 1) { for (i = 0; i < 256; ++i) sum += A[i * 1]; } else { for (i = 0; i < 256; ++i) sum += A[i * Step]; }
Vectorized loop
Original loop
Compiler creates multiple versions of the loop
Selects the correct version at runtime
Check if Step == 1
![Page 68: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/68.jpg)
SLP Vectorization
Superword Level Parallelism (SLP): Parallelism beyond loops
Combine multiple independent scalar calculations
Accelerates general code
![Page 69: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/69.jpg)
SLP Vectorization
struct Point { double x, y; }; !
void FtToCm(Point *P) { P->x *= 30.48; P->y *= 30.48; }
![Page 70: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/70.jpg)
SLP Vectorization
Multiply
Store
Load
Scalarstruct Point { double x, y; }; !
void FtToCm(Point *P) { P->x *= 30.48; P->y *= 30.48; }
![Page 71: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/71.jpg)
SLP Vectorization
Multiply
Store
Load
Multiply
Store
Load
Scalarstruct Point { double x, y; }; !
void FtToCm(Point *P) { P->x *= 30.48; P->y *= 30.48; }
![Page 72: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/72.jpg)
SLP Vectorization
Multiply
Store
Load
Multiply
Store
Load
Scalar Vector
Load
Multiply
Store
struct Point { double x, y; }; !
void FtToCm(Point *P) { P->x *= 30.48; P->y *= 30.48; }
![Page 73: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/73.jpg)
SLP Vectorization Performance Gains
![Page 74: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/74.jpg)
matmul_f64_4x4
Olden power
LoopRerolling
flops6
LinearDependence
nurbs
pi
252_eon
444_namd
0% 15% 30% 45% 60%
4.5%
5.8%
7.3%
8.0%
8.4%
11.0%
14.7%
18.6%
53.7%
SLP Vectorization Performance Gains
![Page 75: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/75.jpg)
Summary: Vectorization
Many improvements to loop vectorizer
New SLP vectorizer
Both vectorizers enabled for optimized builds
![Page 76: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/76.jpg)
Accelerating JavaScript Using LLVM
![Page 77: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/77.jpg)
Uses an interpreter to execute JavaScript
Features two JITs (Just-in-Time compilers) to accelerate JavaScript
Tradeoff between quality of code and time spent in compiler
WebKit Background
InterpreterFast JIT
Optimizing JIT
10 runs 100 runs
![Page 78: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/78.jpg)
JavaScript Is Evolving
Large compute intensive applications in JavaScript
Compile C++ to JavaScript and run in browsers
We need a better JavaScript compiler
![Page 79: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/79.jpg)
+
![Page 80: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/80.jpg)
Fourth Tier LLVM (FTL)
LLVM as a fourth tier compiler
Compile functions that are executed many times
Takes longer to compile, but generates faster code
InterpreterQuick
JITOptimizing
JIT
10 runs 100 runs
![Page 81: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/81.jpg)
Fourth Tier LLVM (FTL)
LLVM as a fourth tier compiler
Compile functions that are executed many times
Takes longer to compile, but generates faster code
InterpreterQuick
JITOptimizing
JIT
10 runs 100 runs
LLVM JIT
1000 runs
![Page 82: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/82.jpg)
Compiling JavaScript Using LLVM
Compiling JavaScript is unlike C
JavaScript is dynamically typed
Type of ’n’?function factorial(n) { if (n === 0) { return 1; } return n * factorial(n - 1); }
![Page 83: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/83.jpg)
Compiler Checks
Use information from previous runs to predict types
Inserts type checks and exception checks
If the checks fail, abort execution and return to interpreter
function factorial(n) { if (n === 0) { return 1; } return n * factorial(n - 1); }
Does not overflow
’n’ is an Int
![Page 84: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/84.jpg)
Continue to interpret from the last valid checkpoint
“On-Stack Replacement” to migrate state to WebKit interpreter
Return to JavaScript Interpreter
InterpreterQuick
JITOptimizing
JITLLVM
JIT
Return to interpreter
![Page 85: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/85.jpg)
FTL Speedups
![Page 86: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/86.jpg)
FTL Speedups
float-mm
dry
quicksort
gcc-loops
towers
bigfib
n-body
container
c-ray
0x 0.5x 1x 1.5x 2x 2.5x
1.2x
1.2x
1.5x
1.6x
1.6x
1.8x
1.9x
1.9x
2.2x
![Page 87: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/87.jpg)
Summary: JavaScript FTL
New Fourth Tier LLVM (FTL) compiler for WebKit
Accelerates compute intensive JavaScript code
![Page 88: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/88.jpg)
Summary
LLVM helps modernize your code • 64-bit iOS support
• Objective-C modernization tool
• User-defined modules
New LLVM optimizations increase performance • Profile Guided Optimization
• Vectorizer advancements
• JavaScript Fourth Tier LLVM
![Page 89: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/89.jpg)
More Information
Dave DeLong Developer Tools Evangelist [email protected]
LLVM Project Open Source LLVM Project http://llvm.org
!
Apple Developer Forums http://devforums.apple.com
![Page 90: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/90.jpg)
Related Sessions
• Integrating Swift with Objective-C Presidio Wednesday 9:00AM
• Testing in Xcode 6 Marina Thursday 9:00AM
![Page 91: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/91.jpg)
Labs
• LLVM Lab Tools Lab B Wednesday 2:00PM
• LLVM Lab Tools Lab B Thursday 2:00PM
![Page 92: What’s New in LLVM · 2016. 7. 8. · These are confidential sessions–please refrain from streaming, blogging, or taking pictures #WWDC14 What’s New in LLVM Session 417 Jim](https://reader035.vdocument.in/reader035/viewer/2022071000/5fbc3894ee194c21af05dfcb/html5/thumbnails/92.jpg)