The document discusses LLVM and its use in building programming language compilers and runtimes. It provides an overview of LLVM, including its core components like its intermediate representation (IR), optimizations, and code generation capabilities. It also discusses how LLVM is used in various applications like Android, browsers, and graphics processing. Examples are given of using Clang and LLVM to compile and run a simple C program.
Scaling API-first – The story of a global engineering organization
Build Programming Language Runtime with LLVM
1. Build Programming Language
Runtime with LLVM
Jim Huang ( 黃敬群 )
Developer & Co-founder, 0xlab
jserv@0xlab.org
@ 1500 / March 27, 2011
2. 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
• 最大的專長就是培養興趣
8. Compilers on Rails 的時代
[ 詞彙 ] on the rails: 正常運行 ; 在正常軌道
[ 啟發 ] Ruby on Rails:
(1) convention over configuration
(2) less software
(3) programmer happiness ultimately leads to better productivity
29. 先從 Hello World 開始
• 完整的 Compiler Driver
$ clang hello.c o hello
• 生成 IR
$ clang O3 emitllvm 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
31. LLVM in Google Android 3.0 SDK
$ cd androidsdklinux_x86/platformtools
$ ./llvmrscc version
Low Level Virtual Machine (http://llvm.org/):
llvm version 2.8svn
Optimized build.
Built Feb 16 2011 (19:26:29).
Host: i386unknownlinux
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 32bit X86: PentiumPro and above
x8664 64bit X86: EM64T and AMD64
$ ./llvmrscc help
OVERVIEW: RenderScript source compiler
31
32. LLVM in Google Android 3.0 SDK
$ ./llvmrscc help
OVERVIEW: RenderScript source compiler
USAGE: llvmrscc [options] <inputs>
• OPTIONS:
I <directory> Add directory to include search path
additionaldeptarget <value>
Additional targets to show up in dependencies output
allowrsprefix Allow userdefined function prefixed with 'rs'
bitcodestorage <value>
<value> should be 'ar' or 'jc'
emitasm Emit target assembly files
emitbc Build ASTs then convert to LLVM, emit .bc file
emitllvm Build ASTs then convert to LLVM, emit .ll file
emitnothing Build ASTs then convert to LLVM, but emit nothing
help Print this help text
javareflectionpackagename <value>
Specify the package name that reflected Java files belong to
javareflectionpathbase <directory>
Base directory for output reflected Java files
32
33. 測試 SDK 內建範例 RenderScript
androidsdklinux_x86/platformtools$ ./llvmrscc
../samples/android11/
RenderScript/HelloWorld/src/com/android/rs/helloworld/helloworld.rs
I ../platforms/android11/renderscript/include
I ../platforms/android11/renderscript/clanginclude
// 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;
}
}
llvmdis < 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
}
}
38. 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
40. 程式語言的變遷 ( 低階觀點 )
• 職業無貴賤,程式語言是提出來解決人類面臨的
問題之用 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
41. 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
42. 遊戲產業驅使 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.
43. 今日的 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
44.
45. 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
46. 在 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
63. 架構於 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