Mercurial > hg > graal-compiler
changeset 22875:fcd9e8f41e2b
Avoid using Graal singleton in Truffle initialization.
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java Thu Oct 22 16:00:44 2015 +0200 @@ -46,6 +46,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.function.Supplier; import java.util.stream.Collectors; import jdk.vm.ci.code.BailoutException; @@ -66,7 +67,7 @@ import jdk.vm.ci.runtime.JVMCI; import jdk.vm.ci.service.Services; -import com.oracle.graal.api.runtime.Graal; +import com.oracle.graal.api.runtime.GraalRuntime; import com.oracle.graal.compiler.CompilerThreadFactory; import com.oracle.graal.compiler.target.Backend; import com.oracle.graal.debug.Debug; @@ -108,7 +109,6 @@ import com.oracle.nfi.api.NativeFunctionInterface; import com.oracle.truffle.api.CompilerAsserts; import com.oracle.truffle.api.RootCallTarget; -import com.oracle.truffle.api.TruffleRuntime; import com.oracle.truffle.api.nodes.RootNode; /** @@ -118,10 +118,6 @@ private final Map<RootCallTarget, Void> callTargets = Collections.synchronizedMap(new WeakHashMap<RootCallTarget, Void>()); - public static TruffleRuntime makeInstance() { - return new HotSpotTruffleRuntime(); - } - static class Lazy { private Map<OptimizedCallTarget, Future<?>> compilations = Collections.synchronizedMap(new IdentityHashMap<>()); private final ExecutorService compileQueue; @@ -157,7 +153,8 @@ } } - private HotSpotTruffleRuntime() { + public HotSpotTruffleRuntime(Supplier<GraalRuntime> graalRuntime) { + super(graalRuntime); setDontInlineCallBoundaryMethod(); } @@ -191,7 +188,7 @@ @Override public TruffleCompiler getTruffleCompiler() { if (truffleCompiler == null) { - truffleCompiler = DefaultTruffleCompiler.create(); + truffleCompiler = DefaultTruffleCompiler.create(this); } return truffleCompiler; } @@ -231,7 +228,7 @@ } @SuppressWarnings("try") - public static void installOptimizedCallTargetCallMethod() { + public void installOptimizedCallTargetCallMethod() { Providers providers = getHotSpotProviders(); MetaAccessProvider metaAccess = providers.getMetaAccess(); ResolvedJavaType type = metaAccess.lookupJavaType(OptimizedCallTarget.class); @@ -248,7 +245,7 @@ } } - private static CompilationResultBuilderFactory getOptimizedCallTargetInstrumentationFactory(String arch) { + private CompilationResultBuilderFactory getOptimizedCallTargetInstrumentationFactory(String arch) { for (OptimizedCallTargetInstrumentationFactory factory : Services.load(OptimizedCallTargetInstrumentationFactory.class)) { if (factory.getArchitecture().equals(arch)) { factory.init(config(), getHotSpotProviders().getRegisters()); @@ -259,7 +256,7 @@ return CompilationResultBuilderFactory.Default; } - private static CompilationResult compileMethod(ResolvedJavaMethod javaMethod) { + private CompilationResult compileMethod(ResolvedJavaMethod javaMethod) { HotSpotProviders providers = getHotSpotProviders(); SuitesProvider suitesProvider = providers.getSuites(); Suites suites = suitesProvider.createSuites(); @@ -281,12 +278,12 @@ return compileGraph(graph, cc, javaMethod, providers, backend, graphBuilderSuite, OptimisticOptimizations.ALL, getProfilingInfo(graph), suites, lirSuites, new CompilationResult(), factory); } - private static HotSpotBackend getHotSpotBackend() { - RuntimeProvider runtimeProvider = Graal.getRequiredCapability(RuntimeProvider.class); + private HotSpotBackend getHotSpotBackend() { + RuntimeProvider runtimeProvider = getRequiredGraalCapability(RuntimeProvider.class); return (HotSpotBackend) runtimeProvider.getHostBackend(); } - private static HotSpotProviders getHotSpotProviders() { + private HotSpotProviders getHotSpotProviders() { return getHotSpotBackend().getProviders(); } @@ -438,7 +435,7 @@ return null; } - public static NativeFunctionInterface createNativeFunctionInterface() { + public NativeFunctionInterface createNativeFunctionInterface() { HotSpotVMConfig config = config(); Backend backend = getHotSpotBackend(); RawNativeCallNodeFactory factory = getRawNativeCallNodeFactory(backend.getTarget().arch.getName());
--- a/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntimeAccess.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntimeAccess.java Thu Oct 22 16:00:44 2015 +0200 @@ -22,14 +22,101 @@ */ package com.oracle.graal.truffle.hotspot; +import java.util.function.Supplier; + +import jdk.vm.ci.common.JVMCIError; +import jdk.vm.ci.options.Option; +import jdk.vm.ci.options.OptionValue; +import jdk.vm.ci.runtime.JVMCI; +import jdk.vm.ci.runtime.JVMCICompiler; +import jdk.vm.ci.runtime.JVMCICompilerFactory; import jdk.vm.ci.service.ServiceProvider; +import jdk.vm.ci.service.Services; +import com.oracle.graal.api.runtime.GraalJVMCICompiler; +import com.oracle.graal.api.runtime.GraalRuntime; +import com.oracle.graal.hotspot.HotSpotGraalCompilerFactory; import com.oracle.truffle.api.TruffleRuntime; import com.oracle.truffle.api.TruffleRuntimeAccess; @ServiceProvider(TruffleRuntimeAccess.class) public class HotSpotTruffleRuntimeAccess implements TruffleRuntimeAccess { + + static class Options { + // @formatter:off + @Option(help = "Select a graal compiler for Truffle compilation (default: use JVMCI system compiler).") + public static final OptionValue<String> TruffleCompiler = new OptionValue<>(null); + // @formatter:on + } + public TruffleRuntime getRuntime() { - return HotSpotTruffleRuntime.makeInstance(); + // initialize JVMCI to make sure the TruffleCompiler option is parsed + JVMCI.initialize(); + + Supplier<GraalRuntime> lazyRuntime; + if (Options.TruffleCompiler.hasDefaultValue()) { + lazyRuntime = new LazySystemGraalRuntime(); + } else { + HotSpotGraalCompilerFactory factory = findCompilerFactory(Options.TruffleCompiler.getValue()); + lazyRuntime = new LazyCustomGraalRuntime(factory); + } + + return new HotSpotTruffleRuntime(lazyRuntime); + } + + private static HotSpotGraalCompilerFactory findCompilerFactory(String name) { + for (JVMCICompilerFactory factory : Services.load(JVMCICompilerFactory.class)) { + if (factory instanceof HotSpotGraalCompilerFactory) { + if (name.equals(factory.getCompilerName())) { + return (HotSpotGraalCompilerFactory) factory; + } + } + } + throw new JVMCIError("Graal compiler configuration '%s' not found.", name); + } + + private abstract static class LazyGraalRuntime implements Supplier<GraalRuntime> { + + private volatile GraalRuntime graalRuntime; + + public GraalRuntime get() { + if (graalRuntime == null) { + synchronized (this) { + if (graalRuntime == null) { + graalRuntime = getCompiler().getGraalRuntime(); + } + } + } + return graalRuntime; + } + + protected abstract GraalJVMCICompiler getCompiler(); + } + + private static final class LazyCustomGraalRuntime extends LazyGraalRuntime { + + private final HotSpotGraalCompilerFactory factory; + + private LazyCustomGraalRuntime(HotSpotGraalCompilerFactory factory) { + this.factory = factory; + } + + @Override + protected GraalJVMCICompiler getCompiler() { + return factory.createCompiler(JVMCI.getRuntime()); + } + } + + private static final class LazySystemGraalRuntime extends LazyGraalRuntime { + + @Override + protected GraalJVMCICompiler getCompiler() { + JVMCICompiler compiler = JVMCI.getRuntime().getCompiler(); + if (compiler instanceof GraalJVMCICompiler) { + return (GraalJVMCICompiler) compiler; + } else { + throw new JVMCIError("JVMCI system compiler '%s' is not a Graal compiler.", compiler.getClass().getName()); + } + } } }
--- a/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/nfi/HotSpotNativeFunctionInterfaceAccess.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/nfi/HotSpotNativeFunctionInterfaceAccess.java Thu Oct 22 16:00:44 2015 +0200 @@ -27,12 +27,18 @@ import com.oracle.graal.truffle.hotspot.HotSpotTruffleRuntime; import com.oracle.nfi.api.NativeFunctionInterface; import com.oracle.nfi.api.NativeFunctionInterfaceAccess; +import com.oracle.truffle.api.Truffle; @ServiceProvider(NativeFunctionInterfaceAccess.class) public class HotSpotNativeFunctionInterfaceAccess implements NativeFunctionInterfaceAccess { - private final NativeFunctionInterface instance = HotSpotTruffleRuntime.createNativeFunctionInterface(); + private final NativeFunctionInterface instance = createNativeFunctionInterface(); public NativeFunctionInterface getNativeFunctionInterface() { return instance; } + + private static NativeFunctionInterface createNativeFunctionInterface() { + HotSpotTruffleRuntime runtime = (HotSpotTruffleRuntime) Truffle.getRuntime(); + return runtime.createNativeFunctionInterface(); + } }
--- a/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/PartialEvaluationTest.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/PartialEvaluationTest.java Thu Oct 22 16:00:44 2015 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -37,6 +37,7 @@ import com.oracle.graal.phases.common.DeadCodeEliminationPhase; import com.oracle.graal.phases.tiers.PhaseContext; import com.oracle.graal.truffle.DefaultTruffleCompiler; +import com.oracle.graal.truffle.GraalTruffleRuntime; import com.oracle.graal.truffle.OptimizedCallTarget; import com.oracle.graal.truffle.TruffleCompiler; import com.oracle.graal.truffle.TruffleDebugJavaMethod; @@ -47,9 +48,8 @@ private final TruffleCompiler truffleCompiler; public PartialEvaluationTest() { - // Make sure Truffle runtime is initialized. - Assert.assertTrue(Truffle.getRuntime() != null); - this.truffleCompiler = DefaultTruffleCompiler.create(); + GraalTruffleRuntime runtime = (GraalTruffleRuntime) Truffle.getRuntime(); + this.truffleCompiler = DefaultTruffleCompiler.create(runtime); DebugEnvironment.initialize(System.out); }
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/DefaultTruffleCompiler.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/DefaultTruffleCompiler.java Thu Oct 22 16:00:44 2015 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,7 +25,6 @@ import java.util.ListIterator; import com.oracle.graal.api.replacements.SnippetReflectionProvider; -import com.oracle.graal.api.runtime.Graal; import com.oracle.graal.compiler.target.Backend; import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins; import com.oracle.graal.java.GraphBuilderPhase; @@ -38,22 +37,26 @@ public final class DefaultTruffleCompiler extends TruffleCompiler { - public static TruffleCompiler create() { - Backend backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend(); + private final SnippetReflectionProvider snippetReflection; + + public static TruffleCompiler create(GraalTruffleRuntime runtime) { + Backend backend = runtime.getRequiredGraalCapability(RuntimeProvider.class).getHostBackend(); Suites suites = backend.getSuites().getDefaultSuites(); LIRSuites lirSuites = backend.getSuites().getDefaultLIRSuites(); GraphBuilderPhase phase = (GraphBuilderPhase) backend.getSuites().getDefaultGraphBuilderSuite().findPhase(GraphBuilderPhase.class).previous(); Plugins plugins = phase.getGraphBuilderConfig().getPlugins(); - return new DefaultTruffleCompiler(plugins, suites, lirSuites, backend); + SnippetReflectionProvider snippetReflection = runtime.getRequiredGraalCapability(SnippetReflectionProvider.class); + return new DefaultTruffleCompiler(plugins, suites, lirSuites, backend, snippetReflection); } - private DefaultTruffleCompiler(Plugins plugins, Suites suites, LIRSuites lirSuites, Backend backend) { + private DefaultTruffleCompiler(Plugins plugins, Suites suites, LIRSuites lirSuites, Backend backend, SnippetReflectionProvider snippetReflection) { super(plugins, suites, lirSuites, backend); + this.snippetReflection = snippetReflection; } @Override protected PartialEvaluator createPartialEvaluator() { - return new PartialEvaluator(providers, config, Graal.getRequiredCapability(SnippetReflectionProvider.class), backend.getTarget().arch); + return new PartialEvaluator(providers, config, snippetReflection, backend.getTarget().arch); } @Override
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java Thu Oct 22 16:00:44 2015 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,15 +30,18 @@ import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; +import java.util.function.Supplier; import jdk.vm.ci.code.CompilationResult; import jdk.vm.ci.code.stack.InspectedFrame; import jdk.vm.ci.code.stack.InspectedFrameVisitor; import jdk.vm.ci.code.stack.StackIntrospection; +import jdk.vm.ci.common.JVMCIError; import jdk.vm.ci.meta.MetaAccessProvider; import jdk.vm.ci.meta.ResolvedJavaMethod; import jdk.vm.ci.service.Services; +import com.oracle.graal.api.runtime.GraalRuntime; import com.oracle.graal.debug.Debug; import com.oracle.graal.debug.Debug.Scope; import com.oracle.graal.debug.TTY; @@ -81,11 +84,22 @@ protected LoopNodeFactory loopNodeFactory; protected CallMethods callMethods; - public GraalTruffleRuntime() { + private final Supplier<GraalRuntime> graalRuntime; + + public GraalTruffleRuntime(Supplier<GraalRuntime> graalRuntime) { + this.graalRuntime = graalRuntime; } public abstract TruffleCompiler getTruffleCompiler(); + public <T> T getRequiredGraalCapability(Class<T> clazz) { + T ret = graalRuntime.get().getCapability(clazz); + if (ret == null) { + throw new JVMCIError("The VM does not expose the required Graal capability %s.", clazz.getName()); + } + return ret; + } + private static <T extends PrioritizedServiceProvider> T loadPrioritizedServiceProvider(Class<T> clazz) { Iterable<T> providers = Services.load(clazz); T bestFactory = null;
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/frame/NewFrameNode.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/frame/NewFrameNode.java Thu Oct 22 16:00:44 2015 +0200 @@ -32,7 +32,6 @@ import jdk.vm.ci.meta.ResolvedJavaType; import com.oracle.graal.api.replacements.SnippetReflectionProvider; -import com.oracle.graal.api.runtime.Graal; import com.oracle.graal.compiler.common.type.Stamp; import com.oracle.graal.compiler.common.type.StampFactory; import com.oracle.graal.graph.IterableNodeType; @@ -62,7 +61,6 @@ import com.oracle.graal.truffle.nodes.AssumptionValidAssumption; import com.oracle.truffle.api.frame.FrameDescriptor; import com.oracle.truffle.api.frame.FrameSlotKind; -import com.oracle.truffle.api.frame.VirtualFrame; /** * Intrinsic node representing the call for creating a frame in the {@link OptimizedCallTarget} @@ -75,14 +73,22 @@ @Input ValueNode descriptor; @Input ValueNode arguments; - public NewFrameNode(Stamp stamp, ValueNode descriptor, ValueNode arguments) { + private final SnippetReflectionProvider snippetReflection; + + public NewFrameNode(SnippetReflectionProvider snippetReflection, Stamp stamp, ValueNode descriptor, ValueNode arguments) { super(TYPE, stamp); this.descriptor = descriptor; this.arguments = arguments; + + /* + * This class requires access to the objects encapsulated in Constants, and therefore breaks + * the compiler-VM separation of object constants. + */ + this.snippetReflection = snippetReflection; } - public NewFrameNode(ResolvedJavaType frameType, ValueNode descriptor, ValueNode arguments) { - this(StampFactory.exactNonNull(frameType), descriptor, arguments); + public NewFrameNode(SnippetReflectionProvider snippetReflection, ResolvedJavaType frameType, ValueNode descriptor, ValueNode arguments) { + this(snippetReflection, StampFactory.exactNonNull(frameType), descriptor, arguments); } public ValueNode getDescriptor() { @@ -93,17 +99,9 @@ return arguments; } - private static SnippetReflectionProvider getSnippetReflection() { - /* - * This class requires access to the objects encapsulated in Constants, and therefore breaks - * the compiler-VM separation of object constants. - */ - return Graal.getRequiredCapability(SnippetReflectionProvider.class); - } - private FrameDescriptor getConstantFrameDescriptor() { assert descriptor.isConstant() && !descriptor.isNullConstant(); - return getSnippetReflection().asObject(FrameDescriptor.class, descriptor.asJavaConstant()); + return snippetReflection.asObject(FrameDescriptor.class, descriptor.asJavaConstant()); } private int getFrameSize() { @@ -190,7 +188,7 @@ if (frameSize > 0) { FrameDescriptor frameDescriptor = getConstantFrameDescriptor(); - ConstantNode objectDefault = ConstantNode.forConstant(getSnippetReflection().forObject(frameDescriptor.getDefaultValue()), tool.getMetaAccessProvider(), graph()); + ConstantNode objectDefault = ConstantNode.forConstant(snippetReflection.forObject(frameDescriptor.getDefaultValue()), tool.getMetaAccessProvider(), graph()); ConstantNode tagDefault = ConstantNode.forByte((byte) 0, graph()); Arrays.fill(objectArrayEntryState, objectDefault); if (virtualFrameTagArray != null) { @@ -265,7 +263,4 @@ return this; } } - - @NodeIntrinsic - public static native VirtualFrame allocate(@ConstantNodeParameter Class<? extends VirtualFrame> frameType, FrameDescriptor descriptor, Object[] args); }
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/TruffleGraphBuilderPlugins.java Wed Oct 21 16:20:23 2015 +0200 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/TruffleGraphBuilderPlugins.java Thu Oct 22 16:00:44 2015 +0200 @@ -98,7 +98,7 @@ registerExactMathPlugins(plugins); registerCompilerDirectivesPlugins(plugins, canDelayIntrinsification); registerCompilerAssertsPlugins(plugins, canDelayIntrinsification); - registerOptimizedCallTargetPlugins(metaAccess, plugins, canDelayIntrinsification); + registerOptimizedCallTargetPlugins(metaAccess, plugins, snippetReflection, canDelayIntrinsification); if (TruffleCompilerOptions.TruffleUseFrameWithoutBoxing.getValue()) { registerFrameWithoutBoxingPlugins(plugins, canDelayIntrinsification); @@ -336,12 +336,12 @@ }); } - public static void registerOptimizedCallTargetPlugins(MetaAccessProvider metaAccess, InvocationPlugins plugins, boolean canDelayIntrinsification) { + public static void registerOptimizedCallTargetPlugins(MetaAccessProvider metaAccess, InvocationPlugins plugins, SnippetReflectionProvider snippetReflection, boolean canDelayIntrinsification) { Registration r = new Registration(plugins, OptimizedCallTarget.class); r.register2("createFrame", FrameDescriptor.class, Object[].class, new InvocationPlugin() { public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode descriptor, ValueNode args) { Class<?> frameClass = TruffleCompilerOptions.TruffleUseFrameWithoutBoxing.getValue() ? FrameWithoutBoxing.class : FrameWithBoxing.class; - b.addPush(JavaKind.Object, new NewFrameNode(StampFactory.exactNonNull(metaAccess.lookupJavaType(frameClass)), descriptor, args)); + b.addPush(JavaKind.Object, new NewFrameNode(snippetReflection, StampFactory.exactNonNull(metaAccess.lookupJavaType(frameClass)), descriptor, args)); return true; } });
--- a/mx.graal/suite.py Wed Oct 21 16:20:23 2015 +0200 +++ b/mx.graal/suite.py Thu Oct 22 16:00:44 2015 +0200 @@ -868,7 +868,10 @@ ], "checkstyle" : "com.oracle.graal.graph", "javaCompliance" : "1.8", - "annotationProcessors" : ["jvmci:JVMCI_SERVICE_PROCESSOR"], + "annotationProcessors" : [ + "jvmci:JVMCI_OPTIONS_PROCESSOR", + "jvmci:JVMCI_SERVICE_PROCESSOR" + ], "workingSets" : "Graal,Truffle", },