從V8 Javascript 引擎淺談現代編譯技術
FromV8 to Modern Compilers
Bekket McClane@SITCON2017
Who am I?
Bekket McClane
Computer Science @NTHU
LinkedIn: bekketmcclane
LLVM x OpenCL x JIT Compilers
Preface
從V8 Javascript 引擎淺談現代編譯技術
FromV8 to Modern Compilers
Talking Compilers with Students…
Talking Compilers with Students…
It’s Not So Boring!
COMPILERS
COMPILERS EVERYWHERE!
Compilers are EVERYWHERE !
PerformanceCompilers
Good Compilers, Better Performance
Syllabus
Syllabus
• Execute Javascript in Old Days
Syllabus
• Execute Javascript in Old Days
• Compiler ✭Magic✭ inV8
Syllabus
• Execute Javascript in Old Days
• Compiler ✭Magic✭ inV8
• FromV8 to Modern Compilers
Execute Javascript in Old Days
Javascript
• Fast Prototyping
• Dynamic
• Well-Accepted Standard
Javascript
• Fast Prototyping
• Dynamic
• Well-Accepted Standard
INTERPRETER!
Simple Interpreter
switch(op){
case OpAdd: {…}
case OpStore: {…}
case …
}
Slow Execution Speed…
Flaw: CPU Pipeline Stalling
• CPU would “pre-fetch” instructions on branch
EXE MEM WBID
Instructions of branch AInstructions of branch B
CPU Pipeline Stages
Flaw: CPU Pipeline Stalling
switch(op){
case OpAdd: {…}
case OpStore: {…}
case …
}
• CPU would also try to “predict” next branch
Flaw: CPU Pipeline Stalling
switch(op){
case OpAdd: {…}
case OpStore: {…}
case …
}
• CPU would also try to “predict” next branch
Hard to Predict!
Flaw: CPU Pipeline Stalling
EXE MEM WBID
Instructions of branch A? Which Branch?
CPU Pipeline Stages
Stalling
Flaw: Lack of Aggressive Optimization
$a = fmul $x, $y
$b = fadd $a, $z
$b = fmadd $x, $y, $z
Example:
JIT(Just-in-Time) Compilation
Introducing…
What is JIT Compilation?
• Compile Code On-The-Fly
• Compile a small region of code once a time
and execute it.
• Can Apply More Optimizations
• Compilations are All Happening in Runtime
• Need FAST compilation speed
Compile Code On-The-Fly
JIT Compiler
Native
Code
Execute
Native Code
Source
Code
Compile Code On-The-Fly
JIT Compiler
Native
Code
Execute
Native Code
Source
Code
Classical Compilation Flow
First, Let’s Go BackTo…
Classical Compilation Flow
Source
Code
Front
End
IR Optimizer
Code
Gen
Native
Code
Back End
(Intermediate Representation)
Classical Compilation Flow
Source
Code
Front
End
IR Optimizer
Code
Gen
Native
Code
Back End
(Intermediate Representation)
Classical Compilation Flow
Source
Code
Front
End
IR Optimizer
Code
Gen
Native
Code
Back End
(Intermediate Representation)
Instruction Selection
foo %v1, %v2
bar %v3, %v1
IR
Instruction Selection
foo %v1, %v2
bar %v3, %v1
IR
MOV $eax, $ebx
ADD $ecx, $eax
x86 Assembly
Instruction Selection
foo %v1, %v2
bar %v3, %v1
IR
mov $r1, $r3
add $r4, $r4, $r1
ARM Assembly
MOV $eax, $ebx
ADD $ecx, $eax
x86 Assembly
Instruction Selection
bar %v1, %v2
foo %v3, %v1
yooo %v3
Instruction Selection
bar %v1, %v2
foo %v3, %v1
yooo %v3
mov $r1, $r3
add $r4, $r4
Instruction Selection
bar %v1, %v2
foo %v3, %v1
yooo %v3
shr $r1, $r3
ld $r4, $r1
mov $r1, $r3
add $r4, $r4
Instruction Selection
bar %v1, %v2
foo %v3, %v1
yooo %v3
shr $r1, $r3
ld $r4, $r1
mov $r1, $r3
add $r4, $r4
?Which One?
(Brief) Categories Of IR
Linear DAG Graph
Multi-Levels IR (Ex: LLVM)
Linear DAG (Another) Linear
Target-Independent
Optimizations • Instruction Selection
• Instruction Scheduling
Target-Specific
Optimizations
Control Flow Graph
x < 4
(Loop Body)
x++
TrueFalse
(Next)
V8: Unified Graph IR
• Use the same graph structure through
the entire compilation process
• Nodes represent operation (Data and
Control). Edges represent their
dependencies
• Without implicit ordering, it has more
freedom than CFG.
IR Graph inV8
IfTrue IfFalse
Branch
Start
Control is also Expressed By Node!
IR Graph inV8
IfTrue IfFalse
Branch
Start
Two Types of Dependencies: Data and Control
+
y 2
z
(z = y + 2)
IR Graph Example inV8
+
y 2
z
return
Start
function(){
z = y + 2;
return z;
}
Control Dependency
Data Dependency
Dependencies Only:
Good for Instruction Scheduling
IR Nodes Lowering / Reducing
High Level
Low Level
Instruction Selection? Graph Reducing
Target-Independent
Machine Code
Optimization? Still Graph Reducing !
Optimization? Still Graph Reducing !
C = (true)? x : y
Extensive Studying
Extensive Studying
• HowV8 Handles DynamicTypes?

(Hint: Inline Cache)
Extensive Studying
• HowV8 Handles DynamicTypes?

(Hint: Inline Cache)
• Ignition,The New INTERPRETER of V8
• Garbage Collection inV8
FromV8 to Modern Compilers
What I’d Learn in Compiler Class
What I’d Learn in Compiler Class
and…
What I’d Learn in Compiler Class
and…
Nothing More
What I’d Learn
in Real-World Compiler Projects
Front End
Back End
Optimization,
Instruction Selection,
Register Allocation…
The Real Battle Field!
What I’d Learn
inTraditional Compiler Research
What I’d Learn
inTraditional Compiler Research
• Optimization Algorithms
What I’d Learn
inTraditional Compiler Research
• Optimization Algorithms
• Optimization Algorithms
What I’d Learn
inTraditional Compiler Research
• Optimization Algorithms
• Optimization Algorithms
• Register Allocation Problems
What I’d Learn
inTraditional Compiler Research
• Optimization Algorithms
• Optimization Algorithms
• Register Allocation Problems
• …Still Optimization Algorithms
What I’d Learn
in Modern Compiler Research
• How to Boost Compilation Speed
• How to Reduce Compilers’ Memory Footprint
• How to Choose Proper Optimizations
• Dynamic-Profiled Optimizations
Yes, This Book is OUT-DATED
Basic
Basic
Modern Compiler Skills:
Just Read the F**king Code!
COMPILERS
COMPILERS EVERYWHERE!
Embrace Compilers,
Embrace Performance
PerformanceCompilers
Appendix
• V8 Source Code(Github Mirror): 

https://github.com/v8/v8
• TurboFan: src/compiler
• Ignition: src/interpreter
• Inline Cache: src/ic
Cites
• Meurer, Benedikt.“An overview of theTurboFan compiler”.
28 October 2016. Google Presentation file
• Titzer, Ben.“TurboFan JIT Design”. 6 May 2016. Google
Presentation file
• Sevcik, Jaroslav.“TurboFan IR”. 11 November 2016. Google
Presentation file
ThankYou All !
Q & A

From V8 to Modern Compilers