Slide 1

Slide 1 text

LLVM Simon Cook, Embecosm

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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]

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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])); ^

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

Copyright © 2014 Embecosm. Freely available under a Creative Commons license TableGen Example class ALU_RR subOp, string asmstr, list 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 subOp, string asmstr, SDNode OpNode> : ALU_RR; 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 num, string n> : Register { let Num = num; } def R0 : Ri< 0, "r0">, DwarfRegNum<[0]>; def R1 : Ri< 1, "r1">, DwarfRegNum<[1]>; def R2 : Ri< 2, "r2">, DwarfRegNum<[2]>;

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Thank You www.embecosm.com