29

Polyglot on the JVM with Graal - JUG · Key Features of Graal • Designed for speculative optimizations and deoptimization – Metadata for deoptimizationis propagated through all

  • Upload
    lamnhan

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

PolyglotontheJVMwithGraal

ThomasWuerthingerSeniorResearchDirector,OracleLabs@thomaswue

JavaUserGroupZurich,15ofDecember2016

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

SafeHarborStatementThefollowingisintendedtoprovidesomeinsightintoalineofresearchinOracleLabs.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedinconnectionwithanyOracleproductorserviceremainsatthesolediscretionofOracle.AnyviewsexpressedinthispresentationaremyownanddonotnecessarilyreflecttheviewsofOracle.

3

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

Onelanguagetorulethemall?

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

OneLanguagetoRuleThemAll?Let’saskStackOverflow…

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 6

3Lowe

risbe

tter

TheWorldIsPolyglot

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

Graal OverviewAnew compiler for HotSpotwritten inJavaand with afocus onspeculative optimizations.JVMCIand Graal included inJDK9,modified version of JDK8available viaOTN.

HotSpot

Compiler Interface

Client Server

HotSpot

JVMCI

Graal

C++

Java

HotSpotVM Graal VM

7

Compiler Interface

Client

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 8

CompilersAreComplexBeasts…inlining,globalvaluenumbering,constantfoldingandpropagation,deadcodeelimination,partialescapeanalysis,conditionalelimination,loop-invariantcodemotion,corelibraryintrinsifications,invariantreassociation,bounds-checkingelimination,readelimination,checkcast elimination,stringbuilderoptimizations,testreordering,strengthreduction,nullcheckelimination,allocationsitemerging,speculativeguardmovement,deoptimization grouping,commonsubexpression elimination,profile-baseddevirtualization,classhierarchyanalysis,redundantlockelision,tailduplication,pathduplication,push-through-phi,de-duplication,aliasclassificationandpointeranalysis,inductionvariableanalysis,loopfusion/inversion/unrolling/splitting/unswitching,automaticvectorization,registerallocation,instructionselection,peepholeoptimizations,instructionscheduling,code-blockreordering

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

KeyFeaturesofGraal• Designedforspeculativeoptimizationsanddeoptimization

– Metadatafordeoptimization ispropagatedthroughalloptimizationphases

• Aggressivehigh-leveloptimizations– Example:partialescapeanalysis

• Modulararchitecture– Configurablecompilerphases

• WritteninJava!– Easiertomaintainandlowerentrybarrier– Blursthelinebetweenuserapplicationanduserlibraryandcompiler– Graal compilingandoptimizingitselfisalsoagoodoptimizationopportunity– https://github.com/graalvm/graal-core

9

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 10

PartialEscapeAnalysis(1)public static Car getCached(int hp, String name) {

Car car = new Car(hp, name, null);Car cacheEntry = null;for (int i = 0; i < cache.length; i++) {

if (car.hp == cache[i].hp &&car.name == cache[i].name) {

cacheEntry = cache[i];break;

}}if (cacheEntry != null) {

return cacheEntry;} else {

addToCache(car);return car;

}}

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 11

PartialEscapeAnalysis(2)public static Car getCached(int hp, String name) {

Car cacheEntry = null;for (int i = 0; i < cache.length; i++) {

if (hp == cache[i].hp &&name == cache[i].name) {

cacheEntry = cache[i];break;

}}if (cacheEntry != null) {

return cacheEntry;} else {

Car car = new Car(hp, name, null);addToCache(car);return car;

}}

§ new Car(...) escapesat:— addToCache(car);

— return car;

§ Mightbeaveryunlikelypath

§ Noallocationinfrequentpath

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

Graal VMPolyglot

12

HotSpot

JVMCI

Graal

Compiler Interface

Client

Truffle

RRubyJavaScript

CLLVM

• JavaScript– BetterECMAScript2016scorethanV8– PerformancecompetitivewithV8– Fullnode.js support

• Ruby– ForkofJRuby for~5-10xspeed

• R– Statisticallanguage

• C,C++,Fortran– NativelanguagesupportviaLLVM

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

Truffle:SystemStructure

Guest Language Implementation

Host Services

Guest Language Application

OS

Application Developer

Language Developer

VM Expert

Guest Language

Managed Host Language

Managed Host Languageor Unmanaged Language

Unmanaged Language(typically C or C++)

Written by: Written in:

OS Expert

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

SpeculateandOptimize…

U

U U

U

U I

I I

G

G I

I I

G

G

Node Rewriting for Profiling Feedback

AST InterpreterRewritten Nodes

AST InterpreterUninitialized Nodes

Compilation usingPartial Evaluation

Compiled Code

Node Transitions

S

UI

D

G

Uninitialized Integer

Generic

DoubleString

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

…andDeoptimize andReoptimize!

I

I I

G

G I

I I

G

G

Deoptimizationto AST Interpreter

D

I D

G

G D

I D

G

G

Node Rewriting to Update Profiling Feedback

Recompilation usingPartial Evaluation

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 16

Graal.js Architecture

JavaScript

C++

Java

nodemoduleswithonlyJavaScriptnodemoduleswithnativeextensions

nodestandardlibrary

nodebindings(socket,http,…)

V8APIthreadpool(libeio)

eventloop(libev)

DNS(c-ares)

crypto(OpenSSL)

Graal.js JavaScriptEngine

AdapterV8APItoGraal.js viaJNI

nativeextensions

Fullycompatibleincludingnativemodulesupport!

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| Confidential– OracleInternal

TheRubyteamaimtomakethisNESemulatorbenchmark3xfasterfortheir

nextversion,3.0

It’snon-academiccode,writtenbasedonwhattheRubyteamthinksis

important tooptimise

• MRI2.3.3runsaround~20FPS• JRuby 9.1.6.0withinvokedynamic~40FPS• TruffleRubyonGraal ~180FPS

https://eregon.me/blog/2016/11/28/optcarrot.html

TruffleRuby – OptCarrot Benchmark

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

FastR

• Goal:realizetheadvantagesoftheTrufflestackforR– SuperiorperformancewithoutresortingtoC/C++/Fortran/…– Designedfordata-heavyandparallelapplications– CRAN/Bioconductorrepositorysupport

• Notan”incrementalimprovement”onGNUR– Newexecutionenginewrittenfromscratch,basedonTruffle– Designedasadrop-inreplacementforGNUR

• SpeedupoverlatestGNURinterpreter– Somewherebetween2and10x

Confidential– OracleInternal

https://github.com/graalvm/fastr

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

ProjectSulong:LLVMfront-endforGraal

int add(x, y) {return x + y;

} define i32 @add(i32 %x, i32 %y) #0 {%1 = alloca i32, align 4%2 = alloca i32, align 4store i32 %x, i32* %1, align 4store i32 %y, i32* %2, align 4%3 = load i32* %1, align 4%4 = load i32* %2, align 4%5 = add nsw i32 %3, %4ret i32 %5

}

FUNCTION add(x, y)INTEGER :: addINTEGER :: aINTEGER :: badd = a + bRETURN

END FUNCTION

func add(x int, y int) int {return x + y;

}

https://github.com/graalvm/sulong

C/C++

Fortran

Go

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

Performance:Graal VM

20

1.02 1.2

4.14.5

0.85 0.9

0

1

2

3

4

5

Java Scala Ruby R Native JavaScript

Speedup,higherisbetter

Performancerelativeto:HotSpot/Server,HotSpot/ServerrunningJRuby,GNUR,LLVMAOTcompiled,V8

Graal

BestSpecializedCompetition

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 21

C• Interoperability

– Javaó languages– Betweenlanguages

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 22

Inlining AcrossLanguageBoundaries

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 23

CompilationAcrossLanguageBoundariesMachine code for loop:

function main() {eval("application/x-ruby",

"def add(a, b) a + b; end;");eval("application/x-ruby",

"Truffle::Interop.export_method(:add);");...

}

function loop(n) {add = import("add");

i = 0; sum = 0; while (i <= n) {

sum = add(sum, i); i = add(i, 1);

} return sum;

}

Mixed JavaScript and Ruby source code:mov r14, 0mov r13, 0jmp L2

L1: safepointmov rax, r13add rax, r14jo L3inc r13mov r14, rax

L2: cmp r13, rbpjle L1...

L3: call transferToInterpreter

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

SubstrateVM:ExecutionModel

Ahead-of-Time Compilation

Points-To Analysis

SubstrateVM

TruffleLanguage

JDK

Reachablemethods,fields, andclasses

MachineCode

InitialHeap

AllJavaclassesfromTrufflelanguage

(oranyapplication),JDK,andSubstrateVM

Application runningwithoutdependency onJDKandwithoutJavaclassloading

DWARFInfo

ELF/MachO Binary

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

OpenSource• https://github.com/graalvm/graal-core

– Graal compiler• https://github.com/graalvm/truffle

– Trufflelanguageimplementationframework• https://github.com/graalvm/fastr

– FastRruntime• https://github.com/graalvm/sulong

– DynamicruntimeforLLVMbitcode• https://github.com/jruby/jruby/wiki/Truffle

– FastRubyruntime

25

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.|

Graal OTNDownload• oracle.com/technetwork/oracle-labs/program-languages• BasedonJava8u92

• IncludesaGraal VMtechnologypreviewrunning

– Javabytecode basedlanguages

– JavaScriptandnode.js

– Ruby

– R

26

Wearelookingforexampleworkloads!

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 27

Graal

Truffle

RRuby

Java Scala

JavaScript

CLLVM

@thomaswue

Questions?

Copyright©2016, Oracleand/oritsaffiliates.Allrightsreserved.| 28