llvm clang
play

LLVM/Clang Mouna Abidi & Manel Grichi 1 Plan What is LLVM? - PowerPoint PPT Presentation

LLVM/Clang Mouna Abidi & Manel Grichi 1 Plan What is LLVM? How will you be using it? LLVM Architecture Compiler Simple case study Conclusion 2 What is LLVM? Low Level Virtual Machine Modern Compiler


  1. LLVM/Clang Mouna Abidi & Manel Grichi 1

  2. Plan • What is LLVM? • How will you be using it? • LLVM Architecture • Compiler • Simple case study • Conclusion 2

  3. What is LLVM? ● Low Level Virtual Machine ● Modern Compiler infrastructure ● Written in C++ ● It provides the front-end and back-end ● Support different languages: C, C++, Objective- C, Ruby, Python … 3

  4. What is LLVM? LLVM Suite LLVM Clang Test Suite 4

  5. Why LLVM/Clang? ● A need for a compiler that allows better diagnostics ● Better integration with IDEs, a license that is compatible with commercial products. ● Fast compiler that is easy to develop and maintain. ● Useful for performing optimizations and transformations on code 5

  6. Why LLVM/Clang? http://appleinsider.com/articles/08/06/20/apples_other_open_secret_the_llvm_complier 6

  7. How will you be using it? ● Compiling programs to bitcode ● Analyzing the bitcode ● Reporting properties of the program 7

  8. Architecture LLVM Front End (Clang) : takes your source code and turns it into an intermediate representation (IR). The passes: transform IR to IR that is more faster that the input IR. Back End (LLVM): generates actual machine code 8

  9. 9

  10. Compiler ● LLVM/GCC (C/C++ and objectiveC) ● DragonEgg (C/C++, Ada, Fortran) ● LLVM/Clang (C/C++, objectiveC and objectiveC++) 10

  11. Compiler Clang GCC Languages (C/C++ and objectiveC) Java, Ada, FORTRAN, Go, (C/C++ and objectiveC) Design modular design, suite of libraries monolithic Reusability Easy to reuse, extensible design Difficult to reuse AST Easy understandable Very old codebase Compile time Faster compile time Longer compile time Error Better error messages 11

  12. Elsa vs clang ● Elsa's parser and AST can be easily extensible by adding grammar rules ● The Elsa community is extremely small ● Elsa is not built as a stack of reusable libraries ● Elsa is even slower and uses more memory than GCC 12

  13. Clang vs PCC (Portable C Compiler) ● The PCC source base is very small and builds quickly with just a C compiler. ● The LLVM/Clang communities are very active. ● PCC doesn't support Objective-C or C++. ● PCC produces very inefficient code. 13

  14. Simple Case study ● Compile this code to LLVM IR ● Run series of optimization passes to iteratively improve code 14

  15. First passe IR clang -S -emit-llvm boolean_optimization.c -o boolean_optimization.ll 15

  16. Second pass first optimisation IR opt -mem2reg -instsimplify -S boolean_optimization.ll -o boolean_optimization.opt.ll 16

  17. Third pass second optimisation IR opt -mem2reg -instcombine -S boolean_optimization.ll -o boolean_optimization.opt.ll (x and z) xor (y and z) == z and (x xor y) 17

  18. Simple Case study ● AST Clang ● Calculate metrics using Visitor ● http://clang.llvm.org/doxygen/classclang_1_1AsmStmt.html 18

  19. Conclusion • LLVM is a modern open source compiler framework – very powerful and easy to use – human readable IR allows for following optimization steps – modular design allows adding own functionality • LLVM may be practical – to replace GCC – to generate code for embedded processors – to learn about compilers and optimizations 19

Recommend


More recommend