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

llvm clang
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

LLVM/Clang

Mouna Abidi & Manel Grichi

1

slide-2
SLIDE 2

Plan

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

2

slide-3
SLIDE 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

slide-4
SLIDE 4

What is LLVM? LLVM

LLVM Suite Clang Test Suite

4

slide-5
SLIDE 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

slide-6
SLIDE 6

Why LLVM/Clang?

http://appleinsider.com/articles/08/06/20/apples_other_open_secret_the_llvm_complier

6

slide-7
SLIDE 7

How will you be using it?

  • Compiling programs to bitcode
  • Analyzing the bitcode
  • Reporting properties of the program

7

slide-8
SLIDE 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

slide-9
SLIDE 9

9

slide-10
SLIDE 10

Compiler

  • LLVM/GCC (C/C++ and objectiveC)
  • DragonEgg (C/C++, Ada, Fortran)
  • LLVM/Clang (C/C++, objectiveC and objectiveC++)

10

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 14

Simple Case study

  • Compile this code to LLVM IR
  • Run series of optimization passes to iteratively improve code

14

slide-15
SLIDE 15

First passe IR

clang -S -emit-llvm boolean_optimization.c -o boolean_optimization.ll

15

slide-16
SLIDE 16

Second pass first optimisation IR

  • pt -mem2reg -instsimplify -S boolean_optimization.ll -o boolean_optimization.opt.ll

16

slide-17
SLIDE 17

Third pass second optimisation IR

  • pt -mem2reg -instcombine -S boolean_optimization.ll -o boolean_optimization.opt.ll

(x and z) xor (y and z) == z and (x xor y)

17

slide-18
SLIDE 18

Simple Case study

  • AST Clang
  • Calculate metrics using Visitor
  • http://clang.llvm.org/doxygen/classclang_1_1AsmStmt.html

18

slide-19
SLIDE 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