SlideShare a Scribd company logo
1 of 60
Download to read offline
身騎 LLVM ,過三關
淺談編譯器技術的嶄新應用




         Jim Huang ( 黃敬群 ) “jserv”
      website: http://jserv.sayya.org/
   blog: http://blog.linux.org.tw/jserv/
            May 5, 2009 @ TOSSUG
姊妹議程:
                              〈窮得只剩下 Compiler 〉
                                  OOSDC.tw 2009
http://www.slideshare.net/jserv/what-can-compilers-do-for-us
公孫龍子:「白馬非馬」
「馬」 指的是馬的形態,「白馬」指的是馬的顏色,
而形態不等於顏色,所以白馬不是馬

LLVM 不是 VM ,也不只是 Compiler




          http://p5.p.pixnet.net/albums/userpics/5/8/270458/1208944435.jpg
編譯器技術領導的時代
運算模式已大幅改觀
 Framework-driven
 SIMD/vectorization, Cell, muticore/SMP, ...
虛擬化 (Virtualization) 技術的時代:更多元、
更安全、更有效率地使用硬體
資訊技術的雜交 (cross-over)
LLVM 正夯!
提綱
Compiler 大不同:走向 Binary translation,
virtualization, JIT/VM 等嶄新的應用模式
LLVM 簡介
 虛擬指令集 /IR
 高度整合的編譯器技術元件 (analyzer, code
 emitter/generator, JIT compiler, GC)
 完整的工具集合 (assembler, debugger,
 linker)
技術展示 (from 0xlab)
淺談開發趨勢
隱藏在我們周遭的編譯器
       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
       繪圖軟體: Adobe PixelBender, Shader
       3D 高品質圖形處理: Gallium3D / OpenGL /
       Direct3D
Apple Inc. 是 LLVM 開發最主要的贊助廠商,僱用 Chris Lattner
多才多藝的
LLVM
「快快樂樂寫
 Compiler 」
        遜!
「快快樂樂玩
 Compiler 」
 讓 LLVM 拉你一把
LLVM =
Low Level Virtual Machine
  三特性、三元素、三頭六臂
LLVM =
Low Level Virtual Machine
         此 VM 非彼 VM
“LLVM does not imply things that you would expect from a
    high-level virtual machine. It does not require garbage
collection or run-time code generation (In fact, LLVM makes a
 great static compiler!). Note that optional LLVM components
  can be used to build high-level virtual machines and other
               systems that need these services.”
                       http://www.llvm.org/
LLVM 三大特性
Low-Level VM

完整的編譯器基礎建設
 可重用的、用以建構編譯器的軟體元件 (compiler
 compiler 顯然不足以應付 )
 允許更快更完整的打造新的編譯器
 static compiler, JIT, trace-based optimizer, ...

開放的編譯器框架
 多種程式語言支援
 高彈性的自由軟體授權模式 (BSD License)
 豐富的編譯輸出: C, machine code (Alpha, ARM, x86,
 Sparc, PowerPC, Cell SPU, 台灣心 Andes Core)
LLVM 三大元件
RISC 式虛擬指令集 (instruction set)
 多種語言適用、與硬體架構無關的
 IR(Intermediate Representation)

完整的高度整合函式庫與編譯器服務
 Analyses, optimizations, code generators,
 JIT compiler, garbage collection support,
 profiling, …

豐富的工具集
 Assemblers, automatic debugger, linker,
 code generator, compiler driver, modular
 optimizer, …
RISC 式虛擬指令集 (instruction set)
完整的高度整合函式庫與編譯器服務
豐富的工具集
LLVM 的 Low-Level VM
            IR 是 Compiler 的心臟
RISC 式虛擬指令集 (instruction set)
  約 50 個 opcedes
  豐富的 Intrinsic Function:llvm.*

SSA (Static Single Assignment Form)
  作為程式語言優化的基礎
允許整體的優化
Offline code generation and optimization
  Install-time target-specific optimization
  Link-Time interprocedural Optimization (LTO)
   whole program analysis
User-based profiling and optimization
  run-time & idle time
LLVM 的 Low-Level VM
        IR 是 Compiler 的心臟
RISC 式虛擬指令集 (instruction set)
SSA (Static Single Assignment Form)
允許整體的優化
LLVM:
三頭六臂
Frontend   LLVM IR   Backend
Frontend   LLVM IR   Backend
一系列變換處理的
API 與銜接的服務
優化與轉換程序




中間輸出與
生成處理
C file         llvmgcc        .o file        C++ file          llvmg++        .o file




 C to LLVM               Compile-time          C++ to LLVM            Compile-time
  Frontend                Optimizer             Frontend               Optimizer

    “cc1”                 “gccas”              “cc1plus”                 “gccas”



            LLVM IR         LLVM         LLVM Analysis &             LLVM .bc
             Parser         Verifier    Optimization Passes          File Writer

    Modified version of GCC                             Modified version of G++
    Emits LLVM IR as text file                          Emits LLVM IR as text file
    Lowers C AST to LLVM                                Lowers C++ AST to LLVM



               Dead Global Elimination, IP Constant Propagation, Dead
            Argument Elimination, Inlining, Reassociation, LICM, Loop Opts,
                Memory Promotion, Dead Store Elimination, ADCE, …
gcc                     llvm-gcc




        %tmp2 = sub i32 %tmp1, 1
           ; <i32> [#uses=1]
        %tmp3 = call i32 (...)* bitcast (i32 (i32)* @fib to
        i32 (...)*)(...)
        %tmp2 ) nounwind     ; <i32> [#uses=1]



           .bc (LLVM
           BitCode)


llvm-gcc -emit-llvm
等等,頭暈了
 LLVM 到底能作
    什麼?
這是一個到處都有虛擬機器 (VM) 的時代
↑JavaScript + DHTML (JavaVM
    Implementation)

↓Java ME
It works on iPhone!




But you can't control because iphone has no keyboard;(
(VM 之 ) 道在屎溺
       Shader / Raytracing (OpenGL/DirectX)
       Web Browser / Adobe Flash
       Android Dalvik VM
       ...
手法:
➔
 JIT : Just-In-Time (Jizz In [My Pants]Time?!) compiler
➔
 IR to backend translation / code generation

       「你想要的,
       LLVM 都給你!」
編譯器流程 ::JIT
AST                    compiler                  checker
           .c
            .c
                                    exe
                                     exe
              .c
               .c
                                       exe
                                        exe
                 .c
                  .c
                                          exe
                                           exe




程式碼的編譯與生成                         執行、評估,與驗證

  建立 IR
  載入必要的函式庫
  連結程式模組
  Optimizations + Transforms
  codegen
                                                           32
當 LLVM 煞到
    OpenGL Shader


動態編譯器就在眼前
Pixel shader
PouetLink: www.pouet.net/prod.php?which=50865
Cocoon website: cocoon.planet-d.net
3D/Shader 的考量點
         光
         反射
         移動的演算法

Reyes(scanline,polygon)   Raytracing




 可引入 SIMD 優化
surface                                  push N
matte(float Ka = 1.0; float Kd = 1.1;)   normalize
{                                        push I
  normal Nf = faceforward(               faceforward
              normalize(N), I);          ...
  Oi = Os;
  Ci = Os * Cs * (Ka * ambient() +
                  Kd * diffuse(Nf));  
 
}
surface                                  push N
matte(float Ka = 1.0; float Kd = 1.1;)   normalize
{                                        push I
  normal Nf = faceforward(               faceforward
              normalize(N), I);          ...
  Oi = Os;
  Ci = Os * Cs * (Ka * ambient() +
                  Kd * diffuse(Nf));  
 
}


   Raytracing 的難題
   無法善用 SIMD
   運算相依性高且繁瑣
   需要動態調整快速運算
   的路徑
Specialize       以 color space 轉換來說,相當大量
                 且繁瑣的運算,如
  技巧             BGRA 444R --> RGBA 8888




 Speedup depends on src/dest format:
 – 5.4x speedup on average, 19.3x max
 speedup: (13.3MB/s to 257.7MB/s)
Mandelbort 碎形運算透過 LLVM JIT 後,
提昇效能達到 11 倍
LLVM 與繪圖處理的應用
手機平台: Android PixelFlinger
繪圖軟體: Adobe PixelBender, Shader
3D 高品質圖形處理: Gallium3D / OpenGL
Android OpenGL|ES
當 LLVM 煞到
       Adobe Flash


安全多元的享受 C/C++ 的美好
有時,你不會想看到遙遙 ...
     ( 不安全、不舒服的 C 語言 )
「可是,男人都想要當
   慣C」
承認吧,慣 C 有很多好處,所以
Adobe 建立 Alchemy(FlaCC) 專案
             當然啦,慣 C 的風險也很高
             「學長,我不敢修電腦」
虛擬機器: AVM2
Adobe Alchemy
實驗性的 LLVM Backend
LLVM → ABC (ActionScript ByteCode)
ABC 類似精簡的 x86 指令集
提供 POSIX 模擬層與完整的 BSD libc + GNU
ISO C++ library
足以執行若干重要的應用程式
背景知識
 from Adobe, Mozilla, and Tamarin
  http://hecker.org/mozilla/adobe-mozilla-and-tamarin
 Flash player 9 在 AVM2 內建 JIT-based VM
    Tamarin 由 Adobe 捐贈予 Mozilla
    Flash player 9 = AVM2 + graphics, musics,
    videos, networking, etc components
ActionScript 3.0 (Flash 5 對 ActionScript 的稱呼 )
 JavaScript 的姊妹規格
 參考 ECMAScript 4 (ECMA-262)
 Free Flex 2. SDK (foo.as -> foo.swf)
 Flash player 9 內部具備 ABC 執行引擎
Tamarin
ECMAScript version 4 (ES4) 的高速實做
Tamarin VM, Mozilla, JavaScript,
SpiderMonkey, Adobe Flash Player, AVM
AVMPlus
AVMPlus (VM library), MMgc (garbage
collection library), avmplus
Avmplus, ABC, JIT
Adobe Flex 2 SDK includes ASC (ActionScript
Compiler)
當 LLVM 煞到
         Android


安全多元的享受 C/C++ 的美好
0xlab LLVM hacks
實驗性的 LLVM Backend
LLVM → Android Dalvik bytecode (DEX)
仿效 Adobe Alchemy ,目標平台則是
DalvikVM
提供 C/C++ 原始碼 → LLVM → DEX
bytecode 的執行環境,無 JNI (Java Native
Interface) 介入

 「我只會寫 Java VM ,不會寫 Java
 應用程式」,宅色夫 (2005)
application.
apk

Dalvik VM
技術大融通
             與
     未來展望
「你的時代到了!」 LLVM 一統天下
LLVM 的時代到了!
clang: 嶄新的 C/C++/Objective-C 語言前端
vmkit: Java/.Net 虛擬機器
llvm + OpenGL
Sun OpenJDK → RedHat Zero/Shark
Trident: VHDL compiler for FPGA
llvmruby, yarv2llvm, RubyComp, MacRuby
PyPy, Google unladen-Swallow
HLVM: Haskell, Ocaml, ...
自由軟體編譯器技術的一統標準已定!
多元的整合,如 clang 可作為 static
compiler ,亦可挪用為 JIT compiler
技術集中,創意多元
參考資料
〈 LLVM and Clang:
Next Generation Compiler Technology 〉 , Chris Lattner
BSDCan 2008


《美麗程式》 (Beautiful Code), O'Reilly
 第八章〈動態產生影像處理程式〉

LLVM – http://www.llvm.org/
Projects built with LLVM – http://www.llvm.org/ProjectsWithLLVM/
Open LLVM Projects – http://www.llvm.org/OpenProjects.html
UI customizing



   Platform Builder
                      三個願望一次滿足
                      一系列的自由軟體
                      四月 27 日開張
   Device Potential
                      ( 42710=0x1ab )
                      http://0xlab.org


感謝您!

More Related Content

What's hot

PHP 良好實踐 (Best Practice)
PHP 良好實踐 (Best Practice)PHP 良好實踐 (Best Practice)
PHP 良好實踐 (Best Practice)Win Yu
 
Javaバイトコード入門
Javaバイトコード入門Javaバイトコード入門
Javaバイトコード入門Kota Mizushima
 
Jenkinsfileのlintで救える命がある
Jenkinsfileのlintで救える命があるJenkinsfileのlintで救える命がある
Jenkinsfileのlintで救える命があるJumpei Miyata
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Jian-Hong Pan
 
用十分鐘 向jserv學習作業系統設計
用十分鐘  向jserv學習作業系統設計用十分鐘  向jserv學習作業系統設計
用十分鐘 向jserv學習作業系統設計鍾誠 陳鍾誠
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介Akira Maruoka
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...Linaro
 
Design and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationDesign and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationKito Cheng
 
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)Mr. Vengineer
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux SystemJian-Hong Pan
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performanceintelliyole
 
Boostのあるプログラミング生活
Boostのあるプログラミング生活Boostのあるプログラミング生活
Boostのあるプログラミング生活Akira Takahashi
 

What's hot (20)

PHP 良好實踐 (Best Practice)
PHP 良好實踐 (Best Practice)PHP 良好實踐 (Best Practice)
PHP 良好實踐 (Best Practice)
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Worksfrom Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
 
Javaバイトコード入門
Javaバイトコード入門Javaバイトコード入門
Javaバイトコード入門
 
Plan 9: Not (Only) A Better UNIX
Plan 9: Not (Only) A Better UNIXPlan 9: Not (Only) A Better UNIX
Plan 9: Not (Only) A Better UNIX
 
Jenkinsfileのlintで救える命がある
Jenkinsfileのlintで救える命があるJenkinsfileのlintで救える命がある
Jenkinsfileのlintで救える命がある
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
用十分鐘 向jserv學習作業系統設計
用十分鐘  向jserv學習作業系統設計用十分鐘  向jserv學習作業系統設計
用十分鐘 向jserv學習作業系統設計
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 
C++11
C++11C++11
C++11
 
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
 
TMUX Rocks!
TMUX Rocks!TMUX Rocks!
TMUX Rocks!
 
Design and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationDesign and Implementation of GCC Register Allocation
Design and Implementation of GCC Register Allocation
 
LLVM
LLVMLLVM
LLVM
 
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)
ZynqMPのブートとパワーマネージメント : (ZynqMP Boot and Power Management)
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux System
 
GIT_In_90_Minutes
GIT_In_90_MinutesGIT_In_90_Minutes
GIT_In_90_Minutes
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
Boostのあるプログラミング生活
Boostのあるプログラミング生活Boostのあるプログラミング生活
Boostのあるプログラミング生活
 

Viewers also liked

LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例National Cheng Kung University
 
LLVM Compiler - Link Time Optimization
LLVM Compiler - Link Time OptimizationLLVM Compiler - Link Time Optimization
LLVM Compiler - Link Time OptimizationVivek Pansara
 
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...Akihiro Hayashi
 
Как приручить дракона: введение в LLVM
Как приручить дракона: введение в LLVMКак приручить дракона: введение в LLVM
Как приручить дракона: введение в LLVMTech Talks @NSU
 
LLVM Internal Architecture par Michel Guillet
LLVM Internal Architecture par Michel GuilletLLVM Internal Architecture par Michel Guillet
LLVM Internal Architecture par Michel GuilletCocoaHeads France
 
Clang Analyzer Tool Review
Clang Analyzer Tool ReviewClang Analyzer Tool Review
Clang Analyzer Tool ReviewDoug Schuster
 
Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvmTao He
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLinaro
 
Introduction to the LLVM Compiler System
Introduction to the LLVM  Compiler SystemIntroduction to the LLVM  Compiler System
Introduction to the LLVM Compiler Systemzionsaint
 
BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 Linaro
 

Viewers also liked (12)

LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
 
LLVM Compiler - Link Time Optimization
LLVM Compiler - Link Time OptimizationLLVM Compiler - Link Time Optimization
LLVM Compiler - Link Time Optimization
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
 
Как приручить дракона: введение в LLVM
Как приручить дракона: введение в LLVMКак приручить дракона: введение в LLVM
Как приручить дракона: введение в LLVM
 
LLVM Internal Architecture par Michel Guillet
LLVM Internal Architecture par Michel GuilletLLVM Internal Architecture par Michel Guillet
LLVM Internal Architecture par Michel Guillet
 
LLVM Overview
LLVM OverviewLLVM Overview
LLVM Overview
 
Clang Analyzer Tool Review
Clang Analyzer Tool ReviewClang Analyzer Tool Review
Clang Analyzer Tool Review
 
Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvm
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
 
Introduction to the LLVM Compiler System
Introduction to the LLVM  Compiler SystemIntroduction to the LLVM  Compiler System
Introduction to the LLVM Compiler System
 
BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2
 

Similar to LLVM introduction

Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure National Cheng Kung University
 
COSCUP 2016 - LLVM 由淺入淺
COSCUP 2016 - LLVM 由淺入淺COSCUP 2016 - LLVM 由淺入淺
COSCUP 2016 - LLVM 由淺入淺宗凡 楊
 
COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺hydai
 
Hcsm lect-20120913
Hcsm lect-20120913Hcsm lect-20120913
Hcsm lect-20120913lusecheng
 
Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化Harvey Zhang
 
網路技術心得分享
網路技術心得分享網路技術心得分享
網路技術心得分享Mux Baxer
 
部門會議 950619 Leon的錦囊妙計
部門會議 950619 Leon的錦囊妙計部門會議 950619 Leon的錦囊妙計
部門會議 950619 Leon的錦囊妙計Leon Chuang
 
Beyond rails server
Beyond rails serverBeyond rails server
Beyond rails serverMichael Chen
 
GDC Taipei Summit review 2012
GDC Taipei Summit review 2012GDC Taipei Summit review 2012
GDC Taipei Summit review 2012Victor Lee
 
2012 java two-desktop-appliction-using-j-ruby-with-swt
2012 java two-desktop-appliction-using-j-ruby-with-swt2012 java two-desktop-appliction-using-j-ruby-with-swt
2012 java two-desktop-appliction-using-j-ruby-with-swttka
 
Non-MVC Web Framework
Non-MVC Web FrameworkNon-MVC Web Framework
Non-MVC Web FrameworkFred Chien
 
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物Wei-Ning Huang
 
Introduction of Reverse Engineering
Introduction of Reverse EngineeringIntroduction of Reverse Engineering
Introduction of Reverse EngineeringYC Ling
 
Graphic programming in js
Graphic programming in jsGraphic programming in js
Graphic programming in jsjay li
 
Nodejs部门分享
Nodejs部门分享Nodejs部门分享
Nodejs部门分享zffl
 
C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述Xiaozhe Wang
 
C++工程实践
C++工程实践C++工程实践
C++工程实践Shuo Chen
 
JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)
JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)
JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)Will Huang
 

Similar to LLVM introduction (20)

Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
 
COSCUP 2016 - LLVM 由淺入淺
COSCUP 2016 - LLVM 由淺入淺COSCUP 2016 - LLVM 由淺入淺
COSCUP 2016 - LLVM 由淺入淺
 
COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺
 
Hcsm lect-20120913
Hcsm lect-20120913Hcsm lect-20120913
Hcsm lect-20120913
 
Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化Avm2虚拟机浅析与as3性能优化
Avm2虚拟机浅析与as3性能优化
 
網路技術心得分享
網路技術心得分享網路技術心得分享
網路技術心得分享
 
部門會議 950619 Leon的錦囊妙計
部門會議 950619 Leon的錦囊妙計部門會議 950619 Leon的錦囊妙計
部門會議 950619 Leon的錦囊妙計
 
Beyond rails server
Beyond rails serverBeyond rails server
Beyond rails server
 
GDC Taipei Summit review 2012
GDC Taipei Summit review 2012GDC Taipei Summit review 2012
GDC Taipei Summit review 2012
 
2012 java two-desktop-appliction-using-j-ruby-with-swt
2012 java two-desktop-appliction-using-j-ruby-with-swt2012 java two-desktop-appliction-using-j-ruby-with-swt
2012 java two-desktop-appliction-using-j-ruby-with-swt
 
Non-MVC Web Framework
Non-MVC Web FrameworkNon-MVC Web Framework
Non-MVC Web Framework
 
Inside VCL
Inside VCLInside VCL
Inside VCL
 
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
EMSCRIPTEN: 將應用快速 PORT 到 JAVASCRIPT 上的神物
 
Introduction of Reverse Engineering
Introduction of Reverse EngineeringIntroduction of Reverse Engineering
Introduction of Reverse Engineering
 
Jobforcompal
JobforcompalJobforcompal
Jobforcompal
 
Graphic programming in js
Graphic programming in jsGraphic programming in js
Graphic programming in js
 
Nodejs部门分享
Nodejs部门分享Nodejs部门分享
Nodejs部门分享
 
C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述
 
C++工程实践
C++工程实践C++工程实践
C++工程实践
 
JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)
JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)
JavaScript 物件導向觀念入門 v.s. TypeScript 開發實戰 (微軟實戰課程日)
 

More from National Cheng Kung University

PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimeNational Cheng Kung University
 
進階嵌入式作業系統設計與實做 (2015 年秋季 ) 課程說明
進階嵌入式作業系統設計與實做 (2015 年秋季 ) 課程說明進階嵌入式作業系統設計與實做 (2015 年秋季 ) 課程說明
進階嵌入式作業系統設計與實做 (2015 年秋季 ) 課程說明National Cheng Kung University
 
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明National Cheng Kung University
 
進階嵌入式系統開發與實做 (2014 年秋季 ) 課程說明
進階嵌入式系統開發與實做 (2014 年秋季 ) 課程說明進階嵌入式系統開發與實做 (2014 年秋季 ) 課程說明
進階嵌入式系統開發與實做 (2014 年秋季 ) 課程說明National Cheng Kung University
 
Develop Your Own Operating Systems using Cheap ARM Boards
Develop Your Own Operating Systems using Cheap ARM BoardsDevelop Your Own Operating Systems using Cheap ARM Boards
Develop Your Own Operating Systems using Cheap ARM BoardsNational Cheng Kung University
 
Lecture notice about Embedded Operating System Design and Implementation
Lecture notice about Embedded Operating System Design and ImplementationLecture notice about Embedded Operating System Design and Implementation
Lecture notice about Embedded Operating System Design and ImplementationNational Cheng Kung University
 
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學
中輟生談教育: 完全用開放原始碼軟體進行 嵌入式系統教學National Cheng Kung University
 
F9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
F9: A Secure and Efficient Microkernel Built for Deeply Embedded SystemsF9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
F9: A Secure and Efficient Microkernel Built for Deeply Embedded SystemsNational Cheng Kung University
 
進階嵌入式系統開發與實作 (2013 秋季班 ) 課程說明
進階嵌入式系統開發與實作 (2013 秋季班 ) 課程說明進階嵌入式系統開發與實作 (2013 秋季班 ) 課程說明
進階嵌入式系統開發與實作 (2013 秋季班 ) 課程說明National Cheng Kung University
 

More from National Cheng Kung University (20)

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

LLVM introduction

  • 1. 身騎 LLVM ,過三關 淺談編譯器技術的嶄新應用 Jim Huang ( 黃敬群 ) “jserv” website: http://jserv.sayya.org/ blog: http://blog.linux.org.tw/jserv/ May 5, 2009 @ TOSSUG
  • 2. 姊妹議程: 〈窮得只剩下 Compiler 〉 OOSDC.tw 2009 http://www.slideshare.net/jserv/what-can-compilers-do-for-us
  • 3.
  • 5. 編譯器技術領導的時代 運算模式已大幅改觀 Framework-driven SIMD/vectorization, Cell, muticore/SMP, ... 虛擬化 (Virtualization) 技術的時代:更多元、 更安全、更有效率地使用硬體 資訊技術的雜交 (cross-over) LLVM 正夯!
  • 6. 提綱 Compiler 大不同:走向 Binary translation, virtualization, JIT/VM 等嶄新的應用模式 LLVM 簡介 虛擬指令集 /IR 高度整合的編譯器技術元件 (analyzer, code emitter/generator, JIT compiler, GC) 完整的工具集合 (assembler, debugger, linker) 技術展示 (from 0xlab) 淺談開發趨勢
  • 7. 隱藏在我們周遭的編譯器 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 繪圖軟體: Adobe PixelBender, Shader 3D 高品質圖形處理: Gallium3D / OpenGL / Direct3D Apple Inc. 是 LLVM 開發最主要的贊助廠商,僱用 Chris Lattner
  • 10. 「快快樂樂玩 Compiler 」 讓 LLVM 拉你一把
  • 11. LLVM = Low Level Virtual Machine 三特性、三元素、三頭六臂
  • 12. LLVM = Low Level Virtual Machine 此 VM 非彼 VM “LLVM does not imply things that you would expect from a high-level virtual machine. It does not require garbage collection or run-time code generation (In fact, LLVM makes a great static compiler!). Note that optional LLVM components can be used to build high-level virtual machines and other systems that need these services.” http://www.llvm.org/
  • 13. LLVM 三大特性 Low-Level VM 完整的編譯器基礎建設 可重用的、用以建構編譯器的軟體元件 (compiler compiler 顯然不足以應付 ) 允許更快更完整的打造新的編譯器 static compiler, JIT, trace-based optimizer, ... 開放的編譯器框架 多種程式語言支援 高彈性的自由軟體授權模式 (BSD License) 豐富的編譯輸出: C, machine code (Alpha, ARM, x86, Sparc, PowerPC, Cell SPU, 台灣心 Andes Core)
  • 14. LLVM 三大元件 RISC 式虛擬指令集 (instruction set) 多種語言適用、與硬體架構無關的 IR(Intermediate Representation) 完整的高度整合函式庫與編譯器服務 Analyses, optimizations, code generators, JIT compiler, garbage collection support, profiling, … 豐富的工具集 Assemblers, automatic debugger, linker, code generator, compiler driver, modular optimizer, …
  • 15. RISC 式虛擬指令集 (instruction set) 完整的高度整合函式庫與編譯器服務 豐富的工具集
  • 16. LLVM 的 Low-Level VM IR 是 Compiler 的心臟 RISC 式虛擬指令集 (instruction set) 約 50 個 opcedes 豐富的 Intrinsic Function:llvm.* SSA (Static Single Assignment Form) 作為程式語言優化的基礎 允許整體的優化 Offline code generation and optimization Install-time target-specific optimization Link-Time interprocedural Optimization (LTO) whole program analysis User-based profiling and optimization run-time & idle time
  • 17. LLVM 的 Low-Level VM IR 是 Compiler 的心臟 RISC 式虛擬指令集 (instruction set) SSA (Static Single Assignment Form) 允許整體的優化
  • 19. Frontend LLVM IR Backend
  • 20. Frontend LLVM IR Backend
  • 23.
  • 24. C file llvmgcc .o file C++ file llvmg++ .o file C to LLVM Compile-time C++ to LLVM Compile-time Frontend Optimizer Frontend Optimizer “cc1” “gccas” “cc1plus” “gccas” LLVM IR LLVM LLVM Analysis & LLVM .bc Parser Verifier Optimization Passes File Writer Modified version of GCC Modified version of G++ Emits LLVM IR as text file Emits LLVM IR as text file Lowers C AST to LLVM Lowers C++ AST to LLVM Dead Global Elimination, IP Constant Propagation, Dead Argument Elimination, Inlining, Reassociation, LICM, Loop Opts, Memory Promotion, Dead Store Elimination, ADCE, …
  • 25. gcc llvm-gcc %tmp2 = sub i32 %tmp1, 1 ; <i32> [#uses=1] %tmp3 = call i32 (...)* bitcast (i32 (i32)* @fib to i32 (...)*)(...) %tmp2 ) nounwind ; <i32> [#uses=1] .bc (LLVM BitCode) llvm-gcc -emit-llvm
  • 26. 等等,頭暈了 LLVM 到底能作 什麼? 這是一個到處都有虛擬機器 (VM) 的時代
  • 27.
  • 28. ↑JavaScript + DHTML (JavaVM Implementation) ↓Java ME
  • 29.
  • 30. It works on iPhone! But you can't control because iphone has no keyboard;(
  • 31. (VM 之 ) 道在屎溺 Shader / Raytracing (OpenGL/DirectX) Web Browser / Adobe Flash Android Dalvik VM ... 手法: ➔ JIT : Just-In-Time (Jizz In [My Pants]Time?!) compiler ➔ IR to backend translation / code generation 「你想要的, LLVM 都給你!」
  • 32. 編譯器流程 ::JIT AST compiler checker .c .c exe exe .c .c exe exe .c .c exe exe 程式碼的編譯與生成 執行、評估,與驗證 建立 IR 載入必要的函式庫 連結程式模組 Optimizations + Transforms codegen 32
  • 33. 當 LLVM 煞到 OpenGL Shader 動態編譯器就在眼前
  • 35. 3D/Shader 的考量點 光 反射 移動的演算法 Reyes(scanline,polygon) Raytracing 可引入 SIMD 優化
  • 36. surface push N matte(float Ka = 1.0; float Kd = 1.1;) normalize { push I   normal Nf = faceforward( faceforward               normalize(N), I); ...   Oi = Os;   Ci = Os * Cs * (Ka * ambient() +                   Kd * diffuse(Nf));     }
  • 37. surface push N matte(float Ka = 1.0; float Kd = 1.1;) normalize { push I   normal Nf = faceforward( faceforward               normalize(N), I); ...   Oi = Os;   Ci = Os * Cs * (Ka * ambient() +                   Kd * diffuse(Nf));     } Raytracing 的難題 無法善用 SIMD 運算相依性高且繁瑣 需要動態調整快速運算 的路徑
  • 38. Specialize 以 color space 轉換來說,相當大量 且繁瑣的運算,如 技巧 BGRA 444R --> RGBA 8888 Speedup depends on src/dest format: – 5.4x speedup on average, 19.3x max speedup: (13.3MB/s to 257.7MB/s)
  • 39.
  • 40. Mandelbort 碎形運算透過 LLVM JIT 後, 提昇效能達到 11 倍
  • 41. LLVM 與繪圖處理的應用 手機平台: Android PixelFlinger 繪圖軟體: Adobe PixelBender, Shader 3D 高品質圖形處理: Gallium3D / OpenGL
  • 43. 當 LLVM 煞到 Adobe Flash 安全多元的享受 C/C++ 的美好
  • 44. 有時,你不會想看到遙遙 ... ( 不安全、不舒服的 C 語言 )
  • 45. 「可是,男人都想要當 慣C」 承認吧,慣 C 有很多好處,所以 Adobe 建立 Alchemy(FlaCC) 專案 當然啦,慣 C 的風險也很高 「學長,我不敢修電腦」 虛擬機器: AVM2
  • 46. Adobe Alchemy 實驗性的 LLVM Backend LLVM → ABC (ActionScript ByteCode) ABC 類似精簡的 x86 指令集 提供 POSIX 模擬層與完整的 BSD libc + GNU ISO C++ library 足以執行若干重要的應用程式
  • 47. 背景知識 from Adobe, Mozilla, and Tamarin http://hecker.org/mozilla/adobe-mozilla-and-tamarin Flash player 9 在 AVM2 內建 JIT-based VM Tamarin 由 Adobe 捐贈予 Mozilla Flash player 9 = AVM2 + graphics, musics, videos, networking, etc components ActionScript 3.0 (Flash 5 對 ActionScript 的稱呼 ) JavaScript 的姊妹規格 參考 ECMAScript 4 (ECMA-262) Free Flex 2. SDK (foo.as -> foo.swf) Flash player 9 內部具備 ABC 執行引擎
  • 48. Tamarin ECMAScript version 4 (ES4) 的高速實做 Tamarin VM, Mozilla, JavaScript, SpiderMonkey, Adobe Flash Player, AVM AVMPlus AVMPlus (VM library), MMgc (garbage collection library), avmplus Avmplus, ABC, JIT Adobe Flex 2 SDK includes ASC (ActionScript Compiler)
  • 49.
  • 50.
  • 51.
  • 52. 當 LLVM 煞到 Android 安全多元的享受 C/C++ 的美好
  • 53. 0xlab LLVM hacks 實驗性的 LLVM Backend LLVM → Android Dalvik bytecode (DEX) 仿效 Adobe Alchemy ,目標平台則是 DalvikVM 提供 C/C++ 原始碼 → LLVM → DEX bytecode 的執行環境,無 JNI (Java Native Interface) 介入 「我只會寫 Java VM ,不會寫 Java 應用程式」,宅色夫 (2005)
  • 54.
  • 56. 技術大融通 與 未來展望 「你的時代到了!」 LLVM 一統天下
  • 57. LLVM 的時代到了! clang: 嶄新的 C/C++/Objective-C 語言前端 vmkit: Java/.Net 虛擬機器 llvm + OpenGL Sun OpenJDK → RedHat Zero/Shark Trident: VHDL compiler for FPGA llvmruby, yarv2llvm, RubyComp, MacRuby PyPy, Google unladen-Swallow HLVM: Haskell, Ocaml, ...
  • 58. 自由軟體編譯器技術的一統標準已定! 多元的整合,如 clang 可作為 static compiler ,亦可挪用為 JIT compiler 技術集中,創意多元
  • 59. 參考資料 〈 LLVM and Clang: Next Generation Compiler Technology 〉 , Chris Lattner BSDCan 2008 《美麗程式》 (Beautiful Code), O'Reilly 第八章〈動態產生影像處理程式〉 LLVM – http://www.llvm.org/ Projects built with LLVM – http://www.llvm.org/ProjectsWithLLVM/ Open LLVM Projects – http://www.llvm.org/OpenProjects.html
  • 60. UI customizing Platform Builder 三個願望一次滿足 一系列的自由軟體 四月 27 日開張 Device Potential ( 42710=0x1ab ) http://0xlab.org 感謝您!