26
<Insert Picture Here> Graal: A quest for the JVM to leverage its own J Doug Simon - Oracle Labs JVM Language Summit, 30th July 2012

Graal: A quest for the JVM to leverage its own J Doug ...wiki.jvmlangsummit.com/images/c/c4/Simon_Graal_Presentation.pdf · Graal: A quest for the JVM

  • Upload
    tranthu

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

<Insert Picture Here>

Graal: A quest for the JVM to leverage its own JDoug Simon - Oracle Labs

JVM Language Summit, 30th July 2012

Copyright © 2012, Oracle. All rights reserved.

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

2

Copyright © 2012, Oracle. All rights reserved. 3

Java, JUnit Eclipse, NetBeans, IntelliJ

C, C++, asmgdb, dbx, VisualStudiovalgrind, vtune

JVM

Copyright © 2012, Oracle. All rights reserved. 4

JVM

Java Eclipse

JUnit

Copyright © 2012, Oracle. All rights reserved. 4

JVMJava EclipseJUnit

Copyright © 2012, Oracle. All rights reserved. 5

graal.api.meta

graal.api.code graal.api.interpreter

graal.api

JVM

Copyright © 2012, Oracle. All rights reserved. 6

GraalCompiler

graal.api.*

JVM

Bytecode Interpreter

ASTInterpreter

ProfileAnalysis?

Copyright © 2012, Oracle. All rights reserved.

Performance: SpecJVM2008

7

0

50

100

150

200

9/11 2/12 4/12

168168168

126117100 104104104

ClientGraalServer

Copyright © 2012, Oracle. All rights reserved.

HotSpot

8

CMS

G1

JVMTI

QA

product

JDWPtemplate interpreter

open source

c1c2 jvmstat

jps

jstack

Windows

MacOSLinux

ARMia32

SPARC

32-bit64-bit

native threadsOpenJDK

OpenJDK

deopt

profiling

AMD64

TCK

Oracle

Copyright © 2012, Oracle. All rights reserved. 9

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

2. Test drive$ hg clone http://hg.openjdk.java.net/graal/graal$ cd graal$ echo JAVA_HOME=/path/to/jdk7 > mx/env$ mx build$ mx -v unittest

3. Browse$ mx ideinit

4. Participate on [email protected]

Copyright © 2012, Oracle. All rights reserved. 10

import java.lang.reflect.*;import com.oracle.graal.api.*;import com.oracle.graal.api.meta.*;

class ProfileDemo { public static void main(String[] args) throws Exception { MetaAccessProvider meta = Graal.getRuntime(). getCapability(MetaAccessProvider.class); Method m = String.class.getMethod("valueOf", Object.class); ResolvedJavaMethod rjm = meta.getResolvedJavaMethod(m); ProfilingInfo info = rjm.profilingInfo(); System.out.println(MetaUtil.profileToString(info, rjm, "\n")); }}

$ mx vm -XX:-BootstrapGraal ProfileDemocanBeStaticallyBound: trueinvocationCount: 923executionCount@1: 2600branchProbability@1: 1.000executionCount@6: 0branchProbability@6: 0.000executionCount@10: 2609types@10: 0.992 (HotSpotType<java/lang/String, resolved>) 0.005 (HotSpotType<com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod, resolved>) 0.003 (HotSpotType<com/oracle/graal/nodes/LocalNode, resolved>) 0.000 <not recorded>

ProfileDemo.java

Copyright © 2012, Oracle. All rights reserved. 11

import java.lang.reflect.*;import org.junit.*;import com.oracle.graal.api.code.*;import com.oracle.graal.api.meta.*;

class CompilerTest { void test(Method m, ResolvedJavaMethod rjm, InstalledCode code, Object obj, Object... args) { Object expect = m.invoke(obj, args); Object actual = code.execute(obj, args); Assert.assertEquals(expect, actual); }}

Copyright © 2012, Oracle. All rights reserved. 12

Create an extensible,

dynamic compiler using

object-oriented Java programming,

a graph intermediate representation,

and Java snippets.

Graal Compiler vision

Copyright © 2012, Oracle. All rights reserved. 13

$ mx projectgraph | dot -Tpdf -oprojects.pdf

Copyright © 2012, Oracle. All rights reserved. 14

class DirectObjectStoreNode extends FixedWithNextNode implements Lowerable { @Input private ValueNode object; @Input private ValueNode value; @Input private ValueNode offset; private final int displacement; ...

@Override public void lower(LoweringTool tool) { // tool.getRuntime().lower(this, tool); StructuredGraph graph = (StructuredGraph) this.graph(); IndexedLocationNode location = create(ANY_LOCATION, value.kind(), displacement, offset, graph, false); WriteNode write = new WriteNode(object, value, location); graph.replaceFixedWithFixed(this, graph.add(write)); }...

Copyright © 2012, Oracle. All rights reserved. 15

$ mx igv

Copyright © 2012, Oracle. All rights reserved. 16

DirectObjectStoreNode(ValueNode object, int displacement, ValueNode offset, ValueNode value) { ... }

@NodeIntrinsic static void storeObject(Object object, @ConstantNodeParameter int displacement, long offset, Object value) { throw new UnsupportedOperationException(); }}

... int offset = secondarySuperCacheOffset(); DirectObjectStoreNode.storeObject(s, offset, 0, t); ...

@Fold static int secondarySuperCacheOffset() { ... }

Copyright © 2012, Oracle. All rights reserved. 17

void initialize(Graph newGraph) { this.graph = newGraph; newGraph.register(this); usages = new NodeUsagesList(); for (Node input : inputs()) { updateUsages(null, input); } for (Node successor : successors()) { updatePredecessor(null, successor); } } ...

Node.java

...

<T extends Node & IterableNodeType> NodeIterable<T> getNodes(final Class<T> type) { ... }

Graph.java

Copyright © 2012, Oracle. All rights reserved. 18

protected void editPhasePlan( ResolvedJavaMethod method, StructuredGraph graph, PhasePlan phasePlan) {} ...

GraalCompilerTest.java

@Override protected void editPhasePlan( ResolvedJavaMethod method, StructuredGraph graph, PhasePlan phasePlan) { phasePlan.disablePhase(InliningPhase.class); }

@Test static String invokespecial(InvokeTest a, String s) { return a.privateMethod(s); }

InvokeTest.java

Copyright © 2012, Oracle. All rights reserved. 19

protected InstalledCode getCode( ResolvedJavaMethod method, StructuredGraph graph, boolean forceCompile) { ... }

GraalCompilerTest.java

JavaTypeProfile profile; // set per test

@Override protected InstalledCode getCode(ResolvedJavaMethod method, StructuredGraph graph) { CheckCastNode ccn = graph.getNodes(CheckCastNode.class).first(); if (ccn != null) { ResolvedJavaType tt = ccn.targetType(); ValueNode tti = ccn.targetTypeInstruction(); CheckCastNode subst = graph.add( new CheckCastNode(tti, tt, ccn.object(), profile)); graph.replaceFixedWithFixed(ccn, subst); } return super.getCode(method, graph);}

CheckCastTest.java

Copyright © 2012, Oracle. All rights reserved.

Snippets: Lowering with Java

20

@Snippetstatic Object checkcastExact( @Parameter("object") Object object, @Parameter("exactHub") Object exactHub, @ConstantParameter("checkNull") boolean checkNull) { if (checkNull && object == null) { return object; } Object objectHub = loadObject(object, 0, hubOffset(), true); if (objectHub != exactHub) { deopt(InvalidateReprofile, ClassCastException); } return object;}

@NodeIntrinsic static Object loadObject(...) {...}@NodeIntrinsic static void deopt(...) {...}@Fold static int hubOffset() {...}

Copyright © 2012, Oracle. All rights reserved. 21

checkcastExact checkcastExact{checkNull==true}

@Parameter("object")

@Parameter("exactHub")

@ConstantParameter("checkNull")

Copyright © 2012, Oracle. All rights reserved. 22

4/12

168

126104

GraalCompiler

graal.api.*

Bytecode Interpreter

ASTInterpreter

+

clie

nt

serv

ergr

aal

?

Copyright © 2012, Oracle. All rights reserved.

Acknowledgements

Oracle LabsDoug SimonChristian WimmerThomas Wuerthinger

23

JKU LinzGilles DuboscqChristian HaeublLukas Stadler

Laurent Daynes, Michael Haupt, Mick Jordan, Rémi Forax, Peter Kessler, Marcus Lagergren, Martin Odersky & the Scala group @ EPFL, Frederik Ohrstrom, John Rose, Christian Thalinger, Michael Van De Vanter, Mikael Vidstedt, Mario Wolczko

Discussions:

Copyright © 2012, Oracle. All rights reserved. 24

$ hg clone http://hg.openjdk.java.net/graal/graal$ cd graal$ echo JAVA_HOME=/path/to/jdk7 > mx/env$ mx build$ mx -v unittest

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

Q & A

Copyright © 2012, Oracle. All rights reserved.