what’s new in llvm · 2016. 7. 8. · these are confidential sessions–please refrain from...

92
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

Upload: others

Post on 16-Aug-2020

5 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

#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

#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

#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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

+

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

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

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

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

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

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

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

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

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

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

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

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

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