Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to LLVM

Simon Cook
January 23, 2014

Introduction to LLVM

At the BCS Central London Offices, I spoke to the BCS Open Source Specialist Group, about LLVM as an alternative to GCC, how being a newer compile changes the way it feels to compiler developers and how ultimately having multiple high-quality open source compilers is very good for users.

Finally I conclude with a comparison of compilation and execution times, and describe how compilers are also useful in the hardware design space.

Simon Cook

January 23, 2014
Tweet

More Decks by Simon Cook

Other Decks in Technology

Transcript

  1. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license What is LLVM? • Open Source Compiler – Taking code written in various languages, translating to machine code for a variety of languages. • Set of Compilation Libraries – For e.g. including JIT functionality in a program.
  2. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license Languages and Targets Supported • Languages – C/C++ – D – Hydra – Java Bitcode (IcedTea JRE) – Rust – Scheme – etc. • Targets – AArch32 (ARM) – AArch64 – MIPS – PowerPC – R600 – Sparc – X86 – etc.
  3. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license How does LLVM differ from GCC? • UIUC vs GPL • Designed as a set of clean C++ libraries – Easier to extend • One binary for many architectures – No need to recompile all common libraries to support a second architecture. • Clang is command line compatible with both gcc and cl.exe – Allows you to try a different compiler without changing your build system. • Warnings
  4. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license Clang Warnings • Take the following C program: #include <stdio.h> #define MAX(x,y) (x > y ? x : y) int (int argv, char **argc) { printf("Max is %i\n", MAX(argv,argc[0])); return 0; }
  5. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license Clang Warnings • Historically GCC will have produced: test.c: In function ‘main’: test.c:6:25: warning: comparison between pointer and integer [enabled by default] test.c:6:25: warning: pointer/integer type mismatch in conditional expression [enabled by default] test.c:6:3: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
  6. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license Clang Warnings • Clang however produces: test.c:6:25: warning: ordered comparison between pointer and integer ('int' and 'char *') printf("Max is %i\n", MAX(argv,argc[0])); ^ ~~~~ ~~~~~~~ test.c:3:21: note: expanded from macro 'MAX' #define MAX(x,y) (x > y ? x : y) ^ test.c:6:25: warning: pointer/integer type mismatch in conditional expression ('int' and 'char *') [-Wconditional-type-mismatch] printf("Max is %i\n", MAX(argv,argc[0])); ^ ~~~~ ~~~~~~~ test.c:3:25: note: expanded from macro 'MAX' #define MAX(x,y) (x > y ? x : y) ^ test.c:6:25: warning: format specifies type 'int' but the argument has type 'char *' [- Wformat] printf("Max is %i\n", MAX(argv,argc[0])); ~~ ^~~~~~~~~~~~~~~~~ %s test.c:3:18: note: expanded from macro 'MAX' #define MAX(x,y) (x > y ? x : y) ^~~~~~~~~~~~~~~ 3 warnings generated.
  7. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license Clang Warnings • This has affected the warnings that GCC provide: test.c: In function ‘main’: test.c:3:21: warning: comparison between pointer and integer [enabled by default] #define MAX(x,y) (x > y ? x : y) ^ test.c:6:25: note: in expansion of macro ‘MAX’ printf("Max is %i\n", MAX(argv,argc[0])); ^ test.c:3:29: warning: pointer/integer type mismatch in conditional expression [enabled by default] #define MAX(x,y) (x > y ? x : y) ^ test.c:6:25: note: in expansion of macro ‘MAX’ printf("Max is %i\n", MAX(argv,argc[0])); ^ test.c:6:3: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=] printf("Max is %i\n", MAX(argv,argc[0])); ^
  8. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license TableGen • The ease of porting to a new Target comes from TableGen. • TableGen is a language used to describe features of an architecture. – e.g register and instruction definitions • When the compiler is built, TableGen definitions are processed by tblgen, generating target-specific classes used during the instruction selection and generation process.
  9. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license TableGen Example class ALU_RR<bits<4> subOp, string asmstr, list<dag> pattern> : InstRR<0x8, (outs GPR:$rD), (ins GPR: $rA, GPR:$rB), !strconcat(asmstr, "\t$rD, $rA, $rB"), pattern> { bits<5> rD; bits<5> rA; bits<5> rB; bits<4> op; let Inst{25-21} = rD; let Inst{20-16} = rA; let Inst{15-11} = rB; let Inst{3-0} = op; let op = subOp; } class ALU1_RR<bits<4> subOp, string asmstr, SDNode OpNode> : ALU_RR<subOp, asmstr, [(set GPR:$rD, (OpNode (i32 GPR:$rA), (i32 GPR:$rB)))]>; let isAsCheapAsAMove = 1 in { def SUB : ALU1_RR<0x2, "l.sub", sub>; let isCommutable=1 in { def ADD : ALU1_RR<0x0, "l.add", add>; def AND : ALU1_RR<0x3, "l.and", and>; def OR : ALU1_RR<0x4, "l.or", or>; def XOR : ALU1_RR<0x5, "l.xor", xor>; } } class Ri<bits<5> num, string n> : Register<n> { let Num = num; } def R0 : Ri< 0, "r0">, DwarfRegNum<[0]>; def R1 : Ri< 1, "r1">, DwarfRegNum<[1]>; def R2 : Ri< 2, "r2">, DwarfRegNum<[2]>;
  10. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license Comparison of Compilation Time Data from http://www.phoronix.com/scan.php?page=article&item=llvm_clang33_3
  11. Copyright © 2014 Embecosm. Freely available under a Creative Commons

    license Comparison of Execution Time Data from http://www.phoronix.com/scan.php?page=article&item=llvm_clang33_3