0
Introduction to the LLVM    Compiler System            Chris Lattner         llvm.org Architect        November 4, 2008   ...
What is the LLVM Project?• Collection of industrial strength compiler technology  ■ Optimizer and Code Generator  ■ llvm-g...
What is the LLVM Project?• Collection of industrial strength compiler technology  ■ Optimizer and Code Generator  ■ llvm-g...
Why New Compilers?                     http://llvm.org
Why New Compilers?• Existing Open Source C Compilers have Stagnated!                                                     h...
Why New Compilers?• Existing Open Source C Compilers have Stagnated!• How?  ■ Based on decades old code generation technol...
What I want!               http://llvm.org
What I want!• A set of production-quality reusable libraries:  ■ ... which implement the best known techniques drawing fro...
LLVM Vision and Approach • Primary mission: build a set of modular compiler components:   ■ Reduces the time & cost to con...
LLVM Vision and Approach • Primary mission: build a set of modular compiler components:   ■  Reduces the time & cost to co...
Talk Overview• Intro and Motivation• LLVM as a C and C++ Compiler• Other LLVM Capabilities• Going Forward                 ...
LLVM-GCC 4.2• C, C++, Objective C, Ada and Fortran• Standard GCC command line options• Supports almost all GCC language fe...
LLVM-GCC 4.2• C, C++, Objective C, Ada and Fortran• Standard GCC command line options• Supports almost all GCC language fe...
LLVM GCC 4.2 Design• Replace GCC optimizer and code generator with LLVM  ■   Reuses GCC parser and runtime libraries      ...
LLVM GCC 4.2 Design• Replace GCC optimizer and code generator with LLVM  ■   Reuses GCC parser and runtime libraries      ...
Linking LLVM and GCC compiled code • Safe to mix and match .o files between compilers • Safe to call into libraries built ...
Linking LLVM and GCC compiled code • Safe to mix and match .o files between compilers • Safe to call into libraries built ...
Linking LLVM and GCC compiled code • Safe to mix and match .o files between compilers • Safe to call into libraries built ...
Potential Impact of LLVM Optimizer• Generated Code  ■   How fast does the code run?                                     ht...
Potential Impact of LLVM Optimizer• Generated Code  ■   How fast does the code run?• Compile Times  ■   How fast can we ge...
Potential Impact of LLVM Optimizer• Generated Code  ■   How fast does the code run?• Compile Times  ■   How fast can we ge...
Potential Impact of LLVM Optimizer• Generated Code  ■   How fast does the code run?• Compile Times  ■   How fast can we ge...
New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across l...
New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across l...
New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across l...
New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across l...
SPEC INT 2000 Compile Time    In seconds: Lower is Better          Optimization Level      http://llvm.org
SPEC INT 2000 Compile Time200s                         In seconds: Lower is Better                                        ...
SPEC INT 2000 Compile Time200s                         In seconds: Lower is Better                                        ...
SPEC INT 2000 Compile Time200s                         In seconds: Lower is Better                                        ...
SPEC 2000 Execution TimeRelative to GCC -O2: Lower is Faster                                  GCC 4.2                     ...
SPEC 2000 Execution Time       Relative to GCC -O2: Lower is Faster100%                                            GCC 4.2...
SPEC 2000 Execution Time        Relative to GCC -O2: Lower is Faster100%                                                 G...
SPEC 2000 Execution Time        Relative to GCC -O2: Lower is Faster100%                                                 G...
llvm-gcc 4.2 Summary• Drop in replacement for GCC 4.2  ■ Compatible with GCC command line options and languages  ■ Works w...
Talk Overview• Intro and Motivation• LLVM as a C and C++ Compiler• Other LLVM Capabilities• LLVM Going Forward            ...
LLVM For Compiler Hackers• LLVM is a great target for new languages  ■ Well defined, simple to program for  ■ Easy to reta...
Colorspace Conversion JIT Optimization • Code to convert from one color format to another:   ■ e.g. BGRA 444R -> RGBA 8888...
Colorspace Conversion JIT Optimization • Code to convert from one color format to another:   ■ e.g. BGRA 444R -> RGBA 8888...
Colorspace Conversion JIT Optimization • Code to convert from one color format to another:    ■ e.g. BGRA 444R -> RGBA 888...
Another example: RegEx Compilation• Many regex’s are matched millions of times:  ■  Match time is critical• Common regex e...
Talk Overview• Intro and Motivation• LLVM as a C and C++ Compiler• Other LLVM Capabilities• LLVM Going Forward            ...
LLVM Going Forward• More of the same...                        http://llvm.org
LLVM Going Forward• More of the same...  ■ Even faster optimizer  ■ Even better optimizations  ■ More features for non-C l...
LLVM Going Forward• More of the same...  ■ Even faster optimizer  ■ Even better optimizations  ■ More features for non-C l...
Clang Frontend: What is it?• C, Objective-C, and C++ front-end• Aggressive project with many goals...  ■ Compatibility wit...
Clang Frontend: What is it? • C, Objective-C, and C++ front-end • Aggressive project with many goals...    ■ Compatibility...
Clang Frontend: What is it? • C, Objective-C, and C++ front-end • Aggressive project with many goals...    ■ Compatibility...
Clang Compile TimePostgreSQL -fsyntax-only Time: 665K lines of C code in 619 files                                        ...
Clang Compile TimePostgreSQL -fsyntax-only Time: 665K lines of C code in 619 files                 49s                    ...
Clang Compile TimePostgreSQL -fsyntax-only Time: 665K lines of C code in 619 files                 49s                    ...
LLVM Overview • New compiler architecture built with reusable components   ■ Retarget existing languages to JIT or static ...
LLVM Overview • New compiler architecture built with reusable components   ■ Retarget existing languages to JIT or static ...
LLVM Overview • New compiler architecture built with reusable components   ■ Retarget existing languages to JIT or static ...
LLVM Overview • New compiler architecture built with reusable components   ■ Retarget existing languages to JIT or static ...
Upcoming SlideShare
Loading in...5
×

Introduction to LLVM

600

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
600
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Introduction to LLVM"

  1. 1. Introduction to the LLVM Compiler System Chris Lattner llvm.org Architect November 4, 2008 ACAT’08 - Erice, Sicily
  2. 2. What is the LLVM Project?• Collection of industrial strength compiler technology ■ Optimizer and Code Generator ■ llvm-gcc and Clang Front-ends ■ MSIL and .NET Virtual Machines http://llvm.org
  3. 3. What is the LLVM Project?• Collection of industrial strength compiler technology ■ Optimizer and Code Generator ■ llvm-gcc and Clang Front-ends ■ MSIL and .NET Virtual Machines• Open Source Project with many contributors ■ Industry, Research Groups, Individuals http://llvm.org/ http://llvm.org
  4. 4. Why New Compilers? http://llvm.org
  5. 5. Why New Compilers?• Existing Open Source C Compilers have Stagnated! http://llvm.org
  6. 6. Why New Compilers?• Existing Open Source C Compilers have Stagnated!• How? ■ Based on decades old code generation technology ■ No modern techniques like cross-file optimization and JIT codegen ■ Aging code bases: difficult to learn, hard to change substantially ■ Can’t be reused in other applications ■ Keep getting slower with every release http://llvm.org
  7. 7. What I want! http://llvm.org
  8. 8. What I want!• A set of production-quality reusable libraries: ■ ... which implement the best known techniques drawing from modern literature ■ ... which focus on compile time ■ ... and performance of the generated code• Ideally support many different languages and applications! http://llvm.org
  9. 9. 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 X86 PPC CBE clang GCC LTO ... Code DWARF Target JIT Optzn linker IPO ... gen BC IO LL IO System Core Support xforms analysis GC ... http://llvm.org
  10. 10. 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 ■ ... for example, a truly great C compiler ■ ... for example, a runtime specialization engine X86 PPC CBE clang GCC LTO ... Code DWARF Target JIT Optzn linker IPO ... gen BC IO LL IO System Core Support xforms analysis GC ... http://llvm.org
  11. 11. Talk Overview• Intro and Motivation• LLVM as a C and C++ Compiler• Other LLVM Capabilities• Going Forward http://llvm.org
  12. 12. LLVM-GCC 4.2• C, C++, Objective C, Ada and Fortran• Standard GCC command line options• Supports almost all GCC language features and extensions• Supports many targets, including X86, X86-64, PowerPC, etc.• Extremely compatible with GCC 4.2 http://llvm.org
  13. 13. LLVM-GCC 4.2• C, C++, Objective C, Ada and Fortran• Standard GCC command line options• Supports almost all GCC language features and extensions• Supports many targets, including X86, X86-64, PowerPC, etc.• Extremely compatible with GCC 4.2 What does it mean to be both LLVM and GCC? http://llvm.org
  14. 14. LLVM GCC 4.2 Design• Replace GCC optimizer and code generator with LLVM ■ Reuses GCC parser and runtime libraries C C++ GCC 4.2 GCC Code Front-end GCC Optimizer Generator .s file ... GCC 4.2 http://llvm.org
  15. 15. LLVM GCC 4.2 Design• Replace GCC optimizer and code generator with LLVM ■ Reuses GCC parser and runtime libraries C GCC 4.2 LLVM Code C++ Front-end LLVM Optimizer Generator .s file ... LLVM GCC 4.2 C C++ GCC 4.2 GCC Code Front-end GCC Optimizer Generator .s file ... GCC 4.2 http://llvm.org
  16. 16. Linking LLVM and GCC compiled code • Safe to mix and match .o files between compilers • Safe to call into libraries built with other compilers .o filesa.c gcc -O2b.c llvm-gcc -O3 Disk Storage http://llvm.org
  17. 17. Linking LLVM and GCC compiled code • Safe to mix and match .o files between compilers • Safe to call into libraries built with other compilers .o filesa.c gcc -O2b.c llvm-gcc -O3 Disk Storage http://llvm.org
  18. 18. Linking LLVM and GCC compiled code • Safe to mix and match .o files between compilers • Safe to call into libraries built with other compilers .o filesa.c gcc -O2b.c llvm-gcc -O3 Linker exe Disk Storage http://llvm.org
  19. 19. Potential Impact of LLVM Optimizer• Generated Code ■ How fast does the code run? http://llvm.org
  20. 20. Potential Impact of LLVM Optimizer• Generated Code ■ How fast does the code run?• Compile Times ■ How fast can we get code from the compiler? http://llvm.org
  21. 21. Potential Impact of LLVM Optimizer• Generated Code ■ How fast does the code run?• Compile Times ■ How fast can we get code from the compiler?• New Features http://llvm.org
  22. 22. Potential Impact of LLVM Optimizer• Generated Code ■ How fast does the code run?• Compile Times ■ How fast can we get code from the compiler?• New Features Link Time Optimization http://llvm.org
  23. 23. New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across language boundaries too! .o filesa.c gcc -O3b.c llvm-gcc -O4 Linker exe Disk Storage http://llvm.org
  24. 24. New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across language boundaries too! .o filesa.c gcc -O3b.c llvm-gcc -O4 Linker exec.c llvm-gcc -O4 Disk Storage http://llvm.org
  25. 25. New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across language boundaries too! .o filesa.c gcc -O3b.c llvm-gcc -O4 Linker exec.c llvm-gcc -O4 LLVM Link Time Optimizer Disk Storage http://llvm.org
  26. 26. New Feature: Link Time Optimization • Optimize (e.g. inline, constant fold, etc) across files with -O4 • Optimize across language boundaries too! .o filesa.c gcc -O3b.c llvm-gcc -O4 Linker exec.c llvm-gcc -O4 LLVM Link Time Optimizerd.cpp llvm-g++ -O4 Disk Storage http://llvm.org
  27. 27. SPEC INT 2000 Compile Time In seconds: Lower is Better Optimization Level http://llvm.org
  28. 28. SPEC INT 2000 Compile Time200s In seconds: Lower is Better 187s GCC 4.2 LLVM GCC 4.2 164s160s 133s120s 90s 79s 80s 40s 0s -O0 -O0 -g -O1 -O2 -O3 -O4: LTO Optimization Level http://llvm.org
  29. 29. SPEC INT 2000 Compile Time200s In seconds: Lower is Better 187s GCC 4.2 LLVM GCC 4.2 164s160s 144s 133s 131s 126s120s 112s 97s 90s 79s 80s 74s 40s 0s -O0 -O0 -g -O1 -O2 -O3 -O4: LTO Optimization Level http://llvm.org
  30. 30. SPEC INT 2000 Compile Time200s In seconds: Lower is Better 187s GCC 4.2 LLVM GCC 4.2 164s160s 144s 133s 126s 131s Faster than120s GCC at -O2! 112s 42% Faster 97s at -O3! 90s 79s 30% Faster 80s 74s 18% Faster at -O2! at -O1! 40s 0s -O0 -O0 -g -O1 -O2 -O3 -O4: LTO Optimization Level http://llvm.org
  31. 31. SPEC 2000 Execution TimeRelative to GCC -O2: Lower is Faster GCC 4.2 LLVM GCC 4.2 Optimization Level http://llvm.org
  32. 32. SPEC 2000 Execution Time Relative to GCC -O2: Lower is Faster100% GCC 4.2 96.3% LLVM GCC 4.295%90%85%80%75% -O2 -O3 -O4 (LTO) Optimization Level http://llvm.org
  33. 33. SPEC 2000 Execution Time Relative to GCC -O2: Lower is Faster100% GCC 4.2 96.3% LLVM GCC 4.295% 95.1% 5% Faster 92.5%90% at -O2! 4% Faster at -O3!85%80%75% -O2 -O3 -O4 (LTO) Optimization Level http://llvm.org
  34. 34. SPEC 2000 Execution Time Relative to GCC -O2: Lower is Faster100% GCC 4.2 96.3% LLVM GCC 4.295% 95.1% 5% Faster 92.5%90% at -O2! 4% Faster at -O3!85% 20% Faster than -O3!80% 80.3%75% -O2 -O3 -O4 (LTO) Optimization Level http://llvm.org
  35. 35. llvm-gcc 4.2 Summary• Drop in replacement for GCC 4.2 ■ Compatible with GCC command line options and languages ■ Works with existing makefiles (e.g. “make CC=llvm-gcc”)• Benefits of LLVM Optimizer and Code Generator ■ Much faster optimizer: ~30-40% at -O3 in most cases ■ Slightly better codegen at a given level: ~5-10% on x86/x86-64 ■ Link-Time Optimization at -O4: optimize across source files http://llvm.org
  36. 36. Talk Overview• Intro and Motivation• LLVM as a C and C++ Compiler• Other LLVM Capabilities• LLVM Going Forward http://llvm.org
  37. 37. LLVM For Compiler Hackers• LLVM is a great target for new languages ■ Well defined, simple to program for ■ Easy to retarget existing compiler to use LLVM backend• LLVM supports Just-In-Time optimization and compilation ■ Optimize code at runtime based on dynamic information ■ Easy to retarget existing bytecode interpreter to LLVM JIT ■ Great for performance, not just for traditional “compilers” http://llvm.org
  38. 38. Colorspace Conversion JIT Optimization • Code to convert from one color format to another: ■ e.g. BGRA 444R -> RGBA 8888 ■ Hundreds of combinations, importance depends on input for each pixel { switch (infmt) { case RGBA 5551: R = (*in >> 11) & C G = (*in >> 6) & C B = (*in >> 1) & C ... } switch (outfmt) { case RGB888: *outptr = R << 16 | G << 8 ... } } http://llvm.org
  39. 39. Colorspace Conversion JIT Optimization • Code to convert from one color format to another: ■ e.g. BGRA 444R -> RGBA 8888 ■ Hundreds of combinations, importance depends on input for each pixel { for each pixel { switch (infmt) { R = (*in >> 11) & C; case RGBA 5551: G = (*in >> 6) & C; R = (*in >> 11) & C B = (*in >> 1) & C; G = (*in >> 6) & C Run-time *outptr = R << 16 | B = (*in >> 1) & C G << 8 ... specialize ... } } switch (outfmt) { case RGB888: *outptr = R << 16 | Compiler optimizes G << 8 ... shifts and masking } } http://llvm.org
  40. 40. Colorspace Conversion JIT Optimization • Code to convert from one color format to another: ■ e.g. BGRA 444R -> RGBA 8888 ■ Hundreds of combinations, importance depends on input for each pixel { for each pixel { switch (infmt) { R = (*in >> 11) & C; case RGBA 5551: G = (*in >> 6) & C; R = (*in >> 11) & C B = (*in >> 1) & C; G = (*in >> 6) & C Run-time *outptr = R << 16 | B = (*in >> 1) & C G << 8 ... specialize ... } } switch (outfmt) { case RGB888: *outptr = R << 16 | Compiler optimizes G << 8 ... shifts and masking } } Speedup depends on src/dest format: 5.4x speedup on average, 19.3x max speedup: (13.3MB/s to 257.7MB/s) http://llvm.org
  41. 41. Another example: RegEx Compilation• Many regex’s are matched millions of times: ■ Match time is critical• Common regex engines ‘compile’ to ‘bytecode’ and interpret: ■ regcomp/regexec• Why not compile to native code? Partial Evaluation! ■ regcomp compiles regex to a native function ■ Much faster matching, could even vectorize common idioms• Excellent way to handle multiple different Unicode encodings http://llvm.org
  42. 42. Talk Overview• Intro and Motivation• LLVM as a C and C++ Compiler• Other LLVM Capabilities• LLVM Going Forward http://llvm.org
  43. 43. LLVM Going Forward• More of the same... http://llvm.org
  44. 44. LLVM Going Forward• More of the same... ■ Even faster optimizer ■ Even better optimizations ■ More features for non-C languages ■ Debug Info Improvements ■ Many others... http://llvm.org
  45. 45. LLVM Going Forward• More of the same... ■ Even faster optimizer ■ Even better optimizations ■ More features for non-C languages ■ Debug Info Improvements ■ Many others... Better tools for source level analysis of C/C++ programs! http://llvm.org
  46. 46. Clang Frontend: What is it?• C, Objective-C, and C++ front-end• Aggressive project with many goals... ■ Compatibility with GCC ■ Fast compilation ■ Expressive error messages• Host for a broad range of source-level tools http://llvm.org
  47. 47. Clang Frontend: What is it? • C, Objective-C, and C++ front-end • Aggressive project with many goals... ■ Compatibility with GCC ■ Fast compilation ■ Expressive error messagest.c:6:49: error: invalid operands to binary expression (int and struct A) return intArg + func(intArg ? ((someA.X+40) + someA) / 42 : someA.X)); ~~~~~~~~~~~~ ^ ~~~~~ • Host for a broad range of source-level tools http://llvm.org
  48. 48. Clang Frontend: What is it? • C, Objective-C, and C++ front-end • Aggressive project with many goals... ■ Compatibility with GCC ■ Fast compilation ■ Expressive error messagest.c:6:49: error: invalid operands to binary expression (int and struct A) return intArg + func(intArg ? ((someA.X+40) + someA) / 42 : someA.X)); ~~~~~~~~~~~~ ^ ~~~~~ • Host for a broad range of source-level tools http://llvm.org
  49. 49. Clang Compile TimePostgreSQL -fsyntax-only Time: 665K lines of C code in 619 files http://llvm.org
  50. 50. Clang Compile TimePostgreSQL -fsyntax-only Time: 665K lines of C code in 619 files 49s GCC 4.2 clang http://llvm.org
  51. 51. Clang Compile TimePostgreSQL -fsyntax-only Time: 665K lines of C code in 619 files 49s 2.3x faster 21s GCC 4.2 clang http://llvm.org
  52. 52. LLVM Overview • New compiler architecture built with reusable components ■ Retarget existing languages to JIT or static compilation ■ Many optimizations and supported targets http://llvm.org
  53. 53. LLVM Overview • New compiler architecture built with reusable components ■ Retarget existing languages to JIT or static compilation ■ Many optimizations and supported targets • llvm-gcc: drop in GCC-compatible compiler ■ Better & faster optimizer ■ Production quality http://llvm.org
  54. 54. LLVM Overview • New compiler architecture built with reusable components ■ Retarget existing languages to JIT or static compilation ■ Many optimizations and supported targets • llvm-gcc: drop in GCC-compatible compiler ■ Better & faster optimizer ■ Production quality • Clang front-end: C/ObjC/C++ front-end ■ Several times faster than GCC ■ Much better end-user features (warnings/errors) http://llvm.org
  55. 55. LLVM Overview • New compiler architecture built with reusable components ■ Retarget existing languages to JIT or static compilation ■ Many optimizations and supported targets • llvm-gcc: drop in GCC-compatible compiler ■ Better & faster optimizer Come join us at: ■ Production quality http://llvm.org http://clang.llvm.org • Clang front-end: C/ObjC/C++ front-end ■ Several times faster than GCC ■ Much better end-user features (warnings/errors) • LLVM 2.4 release this week! http://llvm.org
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×