JVM++: The Graal VM Martin Toshev, BGOUG, 04.06.2016
JVM++: The Graal VM
Martin Toshev,BGOUG, 04.06.2016
Special thanks to Thomas Wuerthinger and Oracle Labs for inspiring this presentation …
Who am I
Software consultant (CoffeeCupConsulting)
BG JUG board member (http://jug.bg)
OpenJDK and Oracle RDBMS enthusiast
Who am IAuthor of Learning RabbitMQ
Agenda
• Overview of the Graal VM
• Building and managing Graal
• Performance benchmarks
• Future directions
Overview of the Graal VM
Overview of the Graal VM
Some background … static compilation vs dynamic compilation
static compiler
source code (e.g. JVM bytecode)
dynamic compilerruntime
machine code
runtime
source code (e.g. JVM bytecode)
machine code
Overview of the Graal VM
• The JVM performs static compilation of Java sources to bytecode
• The JVM may perform dynamic compilation of bytecode to machine code for optimization using a JIT (Just-in-Time) compiler
Overview of the Graal VM
• Graal is a new JIT (Just-in-Time) compiler for the JVM
• Brings the performance of Java to scripting languages (via the Truffle API)
• written in Java
Overview of the Graal VM
• In essence the Graal JIT compiler generates machine code from an optimized AST rather than bytecode
• However the Graal VM has both AST and bytecode interpreters
Overview of the Graal VM
• The Graal VM supports the following types of compilers (apart from the Graal JIT compiler):
--vm server-nograal // server compiler --vm server // server compiler using Graal--vm graal // Graal compiler using Graal--vm client-nograal // client compiler--vm client // client compiler running Graal
Overview of the Graal VM
• The Truffle API:
– is a Java API
– provides AST (Abstract Syntax Tree) representation of source code
– provides a mechanism to convert the generated AST into a Graal IR (intermediate representation)
Overview of the Graal VM
• The Truffle API is declarative (uses Java annotations)
• The AST graph generated by Truffle is a mixture of control flow and data flow graph
• Essential feature of the Truffle API is the ability to specify node specializations used in node rewriting
Overview of the Graal VM
• The Truffle API is used in conjunction with custom annotation processor that generates code based on the Truffle annotations used in the interpreter classes
Truffle Interpreter javac
Annotation processor
Compiled Interpreter
Generated Interpreter source code
Overview of the Graal VM
• General architecture:
HotSpot VM (C++)
Graal (Java)
Truffle (Java) Java Other JVM language
JavaScript Ruby R Other interpreted language
Graal <–> Hotspot adapter (C++, Java)
Maxine VM (Java)
Graal <–> Maxine adapter (Java)
Overview of the Graal VM
• Let’s see, for example, how a JavaScript interpreter works in Graal …
Overview of the Graal VM
HotSpot VM (C++)
Graal (Java)
Truffle (Java) Java Other JVM language
JavaScript Ruby R Other interpreted language
Graal <–> Hotspot adapter (C++, Java)
Run JavaScript file: app.js
Overview of the Graal VM
HotSpot VM (C++)
Graal (Java)
Truffle (Java) Java Other JVM language
JavaScript Ruby R Other interpreted language
Graal <–> Hotspot adapter (C++, Java)
JavaScript Interpreter parses app.js and converts it to Truffle AST
Overview of the Graal VM
HotSpot VM (C++)
Graal (Java)
Truffle (Java) Java Other JVM language
JavaScript Ruby R Other interpreted language
Graal <–> Hotspot adapter (C++, Java)
The app.js Truffle AST is converted to Graal IR (AST)
Overview of the Graal VM
HotSpot VM (C++)
Graal (Java)
Truffle (Java) Java Other JVM language
JavaScript Ruby R Other interpreted language
Graal <–> Hotspot adapter (C++, Java)
The Graal VM has bytecode and AST interpreters along with a JIT compiler(optimizations and AST lowering is performed to generate machine code and perform partial evaluation)
Overview of the Graal VM
HotSpot VM (C++)
Graal (Java)
Truffle (Java) Java Other JVM language
JavaScript Ruby R Other interpreted language
Graal <–> Hotspot adapter (C++, Java)
When parts of app.js are compiled to machine code by the Graal compiler the compiled code is transferred to Hotspot for execution by means of Hotspot APIs and with the help of a Graal – Hotspot adapter interface
Overview of the Graal VM
HotSpot VM (C++)
Graal (Java)
Truffle (Java) Java Other JVM language
JavaScript Ruby R Other interpreted language
Graal <–> Hotspot adapter (C++, Java)
Compiled code can also be deoptimized and control is transferred back to the interpreter (e.g. when an exception occurs, an assumption fails or a guard is reached)
Overview of the Graal VM
• Optimizations done on the Graal IR include:
– method inlining – partial escape analysis– inline caching– constant folding– arithmetic optimizations and others …
Overview of the Graal VM
• Currently supported languages include:
– JavaScript (Graal.JS)– R (FastR)– Ruby (RubyTruffle)– Experimental interpreters for C, Python, Smalltalk,
LLVM IR and others …
(some are not open-sourced as of the time of this presentation)
Overview of the Graal VM
• The SimpleLanguage project provides a showcase on how to use the Truffle APIs
Source file Parser Truffle AST Graal VM
Building the Graal VM
Building the Graal VM
• The Graal VM interoperates with JEP 243: Java-Level JVM Compiler Interface
• In that regard two modes of operation are supported – via the Graal VM or via the JVMCI (JVM Compiler Interface)
Building the Graal VM
• Early access builds of Graal available for download
• You can build Graal on any supported platform (Windows, Linux, MacOS, Solaris) from source code
Building the Graal VM
git clone https://github.com/graalvm/mx.gitexport PATH=$PWD/mx:$PATHgit clone https://github.com/graalvm/graal-core.gitcd graal-coremx --vm graal buildmx vm
Building the Graal VM
• The MX tool is a Python (2.7) tool that manages the Graal projects and allows to:
- update sources and run JUnit tests and benchmarks;
- builds (sub)projects (Java and C++ sources can be built separately);
- runs VM tools (such as IGV or c1visualizer) that can aid development of Graal or language interpreters;
- can be used to generate IDE project files for Graal development (support for Eclipse and NetBeans)
The Graal VMdemo
Performance benchmarks
Performance benchmarks• According to various JVM benchmarks such as SPECjvm2008
and Scala Dacapo the Graal compiler positions between the client and server JVM compilers(source: One VM to Rule Them All, Thomas Wuertinger)
• In general Truffle interpreters provide much better performance for some dynamic languages
Performance benchmarks• Graal.JS – shows improvement of 1.5x in some cases
with a peak of 2.6x compared to V8 (running Google Octane’s benchmark)
• RubyTruffle – shows improvements between 1.5x and 4.5x in some cases with a peak of 14x compared to JRuby
• FastR - shows improvements between 2x and 39x in some cases with a peak of 94x compared to GnuR
(source: One VM to Rule Them All, Thomas Wuertinger)
Future directions
Future directions
• Graal provides an extensible framework for researching and experimenting with other compiler optimizations
• In that regard new types of compiler optimization may be introduced in the VM that address runtime behavior in certain scenarios
Future directions
• Graal provides an extensible framework for creating language interpreters further simplified by the Truffle API
• In that regard more languages can be introduced that run in the Graal VM
• Currently existing languages that compile to bytecode may support Truffle in the future
Future directions
• Graal provides an extensible framework for different types of operating systems and CPU architectures
• In that regard CPU architectures and operating systems might be supported by Graal
Thank you !
Q&A
ReferencesGraal Projecthttp://openjdk.java.net/projects/graal/
Graal - A New Just In Time Compiler for the JVMhttp://www.oracle.com/technetwork/oracle-labs/program-languages/overview/index.html
Graal VM: GitHub repositorieshttps://github.com/graalvm
ReferencesGraal - A Bytecode Agnostic Compiler for the JVM, Thomas Wuerthinger, JVM Language Summithttp://medianetwork.oracle.com/video/player/1113230360001
Graal and Truffle: One VM to Rule Them Allhttp://www.slideshare.net/ThomasWuerthinger/graal-truffle-ethdec2013?qid=848a4965-9768-40d5-b167-ccd8eb7d1659&v=&b=&from_search=2
ReferencesGraal and Truffle: Modularity and Separation of Concerns as Cornerstones for Building a Multipurpose Runtimehttp://www.slideshare.net/ThomasWuerthinger/2014-0424-graal-modularity?qid=0e86ed21-0b8f-4087-9212-68f76546d674&v=&b=&from_search=4
Dynamic Compilation - Thomas Wuerthingerhttps://www.youtube.com/watch?v=RLsG2kE1EMIhttps://www.youtube.com/watch?v=U_QpToYLsO0
JVMLS 2012: Graalhttp://medianetwork.oracle.com/video/player/1785432492001
ReferencesGraal Tutorial at CGO 2015 by Christian Wimmerhttp://lafo.ssw.uni-linz.ac.at/papers/2015_CGO_Graal.pdf
Writing a language in Truffle: Part 1 - Using Truffle and Graalhttps://cesquivias.github.io/blog/2014/10/13/writing-a-language-in-truffle-part-1-a-simple-slow-interpreter/
Writing a language in Truffle: Part 2 - Using Truffle and Graalhttp://cesquivias.github.io/blog/2014/12/02/writing-a-language-in-truffle-part-2-using-truffle-and-graal/
ReferencesGraal publications, JKU, Linzhttp://ssw.jku.at/Research/Projects/JVM/Graal.html
Truffle publications, JKU, Linzhttp://ssw.jku.at/Research/Projects/JVM/Truffle.html
ReferencesR as a citizen in a Polyglot world: The promise of the Truffle frameworkhttp://user2015.math.aau.dk/presentations/112.pdf
Truffle/C Interpreterhttp://www.manuelrigger.at/downloads/trufflec_thesis.pdf
Graal.JS - high-performance JavaScript on the JVM talk by Christian Wirthhttps://www.youtube.com/watch?v=OUo3BFMwQFo