jvm++: the graal vm

45
JVM++: The Graal VM Martin Toshev, BGOUG, 04.06.2016

Upload: martin-toshev

Post on 09-Jan-2017

662 views

Category:

Software


0 download

TRANSCRIPT

Page 1: JVM++: The Graal VM

JVM++: The Graal VM

Martin Toshev,BGOUG, 04.06.2016

Page 2: JVM++: The Graal VM

Special thanks to Thomas Wuerthinger and Oracle Labs for inspiring this presentation …

Page 3: JVM++: The Graal VM

Who am I

Software consultant (CoffeeCupConsulting)

BG JUG board member (http://jug.bg)

OpenJDK and Oracle RDBMS enthusiast

Page 4: JVM++: The Graal VM

Who am IAuthor of Learning RabbitMQ

Page 5: JVM++: The Graal VM

Agenda

• Overview of the Graal VM

• Building and managing Graal

• Performance benchmarks

• Future directions

Page 6: JVM++: The Graal VM

Overview of the Graal VM

Page 7: JVM++: 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

Page 8: JVM++: The Graal VM

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

Page 9: JVM++: The Graal VM

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

Page 10: JVM++: The Graal VM

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

Page 11: JVM++: The Graal VM

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

Page 12: JVM++: The Graal VM

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)

Page 13: JVM++: The Graal VM

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

Page 14: JVM++: The Graal VM

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

Page 15: JVM++: The Graal VM

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)

Page 16: JVM++: The Graal VM

Overview of the Graal VM

• Let’s see, for example, how a JavaScript interpreter works in Graal …

Page 17: JVM++: The Graal VM

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

Page 18: JVM++: The Graal VM

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

Page 19: JVM++: The Graal VM

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)

Page 20: JVM++: The Graal VM

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)

Page 21: JVM++: The Graal VM

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

Page 22: JVM++: The Graal VM

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)

Page 23: JVM++: The Graal VM

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 …

Page 24: JVM++: The Graal VM

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)

Page 25: JVM++: The Graal VM

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

Page 26: JVM++: The Graal VM

Building the Graal VM

Page 27: JVM++: 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)

Page 28: JVM++: The Graal VM

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

Page 29: JVM++: The Graal VM

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

Page 30: JVM++: The Graal 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)

Page 31: JVM++: The Graal VM

The Graal VMdemo

Page 32: JVM++: The Graal VM

Performance benchmarks

Page 33: JVM++: The Graal VM

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

Page 34: JVM++: The Graal VM

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)

Page 35: JVM++: The Graal VM

Future directions

Page 36: JVM++: The Graal VM

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

Page 37: JVM++: The Graal VM

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

Page 38: JVM++: The Graal VM

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

Page 39: JVM++: The Graal VM

Thank you !

Q&A

Page 40: JVM++: The Graal VM

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

Page 41: JVM++: The Graal VM

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

Page 42: JVM++: The Graal VM

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

Page 43: JVM++: The Graal VM

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/

Page 44: JVM++: The Graal VM

ReferencesGraal publications, JKU, Linzhttp://ssw.jku.at/Research/Projects/JVM/Graal.html

Truffle publications, JKU, Linzhttp://ssw.jku.at/Research/Projects/JVM/Truffle.html

Page 45: JVM++: The Graal VM

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