차세대컴파일러, vm의미래: 애플 오픈소스 llvm

75
twitter.com/@godrm 차세대 컴파일러 VM 미래 애플 오픈소스 프로젝트 LLVM facebook.com/godrm

Upload: jung-kim

Post on 06-May-2015

2.357 views

Category:

Technology


3 download

DESCRIPTION

JCO 컨퍼런스에서 발표했던 자료입니다. 애플이 만드는 오픈소스 LLVM 프로젝트가 왜 차세대 컴파일러인지 소개합니다. Java와 여러 다른 언어 사이에서 밀회를 즐길 수 있는 LLVM 활용 사례들을 공개합니다. LLVM과 함께 새로운 경험을 해보실 수 있습니다.

TRANSCRIPT

Page 1: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

twitter.com/@godrm

차세대 컴파일러 VM의 미래애플 오픈소스 프로젝트 LLVM

facebook.com/godrm

Page 2: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

"��,� �#�JCO!���+-��

��

(���)*'���

��$�����

Page 3: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Mac OS XiOS의 아버지MacOS X

Page 4: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Father of OS X

Page 5: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Java vs. Objective-C

�7�.8�89637�498��!*&3�&3)�"42�&6*�'48-�&'74198*1=�(466*(8��#79&11=��8-.7�0.3)�4+�96'&3�1*,*3)�789++�89637�498�84�'*�(4251*8*1=�.3&((96&8*��'98�.3�8-.7�(&7*��8-*=�&6*�6.,-8�43��$-*3���1*+8�!93�84�,4�84��*%"����8-49,-8��'/*(8.:*���;&7�8-*�(441*78�8-.3,�7.3(*�71.(*)�'6*&)��&3)���-&8*)������!4��3&896&11=�;-*3���78&=*)�84�78&68�8-*��*:*389&11=���&:&�564/*(8���'/���-&)�&�'.,�.3+19*3(*���&2*7��471.3,��'*.3,�29(-�41)*6�8-&3���;&7��-*�-&)�1487�4+�*<5*6.*3(*�;.8-�!2&11"&10�&3)�!.291&�����;-.(-�;*�&174�'4664;*)�

+642�1.'*6&11=�!

"-*�48-*6�.3+19*3(*��;&7�8-&8�;*�-&)�1487�4+�+6.*3)7�;460.3,�&8��*%"�&8�8-*�8.2*��;-47*�+&.8-�.3�8-*�'1&(0�(9'*�;&7�+1&,,.3,���69(*��&68.3�;&7�;460.3,�43�8-*��*%"!8*5�����5468���*8*6��.3,���.0*��*243*=��&3)��4-3�

!*&2437�;*6*�;460.3,�43�8-*�2=78*6.497��&3)�3*:*6�7-.55*)��� $���*%"� �!��$46078&8.43������������"-*=�&11�/4.3*)�97�.3�1&8*�������*&61=��� �&+8*6�;*�-&)�;6.88*3�8-*�+.678�:*67.43�4+��&0����2�56*88=�796*�8-&8��&:&�7��.38*6+&(*��.7�&�).6*(8�6.5�4++�4+��'/���7��56484(41��;-.(-�;&7�1&6,*1=�

)*7.,3*)�'=�8-*7*�*<��*%"�*67��&3=�4+�8-47*�786&3,*�56.2.8.:*�;6&55*6�(1&77*7��1.0*��38*,*6�&3)��92'*6�(&2*�+642��**��4=3843��43*�4+�8-*�*&61=�

�*%"��'/���(1&77�1.'6&6=�,9=7�;-4�-&8*)��.38��&3)��+14&8��8=5*7�!

�>@?>�

- Patrick Naughton

Page 6: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

JVM vs Objective-C Runtime

운영체제

하드웨어

JVM

Java App.

Java App.

Runtime

Obj-C App.

Obj-C App.

Page 7: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
Page 8: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

OS X Open-sourceshttp://opensource.apple.com/ http://www.apple.com/opensource/ https://developer.apple.com/opensource/

Page 9: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

� !������&��

����

)*'�����

��%����

LLVM

Page 10: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

아이디어의 시작은…

• JVMs do all optimizations online at JIT time: - Hugely redundant across runs - Applications launch slowly - What if we could do heavy lifting at install time? !

• Problem: Java bytecode is too limiting! - Memory safety prevents some optzns (eg. bounds checks) - JVM type system doesn’t lend itself to machine optzns

Page 11: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

With some sort of low level virtual machine, we could optimize better and a JIT compiler

would have to do less work online!

Page 12: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Introduction• LLVM

- Low-Level Virtual Machine• An Infrastructure for Multi-stage Optimization

- by Chris Arthur Lattner @2002• Design and Implementation of a compiler infrastructure

- support a unique multi-stage optimization system - support inter-procedural and profile-driven optimizations

• LLVM virtual instruction (IR) - with high-level type information

• Sponsored by APPLE

Page 13: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLVM Vision and Approach• Primary mission: Build a set of modular compiler components:

- Reduces the time & cost to construct a particular compiler

- Components are shared across different compilers

- Allows choice of the right component for the job• Secondary mission: Build compilers out of these components

X86 PPC CBE clang GCC LTO

BC IO LL IO System Core Support xforms analysis GC

DWARF Target Code gen JIT Optzn linker IPO ...

Page 14: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Authors• Vikram Adve

- At University of Illinois• Chris Lattner

- LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation @2004

- Macroscopic Data Structure Analysis and Optimization @2005 Ph.D Thesis

- work for Apple from 2007• Related Publications

- 15 < @2007, 30 ~ @2008, 50 ~ @2009, 30 ~ @2010

Page 15: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

osxdev.org

LLVM Timeline

LLVM 1.0 @Oct. 2003 - LTO/LTA

LLVM 1.4 @Dec. 2004

LLVM 2.0 @May. 2007LLVM 2.4 @Nov. 2008

LLVM 2.6 @Oct. 2009Clang C @Oct. 2006

Open @July. 2007

WWDC @June. 2008

LLVM 2.8 @Oct. 2010LLVM 3.0 @Dec. 2011

LLVM 3.2 @Dec. 2012

LLVM 3.4 @Jan. 2014

Page 16: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

osxdev.org

Compiler ArchitecturesGCC#4.2#

프론트엔드#

C"Tree*SSA#최적화기# 코드 생성기#C++"

Objec)ve+C"실행파일"

GCC"4.2"

GCC#4.2#프론트엔드#

C"LLVM#

최적화기#LLVM#

코드 생성기#C++"

Objec)ve+C"실행파일"

LLVM+GCC"4.2"

Clang#프론트엔드#

C"LLVM#

최적화기#LLVM#

코드 생성기#C++"

Objec)ve+C"실행파일"

LLVM"

Page 17: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

The Architecture

Compile Time

Link Time Run-Time

Compiler FE 1

Compiler FE N

.. .o files

LLVM

LLVM

Linker IPO / IPA

Native CodeGen

Libraries

exeLLVM

LLVM

LLVM

exe & LLVM

JIT

CPU

Runtime Optimizer

Offline Reoptimizer

LLVM

LLVM

exe

exeProfile & Trace Info

Profile Info

Page 18: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLVM JIT

운영체제

하드웨어

LLVM JIT

LLVM bytecode

LLVM bytecode

exe. exe.

Page 19: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Virtual Instruction Set

for ( i=0; i<N; ++i) sum(&A[i], &P);

loop: %i.1 = phi int [ 0, %bb0 ], [ %i.2, %loop ] %AiAddr = getelementptr float* %A, int %i.1 call void %sum(float %AiAddr, %pair* %P) %i.2 = add int %i.1, 1 %tmp.4 = setlt int %i.1, %N br bool %tmp.4, label %loop, label %outloophttp://llvm.org/docs/LangRef.html

Page 20: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLVM Demo#include <stdio.h> #include <stdlib.h> !int factorial(int X) { if (X == 0) return 1; return X*factorial(X-1); } !int main(int argc, char **argv) { printf("%d\n", factorial(atoi(argv[1]))); }

http://llvm.org/demo

Page 21: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Effective LLVM ProjectsXcode

OpenCL

OpenGL Optimize (speed)

Optimize (size)

Dynamic Prog.Clang

LLDB LLVM

Page 22: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Summary of the optimization• Analysis Passes (~50)

- Basic Alias Analysis - Basic CallGraph construction - Count alias analysis query response - Dominator Tree Construction - Counts the various types of Instructions - Loop Dependence Analysis - Track pointer bounds

• Transform Passes (~70) - Remove redundant conditional branches - Aggressive Dead Code Elimination - Dead Code Elimination - Deduce function attributes - Unroll loops - Optimize use of memcpy and friends - Strip debug info for unused symbols

• Utility Passes (~10) - Dead Argument Hacking - View CFG of function

http://llvm.org/docs/Passes.html

Page 23: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Optimization Example - IPO

Page 24: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Optimization Example - IPO

Page 25: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Compile Time Performance

• Problem: System headers are huge! - Carbon.h contains:

✴ 558 files ✴ 12.3 megabytes ✴ 10,000 function declarations ✴ 2000 structure definitions, 8000 fields ✴ 3000 enum definitions, 20000 enum constants ✴ 5000 typedefs ✴ 2000 file scoped variables ✴ 6000 macros

Page 26: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

AppKit Framework Compile TimeAppKit Framework Compile Times

-Os -O0 -g

3.9

16.0

4.2

24.5

Co

mp

ile

Tim

e (

min

ute

s)

GCC 4.2LLVM-GCC

35% Faster 5% Faster

20

Page 27: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Full Build Speedup (Debug Option)Full Build Speedup (Debug Config)

0x

1x

2x

3x

Ad

dre

ssBo

ok

Au

tom

ato

r

Da

shco

de

Inte

rface

Bu

ilde

r

Me

ssag

e (M

ail)

Pre

vie

w

Ap

ple

Scrip

t Ed

it

iCa

l

Xco

de

Ad

ium

Gro

wl

Bla

stAp

p

Sk

etch

2.5x

1.9x2.0x

1.6x

2.1x

1.8x

2.2x

1.8x1.9x

2.1x

1.4x

2.2x2.3x

Full Build is 2x Faster!

43

Page 28: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Clang Front-end• C, Objective-C and C++ front-end • Aggressive project with many goals...

- Compatibility with GCC - Fast compilation - Expressive error messages (static analysis) !

!

!

• Host for a broad range of source-level tools

t.c:6:49: error: invalid operands to binary expression (‘int’ and ‘struct A’)! return intArg + func(intArg ? ((someA.X+40) + someA) / 42 : someA.X));! ~~~~~~~~~~~~~ ^ ~~~~~

http://llvm.org/devmtg/2008-08/Kremenek_StaticAnalyzer.pdf

Page 29: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Clang Front-end• C, Objective-C and C++ front-end • Aggressive project with many goals...

- Compatibility with GCC - Fast compilation - Expressive error messages (static analysis) !

!

!

• Host for a broad range of source-level tools

t.c:6:49: error: invalid operands to binary expression (‘int’ and ‘struct A’)! return intArg + func(intArg ? ((someA.X+40) + someA) / 42 : someA.X));! ~~~~~~~~~~~~~ ^ ~~~~~

http://llvm.org/devmtg/2008-08/Kremenek_StaticAnalyzer.pdfImproving Your Application with the Xcode Static Analyzer North Beach

Tuesday 5:00PM

Analyzer Xcode Integration

60

Page 30: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Better Diagnosis of the ProblemBetter Diagnosis of the ProblemGCC 4.2

Clang

test.m:4:1: error: unknown type name 'NSString'

NSString *P = @"good stuff";

^

49

Page 31: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

libclang• Clang is not just a great compiler...

- also a library for processing source code ✴ Translates text into AST ✴ Resolves identifiers and symbols ✴ Expands macros ✴ Makes implicit information explicit

• Features - Parsing - Indexing and cross-referencing - Syntax highlighting - Code completion

NEW

Page 32: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

libclang• Clang is not just a great compiler...

- also a library for processing source code ✴ Translates text into AST ✴ Resolves identifiers and symbols ✴ Expands macros ✴ Makes implicit information explicit

• Features - Parsing - Indexing and cross-referencing - Syntax highlighting - Code completion

NEW

Page 33: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

osxdev.org

ARC• Automatic Reference Counting

- Automatic memory management of Objective-C objects - Just Compile-time, Not Run-time

• Not Garbage-Collector • Migration Tool in Xcode 4.2

- with LLVM 3.0 - build-settings : -fobjc-arc (cf. -fno-objc-arc)

• New Rules - remove dealloc, retain/release/autorelease

✴ can still use CFRetain / CFRelease in CF

- Can’t use NSAllocateObject / NSDeallocateObject - Can’t use object pointer in C Structures - no casual casting id -> void* - Can’t use NSAutoreleasePool -> @autoreleasepool - Can’t use memory zone (NSZone) - Can’t give a property name with new-

Automatic Reference Counting

NEW

Page 34: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

libc++• Another C++ standard library?

- http://libcxx.llvm.org • The C++0x spec introduces several fundamentally new ideas - Move semantics - Perfect forwarding - Variadic templates

• New language features - C++03 implementation from the beginning - driven several low-level design decisions.

NEW

Page 35: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

DragonEgg• gcc plugin

- replaces gcc’s optimizers and code generators - reimplementation of llvm-gcc. with gcc-4.5 or later

• Current Status (v2.8) - C works well, C++ works fairly well - can compile a small amount of Obj-C/C++ - Limited debug info - Requires patching gcc - Only supports x86-32/64 - Only supports linux/darwin

ex) gcc hello.c -S -O1 -o -fplugin=./dragonegg.so

NEW

Page 36: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
Page 37: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLDB• Next-generation!• & High-performance Debugger!• a set of reusable components in LLVM!

• Clang expression parser!• LLVM disassembler!

• C/C++, Objective-C!• Efficient Multi-threading, symbol manager!• Extension - Python script!• Support Remote protocol/debug server

NEW

Page 38: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

ReusabilityExtensibility

LLDB Core

LLDB.framework

Xcode 4 Python

ObjectContainers

DisassemblyProcess

Mac OS X

GDB Remote

Object FIles

Mach-O

ELF

Universal

BSD Archive

Symbols

DWARF

ObjectFile

DynamicLoader

Darwin LLVM

lldb

ReusabilityExtensibility

LLDB Core

LLDB.framework

Xcode 4 Python

ObjectContainers

DisassemblyProcess Object FIles SymbolsDynamicLoader

lldb

ReusabilityExtensibility

LLDB Core

LLDB.framework

Xcode 4 Python

ObjectContainers

DisassemblyProcess Object FIles SymbolsDynamicLoader

lldb

ReusabilityExtensibility

LLDB Core

LLDB.framework

Xcode 4 Python

ObjectContainers

DisassemblyProcess Object FIles SymbolsDynamicLoader

lldb

ReusabilityExtensibility

LLDB Core

LLDB.framework

Xcode 4 Python

ObjectContainers

DisassemblyProcess Object FIles SymbolsDynamicLoader

lldb

Reusability

Page 39: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

IntroductionGDB

LLDB

% gdb a.out (gdb) break main Breakpoint 1 at 0x100000f33:file main.c line4 (gdb) run

% lldb a.out (lldb) breakpoint set --name main Breakpoint created:1:name=‘main’, locations=1 (lldb) process launch

Page 40: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

IntroductionGDB

LLDB

(gdb) info args argc = 1 argv = (const char **) 0x7fff5fbff550 (gdb) info locals i = 32767

(lldb) frame variable argc = 1 argv = 0x00007fff5fbfff68 i = 0

Page 41: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Expression in LLDBLLDB

(lldb) expression x+y->getCount() (int) $0 = 2 (lldb) expression pt (struct point_tag) $1 = { (int) x = 2 (int) y = 3 } (lldb) expression $1.x (int) $2 = 2

Page 42: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLDB Command Syntax

Command Syntax <noun> <verb> [-options [option-value]] [argument [argument...]]

Uses standard getopt_long() for predicate behavior (lldb) process launch a.out --stop-at-entry (lldb) process launch a.out -- --arg0 --arg1 (lldb) process launch a.out -st

Options know which other options they are compatible with (lldb) process attach --pid 123 --name a.out

Page 43: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Common CommandsGDB LLDB

(gdb) ^C (gdb) signal 2 (gdb) info break (gdb) continue (gdb) step (gdb) stepi (gdb) next (gdb) nexti (gdb) finish (gdb) info threads (gdb) backtrace

(lldb) process interrupt (lldb) process signal SIGINT (lldb) breakpoint list (lldb) process continue (lldb) thread step-in (lldb) thread step-inst (lldb) thread step-over (lldb) thread step-over-inst (lldb) thread step-out (lldb) thread list (lldb) thread backtrace

Page 44: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Common CommandsGDB LLDB

(gdb) ^C (gdb) signal 2 (gdb) in br (gdb) c (gdb) s (gdb) si (gdb) n (gdb) ni (gdb) f (gdb) info threads (gdb) bt

(lldb) pro int (lldb) pro s SIGINT (lldb) br l (lldb) c (lldb) s (lldb) si (lldb) n (lldb) ni (lldb) f (lldb) th l (lldb) bt

Page 45: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Java��LLVM ��������&��

�����

Page 46: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Call for help

http://llvm.org/

Call for help!

Python

Common Dynamic Language Representation + Type Inference

Ruby C, C++, Ada, ...

– OSS community needs to unite work on various scripting languages

– Common module to represent/type infer an arbitrary dynamic language

– Who will provide this? pypy? parrot? llvm itself someday (“hlvm”)?

Perl Javascript ...

llvm-gcc

LLVM

OpenGL

GLSL, ARB VP, ...

What Next?

LTO JIT Install TimeCodegenIPO

Cross LangOptzn

DebuggerSupport

Page 47: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLVM sub-projects• Clang & LLDB• OpenMP• Compiler-rt

- provides highly tuned implementations of the low-level code generator. cf. libgcc

• VMKit- is an implementation of the Java and .NET Virtual

Machines• KLEE

- implements a "symbolic virtual machine" - which uses a theorem prover to try to evaluate all

dynamic paths through a program

Page 48: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLVM sub-projects• Polly

- implements a suite of cache-locality optimizations as well as auto-parallelism and vectorization using a polyhedral model.

• libclc- implement the OpenCL standard library.

• SAFECode- is a memory safety compiler for C/C++ programs. - It instruments code with run-time checks to detect

memory safety errors (ex. buffer overflows) at run-time.• LLD (Linker)• ... to be continue ...

Page 49: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

VMKit: a substrate for virtual machines

운영체제

하드웨어

LLVM JIT

ClasspathMMTk Mono Pnetlib

POSIX

http://vmkit.llvm.org/tuto/VMKit_pres_eng.pdf

Page 50: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLJVM

운영체제

하드웨어

JVM

LLJVM Runtime LibraryLLVM bytecode

http://da.vidr.cc/projects/lljvm/

Page 51: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLVM�����!�%�#�� ���

��

Page 52: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

LLVM use in Open Source OSes

• Minix moved to Clang as default compiler• FreeBSD is working on ClangBSD• LLVM a hard dependency for Gallium3D• Building Debian with Clang• Unsupported GCC Flags / C Extensions

출처 : http://llvm.org/devmtg/2012-04-12/Slides/Mark_Charlebois.pdf

Page 53: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Use-case #1 - New Compiler• Cling - CERN • CtoVerilog - Haifa University • OpenCL - AMD • Click - Ericsson • EDGtoLLVM - ARM • Delphi XE- Embarcadero • Jaguar - Cray

Page 54: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

OpenCL

Page 55: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

OpenCL

Page 56: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

OpenGL

http://llvm.org/

Traditional OpenGL Impl - Before LLVM

• Custom JIT for X86-32 and PPC-32:– Very simple codegen: Glued chunks of Altivec or SSE code– Little optimization across operations (e.g. scheduling)– Very fragile, hard to understand and change (hex opcodes)

• OpenGL Interpreter:– JIT didn’t support all OpenGL features: fallback to interpreter– Interpreter was very slow, 100x or worse than JIT

OpenGLParser

GLSLText

Interpreter

GFX Card

Custom JIT

OpenGLAST

LLVM 이전까지는…

Page 57: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

OpenGL

http://llvm.org/

Traditional OpenGL Impl - Before LLVM

• Custom JIT for X86-32 and PPC-32:– Very simple codegen: Glued chunks of Altivec or SSE code– Little optimization across operations (e.g. scheduling)– Very fragile, hard to understand and change (hex opcodes)

• OpenGL Interpreter:– JIT didn’t support all OpenGL features: fallback to interpreter– Interpreter was very slow, 100x or worse than JIT

OpenGLParser

GLSLText

Interpreter

GFX Card

Custom JIT

OpenGLAST

LLVM 이전까지는…

http://llvm.org/

OpenGL to LLVM: At runtime1.Translate OpenGL AST into LLVM call instructions: one per operation

2.Use the LLVM inliner to inline opcodes from precompiled bytecode

3.Optimize/codegen as before

...

vec3 viewVec = normalize(-ecPosition);

float diffuse = max(dot(lightVec, tnorm), 0.0);

...

...

%tmp1 = call opengl_negate(ecPosition)

%viewVec = call opengl_normalize(%tmp1);

%tmp2 = call opengl_dot(%lightVec, %tnorm)

%diffuse = call opengl_max(%tmp2, 0.0);

...

OpenGLto LLVM

LLVM Inliner ...

%tmp1 = sub <4 x float> <0,0,0,0>, %ecPosition

%tmp3 = shuffle <4 x float> %tmp1, ...;

%tmp4 = mul <4 x float> %tmp3, %tmp3

...

Optimize,Codegen

PPC X86

OpenGLParser

GLSLOpenGL to

LLVMLLVM

OptimizerLLVM

JITLLVM IR LLVM IROpenGL

AST

Page 58: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Use-case #2 - Optimization

Flash ActionScript (.as3)➔ ActionScript Bytecode (.abc)➔ LLVM Bytecode (.bc)➔.abc ➔ .swf

Page 59: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Alchemy Project

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 10

ActionScript3 + LLVM?

� Simple AS3 function

function CopyMatrix(B:Array, A:Array):void{

var M:uint = A.length;var N:uint = A[0].length;

var remainder:uint = N & 3; // N mod 4;

for (var i:uint=0; i<M; i++){

var Bi:Array = B[i];var Ai:Array = A[i];for (var j:uint=0; j<remainder; j++)

Bi[j] = Ai[j];for (j=remainder; j<N; j+=4){

Bi[j] = Ai[j];Bi[j+1] = Ai[j+1];Bi[j+2] = Ai[j+2];Bi[j+3] = Ai[j+3];

}}

}

Page 60: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 11

ActionScript3 + LLVM?

� As ABC

function CopyMatrix(Array,Array):void /* disp_id=45 method_id=0 */{// local_count=10 max_scope=1 max_stack=5 code_len=2100 getlocal0 1 pushscope2 pushnull3 coerce Array5 setlocal 77 pushnull8 coerce Array10 setlocal 812 pushbyte 014 convert_u15 setlocal 917 getlocal2 18 getproperty length20 convert_u21 setlocal3 22 getlocal2 23 pushbyte 025 getproperty null27 getproperty length29 convert_u30 setlocal 432 getlocal 434 pushbyte 336 bitand37 convert_u38 setlocal 540 pushbyte 042 convert_u43 setlocal 645 jump L1

Page 61: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 12

ActionScript3 + LLVM?

� As BC; ModuleID = 'SparseCompRow'

declare avm2val @avm2_getproperty(...) readonlydeclare void @avm2_setproperty(...)declare avm2val @avm2_coerce(...) readnone

define avm2val @GO_m6_CopyMatrix(avm2val, avm2val, avm2val) {bb_m6_b0_0_:

%i = call avm2val (...)* @avm2_getproperty( avm2val %2, avm2ref bitcast (i32 24 to avm2ref) ) ; <avm2val> [#uses=1]%i41 = add i32 0, 0 ; <i32> [#uses=1]%i42 = call avm2val @avm2box_i32( i32 %i41 ) ; <avm2val> [#uses=1]%i1 = call avm2val (...)* @avm2_pushbyte( i32 0 ) ; <avm2val> [#uses=0]%i2 = call avm2val (...)* @avm2_getproperty( avm2val %2, avm2val %i42, avm2ref bitcast (i32 5 to avm2ref) ) ; <avm2val> [#uses=1]%i3 = call avm2val (...)* @avm2_getproperty( avm2val %i2, avm2ref bitcast (i32 58 to avm2ref) ) ; <avm2val> [#uses=1]%i4 = call avm2val (...)* @avm2_convert_u( avm2val %i3 ) ; <avm2val> [#uses=3]%i43 = add i32 3, 0 ; <i32> [#uses=2]%i44 = call avm2val @avm2box_i32( i32 %i43 ) ; <avm2val> [#uses=6]%i88 = call double @avm2unbox_double( avm2val %i44 ) ; <double> [#uses=1]%i84 = call double @avm2unbox_double( avm2val %i44 ) ; <double> [#uses=1]%i5 = call avm2val (...)* @avm2_pushbyte( i32 3 ) ; <avm2val> [#uses=0]%i45 = call i32 @avm2unbox_i32( avm2val %i4 ) ; <i32> [#uses=1]%i46 = call i32 @avm2unbox_i32( avm2val %i44 ) ; <i32> [#uses=0]%i47 = and i32 %i45, %i43 ; <i32> [#uses=1]%i48 = call avm2val @avm2box_i32( i32 %i47 ) ; <avm2val> [#uses=1]%i6 = call avm2val (...)* @avm2_bitand( avm2val %i4, avm2val %i44 ) ; <avm2val> [#uses=0]%i7 = call avm2val (...)* @avm2_convert_u( avm2val %i48 ) ; <avm2val> [#uses=3]%i63 = call i32 @avm2unbox_i32( avm2val %i7 ) ; <i32> [#uses=1]%i8 = call avm2val (...)* @avm2_pushuint( i32 0 ) ; <avm2val> [#uses=4]%i53 = call i32 @avm2unbox_i32( avm2val %i8 ) ; <i32> [#uses=1]%i49 = call i32 @avm2unbox_i32( avm2val %i8 ) ; <i32> [#uses=1]br label %bb_m6_b1_0_; ...

Page 62: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Use-case #3 - Cross Language

Crack Scripting Language➔ C/C++/Java-like Scripting Language➔ Speed of a compiled language, ease of use of a scripting language➔ Unladen Sparrow(Python), Rubinius(Ruby), V8(JS)

cf. PNaCl (Portable Native Client)

Page 63: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Use-case #3 - Emscripten

compiles LLVM bytecode into JavaScript➔ C/C++ to JavaScript➔ can be run on the web➔ Python, the Bullet physics engine, eSpeak (TTS)

https://github.com/kripken/emscripten/wiki

Page 64: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Use-case #3 - Emscripten

compiles LLVM bytecode into JavaScript➔ C/C++ to JavaScript➔ can be run on the web➔ Python, the Bullet physics engine, eSpeak (TTS)

https://github.com/kripken/emscripten/wiki

Everything  compiles  into  LLVM  bitcode

The  web  is  everywhere,  and  runs  JavaScript

Compiling  LLVM  bitcode  to  JavaScript  lets  us  run~  everything,  everywhere

Page 65: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

asm.js

https://www.youtube.com/watch?v=XsyogXtyU9o

Page 66: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

asm.js

https://www.youtube.com/watch?v=XsyogXtyU9o

Page 67: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

asmjs.org

Page 68: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

"�$���

�����

Page 69: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

References• Official Site

- http://llvm.org - Online Demo : http://llvm.org/demo

• Developer Meetings - Sponsored by Apple, Google, Adobe, Qualcomm. ,

AMD, CERN, Sony Pictures, Intel • LLVM User

- Adobe, Apple, Cray, Google, Electronic Arts, NVIDIA, Siemens, Sun Microsystems, XMOS ...

- Objective Modula-2, IcedTea, PyPy, iPhone tool chain, IOQuake3, LLVM D, Mono, MacRuby, Pure, LLVM-Lua

- CMU, ETH Zurich, NYU, Stanford ...

Page 70: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

놀라운 사실들• First production JIT compiler for C-based

languages • Clang/LLVM have fully replaced GCC in XCode 5 • Used on both major mobile platforms: iOS and

Android • Most GPU compute languages (OpenCL, CUDA,

Renderscript) use LLVM • First complete C++-11x: language + library • First ARM64 compiler in production (iPhone 5s) • 2012 ACM Software System Award

Page 71: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Hazard in LLVM

0

6.25

12.5

18.75

25

LLVM version1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8

Crash Errors

Page 72: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Bug fixed…

• 2.4 revision simplifies “( a>13 ) & ( a==15)” into “( a>13 )” • 2.8 revision folds “((x==x) | (y&z))==1” into 0 • 2.8 revision reduces this loop into “i=1”

for (i=0; i<5; i++) { if (i) continue; if (i) break;}

From March, 2008 to present:

170 bugs fixed + 3 reported but not yet fixed ( 57 wrong code bugs, 116 crash bugs)

Page 73: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

More to come…

• More complete Windows support • More effective profile-guided

optimization • Improved usability, parallelization for LTO • Improved auto-vectorization • Improved debugging support • State-of-the-art pointer analysis

Page 74: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM

Conclusion

• LLVM is- still continue evolution...- not omnia(all mighty)- maybe a shortcut to the new compiler- new strategy for mobile- alternative solution for HW emulation or VM- can mix with the other languages- synergy with OpenCL, OpenGL- new chance with LLVM projects

Page 75: 차세대컴파일러, VM의미래: 애플 오픈소스 LLVM