building llvm ir
play

Building LLVM-IR Johannes Doerfert and Christoph Mallon Saarbr - PowerPoint PPT Presentation

Building LLVM-IR Johannes Doerfert and Christoph Mallon Saarbr ucken Graduate School of Computer Science Saarland University Saarbr ucken, Germany December 18, 2013 C4 2 / 14 C4 Frontend Lexer Parser C-Code Token Stream AST 2 /


  1. Building LLVM-IR Johannes Doerfert and Christoph Mallon Saarbr¨ ucken Graduate School of Computer Science Saarland University Saarbr¨ ucken, Germany December 18, 2013

  2. C4 2 / 14

  3. C4 Frontend Lexer Parser C-Code Token Stream AST 2 / 14

  4. C4 Frontend Lexer Parser C-Code Token Stream AST Middle-end Optimization Optimization LLVM-IR LLVM-IR LLVM-IR 2 / 14

  5. C4 Frontend Lexer Parser C-Code Token Stream AST Middle-end Optimization Optimization LLVM-IR LLVM-IR LLVM-IR Backend ... X86 ARM 2 / 14

  6. LLVM-IR 3 / 14

  7. LLVM-IR ◮ Low-level, “typed”, SSA-based, assembly language ◮ Types are sign agnostic 3 / 14

  8. LLVM-IR ◮ Low-level, “typed”, SSA-based, assembly language ◮ Types are sign agnostic ◮ Hierarchical structured 3 / 14

  9. LLVM-IR ◮ Low-level, “typed”, SSA-based, assembly language ◮ Types are sign agnostic ◮ Hierarchical structured ◮ Module ◮ Global Variables ◮ Composite Types (“structs”) ◮ Function Declarations ◮ Function Definitions 3 / 14

  10. LLVM-IR ◮ Low-level, “typed”, SSA-based, assembly language ◮ Types are sign agnostic ◮ Hierarchical structured ◮ Module ◮ Global Variables ◮ Composite Types (“structs”) ◮ Function Declarations ◮ Function Definitions ◮ Function Definition ◮ Basic Blocks 3 / 14

  11. LLVM-IR ◮ Low-level, “typed”, SSA-based, assembly language ◮ Types are sign agnostic ◮ Hierarchical structured ◮ Module ◮ Global Variables ◮ Composite Types (“structs”) ◮ Function Declarations ◮ Function Definitions ◮ Function Definition ◮ Basic Blocks ◮ Basic Blocks ◮ Instructions 3 / 14

  12. LLVM-IR Instructions 4 / 14

  13. LLVM-IR Instructions %sum = add i32 4, %var ; Binary operations %cmp = icmp sge i32 %a, %b 4 / 14

  14. LLVM-IR Instructions %sum = add i32 4, %var ; Binary operations %cmp = icmp sge i32 %a, %b %value = load i32* %location ; Memory operations store i32 %value, i32* %location 4 / 14

  15. LLVM-IR Instructions %sum = add i32 4, %var ; Binary operations %cmp = icmp sge i32 %a, %b %value = load i32* %location ; Memory operations store i32 %value, i32* %location br label %next-block ; Terminator Instructions br i1 %cmp, label %then-block, label %else-block ret i32 %a 4 / 14

  16. LLVM-IR Instructions %sum = add i32 4, %var ; Binary operations %cmp = icmp sge i32 %a, %b %value = load i32* %location ; Memory operations store i32 %value, i32* %location br label %next-block ; Terminator Instructions br i1 %cmp, label %then-block, label %else-block ret i32 %a %X = trunc i32 257 to i8 ; Cast Instructions %Y = sext i32 %V to i64 %Z = bitcast i8* %x to i32* 4 / 14

  17. LLVM-IR Instructions %sum = add i32 4, %var ; Binary operations %cmp = icmp sge i32 %a, %b %value = load i32* %location ; Memory operations store i32 %value, i32* %location br label %next-block ; Terminator Instructions br i1 %cmp, label %then-block, label %else-block ret i32 %a %X = trunc i32 257 to i8 ; Cast Instructions %Y = sext i32 %V to i64 %Z = bitcast i8* %x to i32* %ptr = alloca i32 ; Other Instructions %ret = call i32 @foo(i8* %fmt, i32 %val) %phi = phi i32 [ %value-a, %block-a ], [ %value-b, %block-b ] %I-th-element-addr = getelementptr i8** %Array, i64 %I 4 / 14

  18. LLVM-IR Basic Blocks 5 / 14

  19. LLVM-IR Basic Blocks int max(int a, int b) { if (b < a) { return a; } else { return b; } } 5 / 14

  20. LLVM-IR Basic Blocks if-header: %A = load i32* %Aptr %B = load i32* %Bptr %if-condition = icmp slt i32 %B, %A br i1 %if-condition, label %if-cons, label %if-alt if-cons: ; preds = %if-header %A2 = load i32* %Aptr ret i32 %A2 if-alt: ; preds = %if-header %B2 = load i32* %Bptr ret i32 %B2 5 / 14

  21. LLVM-IR Functions 6 / 14

  22. LLVM-IR Functions define i32 @max(i32 %a, i32 %b) { entry: %0 = alloca i32 %1 = alloca i32 store i32 %a, i32* %1 store i32 %b, i32* %0 br label %if-header if-header: ; preds = %entry %2 = load i32* %0 %3 = load i32* %1 %if-condition = icmp slt i32 %2, %3 br i1 %if-condition, label %if-cons, label %if-alt if-con: ; preds = %if-header %4 = load i32* %1 ret i32 %4 if-alt: ; preds = %if-header %5 = load i32* %0 ret i32 %5 } 6 / 14

  23. LLVM-IR The human readable form ◮ Globals start with an “ @ ” ◮ Local values start with an “ % ” ◮ Basic block names start with an “ % ” when used ◮ Basic block names end with an “ : ” when definied ◮ There is always “ label ” written before the branch target ◮ Indention is not important (but nice) 7 / 14

  24. LLVM-IR Ill-formed Examples 8 / 14

  25. LLVM-IR Ill-formed Examples ; Declaration needs to strictly dominate use %x = add i32 1, %x 8 / 14

  26. LLVM-IR Ill-formed Examples ; Declaration needs to strictly dominate use %x = add i32 1, %x ; Binary operations need equal types %valI32 = load i32 * P %valI64 = load i64 * Q %val = add i32 %valI32, %valI64 8 / 14

  27. LLVM-IR Ill-formed Examples ; Declaration needs to strictly dominate use %x = add i32 1, %x ; Binary operations need equal types %valI32 = load i32 * P %valI64 = load i64 * Q %val = add i32 %valI32, %valI64 ; A basic block *needs* a terminator (branch or return) block: %VAL = add i32 %A, %B 8 / 14

  28. LLVM-IR Ill-formed Examples ; Declaration needs to strictly dominate use %x = add i32 1, %x ; Binary operations need equal types %valI32 = load i32 * P %valI64 = load i64 * Q %val = add i32 %valI32, %valI64 ; A basic block *needs* a terminator (branch or return) block: %VAL = add i32 %A, %B ; A terminator *ends* a basic block block: ret i32 %A %VAL = add i32 %A, %B 8 / 14

  29. LLVM-IR Command line Generate (human readable) LLVM-IR from C/C++ input: clang -emit-llvm -c -S -o OUT.ll IN.c Apply an optimization on LLVM-IR: opt -S -o OUT.ll IN.ll Execute (via JIT) an LLVM-IR module: lli IN.ll <argv arguments> Create a binary from an LLVM-IR module: clang -o OUT IN.ll Create architecture specific assembly: llc -o OUT.s IN.ll Create a binary from architecture specific assembly: cc -o OUT IN.s Create API calls for an LLVM-IR module: llc -march=cpp -o OUT.cpp IN.ll Get more help: <TOOL> --help 9 / 14

  30. LLVM-IR Documentation General language reference manual: http://llvm.org/docs/LangRef.html Doxygen code documentation (auto generated): http://llvm.org/docs/doxygen/html/index.html Stable binaries and source code (llvm + clang): http://llvm.org/releases/download.html#3.3 Full command line tools guide: http://llvm.org/docs/CommandGuide/ 10 / 14

  31. LLVM-IR API classes Value Argument Constant Constant Int/FP Global Var Functions Instruction ... Bin. Inst. Load Inst. 11 / 14

  32. LLVM-IR API classes Type Function Type Integer Type Composite Type Struct Type Pointer Type 11 / 14

  33. LLVM Final Notes 12 / 14

  34. LLVM Final Notes ◮ LLVM is HUGE ◮ LLVM is grown, the interface is not consitent 12 / 14

  35. LLVM Final Notes ◮ LLVM is HUGE ◮ LLVM is grown, the interface is not consitent Keep it Simple and Straightforward 12 / 14

  36. Doxygen + Examples 13 / 14

  37. int max(int a, int b) { if (b < a) { return a; } else { return b; } } 14 / 14

  38. ; ModuleID = ’max.c’ 14 / 14

  39. ; ModuleID = ’max.c’ @X = global i32 0 14 / 14

  40. ; ModuleID = ’max.c’ @X = global i32 0 define i32 @max(i32 %a, i32 %b) #0 { } 14 / 14

  41. ; ModuleID = ’max.c’ @X = global i32 0 define i32 @max(i32 %a, i32 %b) #0 { } define i32 @main(i32 %argc, i8** %argv) #0 { } 14 / 14

  42. ; ModuleID = ’max.c’ @X = global i32 0 define i32 @max(i32 %a, i32 %b) #0 { } define i32 @main(i32 %argc, i8** %argv) #0 { } attributes #0 = { nounwind uwtable ... 14 / 14

  43. ; ModuleID = ’max.ll’ target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64" target triple = "x86_64-unknown-linux-gnu" @X = global i32 0 ; Function Attrs: nounwind uwtable define i32 @max(i32 %a, i32 %b) # 0 { %1 = icmp slt i32 %b, %a br i1 %1, label %2, label %3 ; <label>:2 ; preds = %0 ret i32 %a ; <label>:3 ; preds = %0 ret i32 %b } ; Function Attrs: nounwind uwtable define i32 @main(i32 %argc, i8** %argv) # 0 { %1 = load i32* @X, align 4 %2 = call i32 @max(i32 %argc, i32 %1) ret i32 %2 } a t t r i b u t e s # 0 = { nounwind u w t a b l e "no-frame-pointer-elim"="true" } 14 / 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend