SlideShare a Scribd company logo
身騎 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 introduction
公孫龍子:「白馬非馬」
「馬」 指的是馬的形態,「白馬」指的是馬的顏色,
而形態不等於顏色,所以白馬不是馬

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 與銜接的服務
優化與轉換程序




中間輸出與
生成處理
LLVM introduction
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) 的時代
LLVM introduction
↑JavaScript + DHTML (JavaVM
    Implementation)

↓Java ME
LLVM introduction
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)
LLVM introduction
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 introduction
LLVM introduction
LLVM introduction
當 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)
LLVM introduction
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 (20)

PPT
Introduction to gdb
Owen Hsu
 
PDF
The Microkernel Mach Under NeXTSTEP
Gregor Schmidt
 
PDF
ROP 輕鬆談
hackstuff
 
PDF
from Source to Binary: How GNU Toolchain Works
National Cheng Kung University
 
PDF
用十分鐘 向jserv學習作業系統設計
鍾誠 陳鍾誠
 
PDF
Q2.12: Debugging with GDB
Linaro
 
PPTX
LLVM Instruction Selection
Shiva Chen
 
PPTX
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
YujiSoftware
 
DOCX
系統程式 - 第二章
鍾誠 陳鍾誠
 
PDF
Construct an Efficient and Secure Microkernel for IoT
National Cheng Kung University
 
PPTX
Visual C++で使えるC++11
nekko1119
 
PDF
Insecure coding in C (and C++)
Olve Maudal
 
PDF
不遇の標準ライブラリ - valarray
Ryosuke839
 
PPTX
C#や.NET Frameworkがやっていること
信之 岩永
 
PDF
Interpreter, Compiler, JIT from scratch
National Cheng Kung University
 
PDF
Binary exploitation - AIS3
Angel Boy
 
PDF
C++コミュニティーの中心でC++をDISる
Hideyuki Tanaka
 
PDF
Instruction Combine in LLVM
Wang Hsiangkai
 
PDF
LLVM Register Allocation
Wang Hsiangkai
 
PDF
BUD17-302: LLVM Internals #2
Linaro
 
Introduction to gdb
Owen Hsu
 
The Microkernel Mach Under NeXTSTEP
Gregor Schmidt
 
ROP 輕鬆談
hackstuff
 
from Source to Binary: How GNU Toolchain Works
National Cheng Kung University
 
用十分鐘 向jserv學習作業系統設計
鍾誠 陳鍾誠
 
Q2.12: Debugging with GDB
Linaro
 
LLVM Instruction Selection
Shiva Chen
 
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
YujiSoftware
 
系統程式 - 第二章
鍾誠 陳鍾誠
 
Construct an Efficient and Secure Microkernel for IoT
National Cheng Kung University
 
Visual C++で使えるC++11
nekko1119
 
Insecure coding in C (and C++)
Olve Maudal
 
不遇の標準ライブラリ - valarray
Ryosuke839
 
C#や.NET Frameworkがやっていること
信之 岩永
 
Interpreter, Compiler, JIT from scratch
National Cheng Kung University
 
Binary exploitation - AIS3
Angel Boy
 
C++コミュニティーの中心でC++をDISる
Hideyuki Tanaka
 
Instruction Combine in LLVM
Wang Hsiangkai
 
LLVM Register Allocation
Wang Hsiangkai
 
BUD17-302: LLVM Internals #2
Linaro
 

Viewers also liked (10)

PDF
LLVM Compiler - Link Time Optimization
Vivek Pansara
 
PDF
Making Linux do Hard Real-time
National Cheng Kung University
 
PPTX
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
Akihiro Hayashi
 
PDF
Как приручить дракона: введение в LLVM
Tech Talks @NSU
 
PDF
LLVM Internal Architecture par Michel Guillet
CocoaHeads France
 
PDF
LLVM Overview
Constantin Lungu
 
PDF
Clang Analyzer Tool Review
Doug Schuster
 
PPT
Introduction to llvm
Tao He
 
PDF
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
Linaro
 
PDF
Introduction to the LLVM Compiler System
zionsaint
 
LLVM Compiler - Link Time Optimization
Vivek Pansara
 
Making Linux do Hard Real-time
National Cheng Kung University
 
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
Akihiro Hayashi
 
Как приручить дракона: введение в LLVM
Tech Talks @NSU
 
LLVM Internal Architecture par Michel Guillet
CocoaHeads France
 
LLVM Overview
Constantin Lungu
 
Clang Analyzer Tool Review
Doug Schuster
 
Introduction to llvm
Tao He
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
Linaro
 
Introduction to the LLVM Compiler System
zionsaint
 
Ad

Similar to LLVM introduction (20)

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

More from National Cheng Kung University (20)

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

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
  • 4. 公孫龍子:「白馬非馬」 「馬」 指的是馬的形態,「白馬」指的是馬的顏色, 而形態不等於顏色,所以白馬不是馬 LLVM 不是 VM ,也不只是 Compiler http://p5.p.pixnet.net/albums/userpics/5/8/270458/1208944435.jpg
  • 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
  • 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) 的時代
  • 28. ↑JavaScript + DHTML (JavaVM Implementation) ↓Java ME
  • 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)
  • 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)
  • 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)
  • 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 感謝您!