Mercurial > hg > graal-compiler
changeset 21394:706ffad4d773
Allow subclasses of TruffleCompiler
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Thu, 14 May 2015 16:19:40 -0700 |
parents | 01e38e103f95 |
children | f9883cab4580 |
files | graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/PartialEvaluationTest.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/DefaultTruffleCompiler.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompiler.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerImpl.java |
diffstat | 7 files changed, 329 insertions(+), 264 deletions(-) [+] |
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java Thu May 14 16:19:10 2015 -0700 +++ b/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java Thu May 14 16:19:40 2015 -0700 @@ -74,7 +74,6 @@ return new HotSpotTruffleRuntime(); } - private TruffleCompilerImpl truffleCompiler; private Map<OptimizedCallTarget, Future<?>> compilations = newIdentityMap(); private final ThreadPoolExecutor compileQueue; @@ -213,21 +212,12 @@ @Override public void compile(OptimizedCallTarget optimizedCallTarget, boolean mayBeAsynchronous) { if (truffleCompiler == null) { - truffleCompiler = new TruffleCompilerImpl(); + truffleCompiler = DefaultTruffleCompiler.create(); } Runnable r = new Runnable() { @Override public void run() { - boolean success = true; - try (Scope s = Debug.scope("Truffle", new TruffleDebugJavaMethod(optimizedCallTarget))) { - truffleCompiler.compileMethod(optimizedCallTarget); - } catch (Throwable e) { - optimizedCallTarget.notifyCompilationFailed(e); - success = false; - } finally { - optimizedCallTarget.notifyCompilationFinished(success); - - } + doCompile(optimizedCallTarget); } }; Future<?> future = compileQueue.submit(r);
--- a/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/PartialEvaluationTest.java Thu May 14 16:19:10 2015 -0700 +++ b/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/PartialEvaluationTest.java Thu May 14 16:19:40 2015 -0700 @@ -38,12 +38,12 @@ import com.oracle.truffle.api.nodes.*; public class PartialEvaluationTest extends GraalCompilerTest { - private final TruffleCompilerImpl truffleCompiler; + private final TruffleCompiler truffleCompiler; public PartialEvaluationTest() { // Make sure Truffle runtime is initialized. Assert.assertTrue(Truffle.getRuntime() != null); - this.truffleCompiler = new TruffleCompilerImpl(); + this.truffleCompiler = DefaultTruffleCompiler.create(); DebugEnvironment.initialize(System.out); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/DefaultTruffleCompiler.java Thu May 14 16:19:40 2015 -0700 @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2013, 2014, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.truffle; + +import java.util.*; + +import com.oracle.graal.api.replacements.*; +import com.oracle.graal.api.runtime.*; +import com.oracle.graal.compiler.target.*; +import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins; +import com.oracle.graal.java.*; +import com.oracle.graal.lir.phases.*; +import com.oracle.graal.phases.*; +import com.oracle.graal.phases.tiers.*; +import com.oracle.graal.runtime.*; + +public final class DefaultTruffleCompiler extends TruffleCompiler { + + public static TruffleCompiler create() { + Backend backend = Graal.getRequiredCapability(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); + } + + private DefaultTruffleCompiler(Plugins plugins, Suites suites, LIRSuites lirSuites, Backend backend) { + super(plugins, suites, lirSuites, backend); + } + + @Override + protected PartialEvaluator createPartialEvaluator() { + return new PartialEvaluator(providers, config, Graal.getRequiredCapability(SnippetReflectionProvider.class), backend.getTarget().arch); + } + + @Override + protected PhaseSuite<HighTierContext> createGraphBuilderSuite() { + PhaseSuite<HighTierContext> suite = backend.getSuites().getDefaultGraphBuilderSuite().copy(); + ListIterator<BasePhase<? super HighTierContext>> iterator = suite.findPhase(GraphBuilderPhase.class); + iterator.remove(); + iterator.add(new GraphBuilderPhase(config)); + return suite; + } +}
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java Thu May 14 16:19:10 2015 -0700 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java Thu May 14 16:19:40 2015 -0700 @@ -31,6 +31,8 @@ import com.oracle.graal.api.code.stack.*; import com.oracle.graal.api.meta.*; import com.oracle.graal.api.runtime.*; +import com.oracle.graal.debug.*; +import com.oracle.graal.debug.Debug.*; import com.oracle.graal.nodes.*; import com.oracle.graal.truffle.debug.*; import com.oracle.graal.truffle.unsafe.*; @@ -55,7 +57,8 @@ private final List<GraalTruffleCompilationListener> compilationListeners = new ArrayList<>(); private final GraalTruffleCompilationListener compilationNotify = new DispatchTruffleCompilationListener(); - private LoopNodeFactory loopNodeFactory; + protected TruffleCompiler truffleCompiler; + protected LoopNodeFactory loopNodeFactory; public GraalTruffleRuntime() { Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown)); @@ -101,11 +104,14 @@ if (!(repeatingNode instanceof Node)) { throw new IllegalArgumentException("Repeating node must be of type Node."); } + return getLoopNodeFactory().create(repeatingNode); + } + + protected LoopNodeFactory getLoopNodeFactory() { if (loopNodeFactory == null) { loopNodeFactory = loadPrioritizedServiceProvider(LoopNodeFactory.class); } - - return loopNodeFactory.create(repeatingNode); + return loopNodeFactory; } @Override @@ -271,6 +277,18 @@ public abstract void compile(OptimizedCallTarget optimizedCallTarget, boolean mayBeAsynchronous); + protected void doCompile(OptimizedCallTarget optimizedCallTarget) { + boolean success = true; + try (Scope s = Debug.scope("Truffle", new TruffleDebugJavaMethod(optimizedCallTarget))) { + truffleCompiler.compileMethod(optimizedCallTarget); + } catch (Throwable e) { + optimizedCallTarget.notifyCompilationFailed(e); + success = false; + } finally { + optimizedCallTarget.notifyCompilationFinished(success); + } + } + public abstract boolean cancelInstalledTask(OptimizedCallTarget optimizedCallTarget, Object source, CharSequence reason); public abstract void waitForCompilation(OptimizedCallTarget optimizedCallTarget, long timeout) throws ExecutionException, TimeoutException;
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java Thu May 14 16:19:10 2015 -0700 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PartialEvaluator.java Thu May 14 16:19:40 2015 -0700 @@ -71,8 +71,8 @@ @Option(help = "New partial evaluation on Graal graphs", type = OptionType.Expert)// public static final StableOptionValue<Boolean> GraphPE = new StableOptionValue<>(true); - private final Providers providers; - private final Architecture architecture; + protected final Providers providers; + protected final Architecture architecture; private final CanonicalizerPhase canonicalizer; private final SnippetReflectionProvider snippetReflection; private final ResolvedJavaMethod callDirectMethod; @@ -140,11 +140,6 @@ } public boolean apply(GraphBuilderContext builder, ResolvedJavaField staticField) { - if (TruffleCompilerOptions.TruffleExcludeAssertions.getValue() && staticField.getName().equals("$assertionsDisabled")) { - ConstantNode trueNode = builder.add(ConstantNode.forBoolean(true)); - builder.addPush(trueNode); - return true; - } return tryConstantFold(builder, providers.getMetaAccess(), providers.getConstantReflection(), staticField, null); } } @@ -319,19 +314,17 @@ } plugins.setInlineInvokePlugin(inlinePlugin); plugins.setLoopExplosionPlugin(new PELoopExplosionPlugin()); - new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), newConfig, TruffleCompilerImpl.Optimizations, null).apply(graph); + new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), newConfig, TruffleCompiler.Optimizations, null).apply(graph); if (PrintTruffleExpansionHistogram.getValue()) { ((HistogramInlineInvokePlugin) inlinePlugin).print(callTarget, System.out); } } - protected void doGraphPE(OptimizedCallTarget callTarget, StructuredGraph graph) { + protected PEGraphDecoder createGraphDecoder(StructuredGraph graph) { GraphBuilderConfiguration newConfig = configForRoot.copy(); InvocationPlugins parsingInvocationPlugins = newConfig.getPlugins().getInvocationPlugins(); TruffleGraphBuilderPlugins.registerInvocationPlugins(providers.getMetaAccess(), parsingInvocationPlugins, true, snippetReflection); - callTarget.setInlining(new TruffleInlining(callTarget, new DefaultInliningPolicy())); - LoopExplosionPlugin loopExplosionPlugin = new PELoopExplosionPlugin(); newConfig.setUseProfiling(false); @@ -340,12 +333,18 @@ plugins.setInlineInvokePlugin(new ParsingInlineInvokePlugin((ReplacementsImpl) providers.getReplacements(), parsingInvocationPlugins, loopExplosionPlugin, !PrintTruffleExpansionHistogram.getValue())); - CachingPEGraphDecoder decoder = new CachingPEGraphDecoder(providers, newConfig, TruffleCompilerImpl.Optimizations, AllowAssumptions.from(graph.getAssumptions() != null), architecture); + return new CachingPEGraphDecoder(providers, newConfig, TruffleCompiler.Optimizations, AllowAssumptions.from(graph.getAssumptions() != null), architecture); + } + protected void doGraphPE(OptimizedCallTarget callTarget, StructuredGraph graph) { + callTarget.setInlining(new TruffleInlining(callTarget, new DefaultInliningPolicy())); + + PEGraphDecoder decoder = createGraphDecoder(graph); + + LoopExplosionPlugin loopExplosionPlugin = new PELoopExplosionPlugin(); ParameterPlugin parameterPlugin = new InterceptReceiverPlugin(callTarget); - InvocationPlugins decodingInvocationPlugins = new InvocationPlugins(providers.getMetaAccess()); - TruffleGraphBuilderPlugins.registerInvocationPlugins(providers.getMetaAccess(), decodingInvocationPlugins, false, snippetReflection); + InvocationPlugins decodingInvocationPlugins = createDecodingInvocationPlugins(); InlineInvokePlugin decodingInlinePlugin = new PEInlineInvokePlugin(callTarget.getInlining(), (ReplacementsImpl) providers.getReplacements()); if (PrintTruffleExpansionHistogram.getValue()) { decodingInlinePlugin = new HistogramInlineInvokePlugin(graph, decodingInlinePlugin); @@ -358,6 +357,12 @@ } } + protected InvocationPlugins createDecodingInvocationPlugins() { + InvocationPlugins decodingInvocationPlugins = new InvocationPlugins(providers.getMetaAccess()); + TruffleGraphBuilderPlugins.registerInvocationPlugins(providers.getMetaAccess(), decodingInvocationPlugins, false, snippetReflection); + return decodingInvocationPlugins; + } + @SuppressWarnings("unused") private void fastPartialEvaluation(OptimizedCallTarget callTarget, StructuredGraph graph, PhaseContext baseContext, HighTierContext tierContext) { if (GraphPE.getValue()) { @@ -434,13 +439,13 @@ } public StructuredGraph createRootGraph(StructuredGraph graph) { - new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), configForRoot, TruffleCompilerImpl.Optimizations, null).apply(graph); + new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), configForRoot, TruffleCompiler.Optimizations, null).apply(graph); return graph; } public StructuredGraph createInlineGraph(String name, StructuredGraph caller) { StructuredGraph graph = new StructuredGraph(name, callInlinedMethod, AllowAssumptions.from(caller.getAssumptions() != null)); - new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), configForRoot, TruffleCompilerImpl.Optimizations, null).apply(graph); + new GraphBuilderPhase.Instance(providers.getMetaAccess(), providers.getStampProvider(), providers.getConstantReflection(), configForRoot, TruffleCompiler.Optimizations, null).apply(graph); return graph; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompiler.java Thu May 14 16:19:40 2015 -0700 @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2013, 2014, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.truffle; + +import static com.oracle.graal.api.code.CodeUtil.*; +import static com.oracle.graal.compiler.GraalCompiler.*; + +import java.util.*; + +import com.oracle.graal.api.code.*; +import com.oracle.graal.api.code.CallingConvention.Type; +import com.oracle.graal.api.meta.Assumptions.Assumption; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.compiler.target.*; +import com.oracle.graal.debug.*; +import com.oracle.graal.debug.Debug.Scope; +import com.oracle.graal.graphbuilderconf.*; +import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins; +import com.oracle.graal.lir.asm.*; +import com.oracle.graal.lir.phases.*; +import com.oracle.graal.nodes.*; +import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; +import com.oracle.graal.phases.*; +import com.oracle.graal.phases.tiers.*; +import com.oracle.graal.phases.util.*; +import com.oracle.graal.printer.*; +import com.oracle.graal.truffle.nodes.*; +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.nodes.*; + +/** + * Implementation of the Truffle compiler using Graal. + */ +public abstract class TruffleCompiler { + + protected final Providers providers; + protected final Suites suites; + protected final GraphBuilderConfiguration config; + protected final LIRSuites lirSuites; + protected final PartialEvaluator partialEvaluator; + protected final Backend backend; + protected final GraalTruffleCompilationListener compilationNotify; + + // @formatter:off + private static final Class<?>[] SKIPPED_EXCEPTION_CLASSES = new Class[]{ + UnexpectedResultException.class, + SlowPathException.class, + ArithmeticException.class, + IllegalArgumentException.class, + VirtualMachineError.class, + StringIndexOutOfBoundsException.class, + ClassCastException.class + }; + // @formatter:on + + public static final OptimisticOptimizations Optimizations = OptimisticOptimizations.ALL.remove(OptimisticOptimizations.Optimization.UseExceptionProbability, + OptimisticOptimizations.Optimization.RemoveNeverExecutedCode, OptimisticOptimizations.Optimization.UseTypeCheckedInlining, OptimisticOptimizations.Optimization.UseTypeCheckHints); + + public TruffleCompiler(Plugins plugins, Suites suites, LIRSuites lirSuites, Backend backend) { + GraalTruffleRuntime graalTruffleRuntime = ((GraalTruffleRuntime) Truffle.getRuntime()); + this.compilationNotify = graalTruffleRuntime.getCompilationNotify(); + this.backend = backend; + Providers backendProviders = backend.getProviders(); + ConstantReflectionProvider constantReflection = new TruffleConstantReflectionProvider(backendProviders.getConstantReflection(), backendProviders.getMetaAccess()); + this.providers = backendProviders.copyWith(constantReflection); + this.suites = suites; + this.lirSuites = lirSuites; + + ResolvedJavaType[] skippedExceptionTypes = getSkippedExceptionTypes(providers.getMetaAccess()); + + GraphBuilderConfiguration baseConfig = graalTruffleRuntime.enableInfopoints() ? GraphBuilderConfiguration.getInfopointDefault(new Plugins(plugins)) : GraphBuilderConfiguration.getDefault(new Plugins(plugins)); + this.config = baseConfig.withSkippedExceptionTypes(skippedExceptionTypes).withOmitAssertions(TruffleCompilerOptions.TruffleExcludeAssertions.getValue()); + + this.partialEvaluator = createPartialEvaluator(); + + if (Debug.isEnabled()) { + DebugEnvironment.initialize(System.out); + } + } + + protected abstract PartialEvaluator createPartialEvaluator(); + + public static ResolvedJavaType[] getSkippedExceptionTypes(MetaAccessProvider metaAccess) { + ResolvedJavaType[] skippedExceptionTypes = new ResolvedJavaType[SKIPPED_EXCEPTION_CLASSES.length]; + for (int i = 0; i < SKIPPED_EXCEPTION_CLASSES.length; i++) { + skippedExceptionTypes[i] = metaAccess.lookupJavaType(SKIPPED_EXCEPTION_CLASSES[i]); + } + return skippedExceptionTypes; + } + + public static final DebugTimer PartialEvaluationTime = Debug.timer("PartialEvaluationTime"); + public static final DebugTimer CompilationTime = Debug.timer("CompilationTime"); + public static final DebugTimer CodeInstallationTime = Debug.timer("CodeInstallation"); + + public static final DebugMemUseTracker PartialEvaluationMemUse = Debug.memUseTracker("TrufflePartialEvaluationMemUse"); + public static final DebugMemUseTracker CompilationMemUse = Debug.memUseTracker("TruffleCompilationMemUse"); + public static final DebugMemUseTracker CodeInstallationMemUse = Debug.memUseTracker("TruffleCodeInstallationMemUse"); + + public void compileMethod(final OptimizedCallTarget compilable) { + StructuredGraph graph = null; + + compilationNotify.notifyCompilationStarted(compilable); + + try { + PhaseSuite<HighTierContext> graphBuilderSuite = createGraphBuilderSuite(); + + try (DebugCloseable a = PartialEvaluationTime.start(); DebugCloseable c = PartialEvaluationMemUse.start()) { + graph = partialEvaluator.createGraph(compilable, AllowAssumptions.YES); + } + + if (Thread.currentThread().isInterrupted()) { + return; + } + + compilationNotify.notifyCompilationTruffleTierFinished(compilable, graph); + CompilationResult compilationResult = compileMethodHelper(graph, compilable.toString(), graphBuilderSuite, compilable.getSpeculationLog(), compilable); + compilationNotify.notifyCompilationSuccess(compilable, graph, compilationResult); + } catch (Throwable t) { + System.out.println("compilation failed!?"); + compilationNotify.notifyCompilationFailed(compilable, graph, t); + throw t; + } + } + + public CompilationResult compileMethodHelper(StructuredGraph graph, String name, PhaseSuite<HighTierContext> graphBuilderSuite, SpeculationLog speculationLog, InstalledCode predefinedInstalledCode) { + try (Scope s = Debug.scope("TruffleFinal")) { + Debug.dump(1, graph, "After TruffleTier"); + } catch (Throwable e) { + throw Debug.handle(e); + } + + CompilationResult result = null; + try (DebugCloseable a = CompilationTime.start(); Scope s = Debug.scope("TruffleGraal.GraalCompiler", graph, providers.getCodeCache()); DebugCloseable c = CompilationMemUse.start()) { + CodeCacheProvider codeCache = providers.getCodeCache(); + CallingConvention cc = getCallingConvention(codeCache, Type.JavaCallee, graph.method(), false); + CompilationResult compilationResult = new CompilationResult(name); + result = compileGraph(graph, cc, graph.method(), providers, backend, codeCache.getTarget(), graphBuilderSuite, Optimizations, getProfilingInfo(graph), speculationLog, suites, lirSuites, + compilationResult, CompilationResultBuilderFactory.Default); + } catch (Throwable e) { + throw Debug.handle(e); + } + + compilationNotify.notifyCompilationGraalTierFinished((OptimizedCallTarget) predefinedInstalledCode, graph); + + if (graph.isInlinedMethodRecordingEnabled()) { + result.setMethods(graph.method(), graph.getInlinedMethods()); + } else { + assert result.getMethods() == null; + } + + List<AssumptionValidAssumption> validAssumptions = new ArrayList<>(); + Set<Assumption> newAssumptions = new HashSet<>(); + for (Assumption assumption : graph.getAssumptions()) { + processAssumption(newAssumptions, assumption, validAssumptions); + } + + if (result.getAssumptions() != null) { + for (Assumption assumption : result.getAssumptions()) { + processAssumption(newAssumptions, assumption, validAssumptions); + } + } + + result.setAssumptions(newAssumptions.toArray(new Assumption[newAssumptions.size()])); + + InstalledCode installedCode; + try (Scope s = Debug.scope("CodeInstall", providers.getCodeCache()); DebugCloseable a = CodeInstallationTime.start(); DebugCloseable c = CodeInstallationMemUse.start()) { + installedCode = providers.getCodeCache().addMethod(graph.method(), result, speculationLog, predefinedInstalledCode); + } catch (Throwable e) { + throw Debug.handle(e); + } + + for (AssumptionValidAssumption a : validAssumptions) { + a.getAssumption().registerInstalledCode(installedCode); + } + + if (Debug.isLogEnabled()) { + Debug.log(providers.getCodeCache().disassemble(result, installedCode)); + } + return result; + } + + protected abstract PhaseSuite<HighTierContext> createGraphBuilderSuite(); + + public void processAssumption(Set<Assumption> newAssumptions, Assumption assumption, List<AssumptionValidAssumption> manual) { + if (assumption != null) { + if (assumption instanceof AssumptionValidAssumption) { + AssumptionValidAssumption assumptionValidAssumption = (AssumptionValidAssumption) assumption; + manual.add(assumptionValidAssumption); + } else { + newAssumptions.add(assumption); + } + } + } + + public PartialEvaluator getPartialEvaluator() { + return partialEvaluator; + } +}
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCompilerImpl.java Thu May 14 16:19:10 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,231 +0,0 @@ -/* - * Copyright (c) 2013, 2014, 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 - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.truffle; - -import static com.oracle.graal.api.code.CodeUtil.*; -import static com.oracle.graal.compiler.GraalCompiler.*; - -import java.util.*; - -import com.oracle.graal.api.code.*; -import com.oracle.graal.api.code.CallingConvention.Type; -import com.oracle.graal.api.meta.Assumptions.Assumption; -import com.oracle.graal.api.meta.*; -import com.oracle.graal.api.replacements.*; -import com.oracle.graal.api.runtime.*; -import com.oracle.graal.compiler.target.*; -import com.oracle.graal.debug.*; -import com.oracle.graal.debug.Debug.Scope; -import com.oracle.graal.graphbuilderconf.*; -import com.oracle.graal.graphbuilderconf.GraphBuilderConfiguration.Plugins; -import com.oracle.graal.java.*; -import com.oracle.graal.lir.asm.*; -import com.oracle.graal.lir.phases.*; -import com.oracle.graal.nodes.*; -import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; -import com.oracle.graal.phases.*; -import com.oracle.graal.phases.tiers.*; -import com.oracle.graal.phases.util.*; -import com.oracle.graal.printer.*; -import com.oracle.graal.runtime.*; -import com.oracle.graal.truffle.nodes.*; -import com.oracle.truffle.api.*; -import com.oracle.truffle.api.nodes.*; - -/** - * Implementation of the Truffle compiler using Graal. - */ -public class TruffleCompilerImpl { - - private final Providers providers; - private final Suites suites; - private final LIRSuites lirSuites; - private final PartialEvaluator partialEvaluator; - private final Backend backend; - private final GraphBuilderConfiguration config; - private final RuntimeProvider runtime; - private final GraalTruffleCompilationListener compilationNotify; - - // @formatter:off - private static final Class<?>[] SKIPPED_EXCEPTION_CLASSES = new Class[]{ - UnexpectedResultException.class, - SlowPathException.class, - ArithmeticException.class, - IllegalArgumentException.class, - VirtualMachineError.class, - StringIndexOutOfBoundsException.class, - ClassCastException.class - }; - // @formatter:on - - public static final OptimisticOptimizations Optimizations = OptimisticOptimizations.ALL.remove(OptimisticOptimizations.Optimization.UseExceptionProbability, - OptimisticOptimizations.Optimization.RemoveNeverExecutedCode, OptimisticOptimizations.Optimization.UseTypeCheckedInlining, OptimisticOptimizations.Optimization.UseTypeCheckHints); - - public TruffleCompilerImpl() { - GraalTruffleRuntime graalTruffleRuntime = ((GraalTruffleRuntime) Truffle.getRuntime()); - this.runtime = Graal.getRequiredCapability(RuntimeProvider.class); - this.compilationNotify = graalTruffleRuntime.getCompilationNotify(); - this.backend = runtime.getHostBackend(); - Providers backendProviders = backend.getProviders(); - ConstantReflectionProvider constantReflection = new TruffleConstantReflectionProvider(backendProviders.getConstantReflection(), backendProviders.getMetaAccess()); - this.providers = backendProviders.copyWith(constantReflection); - this.suites = backend.getSuites().getDefaultSuites(); - this.lirSuites = backend.getSuites().getDefaultLIRSuites(); - - ResolvedJavaType[] skippedExceptionTypes = getSkippedExceptionTypes(providers.getMetaAccess()); - - GraphBuilderPhase phase = (GraphBuilderPhase) backend.getSuites().getDefaultGraphBuilderSuite().findPhase(GraphBuilderPhase.class).previous(); - // copy all plugins from the host - Plugins plugins = new Plugins(phase.getGraphBuilderConfig().getPlugins()); - GraphBuilderConfiguration baseConfig = graalTruffleRuntime.enableInfopoints() ? GraphBuilderConfiguration.getInfopointDefault(plugins) : GraphBuilderConfiguration.getDefault(plugins); - this.config = baseConfig.withSkippedExceptionTypes(skippedExceptionTypes); - - this.partialEvaluator = new PartialEvaluator(providers, config, Graal.getRequiredCapability(SnippetReflectionProvider.class), backend.getTarget().arch); - - if (Debug.isEnabled()) { - DebugEnvironment.initialize(System.out); - } - } - - public static ResolvedJavaType[] getSkippedExceptionTypes(MetaAccessProvider metaAccess) { - ResolvedJavaType[] skippedExceptionTypes = new ResolvedJavaType[SKIPPED_EXCEPTION_CLASSES.length]; - for (int i = 0; i < SKIPPED_EXCEPTION_CLASSES.length; i++) { - skippedExceptionTypes[i] = metaAccess.lookupJavaType(SKIPPED_EXCEPTION_CLASSES[i]); - } - return skippedExceptionTypes; - } - - public static final DebugTimer PartialEvaluationTime = Debug.timer("PartialEvaluationTime"); - public static final DebugTimer CompilationTime = Debug.timer("CompilationTime"); - public static final DebugTimer CodeInstallationTime = Debug.timer("CodeInstallation"); - - public static final DebugMemUseTracker PartialEvaluationMemUse = Debug.memUseTracker("TrufflePartialEvaluationMemUse"); - public static final DebugMemUseTracker CompilationMemUse = Debug.memUseTracker("TruffleCompilationMemUse"); - public static final DebugMemUseTracker CodeInstallationMemUse = Debug.memUseTracker("TruffleCodeInstallationMemUse"); - - public void compileMethod(final OptimizedCallTarget compilable) { - StructuredGraph graph = null; - - compilationNotify.notifyCompilationStarted(compilable); - - try { - PhaseSuite<HighTierContext> graphBuilderSuite = createGraphBuilderSuite(); - - try (DebugCloseable a = PartialEvaluationTime.start(); DebugCloseable c = PartialEvaluationMemUse.start()) { - graph = partialEvaluator.createGraph(compilable, AllowAssumptions.YES); - } - - if (Thread.currentThread().isInterrupted()) { - return; - } - - compilationNotify.notifyCompilationTruffleTierFinished(compilable, graph); - CompilationResult compilationResult = compileMethodHelper(graph, compilable.toString(), graphBuilderSuite, compilable.getSpeculationLog(), compilable); - compilationNotify.notifyCompilationSuccess(compilable, graph, compilationResult); - } catch (Throwable t) { - System.out.println("compilation failed!?"); - compilationNotify.notifyCompilationFailed(compilable, graph, t); - throw t; - } - } - - public CompilationResult compileMethodHelper(StructuredGraph graph, String name, PhaseSuite<HighTierContext> graphBuilderSuite, SpeculationLog speculationLog, InstalledCode predefinedInstalledCode) { - try (Scope s = Debug.scope("TruffleFinal")) { - Debug.dump(1, graph, "After TruffleTier"); - } catch (Throwable e) { - throw Debug.handle(e); - } - - CompilationResult result = null; - try (DebugCloseable a = CompilationTime.start(); Scope s = Debug.scope("TruffleGraal.GraalCompiler", graph, providers.getCodeCache()); DebugCloseable c = CompilationMemUse.start()) { - CodeCacheProvider codeCache = providers.getCodeCache(); - CallingConvention cc = getCallingConvention(codeCache, Type.JavaCallee, graph.method(), false); - CompilationResult compilationResult = new CompilationResult(name); - result = compileGraph(graph, cc, graph.method(), providers, backend, codeCache.getTarget(), graphBuilderSuite, Optimizations, getProfilingInfo(graph), speculationLog, suites, lirSuites, - compilationResult, CompilationResultBuilderFactory.Default); - } catch (Throwable e) { - throw Debug.handle(e); - } - - compilationNotify.notifyCompilationGraalTierFinished((OptimizedCallTarget) predefinedInstalledCode, graph); - - if (graph.isInlinedMethodRecordingEnabled()) { - result.setMethods(graph.method(), graph.getInlinedMethods()); - } else { - assert result.getMethods() == null; - } - - List<AssumptionValidAssumption> validAssumptions = new ArrayList<>(); - Set<Assumption> newAssumptions = new HashSet<>(); - for (Assumption assumption : graph.getAssumptions()) { - processAssumption(newAssumptions, assumption, validAssumptions); - } - - if (result.getAssumptions() != null) { - for (Assumption assumption : result.getAssumptions()) { - processAssumption(newAssumptions, assumption, validAssumptions); - } - } - - result.setAssumptions(newAssumptions.toArray(new Assumption[newAssumptions.size()])); - - InstalledCode installedCode; - try (Scope s = Debug.scope("CodeInstall", providers.getCodeCache()); DebugCloseable a = CodeInstallationTime.start(); DebugCloseable c = CodeInstallationMemUse.start()) { - installedCode = providers.getCodeCache().addMethod(graph.method(), result, speculationLog, predefinedInstalledCode); - } catch (Throwable e) { - throw Debug.handle(e); - } - - for (AssumptionValidAssumption a : validAssumptions) { - a.getAssumption().registerInstalledCode(installedCode); - } - - if (Debug.isLogEnabled()) { - Debug.log(providers.getCodeCache().disassemble(result, installedCode)); - } - return result; - } - - private PhaseSuite<HighTierContext> createGraphBuilderSuite() { - PhaseSuite<HighTierContext> suite = backend.getSuites().getDefaultGraphBuilderSuite().copy(); - ListIterator<BasePhase<? super HighTierContext>> iterator = suite.findPhase(GraphBuilderPhase.class); - iterator.remove(); - iterator.add(new GraphBuilderPhase(config)); - return suite; - } - - public void processAssumption(Set<Assumption> newAssumptions, Assumption assumption, List<AssumptionValidAssumption> manual) { - if (assumption != null) { - if (assumption instanceof AssumptionValidAssumption) { - AssumptionValidAssumption assumptionValidAssumption = (AssumptionValidAssumption) assumption; - manual.add(assumptionValidAssumption); - } else { - newAssumptions.add(assumption); - } - } - } - - public PartialEvaluator getPartialEvaluator() { - return partialEvaluator; - } -}