Graal & Truffle How to write fast interpreters on the JVM - - PowerPoint PPT Presentation

graal truffle
SMART_READER_LITE
LIVE PREVIEW

Graal & Truffle How to write fast interpreters on the JVM - - PowerPoint PPT Presentation

Graal & Truffle How to write fast interpreters on the JVM download: html / pdf Manuel Leduc (https://mleduc.xyz/) 1 / 17 Domain Specific Language A domain-specific language (DSL) is a computer language specialized to a particular


slide-1
SLIDE 1

Graal & Truffle

How to write fast interpreters on the JVM

download: html / pdf

Manuel Leduc (https://mleduc.xyz/) 1 / 17

slide-2
SLIDE 2

Domain Specific Language

A domain-specific language (DSL) is a computer language specialized to a particular application domain

Examples

awk pom CSS ...

To Read

Martin fowler https://martinfowler.com/dsl.html

How to write fast interpreters on the JVM - download: html / pdf

2 / 17

slide-3
SLIDE 3

Program

(1x2)+3

Abstract Syntax tree

r l r Mult l Add Lit value = 1 Lit value = 2 Lit value = 3

Semantics

[Mult] = [l] * [r] [Add] = [l] + [r] [Lit] = value

Domain Specific Language

Result = 6

How to write fast interpreters on the JVM - download: html / pdf

3 / 17

slide-4
SLIDE 4

Compiler

+ Fast

  • Hard to implement
  • Hard to reuse (compose, extends...)

Interpreter

  • Slow(er)

+ Easier to implement + Easier to reuse (compose, extends...)

Interpreter or Compiler?

Objective

Obtaining interpreters with acceptable performances on the first try.

How to write fast interpreters on the JVM - download: html / pdf

4 / 17

slide-5
SLIDE 5

Graal

5 / 17

slide-6
SLIDE 6

Performance

How to write fast interpreters on the JVM - download: html / pdf

6 / 17

slide-7
SLIDE 7

Just-In-Time Compiler and the JVM

Just-In-Time (JIT) Compiler

Instead of interpreting bytecode instruction one by one, parts of the bycecode are compiled at runtime before being executed

JDK9 and JVMCI

JVMCI = Java-Level JVM Compiler Interface C++ is replaced by Java Better Modularity Avoid JVM (Re)-Compilation

How to write fast interpreters on the JVM - download: html / pdf

7 / 17

slide-8
SLIDE 8

JVMCI internals

It simply takes a bunch of bytecode operations and yield equivalent machine level code.

interface JVMCICompiler { byte[] compileMethod(byte[] bytecode); }

How to write fast interpreters on the JVM - download: html / pdf

8 / 17

slide-9
SLIDE 9

Graal

Graal is a JIT implementation build on top of the JVMCICompiler interface.

Sea of Node graph

Graal is based on a "sea of nodes" connected by Data Flow (blue) and Control Flow (red) annotations.

Partial interpretation

This graph is build by partially interpreting the bytecode.

How to write fast interpreters on the JVM - download: html / pdf

9 / 17

slide-10
SLIDE 10

Graph Properties

Annotated with runtime information Garbage collection analysis High-Level static optimizations Method inlining Constant folding Arithmetic optimizations Speculative optimizations Can be visualy inspected

How to write fast interpreters on the JVM - download: html / pdf

10 / 17

slide-11
SLIDE 11

Truffle

11 / 17

slide-12
SLIDE 12

Truffle in a picture

How to write fast interpreters on the JVM - download: html / pdf

12 / 17

slide-13
SLIDE 13

Truffle ideas

High level representation of the language AST Annotated AST to assist Graal speculative optimizations.

How to write fast interpreters on the JVM - download: html / pdf

13 / 17

slide-14
SLIDE 14

Simple concepts

nodes methods specialization "classical" interpreters language metadata stack frames

@NodeInfo(shortName = "+") public abstract class ELTAddNode extends ELTBinaryNode { @Specialization protected int add(int left, int right) { return left + right; } @Specialization protected float add(float left, float right) { return left + right; } @Specialization protected String add(String left, String right) { return left + right; } @Specialization protected int add(Object left, int right) { return ((Integer) left) + right; } }

Language implementation with Truffle

How to write fast interpreters on the JVM - download: html / pdf

14 / 17

slide-15
SLIDE 15

Polyglot

Example

#include <polyglot.h> int main() { void *array = polyglot_eval("js", "[1,2,42,4]"); int element = polyglot_as_i32(polyglot_get_array_element(array, 2)); printf("%d\n", element); return element; }

To Read

http://www.graalvm.org/docs/reference-manual/polyglot/

How to write fast interpreters on the JVM - download: html / pdf

15 / 17

slide-16
SLIDE 16

Scientific questions

Can we reuse Truffle to define interpreters using ALE/K3/Gemoc... solutions? What new can we do if everything run fast on the JVM? What is the consequence of Polyglot for the DSL intercompatibility?

How to write fast interpreters on the JVM - download: html / pdf

16 / 17

slide-17
SLIDE 17

Resources

Really good blog post: http://chrisseaton.com/truffleruby/jokerconf17/ Another blog post: https://zeroturnaround.com/rebellabs/graal-and-truffle-for- polyglot-languages-on-jvm/ Comprehensive slides: http://lafo.ssw.uni- linz.ac.at/papers/2017_PLDI_GraalTutorial.pdf Publications: https://github.com/oracle/graal/blob/master/docs/Publications.md

How to write fast interpreters on the JVM - download: html / pdf

17 / 17