7. Overview of the Graal VM
Some background …
static compilation vs dynamic compilation
static compiler
source code (e.g. JVM bytecode)
dynamic compiler
runtime
machine code
runtime
source code (e.g. JVM bytecode)
machine code
8. 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
9. 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
10. 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)
11. 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
12. 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
13. 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)
14. Overview of the Graal VM
• Let’s see, for example, how a JavaScript interpreter works
in Graal …
15. 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
16. 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
17. 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)
18. 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)
19. 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
20. 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)
21. 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 …
22. 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 yet as of the time of this presentation)
23. 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
25. 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)
26. 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
27. Building the Graal VM
git clone https://github.com/graalvm/mx.git
export PATH=$PWD/mx:$PATH
git clone https://github.com/graalvm/graal-core.git
cd graal-core
mx --vm graal build
mx vm
28. 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)
31. 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
32. 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)
34. Future directions
• Graal provides an extensible framework for researching
and experimenting with other compiler optimizations
• In that regard new types of compiler optimizations that
address runtime behavior in certain scenarios may be
introduced in the VM
35. 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
36. Future directions
• Graal provides an extensible framework for different types
of operating systems and CPU architectures
• In that regard more CPU architectures and operating
systems might be supported by Graal
39. References
Graal - A Bytecode Agnostic Compiler for the JVM, Thomas Wuerthinger, JVM
Language Summit
http://medianetwork.oracle.com/video/player/1113230360001
Graal and Truffle: One VM to Rule Them All
http://www.slideshare.net/ThomasWuerthinger/graal-truffle-
ethdec2013?qid=848a4965-9768-40d5-b167-
ccd8eb7d1659&v=&b=&from_search=2
40. References
Graal and Truffle: Modularity and Separation of Concerns as Cornerstones for
Building a Multipurpose Runtime
http://www.slideshare.net/ThomasWuerthinger/2014-0424-graal-
modularity?qid=0e86ed21-0b8f-4087-9212-
68f76546d674&v=&b=&from_search=4
Dynamic Compilation - Thomas Wuerthinger
https://www.youtube.com/watch?v=RLsG2kE1EMI
https://www.youtube.com/watch?v=U_QpToYLsO0
JVMLS 2012: Graal
http://medianetwork.oracle.com/video/player/1785432492001
41. References
Graal Tutorial at CGO 2015 by Christian Wimmer
http://lafo.ssw.uni-linz.ac.at/papers/2015_CGO_Graal.pdf
Writing a language in Truffle: Part 1 - Using Truffle and Graal
https://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 Graal
http://cesquivias.github.io/blog/2014/12/02/writing-a-language-in-truffle-
part-2-using-truffle-and-graal/
43. References
R as a citizen in a Polyglot world: The promise of the Truffle framework
http://user2015.math.aau.dk/presentations/112.pdf
Truffle/C Interpreter
http://www.manuelrigger.at/downloads/trufflec_thesis.pdf
Graal.JS - high-performance JavaScript on the JVM talk by Christian Wirth
https://www.youtube.com/watch?v=OUo3BFMwQFo