javantura v4 - jvm++ the graalvm - martin toshev

43
JVM++: The Graal VM Martin Toshev @martin_fmi

Category:

Technology


1 download

TRANSCRIPT

Page 1: Javantura v4 - JVM++ The GraalVM - Martin Toshev

JVM++: The Graal VM

Martin Toshev

@martin_fmi

Page 2: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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

Page 3: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Who am ISoftware consultant (CoffeeCupConsulting)

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

OpenJDK and Oracle RBDMS enthusiast

Twitter: @martin_fmi

Page 4: Javantura v4 - JVM++ The GraalVM - Martin Toshev
Page 5: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Agenda

• Overview of the Graal VM

• Building and managing Graal

• Performance benchmarks

• Future directions

Page 6: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

Page 7: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 9: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 10: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 GraalIR (intermediate representation)

Page 11: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 12: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 13: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

• General architecture:

HotSpot VM (C++)

Graal (Java)

Truffle (Java) Java Other JVM language

JavaScript Ruby ROther interpreted

language

Graal <–> Hotspot adapter (C++, Java)

Maxine VM (Java)

Graal <–> Maxine adapter (Java)

Page 14: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

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

Page 15: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

HotSpot VM (C++)

Graal (Java)

Truffle (Java) Java Other JVM language

JavaScript Ruby ROther interpreted

language

Graal <–> Hotspot adapter (C++, Java)

Run JavaScript file: app.js

Page 16: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

HotSpot VM (C++)

Graal (Java)

Truffle (Java) Java Other JVM language

JavaScript Ruby ROther interpreted

language

Graal <–> Hotspot adapter (C++, Java)

JavaScript Interpreter parses app.js and converts it to Truffle AST

Page 17: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

HotSpot VM (C++)

Graal (Java)

Truffle (Java) Java Other JVM language

JavaScript Ruby ROther interpreted

language

Graal <–> Hotspot adapter (C++, Java)

The app.js Truffle AST is converted to Graal IR (AST)

Page 18: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

HotSpot VM (C++)

Graal (Java)

Truffle (Java) Java Other JVM language

JavaScript Ruby ROther 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 19: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

HotSpot VM (C++)

Graal (Java)

Truffle (Java) Java Other JVM language

JavaScript Ruby ROther 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 20: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Overview of the Graal VM

HotSpot VM (C++)

Graal (Java)

Truffle (Java) Java Other JVM language

JavaScript Ruby ROther 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 21: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 22: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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)

Page 23: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 24: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Building the Graal VM

Page 25: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 26: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 27: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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

Page 28: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 29: Javantura v4 - JVM++ The GraalVM - Martin Toshev

The Graal VMdemo

Page 30: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Performance benchmarks

Page 31: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 32: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 33: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Future directions

Page 34: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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

Page 35: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 36: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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

Page 37: Javantura v4 - JVM++ The GraalVM - Martin Toshev

Thank you !

Q&A

Page 38: Javantura v4 - JVM++ The GraalVM - Martin Toshev

ReferencesGraal Project

http://openjdk.java.net/projects/graal/

Graal - A New Just In Time Compiler for the JVM

http://www.oracle.com/technetwork/oracle-labs/program-languages/overview/index.html

Graal VM: GitHub repositories

https://github.com/graalvm

Page 39: Javantura v4 - JVM++ The GraalVM - Martin Toshev

ReferencesGraal - 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

Page 40: Javantura v4 - JVM++ The GraalVM - Martin Toshev

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 41: Javantura v4 - JVM++ The GraalVM - Martin Toshev

ReferencesGraal 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/

Page 42: Javantura v4 - JVM++ The GraalVM - Martin Toshev

ReferencesGraal publications, JKU, Linz

http://ssw.jku.at/Research/Projects/JVM/Graal.html

Truffle publications, JKU, Linz

http://ssw.jku.at/Research/Projects/JVM/Truffle.html

Page 43: Javantura v4 - JVM++ The GraalVM - Martin Toshev

ReferencesR 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