2. 2
Agenda
§ about compilers in general
– … and JIT-compilers in particular
§ about JIT-compilers in HotSpot JVM
§ monitoring JIT-compilers in HotSpot JVM
4. 4
Dynamic and Static Compilation Differences
§ Static compilation
– “ahead-of-time”(AOT) compilation
– Source code → Native executable
– Most of compilation work happens before executing
5. 5
Dynamic and Static Compilation Differences
§ Static compilation
– “ahead-of-time”(AOT) compilation
– Source code → Native executable
– Most of compilation work happens before executing
§ Modern Java VMs use dynamic compilers (JIT)
– “just-in-time” (JIT) compilation
– Source code → Bytecode → Interpreter + JITted executable
– Most of compilation work happens during application execution
6. 6
Dynamic and Static Compilation Differences
§ Static compilation (AOT)
– can utilize complex and heavy analyses and optimizations
7. 7
Dynamic and Static Compilation Differences
§ Static compilation (AOT)
– can utilize complex and heavy analyses and optimizations
§ … but static information sometimes isn’t enough
§ … and it’s hard to rely on profiling info, if any
8. 8
Dynamic and Static Compilation Differences
§ Static compilation (AOT)
– can utilize complex and heavy analyses and optimizations
§ … but static information sometimes isn’t enough
§ … and it’s hard to rely on profiling info, if any
– moreover, how to utilize specific platform features?
§ like SSE4.2 / AVX / AVX 2, TSX, AES-NI, RdRand
9. 9
Dynamic and Static Compilation Differences
§ Modern Java VMs use dynamic compilers (JIT)
– aggressive optimistic optimizations
§ through extensive usage of profiling info
10. 10
Dynamic and Static Compilation Differences
§ Modern Java VMs use dynamic compilers (JIT)
– aggressive optimistic optimizations
§ through extensive usage of profiling info
§ … but budget is limited and shared with an application
11. 11
Dynamic and Static Compilation Differences
§ Modern Java VMs use dynamic compilers (JIT)
– aggressive optimistic optimizations
§ through extensive usage of profiling info
§ … but budget is limited and shared with an application
– thus:
§ startup speed suffers
§ peak performance may suffer as well (but not necessarily)
12. 12
Profiling
§ Gathers data about code during execution
– invariants
§ types, constants (e.g. null pointers)
– statistics
§ branches, calls
§ Gathered data is used during optimization
– Educated guess
– Guess can be wrong
13. 13
Optimistic Compilers
§ Assume profile is accurate
– Aggressively optimize based on profile
– Bail out if they’re wrong
§ ...and hope that they’re usually right
14. 14
Profile-guided optimizations (PGO)
§ Use profile for more efficient optimization
§ PGO in JVMs
– Always have it, turned on by default
– Developers (usually) not interested or concerned about it
– Profile is always consistent to execution scenario
16. 16
Dynamic Compilation (JIT)
§ Can do non-conservative optimizations in dynamic
§ Separates optimization from product delivery cycle
– Update JVM, run the same application, realize improved performance!
– Can be "tuned" to the target platform
17. 17
Dynamic Compilation (JIT)
§ Knows about
– loaded classes, methods the program has executed
§ Makes optimization decisions based on code paths executed
– Code generation depends on what is observed:
§ loaded classes, code paths executed, branches taken
§ May re-optimize if assumption was wrong, or alternative code paths
taken
– Instruction path length may change between invocations of methods as a
result of de-optimization / re-compilation
18. 18
JVM
§ Runtime
– class loading, bytecode verification, synchronization
§ JIT
– profiling, compilation plans, OSR
– aggressive optimizations
§ GC
– different algorithms: throughput vs. response time
19. 19
JVM: Makes Bytecodes Fast
§ JVMs eventually JIT bytecodes
– To make them fast
– compiled when needed
§ Maybe immediately before execution
§ ...or when we decide it’s important
§ ...or never?
– Some JITs are high quality optimizing compilers
20. 20
JVM: Makes Bytecodes Fast
§ JVMs eventually JIT bytecodes
§ But cannot use existing static compilers directly
– different cost model
§ time & resource constraints (CPU, memory)
– tracking OOPs (ptrs) for GC
– Java Memory Model (volatile reordering & fences)
– New code patterns to optimize
21. 21
JVM: Makes Bytecodes Fast
§ JIT'ing requires Profiling
– Because you don't want to JIT everything
§ Profiling allows focused code-gen
§ Profiling allows better code-gen
– Inline what’s hot
– Loop unrolling, range-check elimination, etc
– Branch prediction, spill-code-gen, scheduling
22. 22
Dynamic Compilation (JIT)
§ Is dynamic compilation overhead essential?
– The longer your application runs, the less the overhead
§ Trading off compilation time, not application time
– Steal some cycles very early in execution
– Done automagically and transparently to application
§ Most of “perceived” overhead is compiler waiting for more data
– ...thus running semi-optimal code for time being
Overhead
26. 26
Deoptimization
§ Bail out of running native code
– stop executing native (JIT-generated) code
– start interpreting bytecode
§ It’s a complicated operation at runtime…
27. 27
OSR: On-Stack Replacement
§ Running method never exits?
§ But it’s getting really hot?
§ Generally means loops, back-branching
§ Compile and replace while running
§ Not typically useful in large systems
§ Looks great on benchmarks!
29. 29
Optimizations in HotSpot JVM
§ compiler tactics
delayed compilation
tiered compilation
on-stack replacement
delayed reoptimization
program dependence graph rep.
static single assignment rep.
§ proof-based techniques
exact type inference
memory value inference
memory value tracking
constant folding
reassociation
operator strength reduction
null check elimination
type test strength reduction
type test elimination
algebraic simplification
common subexpression elimination
integer range typing
§ flow-sensitive rewrites
conditional constant propagation
dominating test detection
flow-carried type narrowing
dead code elimination
§ language-specific techniques
class hierarchy analysis
devirtualization
symbolic constant propagation
autobox elimination
escape analysis
lock elision
lock fusion
de-reflection
§ speculative (profile-based) techniques
optimistic nullness assertions
optimistic type assertions
optimistic type strengthening
optimistic array length strengthening
untaken branch pruning
optimistic N-morphic inlining
branch frequency prediction
call frequency prediction
§ memory and placement transformation
expression hoisting
expression sinking
redundant store elimination
adjacent store fusion
card-mark elimination
merge-point splitting
§ loop transformations
loop unrolling
loop peeling
safepoint elimination
iteration range splitting
range check elimination
loop vectorization
§ global code shaping
inlining (graph integration)
global code motion
heat-based code layout
switch balancing
throw inlining
§ control flow graph transformation
local code scheduling
local code bundling
delay slot filling
graph-coloring register allocation
linear scan register allocation
live range splitting
copy coalescing
constant splitting
copy removal
address mode matching
instruction peepholing
DFA-based code generator
30. 30
JVM: Makes Virtual Calls Fast
§ C++ avoids virtual calls – because they are slow
§ Java embraces them – and makes them fast
– Well, mostly fast – JIT's do Class Hierarchy Analysis (CHA)
– CHA turns most virtual calls into static calls
– JVM detects new classes loaded, adjusts CHA
§ May need to re-JIT
– When CHA fails to make the call static, inline caches
– When IC's fail, virtual calls are back to being slow
31. 31
Inlining
§ Combine caller and callee into one unit
– e.g. based on profile
– … or prove smth using CHA (Class Hierarchy Analysis)
– Perhaps with a guard/test
§ Optimize as a whole
– More code means better visibility
34. 34
Inlining and devirtualization
§ Inlining is the most profitable compiler optimization
– Rather straightforward to implement
– Huge benefits: expands the scope for other optimizations
§ OOP needs polymorphism, that implies virtual calls
– Prevents naïve inlining
– Devirtualization is required
– (This does not mean you should not write OOP code)
35. 35
Call Site
§ The place where you make a call
§ Monomorphic (“one shape”)
– Single target class
§ Bimorphic (“two shapes”)
§ Polymorphic (“many shapes”)
§ Megamorphic
36. 36
JVM Devirtualization
§ Analyzes hierarchy of currently loaded classes
§ Efficiently devirtualizes all monomorphic calls
§ Able to devirtualize polymorphic calls
§ JVM may inline dynamic methods
– Reflection calls
– Runtime-synthesized methods
– JSR 292
37. 37
Feedback multiplies optimizations
§ Profiling and CHA produces information
– ...which lets the JIT ignore unused paths
– ...and helps the JIT sharpen types on hot paths
– ...which allows calls to be devirtualized
– ...allowing them to be inlined
– ...expanding an ever-widening optimization horizon
§ Result:
Large native methods containing tightly optimized machine code for
hundreds of inlined calls!
46. 46
Monitoring JIT-Compiler
§ how to print info about compiled methods?
– -XX:+PrintCompilation
§ how to print info about inlining decisions
– -XX:+PrintInlining
§ how to control compilation policy?
– -XX:CompileCommand=…
§ how to print assembly code?
– -XX:+PrintAssembly
– -XX:+PrintOptoAssembly (C2-only)
49. 49
Print Compilation
§ 2043 470 % ! jdk.nashorn.internal.ir.FunctionNode::accept @ 136 (265 bytes)
% == OSR compilation
! == has exception handles (may be expensive)
s == synchronized method
§ 2028 466 n java.lang.Class::isArray (native)
n == native method
Other useful info
50. 50
Print Compilation
§ 621 160 java.lang.Object::equals (11 bytes) made not entrant
– don‘t allow any new calls into this compiled version
§ 1807 160 java.lang.Object::equals (11 bytes) made zombie
– can safely throw away compiled version
Not just compilation notifications
51. 51
No JIT At All?
§ Code is too large
§ Code isn’t too «hot»
– executed not too often
55. 55
Intrinsic
§ Known to the JIT compiler
– method bytecode is ignored
– inserts “best” native code
§ e.g. optimized sqrt in machine code
§ Existing intrinsics
– String::equals, Math::*, System::arraycopy, Object::hashCode,
Object::getClass, sun.misc.Unsafe::*
56. 56
Inlining Tuning
§ -XX:MaxInlineSize=35
– Largest inlinable method (bytecode)
§ -XX:InlineSmallCode=#
– Largest inlinable compiled method
§ -XX:FreqInlineSize=#
– Largest frequently-called method…
§ -XX:MaxInlineLevel=9
– How deep does the rabbit hole go?
§ -XX:MaxRecursiveInlineLevel=#
– recursive inlining
57. 57
Machine Code
§ -XX:+PrintAssembly
§ http://wikis.sun.com/display/HotSpotInternals/PrintAssembly
§ Knowing code compiles is good
§ Knowing code inlines is better
§ Seeing the actual assembly is best!
59. 59
What Have We Learned?
§ How JIT compilers work
§ How HotSpot’s JIT works
§ How to monitor the JIT in HotSpot
60. 60
Future work
§ Da Vinci Machine Project (MLVM)
– project page: http://openjdk.java.net/projects/mlvm/
– repository: http://hg.openjdk.java.net/mlvm/mlvm
§ JSR292 matured there
§ Some other projects:
– value objects
– coroutines / continuations
– tail calls (hard / guaranteed)
Where does innovation take place?