Polyglot on the JVM with Graal (English)

Preview:

Citation preview

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

PolyglotontheJVMwithGraal

Akihiro Nishikawa

May 20, 2017JJUGCCC2017Spring

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

SafeHarborStatementThefollowingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.

2

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

ProgramAgenda

3

What’sGraal?What’sGraal VM?

PolyglotinGraal VM

Howtouse

Appendix

1

2

3

4

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

What’sGraal?What’sGraal VM?

4

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Graal andGraal VM

• Anew compiler for HotSpot written inJavaand with afocus onspeculativeoptimizations.– BasedHotSpot– Aggressivehigh-leveloptimizations– JVMCIand Graal included inJDK9(modified version of JDK8available viaOTN)

5

Graal

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Graal andGraal VM

• JVMpackagedwithGraal,Truffle,andotherlanguagessuchasJavaScript,R,Ruby,andsoon.

6

Graal VM

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 7

Graal andGraal VM

C++

Java

HotSpot

Compiler Interface

Client Server

HotSpot

JVMCI

Graal

HotSpotVM Graal VM

Compiler Interface

Client

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

PolyglotinGraal VM

8

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

TheOneVMConceptHighperformancepolyglotvirtualmachine

9

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

TheRubyLogoisCopyright(c)2006,YukihiroMatsumoto.ItislicensedunderthetermsoftheCreativeCommonsAttribution-ShareAlike 2.5agreementJSLogoCopyright(c)2011ChristopherWilliams<chris@iterativedesigns.com>,MITlicence

YoucandistributetheRlogounderthetermsoftheCreativeCommonsAttribution-ShareAlike 4.0Internationallicense(CC-BY-SA4.0)or(atyouroption)theGNUGeneralPublicLicenseversion2(GPL-2).

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Impl Impl Impl Impl

11

VM VM VM VM

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

VM

Impl Impl Impl Impl

12

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

• JavaScript– BetterECMAScript2016scorethanV8– AlmostsameperformanceasV8– Fullysupportfornode.js

• R– Statisticallanguage

13

• Ruby– ForkofJRuby– 5〜10xbetterperformance

• C、C++、Fortran– NativelanguagesupportviaLLVM

GraalVMPolyglot

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 14

GraalVMPolyglot

C

HotSpot

JVMCI

Graal

Truffle

LLVM(Sulong)RRubyJavaScript

Java

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Truffle

• Togetherwiththe Graal compiler,Truffleinterpretersareautomaticallyjust-in-timecompiledandprogramsrunningontopofthemcanreachperformanceofnormalJava.

• Providesthebasicfoundationforbuildingabstract-syntax-tree(AST)interpretersthatperform self-optimizations atruntime– TheincludedTruffleDSL providesaconvenientwaytoexpresssuchoptimizations.

15

Frameworkforimplementinglanguagesassimpleinterpreters

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

Fullycompatibleincludingnativemodulesupport!Graal.js Architecture

C++JavaScript Java

nodemoduleswithonlyJavaScript

nodestandardlibrary

V8API Thread pool(libeio)

Event loop(libev)

DNS(c-ares)

crypto(OpenSSL)

Graal.js JavaScriptEngine

AdapterV8APItoGraal.js (viaJNI)

Nativeextensions nodebindings(socket,http,…)

nodemoduleswithnativeextensions

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

FastR

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

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

• SpeedupoverlatestGNURinterpreter– Somewherebetween2and10x

17

https://github.com/graalvm/fastr

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

JavaScriptinGraal VM

18

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Completeness

19

ECMAScript2015(ES6)(16298tests/16417tests)

FailingtestsaretoalargepartUnicodeRegularExpressions

93.4%ECMAScript2016(ES7)Supportsexponentiationoperator,Array.prototype.includes

Failsduetonewblock-levelfunctiondeclarationandcorner-casetestsofthespreadoperator

99.3%

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| OracleConfidential– Internal

Classicresearchbenchmarks(Octane)

0

0.2

0.4

0.6

0.8

1

1.2

1.4

V8(5.4.500.6) Graal.js

20

roughlylevelwithV8

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

RubyinGraal VM

21

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Completeness– languageandcorelibrary

22

RubyLanguageJRuby:94%

96% Ruby corelibrariesJRuby: 95%

99%

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Classicresearchbenchmarks– 10-20xfaster

0

5

10

15

20

25

30

35

40

45GraalVMJRuby+invokedynamicRuby

23

Biggerscoreisbetter(basedonRuby score)

BenchmarksboundbyallocationorBigInteger performance

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

“But,it’seasytooptimizethatkindofcode!”

24

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 25

Simplelocalvariables

Vectorisation opportunities

Simplefloatingpointarithmetic

Basicloops

Nomethodcalls(exceptoperators)

Onlytypesarenumerical orboolean

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

“RealRubyismuchmorecomplex!”

26

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

Smalltalk-styleblocksinsteadofloops

Loopboundsareobjectsinsteadofsimplevalues

Instancevariables

Logichiddeninmethods

Arrays

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

ArithmetichiddenincorelibrarymethodsIntermediateobjects

Hashmappedtoanarrayofarrays,viaanotherarray,convertedbacktoahash

Innerlooppixels representedasahashof r,g,b

Nolocalvariables,onlymethodcalls

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 29

Metaprogrammingsend Dynamicallycreatedsymbol

Actuallogicmethoddynamicmethodcalls

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

ChunkyPNGkernels

0.01

0.1

1

10

100

1000 GraalVMJRuby+invokedynamicRuby

30

Manyofthesebenchmarksareoptimised awayentirelybyGraalVM

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

PSD.rb kernels

0.1

1

10

100

1000 GraalVM JRuby+invokedynamic Ruby

31

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Polyglot

32

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

RESTpublicinternet

RESTprivateintranet

33

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

RESTpublicinternet

34

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

RESTpublicinternet

35

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

HowwedopolyglotinGraal VM

36

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

RubyTruffle::Interop.eval('application/language', source)

value = Truffle::Interop.import(name)

Truffle::Interop.export(name)

37

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

JavaScriptInterop.eval('application/language', source)

value = Interop.import(name)

Interop.export(name)

// Returns 20Interop.eval('application/javascript', '14 + 6')

38

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

JavaScriptinRuby

puts Truffle::Interop.eval('application/javascript','14 + 6')

39

Thisreturns20

JavaScript Ruby

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

JavaScriptinRuby

Truffle::Interop.eval('application/javascript', "function add(a,b) {

return a + b;}Interop.export('add',

add.bind(this));")

add = Truffle::Interop.import('add')

puts add.call(14,2)

40

UsesfunctiondefinedinJavaScriptfromRuby

JavaScript

Ruby

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Ruby

JavaScript

41

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Ruby

JS

42

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Performance

43

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 44

Clamp

Warmsupandthenreportsiterationspersecond

Randominputsstopthewholethingbeingtotallyoptimizedaway

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Ruby

010000002000000300000040000005000000600000070000008000000900000010000000

GraalVM JRuby+invokedynamic Ruby

Ope

ratio

nsPerSecon

d

45

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 46

JavaScript(V8)RewritteninJavaScriptandcallitonV8

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Ruby+JavaScript(V8)

0

50000

100000

150000

200000

250000

300000

350000

Ruby(justRuby) Ruby(Ruby+JSwithV8)

Ope

ratio

nsPerSecon

d

47

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 48

IncaseofRhino

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

JRuby +JavaScript(Rhino)

0

100000

200000

300000

400000

500000

600000

JRuby+indy(justRuby) JRuby+indy(Ruby+JSwithRhino)

Ope

ratio

nsPerSecon

d

49

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 50

IncaseofNashorn

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

JRuby +JavaScript(Nashorn)

0

100000

200000

300000

400000

500000

600000

JRuby+indy(justRuby) JRuby+indy(Ruby+JSwithRhino)

JRuby+indy(Ruby+JSwithNashorn)

Ope

ratio

nsPerSecon

d

51

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 52

IncaseofGraal VM

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Ruby+JavaScript(Graal VM)

010000002000000300000040000005000000600000070000008000000900000010000000

GraalVM(justRuby) GraalVM(Ruby+JS) JRuby+invokedynamic Ruby

Ope

ratio

nsPerSecon

d

53

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

010000002000000300000040000005000000600000070000008000000900000010000000

GraalVM(justRuby)

GraalVM(Ruby+JS)

JRuby+indy(justRuby)

JRuby+indy(Ruby+JSwithRhino)

JRuby+indy(Ruby+JS

withNashorn)

Ruby(justRuby)

Ruby(Ruby+JSwith

V8)

Ope

ratio

nsPerSecon

d

54

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

1

10

100

1000

10000

100000

1000000

10000000

GraalVM(justRuby)

GraalVM(Ruby+JS)

JRuby+indy(justRuby)

JRuby+indy(Ruby+JSwithRhino)

JRuby+indy(Ruby+JS

withNashorn)

Ruby(justRuby)

Ruby(Ruby+JSwith

V8)

Ope

ratio

nsPerSecon

d

55

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

HowGraal achievesthis

56

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Hotspot

JIT

57

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Hotspot

JITGraal

Truffle

58

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

BasicsofTruffleandGraal

59

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

BasicConcepts• Commonrepresentationofprograms

• Keepitrichenoughtonothavetothrowawaymeaning

• Commonoptimizationoftherepresentation

60

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

x + y * z

+

x *

y z

load_local xload_local yload_local zcall *call +

pushq %rbpmovq %rsp, %rbpmovq %rdi, -8(%rbp)movq %rsi, -16(%rbp)movq %rdx, -24(%rbp)movq -16(%rbp), %raxmovl %eax, %edxmovq -24(%rbp), %raximull %edx, %eaxmovq -8(%rbp), %rdxaddl %edx, %eaxpopq %rbpret

61

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

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

T.Würthinger,C.Wimmer,A.Wöß,L.Stadler,G.Duboscq,C.Humer,G.Richards,D.Simon,andM.Wolczko.OneVMtorulethemall.InProceedingsofOnward!,2013.

62

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

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

T.Würthinger,C.Wimmer,A.Wöß,L.Stadler,G.Duboscq,C.Humer,G.Richards,D.Simon,andM.Wolczko.OneVMtorulethemall.InProceedingsofOnward!,2013.

63

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

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

T.Würthinger,C.Wimmer,A.Wöß,L.Stadler,G.Duboscq,C.Humer,G.Richards,D.Simon,andM.Wolczko.OneVMtorulethemall.InProceedingsofOnward!,2013.

64

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

T.Würthinger,C.Wimmer,A.Wöß,L.Stadler,G.Duboscq,C.Humer,G.Richards,D.Simon,andM.Wolczko.OneVMtorulethemall.InProceedingsofOnward!,2013.

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

65

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

T.Würthinger,C.Wimmer,A.Wöß,L.Stadler,G.Duboscq,C.Humer,G.Richards,D.Simon,andM.Wolczko.OneVMtorulethemall.InProceedingsofOnward!,2013.

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

66

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 67

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 68

Frequently executed call

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 69

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 70

Evenifanotherlanguage…

Frequently executed call

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 71

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 72

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Lookingathoweffectivethisis

73

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 74

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 75

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 76

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 77

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 78

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 79

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 80

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

HowtouseGraal VM

81

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

• Includes:– JVM(REorDK)– Java– JavaScript– Ruby– R–Moreinthefuture

• Binarytarball release• MacorLinux

82

Graal VM– everythinginonepackagetoday

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 83

Java9– runsonanunmodifiedJVM

Hotspot

Graal

Truffle

JS others…Ruby

Java

C++

JVMCI(JVMCompilerInterface)

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 84

Java9– runsonanunmodifiedJVM

Hotspot

Graal

Truffle

JS others…Ruby

viaMavenetc

Java 9

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

Appendix

85

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 86

http://www.oracle.com/technetwork/oracle-labs/program-languagesSearchfor“Graal OTN”

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.| 87

https://github.com/graalvmSearchfor“GitHub GraalVM”

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

AcknowledgementsOracleDaniloAnsaloniStefanAnzingerCosminBascaDanieleBonettaMatthiasBrantnerPetrChalupaJürgenChristLaurentDaynèsGillesDuboscqMartinEntlicherBrandonFishBastianHossbachChristianHumerMickJordanVojinJovanovicPeterKesslerDavidLeopoldsederKevinMenardJakubPodlešákAleksandarProkopecTomRodriguez

Oracle(continued)RolandSchatzChrisSeatonDougSimonŠtěpánŠindelářZbyněkŠlajchrtLukasStadlerCodrutStancuJanŠtolaJaroslavTulachMichaelVanDeVanterAdamWelcChristianWimmerChristianWirthPaulWögererMarioWolczkoAndreasWößThomasWürthinger

JKULinzProf.HanspeterMössenböckBenoitDalozeJosefEislThomasFeichtingerMatthiasGrimmerChristianHäublJosefHaiderChristianHuberStefanMarrManuelRiggerStefanRumzuckerBernhardUrban

Universityof EdinburghChristopheDubachJuanJoséFumero AlfonsoRanjeet SinghToomas Remmelg

LaBRIFloréal Morandat

Universityof California,IrvineProf.MichaelFranzGulfemSavrunYeniceriWeiZhang

Purdue UniversityProf.JanVitekTomasKaliberaPetrMajLeiZhao

T.U.DortmundProf.PeterMarwedelHelenaKotthausIngoKorb

Universityof California,DavisProf.DuncanTempleLangNicholasUlle

Universityof Lugano,SwitzerlandProf.WalterBinderSunHaiyangYudiZheng

OracleInternsBrianBellevilleMiguelGarciaShamsImamAlexeyKaryakinStephenKellAndreasKunftVolkerLantingGeroLeinemannJulianLettnerJoeNashDavidPiorkowskiGregorRichardsRobertSeilbeckRifatShariyar

AlumniErikEcksteinMichaelHauptChristosKotselidisHyunjinLeeDavidLeibsChrisThalingerTillWestmann

Copyright©2017,Oracleand/oritsaffiliates.Allrightsreserved.|

SafeHarborStatementTheprecedingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.

89