CS593: Compiler Design OOLs and JIT Manas Thakur Aug-Dec 2019
CS593: Compiler Design
OOLs and JIT
Manas Thakur
Aug-Dec 2019
Manas Thakur CS593: Aug-Dec ‘19 2
Common properties of OOLs such as Java
● Encapsulation
– Data abstraction
– Classes and objects
● Inheritance
– Extension of features
– Classes can extend classes / implement interfaces
● Polymorphism
– Method overriding in inherited classes
– Call targets known at run-time
Manas Thakur CS593: Aug-Dec ‘19 3
Common issues with OOLs such as Java
● Encapsulation
– Reclaiming the memory occupied by objects is costly
– Optimization: Remove objects!
● Inheritance
– Type casts, checks, field resolution: everything is costly
– Optimization: Resolve (remove) inheritance!
● Polymorphism
– Determining call targets is costly
– Optimization: Dismbiguate (remove) polymorphism!
Our target today
Manas Thakur CS593: Aug-Dec ‘19 4
How to optimize across methods without inlining?
● Interprocedural analysis
– Have the effects of method calls
– Increase the scope of analysis
– Requires a call graph (CG)
● Opposite: intraprocedural analysis
– Per procedure control-flow graphs (CFGs)
– Conservative assumptions at method boundaries
● We will also cover in this course:
– How (do) these things (can) happen in JIT compilers?
Manas Thakur CS593: Aug-Dec ‘19 5
Call Graph Construction:A (Very Long) Story
Manas Thakur CS593: Aug-Dec ‘19 6
Need of a call graph
● Where should the control jump at lines c1 and c2?
● Interprocedural analysis affects the precision of other analyses:
– Is y a constant?
– Can y > 40 be folded?
– Is the code in one of the branches unreachable?
void foo(T x) { U z;c1: int y = x.bar(); if (y > 40) z = new V(); else z = new W();c2: z.zap();}
Manas Thakur CS593: Aug-Dec ‘19 7
Class Hierarchy Analysis (CHA)1
● Look at the class hierarchy to determine what classes of objects can be pointed to by a reference variable.
● If a class re-defines a method foo defined in its parent class:
– Only the redefined foo can be called using the objects of the extended class.
– Methods not redefined are still accessed from the parent class.
● CHA helps in determining that only one implementation of m can be called from the blue call.
1J Dean, D Grove, C Chambers. Optimization of OO Programs Using Static Class Hierarchy. ECOOP’95.
Manas Thakur CS593: Aug-Dec ‘19 8
Rapid Type Analysis (RTA)2
● Improves CHA with extra information:
– Find if a class is ever instantiated in a program
– If not, then remove it from the sets obtained using CHA
● Catch:
– Assumption that the whole-program is available
– What about code that cannot be “seen”?● e.g., dynamically linked libraries
2D Bacon and P Sweeney. Fast Static Analysis of C++ Programs. OOPSLA’96.
Manas Thakur CS593: Aug-Dec ‘19 9
Context-Insensitive Control Flow Analysis (0-CFA)
● Tries to find which classes of objects can flow to various reference variables
● The underlying analysis is context-insensitive
class A { A foo(A x) { return x; } }class B extends A { A foo(A x) { return new D(); } }class D extends A { A foo(A x) { return new A(); } }class C extends A { A foo(A x) { return this; } }
void main() { A x = new A(); while (...)c1: x = x.foo(new B()); A y = new C();c2: y.foo(x);}
● CHA/RTA: Any foo can be called at both c1 and c2.
● 0-CFA:
– c1: A.foo, B.foo, D.foo
– c2: C.foo
Manas Thakur CS593: Aug-Dec ‘19 10
Context-sensitive analysis (k-CFA3)● The underlying points-to analysis is context-sensitive.
● The context is identified using the last k methods in the call chain.
3Olin G. Shivers. Control-Flow Analysis of Higher-Order Languages. PhD thesis, CMU’91.
class A { fb() { ... } }class B extends A { fb() { ... } }
class C { void foo() { A a1, a2; a1 = new A(); //l1c1: bar(a1); a2 = new B(); //l2c2: bar(a2); } void bar(A p1) { p1.fb(); } }
● Context-insensitive:
– p1 -> {l1,l2}
– Both A.fb and B.fb can be called.
● 1-CFA:
– At c1:
● p1 -> {l1}
A.fb will be called.
– At c2:
● p1 -> {l2}
B.fb will be called.
Manas Thakur CS593: Aug-Dec ‘19 11
k-CFA (Cont.)
class C { void foo1() { bar(); }
void foo2() { bar(); }
void bar() { fb(); }}
● 1-CFA:
– 1 context for fb
● 2-CFA:
– 2 contexts for fb
Manas Thakur CS593: Aug-Dec ‘19 12
The Opposite of Polymorphism
● Monomorphic call:
– Only one method can be called
– Target can be bound statically
● Which calls in Java are always monomorphic?
– Calls to static methods
– Calls to final methods
– Calls to methods of final classes
● We increase the size of the above set using the different analyses seen in the previous slides.
● How could we compare the precision of two CG construction algorithms empirically?
– Count the identified number of monomorphic call-sites.
Manas Thakur CS593: Aug-Dec ‘19 13
What we have seen today
● CFA: only look at inheritance relations● RTA: also look at code● 0-CFA: also analyze code
● k-CFA: improve the precision of the analysis
● Some points to note:
– k-CFA is a context-sensitive analysis
– The length k is the length of the call-stack (recall RTE?)
– Here the last k call-sites form our context
Manas Thakur CS593: Aug-Dec ‘19 14
What we haven’t seen today
● There are multiple ways we can define what is a context– Called the context abstraction of a given context-sensitive analysis
– Object-sensitive contexts● Ana Milanova, Atanas Rountev, Barbara G. Ryder. TOSEM ‘05.
– Value contexts● Uday P. Khedker and Bageshri Karkare. CC’08.
– Type-sensitive contexts● Yannnis Smaragdakis, Martin Bravenboer, Ondřej Lhoták. POPL ‘11.
– LSRV contexts● Manas Thakur and V. Krishna Nandivada. CC’19.
● Many more non-context-sensitive CG construction algorithms:
– Frank Tip and Jens Palsberg. Scalable Propagation-Based Call Graph Construction Algorithms. OOPSLA ’00.
Next from IIT Mandi?
Manas Thakur CS593: Aug-Dec ‘19 15
What we’ll see from Wednesday: Case study on Java static and JIT compilers
Manas Thakur CS593: Aug-Dec ‘19 16
Recall the second class?
Image source: https://stackoverflow.com/a/31551282
Manas Thakur CS593: Aug-Dec ‘19 17
Again from the second class:Compilers vs Interpreters: Demo
Manas Thakur CS593: Aug-Dec ‘19 18
The Java Compilation+Execution Model
Manas Thakur CS593: Aug-Dec ‘19 19
FFT1: Which implementation is better?
Manas Thakur CS593: Aug-Dec ‘19 20
A Bit of Bytecode
● Generated by the static Java compiler
● Mid-level IR
● Machine independent
● Follows a stack model
● Format:
– <opcode> <operands>
● Opcode is one Byte (8 bits)– 256 (28) in number
Manas Thakur CS593: Aug-Dec ‘19 21
Bytecode example
Bytecode indices
Manas Thakur CS593: Aug-Dec ‘19 22
Bytecode example (Cont.)
Type expressionsMethod invocations
Manas Thakur CS593: Aug-Dec ‘19 23
Type expressions in Bytecode
Manas Thakur CS593: Aug-Dec ‘19 24
Class file format
Manas Thakur CS593: Aug-Dec ‘19 25
The Java Class File Disassembler (javap)
Manas Thakur CS593: Aug-Dec ‘19 26
Now into the JVM
Manas Thakur CS593: Aug-Dec ‘19 27
Runtime data areas
Manas Thakur CS593: Aug-Dec ‘19 28
Per thread view
Manas Thakur CS593: Aug-Dec ‘19 29
Before we leave: Which implementation is better?
Manas Thakur CS593: Aug-Dec ‘19 30
The Last Lecture
Manas Thakur CS593: Aug-Dec ‘19 31
FFT2: Which programs are faster?
● C
● C++
● Java
Manas Thakur CS593: Aug-Dec ‘19 32
The Language War
Manas Thakur CS593: Aug-Dec ‘19 33
The JVMJava Bytecode is interpreted as well as compiled!!
Oracle HotSpot Execution Engine
C++/TemplateInterpreter
ClientCompiler
(C1)
ServerCompiler
(C2)
Manas Thakur CS593: Aug-Dec ‘19 34
The “HotSpot” JVM
● HotSpot uses tiered compilation
– Starts off with interpreter
– Hot spots get compiled as they get executed● Identified using profiling
– Method invocation counts– Backedge counts
● Two interpreters:
– C++ interpreter (deprecated)
– Template interpreter
● Just-In-Time (JIT) Compilers:
– C1 (aka client)
– C2 (aka server)
Oracle HotSpot Execution Engine
C++/TemplateInterpreter
ClientCompiler
(C1)
ServerCompiler
(C2)
Manas Thakur CS593: Aug-Dec ‘19 35
The C1 Compiler
● Targets fast compilation
● Still performs several optimizations:– Method inlining
– Dead code/path elimination
– Heuristics for optimizing call sites
– Constant folding
– Peephole optimizations
– Linear-scan register allocation, etc.
● Threshold: 1000 to 2000
Manas Thakur CS593: Aug-Dec ‘19 36
The C2 Compiler
● Targets more-and-more optimization
● Performs expensive optimizations (apart from the ones performed by C1):
– Escape analysis
– Null-check elimination
– Loop unrolling/unswitching
– Branch prediction
– Graph-coloring based register allocation, etc.
● Threshold: 10000 to 15000
Manas Thakur CS593: Aug-Dec ‘19 37
JIT Compilation in the HotSpot JVM
● Hot methods are inserted into a compilation queue
● Compiler threads compile methods in the background, while interpretation continues
● Entry points of methods are changed dynamically
● Hot loops are replaced on-the-stack
– called On-Stack Replacement (OSR)
Manas Thakur CS593: Aug-Dec ‘19 38
Compilation levels
● 0 – Interpreter
● 1 – Pure C1
● 2 – C1 with invocation and backedge counting
● 3 – C1 with full profiling
● 4 – C2 (full optimization)
0 3 4
4
030
0
Some possible sequences:
Manas Thakur CS593: Aug-Dec ‘19 39
Deoptimization
● Speculative optimizations:
– Branch prediction
– Implicit null checks
– Monomorphism
● When an assumption fails, the compiled method is invalidated, and the execution falls back to the interpreter
● Compiled method states:
– in use, not entrant, zombie, unloaded
● Deoptimization is costly; happens lesser the better
Manas Thakur CS593: Aug-Dec ‘19 40
HotSpot in Action
Manas Thakur CS593: Aug-Dec ‘19 41
Before we say “Goodbye”
Manas Thakur CS593: Aug-Dec ‘19 42
FFT2: Which programs are faster?
● C
● C++
● Java
It’s the compiler that makes programs fast.
Manas Thakur CS593: Aug-Dec ‘19 43
● A new programming language: Java
– Perhaps the most popular OO language
– Perhaps the most popular language as well
● Several new frameworks:
– Lexer generator: Flex
– Parser generator: JavaCC/JTB
● A new design pattern:
– Visitors
Learnings that you can put on your CV
Manas Thakur CS593: Aug-Dec ‘19 44
Few words about the end sem
● Not lengthy
● Nothing mechanical
● Understand the whole syllabus...
● ... and the syllabus as a whole
● Remember what the chief guest said on Teachers’ Day?
Manas Thakur CS593: Aug-Dec ‘19 45
What’s a program?
● A sequence of instructions?
– Only in imperative languages
● An application of expressions?
– Only for functional languages
● A set of constraints
– Only for declarative languages
● Then a tree?
– Parse tree, AST
● A graph?
– CFG, CG, Super CFG/CG
Manas Thakur CS593: Aug-Dec ‘19 46
What’s a program then?
● Just a string
● Who makes it meaningful?
● How do we do it?
– Parse
– Assign semantics
– Form abstractions
– Execute
“A world without string is chaos.”
Manas Thakur CS593: Aug-Dec ‘19 47
We began the course with languages
● Use them to remove barriers,not create them
● Learn more about programminglanguages in the next semester
● And remember:
You can always design a compiler that solves the issues.