SlideShare a Scribd company logo
Build Programming Language
     Runtime with LLVM



Jim Huang ( 黃敬群 )
Developer & Co-founder, 0xlab
                                       jserv@0xlab.org

                                @ 1500 / March 27, 2011
150010 = 05DC16
• OSDC !
   • Open Source Developer Conference
   • 0x05DC
• About 0xlab
   • The meaning of open
   • 0x1ab16 = 42710 → 2009/04/27
• About me
   • http://about.me/jserv
  • 最大的專長就是培養興趣
Rights to copy
                                                                    © Copyright 2011 0xlab
                                                                           http://0xlab.org/
                                                                            contact@0xlab.org
Attribution – ShareAlike 3.0
You are free                                                  Corrections, suggestions, contributions and
                                                                               translations are welcome!
   to copy, distribute, display, and perform the work
   to make derivative works                                                Latest update:March 27, 2011
   to make commercial use of the work
Under the following conditions
      Attribution. You must give the original author credit.
      Share Alike. If you alter, transform, or build upon this work, you may distribute the
      resulting work only under a license identical to this one.
   For any reuse or distribution, you must make clear to others the license terms of this
   work.
   Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.
License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode
姊妹議程:
                             〈窮得只剩下 Compiler 〉
                                  OSDC.tw 2009
http://www.slideshare.net/jserv/what-can-compilers-do-for-us
姊妹議程:
                                   〈身騎 LLVM , 過三關:
                               淺談編譯器技術的嶄新應用〉
                                              TOSSUG 2009
            http://www.slideshare.net/jserv/llvm-introduction

                                           姊妹議程:
     〈 Applied Computer Science Concepts in Android 〉
                                    台大資訊系 2010
http://www.slideshare.net/jserv/applied-computer-science-concepts-in-android


                                           姊妹議程:
〈 from Source to Binary -- How GNU Toolchain Works 〉
                  臺北科技大學資訊工程所 2011/03/31
提綱   (1) Compilers on Rails 的時代
     (2) 探索 LLVM
     (3) 程式語言的變遷 (Low-Level 觀點 )
       傳統 -> 動態 -> 移動運算
     (4) LLVM 實例
Compilers on Rails 的時代
Compilers on Rails 的時代

[ 詞彙 ] on the rails: 正常運行 ; 在正常軌道
[ 啟發 ] Ruby on Rails:
 (1) convention over configuration
 (2) less software
 (3) programmer happiness ultimately leads to better productivity
隱藏在我們身邊的 Compiler
• Java / .Net ( 虛擬機器 +Just-In-Time compiler)
• 網路瀏覽器
   • Mozilla/Firefox (ActionMonkey/Tamarin)
   • WebKit (SquirrelFish)
   • Google Chrome (V8 engine)
• Web 應用程式: JSP/Servlet, SilverLight/.Net
• 移動通訊平台: Java ME, Android, iPhone,
  Portable Native Client
• 繪圖軟體: Adobe PixelBender, Shader
• 3D 高品質圖形處理: Gallium3D / OpenGL /
  Direct3D / RenderScript (Android)
傳統的 Compiler 流程
 Source Code           Assembly Code        Runnable Code




Compiler   Assembler    Loader     System        User




           Intermediate Code       Binary Code              DATA

IR (Intermediate Representation) 可說是 Compiler 的心臟

LLVM = Low Level Virtual Machine
以 color space 轉換來說,執行
                     以 color space 轉換來說,執行
   Specialize 技巧     時期得負擔大量且繁瑣的運算,
                     時期得負擔大量且繁瑣的運算,
                     如 BGRA 444R→ RGBA 8888
                     如 BGRA 444R→ RGBA 8888




Speedup depends on src/dest format:
 Speedup depends on src/dest format:
–– 5.4x speedup average, 19.3x speedup max
   5.4x speedup average, 19.3x speedup max
(13.3MB/s to 257.7MB/s)
 (13.3MB/s to 257.7MB/s)
Compiler 領導技術的時代
    • 運算模式已大幅改觀
    • Framework-driven
    • SIMD/vectorization, Cell, SMP/multi-core
    • 虛擬化 (Virtualization) 技術的時代
        → 更多元、更安全、更有效率地使用硬體
    • 資訊技術的雜交 (cross-over)
    • LLVM 的大一統宏願



案例:
Portable Native Client, OpenCL (GPGPU)
到處都有 VM
Java Virtual Machine (JVM)   Tamarin (ActionScript)
.NET Common Language         Valgrind (C++)
  Runtime (CLR)
                             Lua
Smalltalk
                             TrueType
Squeak
                             Dalvik
Parrot (Perl 6)
                             Adobe Flash (AVM2)
Python
                             p-code (USCD Pascal)
YARV (Ruby 1.9)
                             Zend
Rubinius

                                                      13
LLVM 可作為上述的編譯器應用的根基
探索 LLVM
LLVM
• Low-Level VM → bit-code
• 完整的編譯器基礎建設
   • 可重用的、用以建構編譯器的軟體元件
   • 允許更快更完整的打造新的編譯器
   • static compiler, JIT, trace-based optimizer, …
• 開放的編譯器框架
   • 多種程式語言支援
   • 高彈性的自由軟體授權模式 (BSD License)
   • 活躍的開發 (50% 開發者來自 Apple Inc.)
   • 豐富的編譯輸出: C, ARM, x86, PowerPC, …
豐富的工具集
Assemblers, automatic debugger, linker, code
generator, compiler driver, modular optimizer, …
Eric Schmidt 是 UNIX lex 共同作者
Eric Schmidt 是 UNIX lex 共同作者
GCC vs. LLVM
GCC                                      LLVM
C, C++, Obj-C, Fortran, Java, Ada, ...   C, C++, Obj-C
x86, ARM, MIPS, PowerPC, …

binutils (ld as)                         BSD-Style License

                                         JIT/Interpreter




Compiler Driver
Frontend   LLVM IR   Backend
Frontend   LLVM IR   Backend
CodeMark 參考數據
 Pentium4 3.0GHz Ubuntu: -Os -O3 -fast

 size: kbytes
10000                                                                                                                             40



9000      LLVM                                      GCC                                      Intel CC                             35


8000
                                                                                                                                  30

7000

                                                                                                                                  25
6000


5000                                                                                                                              20



4000
                                                                                                                                  15


3000
                                                                                                                                  10

2000

                                                                                                                                  5
1000


   0                                                                                                                              0
        clang-2.8 Os clang-2.6 O3 clang-2.8 fast gcc-4.4.3 Os gcc-4.4.3 O3 gcc-4.4.3 fast   icc-12 Os   icc-12 O3   icc-12 fast
看起來, LLVM 效能還不是最好,
  那為何我們還要關注?
LLVM 不只是個具工業強度的編譯器
美妙的 LLVM + Clang
Clang: LLVM 的程式語言前端

 C                                 x86 機械碼

                 Clang
C++       $ clang main.c mod.c     ARM 機械碼




Obj-C


               Clang driver

           libClang           ld

           libLLVM            as
Clang 與 LLVM 的關聯
         libClang                   libLLVM
 [C]                BitCode
main.c              main.bc
                                                      x86 組語
                                                       main.s
 [C]     Source →   BitCode                Code
                              Optimizer
mod.c     BitCode   mod.bc                Generator
Clang 功能示意
 [C]                         libClang               BitCode
main.c                                              main.bc
            lex
                                        codegen
 [C]                   AST                          BitCode
mod.c                                               mod.bc
          parse
                                        Analysis
                    Serialize/
         semantics Deserialize
                                 Checker Indexer




                   AST         AST                 IDE 整合
                  main.ast    mod.ast    輸出 / 解析    (Xcode)
BitCode + Optimizer
                                                  LTO: Link-Time Optimization
                                                  LTO: Link-Time Optimization
BitCode
main.bc                          Inter-precedual
                    BitCode
                     Linker
BitCode
                               Analysis Transforms
mod.bc    BitCode                                     BitCode        BitCode
           parser                                      writer       main.lto.bc
                    BitCode
                               Analysis Transforms




                        BitCode       BitCode
                       main.opt.bc   mod.opt.bc
LLVM Code Generation
                                         libLLVM

 BitCode                                              Machine
main.lto.bc                                           Analysis                  x86 組語
                BitCode                   Machine                                main.s
                          BitCode
                 parser                    Instr
                                                       Machine    Asm
                                                      Transforms Emitter

   Execution                            Transform
                          Transform                    Register
    Engine    CodeGen                    Machine
                          SDNode                      Allocation
  InterPreter                              Instr


                                                                          ARM.td
                                                                          InstrInfo
                                                                          Register
                          Instruction   Instruction             X86.td
                                                                         Subtarget
      [C++]                Selection    Scheduler              InstrInfo
                                                                           Frame
                                                               Register
     main.cpp    [C]                                                     Lowering x86
                                                              Subtarget         CallbackUtils
                main.c           SDNode                                 CallingConv
                                                                Frame            Transforms
                                                              Lowering
                          Selection DAG                      CallingConv
                          Selection DAG
先從 Hello World 開始
       • 完整的 Compiler Driver
              $ clang hello.c ­o hello
       • 生成 IR
              $ clang ­O3 ­emit­llvm hello.c ­c ­o hello.bc
       • 以 Just-In-Time compiler 模式執行 BitCode
              $ lli hello.bc




Getting Started with the LLVM System
Getting Started with the LLVM System
http://llvm.org/docs/GettingStarted.html
http://llvm.org/docs/GettingStarted.html
#include <stdio.h>
 #include <stdio.h>
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
    printf("Hello world!n");
    printf("Hello world!n");
    return 0;
    return 0;
 }
 }


      函式 printf() 後方僅有一個
      函式 printf() 後方僅有一個                  • 反組譯 BitCode
      字串參數,前端預設將其
      字串參數,前端預設將其
          轉換為 puts()                          $ llvm­dis < hello.bc
           轉換為 puts()

  ; ModuleID = '<stdin>'
  ; ModuleID = '<stdin>'
  target datalayout = "e­p:32:32:32­...
  target datalayout = "e­p:32:32:32­...
  target triple = "i386­pc­linux­gnu"
  target triple = "i386­pc­linux­gnu"

  @str = internal constant [13 x i8] c"Hello world!00"
  @str = internal constant [13 x i8] c"Hello world!00"

  define i32 @main(i32 %argc, i8** nocapture %argv) nounwind {
  define i32 @main(i32 %argc, i8** nocapture %argv) nounwind {
  entry:
  entry:
    %puts = tail call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @str, i32 0, i32 0))
    %puts = tail call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @str, i32 0, i32 0))
    ret i32 0
    ret i32 0
  }
  }

  Declare i32 @puts(i8* nocapture) nounwind
  Declare i32 @puts(i8* nocapture) nounwind

        • 輸出 x86 後端組合語言
               $ llc hello.bc ­o hello.s
LLVM in Google Android 3.0 SDK
$ cd android­sdk­linux_x86/platform­tools
$ ./llvm­rs­cc ­­version
Low Level Virtual Machine (http://llvm.org/):
  llvm version 2.8svn
  Optimized build.
  Built Feb 16 2011 (19:26:29).
  Host: i386­unknown­linux
  Host CPU: penryn                 Android SDK (3.0 = API version 11)
                                   Android SDK (3.0 = API version 11)
                                   http://developer.android.com/sdk/
                                   http://developer.android.com/sdk/
  Registered Targets:
    arm    ­ ARM
    thumb  ­ Thumb
    x86    ­ 32­bit X86: Pentium­Pro and above
    x86­64 ­ 64­bit X86: EM64T and AMD64
$ ./llvm­rs­cc ­­help
OVERVIEW: RenderScript source compiler
                                                                31
LLVM in Google Android 3.0 SDK
    $ ./llvm­rs­cc ­­help
    OVERVIEW: RenderScript source compiler
    USAGE: llvm­rs­cc [options] <inputs>
•   OPTIONS:
      ­I <directory>          Add directory to include search path
      ­additional­dep­target <value>
                              Additional targets to show up in dependencies output
      ­allow­rs­prefix        Allow user­defined function prefixed with 'rs'
      ­bitcode­storage <value>
                              <value> should be 'ar' or 'jc'
      ­emit­asm               Emit target assembly files
      ­emit­bc                Build ASTs then convert to LLVM, emit .bc file
      ­emit­llvm              Build ASTs then convert to LLVM, emit .ll file
      ­emit­nothing           Build ASTs then convert to LLVM, but emit nothing
      ­help                   Print this help text
      ­java­reflection­package­name <value>
                              Specify the package name that reflected Java files belong to
      ­java­reflection­path­base <directory>
                              Base directory for output reflected Java files
                                                                                             32
測試 SDK 內建範例 RenderScript
android­sdk­linux_x86/platform­tools$ ./llvm­rs­cc 
  ../samples/android­11/ 
RenderScript/HelloWorld/src/com/android/rs/helloworld/helloworld.rs 
  ­I ../platforms/android­11/renderscript/include 
  ­I ../platforms/android­11/renderscript/clang­include
                                   // helloworld.rs
                                   // helloworld.rs
                                   // This is invoked automatically
                                   // This is invoked automatically
                                   // when the script is created
                                   // when the script is created
                                   void init() {
                                   void init() {
                                       gTouchX = 50.0f;
                                       gTouchX = 50.0f;
                                       gTouchY = 50.0f;
                                       gTouchY = 50.0f;
                                   }
                                   }

llvm­dis < helloworld.bc

@gTouchX = common global i32 0, align 4
@gTouchX = common global i32 0, align 4
@gTouchY = common global i32 0, align 4
@gTouchY = common global i32 0, align 4

define void @init() nounwind {
define void @init() nounwind {
  store i32 50, i32* @gTouchX, align 4
  store i32 50, i32* @gTouchX, align 4
  store i32 50, i32* @gTouchY, align 4
  store i32 50, i32* @gTouchY, align 4
  ret void                                                              33
  ret void
}
}
LLVM 給予無限可能

C/C++/       libClang      AST
Obj-C


           BitCode


            LibLLVM
            Optimizer

                 BitCode



Arch.td      libLLVM       xxx.s
          CodeGenerator
提供 IDE 的模組化
                                   有了 AST 後,即可針對
                 靜態編譯解析器
可用許多程式語言                           語言特性,做出特定的應用
撰寫 BitCode 生成
器 ( 前端編譯器 )
      C/C++/           libClang
,如 PerlObj-C
        module                            AST


                    BitCode
      其他語言                         系統優化處理
                      LibLLVM
                      Optimizer         作為其他語言的
                                        Source-to-source
 特定的後端架構。                               轉換器
 可以是硬體或軟體                            其他語言
                         BitCode


     Arch.td              LibLLVM        xxx.s
                        CodeGenerator
可增添其他模組,如
Polyhedral optimization           針對後端硬體的 Selection DAG
Xcode                     OpenCL Compiler
OpenCL →
Haskell →
Clay → C/C++/              libClang                  AST
        Obj-C
Crack →
                                          StrengthReduction
                        BitCode           Profile-gen/use
       其他語言
                                          AliasAnalysis
                          LibLLVM
                          Optimizer                →       JavaScript
 FPGA;
                                                   →       Java ByteCode
 GPGPU                                         其他語言
                                BitCode            →       CLI
                                                   →       PTX

      Arch.td             LibLLVM              regaloc=Pazzle|Graph
                                                     xxx.s
                        CodeGenerator          ObjectCodeEmitter
                Polly
將 LLVM 應用在非編譯器領域
LLVM 力:
「不是為了取悅硬體而寫編譯器,而為自己寫編譯器」
 • LLVM + Gallium3D: Mixing a Compiler With a
   Graphics Framework
   • http://people.freedesktop.org/~marcheu/fosdem09-g3dllvm.pdf


 • Runtime Code Generation for Huffman Decoders
    • “The speedup improvement is 23.2% at
      average and ranges from 32.2% to 14.2%.”
     http://solar.cslab.ece.ntua.gr/~kkourt/papers/huff-jit-report.pdf


 • A method for JIT’ing algorithms and data
   structures with LLVM
    • “For small AVL Trees (with less than ~3.000
      nodes), we can get an average performance
      of 26% over traditional method”
   • http://pyevolve.sourceforge.net/wordpress/?p=914
程式語言的變遷
(Low-Level 觀點 )
程式語言的變遷 ( 低階觀點 )
       • 職業無貴賤,程式語言是提出來解決人類面臨的
         問題之用             System Bus

                                       DMA        CPU              DSP

CPU       DMA
          DSP      Memory

                                      Memory
Interconnection network (BUS)                      Bridge
                                     controller
 DSP      Dedicated I/O
             IP
                                Peripherals

                                                  Peripheral Bus
               Programming Language
Indirection
 “All problems in computer
 science can be solved by
 another level of indirection."
      ~ Butler Lampson, 1972 ~
• UNIX v6 (1976) 提供以 C 語言重寫的作業系統
• 需求驅使的發展模式
  • 數學 / 工程 → Lisp → Lisp machine (?)
  • 軟體工程 → Smalltalk
  • 網際網路 → Java
遊戲產業驅使 Programming Language
      1972 Pong ( 硬體 )

      1980 Zork ( 高階直譯語言 )


      1993 DOOM (C)

      1998 Unreal (C++, Java-style scripting)


      2005-6 Xbox 360, PlayStation 3
      with 6-8 hardware threads

      2009 Next console generation.
      Unification of the CPU, GPU.
      Massive multi-core, data parallelism, etc.
今日的 Indirection 以 VM 形式存在
Charles Oliver Nutter (JRuby)
    “Building a Multilanguage VM” (2009)
    • Today, it is silly for a compiler to target actual hardware
       • Much more effective to target a VM
       • Writing a native compiler is lots more work!
    • Languages need runtime support
       • C runtime is tiny and portable (and wimpy)
       • More sophisticated language runtimes need
          • Memory management
          • Security
          • Reflection
          • Concurrency control
          • Libraries
          • Tools (debuggers, profilers, etc)
    • Many of these features are baked into VMs
Build Programming Language Runtime with LLVM
JVM vs. Java Language vs. Ruby
              Java language        Ruby language


                    Checked exceptions     Open classes
                    Generics               Dynamic typing
                    Enums                  'eval'
JVM 特徵              Overloading            Closures
                    Constructor chaining   Mixins
                    Program analysis       Rich set of literals
                    Primitive types+ops    Primitive types+ops
Primitive types+ops
                    Object model           Object model
Object model
                    Memory model           Memory model
Memory model
                    Dynamic linking        Dynamic linking
Dynamic linking
                    Access control         Access control
Access control
                    GC                     GC
GC
                    Unicode                Unicode
Unicode                                                        45
在 JVM 上實做 Ruby 語言 (JRuby)
       • 好處:
         • 利用既有 JVM 在平台優化的效能與彈性
         • 存取豐富的 Java 資源
       • 難處:
         • Dynamic typing 讓已有優化技術變得難以發揮
         • JRuby 必須維護自己的 type system
            • Reflection overhead
         • 無法以有效率且健全的方式來實做 Ruby
           ”eval”
The Da Vinci Machine Project
The Da Vinci Machine Project
http://openjdk.java.net/projects/mlvm/
http://openjdk.java.net/projects/mlvm/   46
OpenJDK Multi-language VM
OpenJDK Multi-language VM
在 LLVM 上實做 Ruby 語言
• 開放實做:
  • MacRuby : http://www.macruby.org/
  • Rubinius : http://rubini.us/
• 若充分對應到 LLVM 的設計,應可發揮若干效能
  的改善與 runtime support
  • Out-source'd JIT Runtime
  • Why MacRuby Matters (Present & Future)
   http://programmingzen.com/2009/03/29/why-macruby-matters/




                                                               47
48
http://programmingzen.com/images/macruby-large-chart.png
LLVM 與程式語言實做
•   LLVM 在 bitcode 層面即考慮到動態語言的需求
•   VMKit: Java, .Net
•   允許多個階段的優化 : profiler, offline optimizing
•   提供 Accurate Garbage Collection
•   在移動裝置上的 JIT compiler
     • [RenderScript] Android 3.0: 實做 code
       cache ,試圖降低編譯的成本
     • 目前 LLVM 的效能仍無法趕上若干特製的
       VM 。案例: IcedTea/Shark, Dalvik

                                         49
Build Programming Language
     Runtime with LLVM 實例
Brainfuck
• Brainfuck 是種極為精簡的程式語言,由 Urban
  Müller 發展。當初的目標為提出一種簡單的、可
  用最小的編譯器來實現、符合 Turing complete
  的程式
• Brainfuck 僅有八個指令,其中兩個是 I/O 動作




                             51
Brainfuck
• 對應到 C 語言:若 char *p 指向記憶體區塊的
  話, Brainfuck 語言的八個指令可對照為以下:
• Brainfuck      C
  >              ++p;
  <              ­­p;
  +              ++*p;
  ­              ­­*p;
  .              putchar(*p);
  ,              *p = getchar();
  [              while (*p) {
  ]              }               52
Brainfuck
• Brainfuck 語言
  +++++[­]          Brainfuck   C

• 等價於 C 語言          >           ++p;
                    <           ­­p;
  *p+=5;            +           ++*p;
  while(*p != 0){   ­           ­­*p;
                    .           putchar(*p);
      *p­­;         ,           *p = getchar();
  }                 [           while (*p) {
                    ]           }




                                               53
#include <stdio.h>
#include <stdio.h>
void foo(char c) { putchar(c); }
void foo(char c) { putchar(c); }


define void @foo(i8 signext %c) nounwind {
define void @foo(i8 signext %c) nounwind {
  %1 = alloca i8, align 1                         ; <i8*> [#uses=2]
  %1 = alloca i8, align 1                         ; <i8*> [#uses=2]
  store i8 %c, i8* %1
  store i8 %c, i8* %1
  %2 = load i8* %1                                ; <i8> [#uses=1]
  %2 = load i8* %1                                ; <i8> [#uses=1]
  %3 = sext i8 %2 to i32                          ; <i32> [#uses=1]
  %3 = sext i8 %2 to i32                          ; <i32> [#uses=1]
  %4 = call i32 @putchar(i32 %3)                  ; <i32> [#uses=0]
  %4 = call i32 @putchar(i32 %3)                  ; <i32> [#uses=0]
  ret void
  ret void
}
}

declare i32 @putchar(i32)
declare i32 @putchar(i32)


   // declare i32 @putchar(i32)
   Function* putchar = cast<Function>(
       module­>getOrInsertFunction(
           "putchar", voidType, cellType, NULL));
   putchar­>setCallingConv(CallingConv::C);


                                                                 54
                         呼叫底層系統 libc 的 putchar 函式
Function* makeFunc(Module* module,
                   const char* source,
                   int tapeSize = 400) {
  ...
  // declare i32 @getchar()
  Function* getchar = cast<Function>(
     module­>getOrInsertFunction("getchar", cellType, NULL));
  getchar­>setCallingConv(CallingConv::C);
  
  // declare i32 @putchar(i32)
  Function* putchar = cast<Function>(
     module­>getOrInsertFunction("putchar",
      voidType, cellType, NULL));
  putchar­>setCallingConv(CallingConv::C);

  // Contruct void main(char* tape)
  Function* main = cast<Function>(
     module­>getOrInsertFunction(“main", voidType, NULL));
  main­>setCallingConv(CallingConv::C);
  ...
                                                           55
               http://0xlab.org/~jserv/llvm/bf-llvm.cpp
Function* makeFunc(Module* module,
                   const char* source,
                   int tapeSize = 400) {
  ...
  Value* zero = ConstantInt::get(cellType, 0);
  Value* one = ConstantInt::get(cellType, 1);
  Value* minOne = ConstantInt::get(cellType, ­1);
  ...
                    在 LLVM IR 中,預先若干定義的常數
                    zero = 0, one =1, minOne = -1
  BasicBlock* block =
      BasicBlock::Create(getGlobalContext(), "code", main);
  std::stack<bfLoopInfo> loops;
  IRBuilder<> codeIR(block);
  Value *head = codeIR.CreateAlloca(cellType, 
      ConstantInt::get(indexType, tapeSize));
  Value *it = head;                             建立 LLVM IR
  for (int i = 0; i < tapeSize; i++) {
    codeIR.CreateStore(zero, it);
    it = codeIR.CreateGEP(it, one);                       56
  }
Function* makeFunc(Module* module,
                   const char* source,
                   int tapeSize = 400) {
  ...
  while(*source) {
    IRBuilder<> builder(block); 將 brainfuck 轉成 LLVM IR
    switch(*source++) {
      case '>':
        head = builder.CreateGEP(head, one);
        break;
      case '<':
        head = builder.CreateGEP(head, minOne);
        break;
      case '+': {
        Value *headValue = builder.CreateLoad(head);
        Value *result =
            builder.CreateAdd(headValue, one);
        builder.CreateStore(result, head);
        break;
      }                                           57
      ...
Function* makeFunc(Module* module,
                   const char* source,
                   int tapeSize = 400) {
      ...
      case '­': {
        Value *headValue = builder.CreateLoad(head);
        Value *result =
             builder.CreateSub(headValue, one);
        builder.CreateStore(result, head);
        break;
      }
      case '.': {
        Value* output = builder.CreateLoad(head);
        builder.CreateCall(putchar, output);
        break;
      }                     稍早準備的 putchar 函式
      ...


                                                 58
Function* makeFunc(Module* module,
                   const char* source,
                   int tapeSize = 400) {
  ...
  // Close the function
  IRBuilder<> builder(block);
  builder.CreateRetVoid();
  return main;
}

int main(int argc, char* argv[]) {
  ...
  // Setup a module and engine for JIT­ing
  std::string error;
  InitializeNativeTarget();
  Module* module = new Module("bfcode", getGlobalContext());
  ExecutionEngine *engine = EngineBuilder(module)
    .setErrorStr(&error)
    .setOptLevel(CodeGenOpt::Aggressive)
    .create();                                              59
                  開啟進階優化的 ExecutionEngine (JIT)
Function *makeFunc(Module *module,
                   const char *source,
                   int tapeSize = 400) {
  ...
}

int main(int argc, char* argv[]) {
  ...
  // Compile the Brainfuck to IR
  std::cout << "Parsing..." << std::flush;
  Function* func = makeFunc(module, source.c_str());

  // Run optimization passes
  std::cout << "Optimizing..." << std::flush;
  FunctionPassManager pm(module);
  pm.add(new TargetData(
      *(engine­>getTargetData())));
  pm.add(createVerifierPass());
  pm.run(*func);
  ...                                            60
                       讓 LLVM 串起整個編譯器架構
int main(int argc, char* argv[]) {
  ...
  // Compile
  std::cout << "Compiling..." << std::flush;
  void (*bf)() = (void (*)())
      engine­>getPointerToFunction(func);
  std::cout << " done" << std::endl;
 
  // and run!               讓 function pointer 指向經由 JIT
  bf();                     編譯過的機械碼

  return 0;
}




                                                     61
架構於 LLVM 的程式語言實做 (1)
• Unladen Swallow (Google): faster Python
 $ ./perf.py -r -b call_simple --args "-j always," 
            ../q2/python ../q3/python
  • Min: 1.618273 -> 0.908331: 78.16% faster
  • Avg: 1.632256 -> 0.924890: 76.48% faster
    http://code.google.com/p/unladen-swallow

• GHC/Haskell's LLVM codegen
  • 3x faster in some cases
    http://donsbot.wordpress.com/2010/02/21/
    smoking-fast-haskell-code-using-ghcs-new-llvm-codegen/

• LLVM-Lua : JIT/static Lua compiler
  • http://code.google.com/p/llvm-lua/
                                                             62
架構於 LLVM 的程式語言實做 (2)
• IcedTea Version of Sun's OpenJDK (RedHat)
   • Zero: processor-independent layer that allows
     OpenJDK to build and run using any processor
   • Shark: Zero's JIT compiler: uses LLVM to provide
     native code generation without introducing
     processor-dependent code.
     http://icedtea.classpath.org
• Emscripten
   • LLVM-to-JavaScript compiler
   • It takes LLVM bitcode and compiles that into
     JavaScript, which can be run on the web (or
     anywhere else JavaScript can run).
     http://code.google.com/p/emscripten/
                                                    63
http://0xlab.org

More Related Content

PDF
Introduction to eBPF and XDP
PDF
ELFの動的リンク
PDF
ROS 2 Client Library for E^2
PDF
Vivado hls勉強会1(基礎編)
PDF
LLVM Register Allocation (2nd Version)
PPTX
DMA Survival Guide
PDF
The linux networking architecture
PDF
The Internals of "Hello World" Program
Introduction to eBPF and XDP
ELFの動的リンク
ROS 2 Client Library for E^2
Vivado hls勉強会1(基礎編)
LLVM Register Allocation (2nd Version)
DMA Survival Guide
The linux networking architecture
The Internals of "Hello World" Program

What's hot (20)

ODP
SR-IOV Introduce
PDF
EVPN for Cloud Builders
PDF
05.2 virtio introduction
PPTX
Linux MMAP & Ioremap introduction
PDF
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
PDF
Making Linux do Hard Real-time
PDF
Rust で RTOS を考える
PDF
BPF: Tracing and more
PDF
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
PDF
Zebra SRv6 CLI on Linux Dataplane (ENOG#49)
PDF
Hyper vを理解する
PDF
OpenJDK トラブルシューティング #javacasual
PDF
Linux BPF Superpowers
PDF
OverlayFS as a Docker Storage Driver
POTX
Performance Tuning EC2 Instances
PDF
Embedded Virtualization applied in Mobile Devices
PDF
A whirlwind tour of the LLVM optimizer
PDF
Linux Profiling at Netflix
PPTX
Monitoring Apache Kafka
PDF
Container Performance Analysis
SR-IOV Introduce
EVPN for Cloud Builders
05.2 virtio introduction
Linux MMAP & Ioremap introduction
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
Making Linux do Hard Real-time
Rust で RTOS を考える
BPF: Tracing and more
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
Zebra SRv6 CLI on Linux Dataplane (ENOG#49)
Hyper vを理解する
OpenJDK トラブルシューティング #javacasual
Linux BPF Superpowers
OverlayFS as a Docker Storage Driver
Performance Tuning EC2 Instances
Embedded Virtualization applied in Mobile Devices
A whirlwind tour of the LLVM optimizer
Linux Profiling at Netflix
Monitoring Apache Kafka
Container Performance Analysis
Ad

Viewers also liked (19)

PDF
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
PPTX
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
PDF
Phpcompilerinternals 090824022750-phpapp02
PDF
PPTX
PHP Internals
PDF
PHP, Under The Hood - DPC
PPT
The Php Life Cycle
PPTX
Php Extensions for Dummies
PDF
Under the Hood of the Testarossa JIT Compiler
PDF
PHP 7 new engine
PPT
How PHP Works ?
PDF
JVM JIT-compiler overview @ JavaOne Moscow 2013
PDF
淺談編譯器最佳化技術
ODP
About Tokens and Lexemes
PDF
from Source to Binary: How GNU Toolchain Works
PDF
JVM JIT compilation overview by Vladimir Ivanov
PDF
Interpreter, Compiler, JIT from scratch
PPTX
Recognition-of-tokens
PPTX
Compiler Chapter 1
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
Phpcompilerinternals 090824022750-phpapp02
PHP Internals
PHP, Under The Hood - DPC
The Php Life Cycle
Php Extensions for Dummies
Under the Hood of the Testarossa JIT Compiler
PHP 7 new engine
How PHP Works ?
JVM JIT-compiler overview @ JavaOne Moscow 2013
淺談編譯器最佳化技術
About Tokens and Lexemes
from Source to Binary: How GNU Toolchain Works
JVM JIT compilation overview by Vladimir Ivanov
Interpreter, Compiler, JIT from scratch
Recognition-of-tokens
Compiler Chapter 1
Ad

Similar to Build Programming Language Runtime with LLVM (20)

PDF
Android Optimization: Myth and Reality
PPTX
Node.js meetup at Palo Alto Networks Tel Aviv
PDF
Isomorphic JavaScript with Nashorn
PDF
The Netflix API Platform for Server-Side Scripting
PDF
Os Lattner
PDF
olibc: Another C Library optimized for Embedded Linux
PDF
Flex For Java Architects Ledroff Breizh Jug V Blog Cc
PDF
Tech talk with lampro mellon an open source solution for accelerating verific...
PDF
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
PDF
POCO C++ Libraries Intro and Overview
PDF
Learn OpenStack from trystack.cn ——Folsom in practice
PPTX
(java2days) Is the Future of Java Cloudy?
PDF
Android RenderScript on LLVM
PPTX
Mastering Chaos - A Netflix Guide to Microservices
PPT
Introdot Netc Sharp En
KEY
Using Smalltalk for controlling robotics systems
PPTX
Cont0519
PDF
QConSF2016-JoshEvans-MasteringChaosANetflixGuidetoMicroservices-compressed.pdf
PPTX
From nothing to production in 1 hour
Android Optimization: Myth and Reality
Node.js meetup at Palo Alto Networks Tel Aviv
Isomorphic JavaScript with Nashorn
The Netflix API Platform for Server-Side Scripting
Os Lattner
olibc: Another C Library optimized for Embedded Linux
Flex For Java Architects Ledroff Breizh Jug V Blog Cc
Tech talk with lampro mellon an open source solution for accelerating verific...
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
POCO C++ Libraries Intro and Overview
Learn OpenStack from trystack.cn ——Folsom in practice
(java2days) Is the Future of Java Cloudy?
Android RenderScript on LLVM
Mastering Chaos - A Netflix Guide to Microservices
Introdot Netc Sharp En
Using Smalltalk for controlling robotics systems
Cont0519
QConSF2016-JoshEvans-MasteringChaosANetflixGuidetoMicroservices-compressed.pdf
From nothing to production in 1 hour

More from National Cheng Kung University (20)

PDF
PyPy's approach to construct domain-specific language runtime
PDF
2016 年春季嵌入式作業系統課程說明
PDF
進階嵌入式作業系統設計與實做 (2015 年秋季 ) 課程說明
PDF
Construct an Efficient and Secure Microkernel for IoT
PDF
How A Compiler Works: GNU Toolchain
PDF
Virtual Machine Constructions for Dummies
PDF
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
PDF
從線上售票看作業系統設計議題
PDF
進階嵌入式系統開發與實做 (2014 年秋季 ) 課程說明
PDF
Xvisor: embedded and lightweight hypervisor
PDF
Making Linux do Hard Real-time
PDF
Implement Runtime Environments for HSA using LLVM
PDF
Priority Inversion on Mars
PDF
Develop Your Own Operating Systems using Cheap ARM Boards
PDF
Lecture notice about Embedded Operating System Design and Implementation
PDF
Explore Android Internals
PDF
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學
PDF
F9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
PDF
Open Source from Legend, Business, to Ecosystem
PDF
Summer Project: Microkernel (2013)
PyPy's approach to construct domain-specific language runtime
2016 年春季嵌入式作業系統課程說明
進階嵌入式作業系統設計與實做 (2015 年秋季 ) 課程說明
Construct an Efficient and Secure Microkernel for IoT
How A Compiler Works: GNU Toolchain
Virtual Machine Constructions for Dummies
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
從線上售票看作業系統設計議題
進階嵌入式系統開發與實做 (2014 年秋季 ) 課程說明
Xvisor: embedded and lightweight hypervisor
Making Linux do Hard Real-time
Implement Runtime Environments for HSA using LLVM
Priority Inversion on Mars
Develop Your Own Operating Systems using Cheap ARM Boards
Lecture notice about Embedded Operating System Design and Implementation
Explore Android Internals
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學
F9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
Open Source from Legend, Business, to Ecosystem
Summer Project: Microkernel (2013)

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
KodekX | Application Modernization Development
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Cloud computing and distributed systems.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Understanding_Digital_Forensics_Presentation.pptx
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Chapter 3 Spatial Domain Image Processing.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 2 Digital Image Fundamentals.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
Cloud computing and distributed systems.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Build Programming Language Runtime with LLVM