Clang/LLVM, eine Alternative zu gcc V 1.1 Hans Buchmann Institut für Mikroelektronik IME Fachhochschule Nordwestschweiz FHNW hans.buchmann@fhnw.ch 27.August 2013 H.Buchmann Clang/LLVM 1 / 18
Ein Zwerg auf den Schultern eines Riesen Lessing J. Rosenwald Collection,1410 einmal mehr H.Buchmann Clang/LLVM 2 / 18
Ein Zwerg auf den Schultern eines Riesen /L ng M LV un r se Th a em H.Buchmann a Cl Lessing J. Rosenwald Collection,1410 einmal mehr Clang/LLVM 2 / 18
Die Compiler Fauna GNU Compiler Collection: gcc.gnu.org LLVM Compiler Infrastructure: llvm.org H.Buchmann Clang/LLVM 3 / 18
Die Compiler Fauna Seit 1987 170 Releases aktuell 4.8.1 494 Mitarbeiter 39 Targets (backends) Geschrieben in C/C++ Lizenz: GPL Seit 2003 24 Releases aktuell 3.3 340 Mitarbeiter 14 Targets (backends) Geschrieben in C++ Lizenz: University of Illinois/NCSA Open Source License BSD Style H.Buchmann Clang/LLVM 3 / 18
Die Compiler Fauna Seit 1987 170 Releases aktuell 4.8.1 494 Mitarbeiter 39 Targets (backends) Geschrieben in C/C++ Lizenz: GPL Seit 2003 24 Releases aktuell 3.3 340 Mitarbeiter 14 Targets (backends) Geschrieben in C++ Lizenz: University of Illinois/NCSA Open Source License BSD Style Der C++ Compiler H.Buchmann Clang/LLVM 3 / 18
Zwei wichtige Sponsoren/Konkurrenten was auch immer H.Buchmann Clang/LLVM 4 / 18
Zwei wichtige Sponsoren/Konkurrenten was auch immer GN U/ L bj O inu xk o XC? ac C /M de e tiv ec ern el S O X H.Buchmann Clang/LLVM 4 / 18
Zwei wichtige Sponsoren/Konkurrenten was auch immer GN U/ L bj O inu xk o XC? ac C /M de e tiv ec ern el S O X Clang/LLVM auch strategische Software? H.Buchmann Clang/LLVM 4 / 18
2012 ACM System Software Award LLVM/Clang Strategische Software?... Key factors in its success are the openness of its technology and the quality of its architecture and engineering as well as its clean, flexible design and easy-to-use programming interfaces... awards.acm.org/software_system Chris Lattner Vikram Adve Evan Cheng UIUC H.Buchmann Clang/LLVM 5 / 18
2012 ACM System Software Award LLVM/Clang Strategische Software?... Key factors in its success are the openness of its technology and the quality of its architecture and engineering as well as its clean, flexible design and easy-to-use programming interfaces... Andere Preisträger (Auswahl) 2011 Eclipse 2009 VMware Workstation for Linux 1.0 2003 Make 2002 Java 1983 UNIX awards.acm.org/software_system H.Buchmann Clang/LLVM 5 / 18
Clang/LLVM oder umgekehrt Aktuell The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Despite its name, LLVM has little to do with traditional virtual machines, though it does provide helpful libraries that can be used to build them. Clang The name LLVM itself is not an acronym; it is the full name of the the project Teil einer ganzen Suite llvm.org Historisch The LLVM Compiler Infrastructure LLVM Low Level Virtual Machine University of Illinois at Urbana-Champaign H.Buchmann Clang/LLVM 6 / 18
Clang/LLVM oder umgekehrt Aktuell The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Despite its name, LLVM has little to do with traditional virtual machines, though it does provide helpful libraries that can be used to build them. Clang The name LLVM itself is not an acronym; it is the full name of the the project Teil einer ganzen Suite llvm.org Historisch The LLVM Compiler Infrastructure LLVM Low Level Virtual Machine University of Illinois at Urbana-Champaign H.Buchmann Clang/LLVM 6 / 18
Clang/LLVM oder umgekehrt Aktuell The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Despite its name, LLVM has little to do with traditional virtual machines, though it does provide helpful libraries that can be used to build them. Clang The name LLVM itself is not an acronym; it is the full name of the the project Teil einer ganzen Suite llvm.org Historisch The LLVM Compiler Infrastructure LLVM Low Level Virtual Machine University of Illinois at Urbana-Champaign H.Buchmann Clang/LLVM 6 / 18
Black Box H L L High Level programming Language frontend Compiler I L optimize IR Intermediate Representation universeller Assembler pro HLL ein frontend pro TASM ein backend I L backend #Komponenten = #HLL + #TASM + 1 T A S M Compiler Infrastruktur Target Assembler H.Buchmann Clang/LLVM 7 / 18
Die Komponenten Compiler Infrastruktur H L L frontend Compiler I R optimize I R backend IR Intermediate Representation universeller Assembler pro HLL ein frontend pro TASM ein backend #Komponenten = #HLL + #TASM + 1 T A S M H.Buchmann Clang/LLVM 7 / 18
Die Komponenten Compiler Infrastruktur H L L frontend Compiler I R nur ein optimize optimize I R backend IR Intermediate Representation universeller Assembler pro HLL ein frontend pro TASM ein backend #Komponenten = #HLL + #TASM + 1 T A S M H.Buchmann Clang/LLVM 7 / 18
Die Kommandozeile Clang/LLVM Compiler kompatibel zu GCC src dst Optionen clang++../src/hello-world.cc -o hello-world -O2 -std=c++11 g++ Fehlermeldungen: Clang../src/hello-world.cc:22:22: error: expected ';' after expression std::cout<<"world\n" ^ ; GCC../src/hello-world.cc: In destructor World:: World() :../src/hello-world.cc:23:1: error: expected ; before } token } ^ H.Buchmann Clang/LLVM 8 / 18
Die Kommandozeile Clang/LLVM Compiler kompatibel zu GCC src dst Optionen clang++../src/hello-world.cc -o hello-world -O2 -std=c++11 g++ Fehlermeldungen: Clang../src/hello-world.cc:22:22: error: expected ';' after expression std::cout<<"world\n" ^ ; GCC../src/hello-world.cc: In destructor World:: World() :../src/hello-world.cc:23:1: error: expected ; before } token } ^ H.Buchmann Clang/LLVM 8 / 18
Die Kommandozeile Clang/LLVM Compiler kompatibel zu GCC src dst Optionen clang++../src/hello-world.cc -o hello-world -O2 -std=c++11 g++ Fehlermeldungen: Clang../src/hello-world.cc:22:22: error: expected ';' after expression std::cout<<"world\n" ^ ; GCC../src/hello-world.cc: In destructor World:: World() :../src/hello-world.cc:23:1: error: expected ; before } token } ^ wird/ist besser in GCC-4.9 H.Buchmann Clang/LLVM 8 / 18
HLL // // d i v 1 0 // ( c ) H. Buchmann FHNW 2013 // unsigned d i v 1 0 ( unsigned x ) { r e t u r n x / 1 0 ; } HLL IR TASM Ein Beispiel: x/10 IR Intermediate Representation ; Function Attrs: nounwind readnone uwtable define i32 @ Z5div10j(i32 %x) #0 { %1 = udiv i32 %x, 10 ret i32 %1 x86 64 } ARM Z5div10j: movl %edi, %ecx movl $3435973837, %eax imulq %rcx, %rax shrq $35, %rax ret Z5div10j: ldr r2,.lcpi0 0 umull r3,r1, r0, r2 lsr r0,r1, #3 mov pc,lr.lcpi0 0:.long 3435973837 H.Buchmann Clang/LLVM 9 / 18
HLL // // d i v 1 0 // ( c ) H. Buchmann FHNW 2013 // unsigned d i v 1 0 ( unsigned x ) { r e t u r n x / 1 0 ; } HLL IR TASM Ein Beispiel: x/10 IR Intermediate Representation ; Function Attrs: nounwind readnone uwtable define i32 @ Z5div10j(i32 %x) #0 { %1 = udiv i32 %x, 10 ret i32 %1 x86 64 } ARM Z5div10j: movl %edi, %ecx movl $3435973837, %eax imulq %rcx, %rax shrq $35, %rax ret Z5div10j: ldr r2,.lcpi0 0 umull r3,r1, r0, r2 lsr r0,r1, #3 mov pc,lr.lcpi0 0:.long 3435973837 H.Buchmann Clang/LLVM 9 / 18
HLL // // d i v 1 0 // ( c ) H. Buchmann FHNW 2013 // unsigned d i v 1 0 ( unsigned x ) { r e t u r n x / 1 0 ; } HLL IR TASM Ein Beispiel: x/10 IR Intermediate Representation ; Function Attrs: nounwind readnone uwtable define i32 @ Z5div10j(i32 %x) #0 { %1 = udiv i32 %x, 10 ret i32 %1 x86 64 } ARM Z5div10j: movl %edi, %ecx movl $3435973837, %eax imulq %rcx, %rax shrq $35, %rax ret Z5div10j: ldr r2,.lcpi0 0 umull r3,r1, r0, r2 lsr r0,r1, #3 mov pc,lr.lcpi0 0:.long 3435973837 H.Buchmann Clang/LLVM 9 / 18
HLL // // d i v 1 0 // ( c ) H. Buchmann FHNW 2013 // unsigned d i v 1 0 ( unsigned x ) { r e t u r n x / 1 0 ; } HLL IR TASM Ein Beispiel: x/10 x 10 3435973837 2 x = 0.100000000005821x 35 IR Intermediate Representation ; Function Attrs: nounwind readnone uwtable define i32 @ Z5div10j(i32 %x) #0 { %1 = udiv i32 %x, 10 ret i32 %1 x86 64 } ARM Z5div10j: movl %edi, %ecx movl $3435973837, %eax imulq %rcx, %rax shrq $35, %rax ret Z5div10j: ldr r2,.lcpi0 0 umull r3,r1, r0, r2 lsr r0,r1, #3 mov pc,lr.lcpi0 0:.long 3435973837 H.Buchmann Clang/LLVM 9 / 18
Clang/LLVM Crosskompilation am Beispiel ARM src obj compile Optionen clang++../src/div10.cc -o div10.o -c -O3 -std=c++11 Target -target arm -integrated-as H.Buchmann Clang/LLVM 10 / 18
Clang/LLVM Crosskompilation am Beispiel ARM src obj compile Optionen clang++../src/div10.cc -o div10.o -c -O3 -std=c++11 braucht externen assembler nicht Target -target arm -integrated-as H.Buchmann Clang/LLVM 10 / 18
Clang/LLVM Crosskompilation am Beispiel ARM src Viele toolchains pro Architektur (target) eine toolchain z.b obj arm-none-linux-gnueabi-tool compile Optionen clang++../src/div10.cc -o div10.o -c -O3 -std=c++11 Target -target arm -integrated-as ein make H.Buchmann Clang/LLVM 10 / 18
H L L frontend LLVM Virtuelle Maschine abgesetztes backend Compiler VM I R optimize I R 1. erzeuge IR Code clang++../src/prime-number.cc -oprime-number.bc -c -O3 -std=c++11 -emit-llvm 2. starte virtuelle Maschine VM lli prime-number.bc H.Buchmann Clang/LLVM 11 / 18
H L L frontend LLVM Virtuelle Maschine abgesetztes backend Compiler VM I R optimize I R a la Java 1. erzeuge IR Code clang++../src/prime-number.cc -oprime-number.bc -c -O3 -std=c++11 -emit-llvm 2. starte virtuelle Maschine VM lli prime-number.bc H.Buchmann Clang/LLVM 11 / 18
H L L frontend LLVM Virtuelle Maschine abgesetztes backend Compiler VM I R optimize I R 1. erzeuge IR Code clang++../src/prime-number.cc -oprime-number.bc -c -O3 -std=c++11 -emit-llvm 2. starte virtuelle Maschine VM lli prime-number.bc H.Buchmann Clang/LLVM 11 / 18
Sekunden Clang GCC interpretiert kompiliert kompiliert Bemerkungen 2.19 2.19 1.13 A 1.59 1.68 1.70 B Bemerkungen A GCC optimiert die Instruktionen auto rem=v%pi; auto quot=v/pi; B Gemacht mit auto res=std::div(v,pi); mit res.rem und res.quot Ein kleiner benchmark prime-numbers.cc H.Buchmann Clang/LLVM 12 / 18
Sekunden Clang GCC interpretiert kompiliert kompiliert Bemerkungen 2.19 2.19 1.13 A 1.59 1.68 1.70 B Bemerkungen A GCC optimiert die Instruktionen auto rem=v%pi; auto quot=v/pi; B Gemacht mit auto res=std::div(v,pi); mit res.rem und res.quot Ein kleiner benchmark prime-numbers.cc H.Buchmann Clang/LLVM 12 / 18
Compiler Das Clang API für eigene Anwendungen C/C++ Objective-C clang I R optimize I R Clang API frontend Clang baut AST backend Eigene Anwendung API Sammlung von h Files H.Buchmann Clang/LLVM 13 / 18
Compiler Das Clang API für eigene Anwendungen C/C++ Objective-C C/C++ Objective-C clang frontend I R optimize clang A S T I R Clang API Abstract Syntax Tree backend frontend Clang baut AST backend Eigene Anwendung API Sammlung von h Files H.Buchmann Clang/LLVM 13 / 18
Compiler Das Clang API für eigene Anwendungen C/C++ Objective-C C/C++ Objective-C clang frontend I R optimize clang A S T I R Clang API Abstract Syntax Tree backend frontend Clang baut AST backend Eigene Anwendung API Sammlung von h Files H.Buchmann Clang/LLVM 13 / 18
Das Clang plugin API PrintFunctionNames.cc c UIUC Das API #i n c l u d e c l a n g / Frontend / F r o n t e n d P l u g i n R e g i s t r y. h #i n c l u d e c l a n g /AST/AST. h #i n c l u d e c l a n g /AST/ASTConsumer. h #i n c l u d e c l a n g / Frontend / C o m p i l e r I n s t a n c e. h #i n c l u d e l l v m / Support / r a w o s t r e a m. h Der Code 2 Klassen,71 Zeilen Aufruf clang++ -c\ -Xclang -load -Xclang PrintFunctionNames.so \ -Xclang -plugin -Xclang print-fns\ cc-file H.Buchmann Clang/LLVM 14 / 18
Das Clang plugin API PrintFunctionNames.cc c UIUC Das API #i n c l u d e c l a n g / Frontend / F r o n t e n d P l u g i n R e g i s t r y. h #i n c l u d e c l a n g /AST/AST. h #i n c l u d e c l a n g /AST/ASTConsumer. h #i n c l u d e c l a n g / Frontend / C o m p i l e r I n s t a n c e. h #i n c l u d e l l v m / Support / r a w o s t r e a m. h Der Code 2 Klassen,71 Zeilen Aufruf clang++ -c\ -Xclang -load -Xclang PrintFunctionNames.so \ -Xclang -plugin -Xclang print-fns\ cc-file H.Buchmann Clang/LLVM 14 / 18
Das Clang plugin API PrintFunctionNames.cc c UIUC Das API #i n c l u d e c l a n g / Frontend / F r o n t e n d P l u g i n R e g i s t r y. h #i n c l u d e c l a n g /AST/AST. h #i n c l u d e c l a n g /AST/ASTConsumer. h #i n c l u d e c l a n g / Frontend / C o m p i l e r I n s t a n c e. h #i n c l u d e l l v m / Support / r a w o s t r e a m. h Der Code 2 Klassen,71 Zeilen Aufruf clang++ -c\ -Xclang -load -Xclang PrintFunctionNames.so \ -Xclang -plugin -Xclang print-fns\ cc-file H.Buchmann Clang/LLVM 14 / 18
Das Clang plugin API PrintFunctionNames.cc c UIUC Das API #i n c l u d e c l a n g / Frontend / F r o n t e n d P l u g i n R e g i s t r y. h #i n c l u d e c l a n g /AST/AST. h #i n c l u d e c l a n g /AST/ASTConsumer. h #i n c l u d e c l a n g / Frontend / C o m p i l e r I n s t a n c e. h #i n c l u d e l l v m / Support / r a w o s t r e a m. h Der Code 2 Klassen,71 Zeilen Aufruf clang++ -c\ -Xclang -load -Xclang PrintFunctionNames.so \ -Xclang -plugin -Xclang print-fns\ cc-file gibt es bei GCC auch H.Buchmann Clang/LLVM 14 / 18
Das Clang standalone API ClangFormat.cc c UIUC Das API #i n c l u d e c l a n g / B a s i c / D i a g n o s t i c. h #i n c l u d e c l a n g / B a s i c / D i a g n o s t i c O p t i o n s. h #i n c l u d e c l a n g / B a s i c / F i l e M a n a g e r. h #i n c l u d e c l a n g / B a s i c / SourceManager. h #i n c l u d e c l a n g / Format / Format. h #i n c l u d e c l a n g / Lex / L e x e r. h #i n c l u d e c l a n g / R e w r i t e / Core / R e w r i t e r. h #i n c l u d e l l v m / Support / F i l e S y s t e m. h #i n c l u d e l l v m / Support / S i g n a l s. h Der Code Keine Klassen, 4 Funktionen incl main Aufruf ClangFormat cc-file 193 Zeilen H.Buchmann Clang/LLVM 15 / 18
Das Clang standalone API ClangFormat.cc c UIUC Das API #i n c l u d e c l a n g / B a s i c / D i a g n o s t i c. h #i n c l u d e c l a n g / B a s i c / D i a g n o s t i c O p t i o n s. h #i n c l u d e c l a n g / B a s i c / F i l e M a n a g e r. h #i n c l u d e c l a n g / B a s i c / SourceManager. h #i n c l u d e c l a n g / Format / Format. h #i n c l u d e c l a n g / Lex / L e x e r. h #i n c l u d e c l a n g / R e w r i t e / Core / R e w r i t e r. h #i n c l u d e l l v m / Support / F i l e S y s t e m. h #i n c l u d e l l v m / Support / S i g n a l s. h Der Code Keine Klassen, 4 Funktionen incl main Aufruf ClangFormat cc-file 193 Zeilen H.Buchmann Clang/LLVM 15 / 18
Das Clang standalone API ClangFormat.cc c UIUC Das API #i n c l u d e c l a n g / B a s i c / D i a g n o s t i c. h #i n c l u d e c l a n g / B a s i c / D i a g n o s t i c O p t i o n s. h #i n c l u d e c l a n g / B a s i c / F i l e M a n a g e r. h #i n c l u d e c l a n g / B a s i c / SourceManager. h #i n c l u d e c l a n g / Format / Format. h #i n c l u d e c l a n g / Lex / L e x e r. h #i n c l u d e c l a n g / R e w r i t e / Core / R e w r i t e r. h #i n c l u d e l l v m / Support / F i l e S y s t e m. h #i n c l u d e l l v m / Support / S i g n a l s. h Der Code Keine Klassen, 4 Funktionen incl main Aufruf ClangFormat cc-file 193 Zeilen H.Buchmann Clang/LLVM 15 / 18
GPL BSD Style Lizenzen Clang vs. GCC Compile: Tooling: GCC Clang eigener Code kompilieren eigene Tools schreiben Eigener Code muss nicht GPL sein eigene Arbeit muss GPL sein H.Buchmann Clang/LLVM 16 / 18
Vergleich mit GCC Compiler + c++11: beide Codequalität: GCC Unterschiede sind minim Fehlermeldungen: Clang GCC holt auf Toolchain: Clang GCC pro target eine toolchain Crosscompile: beide Clang hat weniger targets Tooling plugin: beide Lizenzen standalone: Clang nicht vorgesehen in GCC H.Buchmann Clang/LLVM 17 / 18
Informationen & Hilfen llvm.org der Clang Compiler gcc.gnu.org der GCC Compiler und natürlich www.fhnw.ch/technik/ime Das Institut für Mikroelektronik der FHNW Remark(s): Für den Code: hans.buchmann@fhnw.ch Es wurden nur open source tools auf GNU/Linux verwendet H.Buchmann Clang/LLVM 18 / 18
Informationen & Hilfen llvm.org der Clang Compiler gcc.gnu.org der GCC Compiler und natürlich www.fhnw.ch/technik/ime Das Institut für Mikroelektronik der FHNW Remark(s): Für den Code: hans.buchmann@fhnw.ch Es wurden nur open source tools auf GNU/Linux verwendet H.Buchmann Clang/LLVM 18 / 18
Literature https://www.ohloh.net/p http://www.gnu.org/software/gcc/releases.html http://gcc.gnu.org/onlinedocs/gcc/contributors.html http://llvm.org/releases/ H.Buchmann Clang/LLVM 18 / 18