![Page 1: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/1.jpg)
1 | © 2011 Oracle Corporation
![Page 2: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/2.jpg)
2 | © 2011 Oracle Corporation
Graal - A Bytecode Agnostic Compiler for the JVM
Thomas Wuerthinger
Oracle Labs JVM Language Summit, 20st July 2011
![Page 3: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/3.jpg)
3 | © 2011 Oracle Corporation
The following is intended to outline our general
product direction. It is intended for information
purposes only, and may not be incorporated into any
contract. It is not a commitment to deliver any
material, code, or functionality, and should not be
relied upon in making purchasing decisions.
The development, release, and timing of any
features or functionality described for Oracle’s
products remains at the sole discretion of Oracle.
![Page 4: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/4.jpg)
4 | © 2011 Oracle Corporation
My Background
• Dynamic Code Evolution VM
– Supports unlimited class redefinition.
– Binaries are available from http://ssw.jku.at/dcevm/.
• Working on Crankshaft/V8
– Optimizing compiler for JavaScript.
– Uses an SSA form intermediate representation.
– 1.5x speedup compared to previous V8 version.
• Since April 2011: Oracle Labs
![Page 5: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/5.jpg)
5 | © 2011 Oracle Corporation
Program Agenda
• Graal Compiler
• Compiler Extensions
• DLR within the JVM
• Workshop…
![Page 6: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/6.jpg)
6 | © 2011 Oracle Corporation
The Graal Compiler
• Java JIT Compiler written in Java
– Port of C1 from C++ to Java (C1X)
– New high-level IR
• Basic Design
– SSA form
– Program Dependence Graph (“sea of nodes”)
– Linear scan register allocator
• Extensibility
– A compiler for multiple VMs
– Customizable from Java application code
![Page 7: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/7.jpg)
7 | © 2011 Oracle Corporation
Compiler-Runtime Separation
Graal
CRI
Adapter
HotSpot VM
Maxine VM
Java
C++
![Page 8: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/8.jpg)
8 | © 2011 Oracle Corporation
Compiler Extensions
Extension
Interface
Guide
Optimization
Intrinsification Graal
CRI
JVM
![Page 9: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/9.jpg)
9 | © 2011 Oracle Corporation
Guide Extensions (1) Example: Influencing Inlining Decisions
enum InliningHint { NONE, NEVER, LESS, MORE, ALWAYS }
interface InliningGuide { InliningHint getHint(int depth, RiMethod caller, int bci, RiMethod target); }
• Different parts of an application need different
inlining settings (e.g. JRuby generated code versus
Java code).
![Page 10: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/10.jpg)
10 | © 2011 Oracle Corporation
Guide Extensions (2) Example: Influencing Inlining Decisions
class InliningGuideImpl implements InliningGuide { public InliningHint getHint(int depth, RiMethod caller, int bci, RiMethod m) { if (m.name().equals("neverInline")) return InliningHint.NEVER; if (m.name().equals("alwaysInline") && depth < 50) return InliningHint.ALWAYS; return InliningHint.NONE; } }
int test() { return alwaysInline(30); } int alwaysInline(int value) { if (value < 0) return neverInline(value); return alwaysInline(value - 1); } int neverInline(int value) { return value; }
Register as a service at META-INF/services/com.oracle.max.graal.extensions.InliningGuide
![Page 11: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/11.jpg)
11 | © 2011 Oracle Corporation
Intrinsification Extensions (1) Example: Efficient Detection of Integer Overflow
int safeAdd(int a, int b) { int result = a + b; if (b < 0 && result > a) { throw new IllegalStateException("underflow"); } else if (b > 0 && result < a) { throw new IllegalStateException("overflow"); } return result; }
int test() { int sum = 0; for (int i = 0; i < N; i = safeAdd(i, 1)) { sum = safeAdd(sum, i); } return sum; }
![Page 12: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/12.jpg)
12 | © 2011 Oracle Corporation
Intrinsification Extensions (2) Example: Efficient Detection of Integer Overflow
• Special language constructs need an optimized
machine code sequence.
interface Intrinsifier { Graph intrinsicGraph(RiRuntime runtime, RiMethod caller, int bci, RiMethod method, List<? extends Node> parameters); }
![Page 13: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/13.jpg)
13 | © 2011 Oracle Corporation
Intrinsification Extensions (3) Example: Efficient Detection of Integer Overflow
class SafeAddNode extends IntegerArithmeticNode { // ... public void generate(LIRGenerator generator) { super.generate(generator); generator.deoptimizeOn(Condition.Overflow); } }
class IntrinsifierImpl implements Intrinsifier { public Graph intrinsicGraph(RiRuntime runtime, RiMethod caller, int bci, RiMethod method, List<? extends Node> params) { if (method.name().equals("safeAdd")) { SafeAddNode node = new SafeAddNode(params.get(0), params.get(1)); return CompilerGraph.create(node); } return null; } }
![Page 14: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/14.jpg)
14 | © 2011 Oracle Corporation
Optimization Extensions Example: Elimination of Overflow Checks
• Specific knowledge about the semantics of library
methods enables custom optimization phases.
interface Optimizer { void optimize(RiRuntime runtime, Graph graph); }
public class OptimizerImpl implements Optimizer { public void optimize(RiRuntime runtime, Graph graph) { for (SafeAddNode safeAdd : graph.getNodes(SafeAddNode.class)) { if (!canOverflow(safeAdd)) { IntegerAdd add = new IntegerAdd(safeAdd.x(), safeAdd.y()); safeAdd.replaceAndDelete(add); } } } // ... }
![Page 15: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/15.jpg)
15 | © 2011 Oracle Corporation
DLR within the JVM (1)
int safeAdd(int a, int b) { int result = a + b; if (b < 0 && result > a) { throw new IllegalStateException("underflow"); } else if (b > 0 && result < a) { throw new IllegalStateException("overflow"); } return result; }
int test() { int sum = 0; for (int i = 0; i < N; i = safeAdd(i, 1)) { sum = safeAdd(sum, i); } return sum; }
Direct Deoptimization into the DLR Interpreter
Dynamic Language
Runtime Interpreter
![Page 16: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/16.jpg)
16 | © 2011 Oracle Corporation
DLR within the JVM (2) Direct Deoptimization into the DLR Interpreter
• Graal enables changes to the deoptimization
information using extensions.
interface FrameModifier { CiFrame getFrame(RiRuntime runtime, CiFrame frame); }
![Page 17: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/17.jpg)
17 | © 2011 Oracle Corporation
Object[]
DLR within the JVM (3) Direct Deoptimization into the DLR Interpreter
public class FrameModifierImpl implements FrameModifier { public CiFrame getFrame(RiRuntime runtime, CiFrame frame) { if (frame.method.name().equals("test")) { RiMethod handlerMethod = runtime.getMethod("DeoptHandler.handle"); CiValue[] values = new CiValue[frame.values.length]; for (int i = 0; i < values.length; i++) { values[i] = CiVirtualObject.proxy(frame.values[i]); } CiVirtualObject local = CiVirtualObject.createObjectArray(values); return new CiFrame(method, new CiValue[]{local}); } return frame; } }
Locals
Expressions
Locks
Original Frame New Frame
Locals
Expressions
Locks
![Page 18: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/18.jpg)
18 | © 2011 Oracle Corporation
DLR within the JVM (4) Direct Deoptimization into the DLR Interpreter
public class DeoptHandler { public static int handle(Object[] values) { // Call DLR interpreter with the state (values). return 0; } }
int test() { int sum = 0; for (int i = 0; i < N; i = safeAdd(i, 1)) { sum = safeAdd(sum, i); } return sum; }
Jump on
overflow / underflow
![Page 19: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/19.jpg)
19 | © 2011 Oracle Corporation
DLR within the JVM (5) Direct Deoptimization into the DLR Interpreter
• The generated method is never executed in the
interpreter => No need for bytecodes.
AST of dynamic
language Graal IR
Language-
specific
optimizations
Machine code
DLR
Interpreter
execution and gathering of
profiling data
direct deoptimization to the
DLR interpreter
![Page 20: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/20.jpg)
20 | © 2011 Oracle Corporation
Summary
Libraries
DLRs
Extension
Interface Graal CRI
JVM
![Page 21: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/21.jpg)
21 | © 2011 Oracle Corporation
Acknowledgements
• Maxine Team
– Mario Wolczko (manager)
– Doug Simon
– Laurent Daynès
– Michael Van De Vanter
– Mick Jordan
– Michael Haupt
– Christian Wimmer
• Discussions
– John Rose
– Tom Rodriguez
– Peter Kessler
• Other
– Ben Titzer
• Implementation
– Lukas Stadler (JKU Linz)
– Gilles Duboscq (Intern)
![Page 22: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/22.jpg)
22 | © 2011 Oracle Corporation
Q&A
Workshop…
![Page 23: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/23.jpg)
23 | © 2011 Oracle Corporation
![Page 24: © 2011 Oracle Corporation - JVMLangSummitwiki.jvmlangsummit.com/images/8/8e/GraalJVMSummit2011.pdf · 2 | © 2011 Oracle Corporation Graal - A Bytecode Agnostic Compiler for the](https://reader030.vdocuments.site/reader030/viewer/2022041007/5ead9cd5a4ca3d5a545db093/html5/thumbnails/24.jpg)
24 | © 2011 Oracle Corporation