Skip to content

Commit

Permalink
[GR-15757] Record speculations for TruffleBoundary methods assumed no…
Browse files Browse the repository at this point in the history
…t to throw.

PullRequest: graal/3567
  • Loading branch information
woess committed May 22, 2019
2 parents 68bb1e0 + c88b1df commit 2c94ee1
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void test() {
registerPlugins(graphBuilderConfig.getPlugins().getInvocationPlugins());
targetGraph = new StructuredGraph.Builder(getInitialOptions(), debug, AllowAssumptions.YES).method(testMethod).build();
CachingPEGraphDecoder decoder = new CachingPEGraphDecoder(getTarget().arch, targetGraph, getProviders(), graphBuilderConfig, OptimisticOptimizations.NONE, AllowAssumptions.YES,
null, null, new InlineInvokePlugin[]{new InlineAll()}, null, null, null, null);
null, null, new InlineInvokePlugin[]{new InlineAll()}, null, null, null, null, null);

decoder.decode(testMethod, false, false);
debug.dump(DebugContext.BASIC_LEVEL, targetGraph, "Target Graph");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin;
import org.graalvm.compiler.nodes.spi.CoreProviders;
import org.graalvm.compiler.phases.BasePhase;
import org.graalvm.compiler.phases.OptimisticOptimizations;
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
import org.graalvm.compiler.phases.util.Providers;
Expand All @@ -64,18 +66,21 @@ public class CachingPEGraphDecoder extends PEGraphDecoder {
protected final OptimisticOptimizations optimisticOpts;
private final AllowAssumptions allowAssumptions;
private final EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache;
private final BasePhase<? super CoreProviders> postParsingPhase;

public CachingPEGraphDecoder(Architecture architecture, StructuredGraph graph, Providers providers, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts,
AllowAssumptions allowAssumptions, LoopExplosionPlugin loopExplosionPlugin, InvocationPlugins invocationPlugins, InlineInvokePlugin[] inlineInvokePlugins,
ParameterPlugin parameterPlugin,
NodePlugin[] nodePlugins, ResolvedJavaMethod callInlinedMethod, SourceLanguagePositionProvider sourceLanguagePositionProvider) {
NodePlugin[] nodePlugins, ResolvedJavaMethod callInlinedMethod, SourceLanguagePositionProvider sourceLanguagePositionProvider,
BasePhase<? super CoreProviders> postParsingPhase) {
super(architecture, graph, providers, loopExplosionPlugin,
invocationPlugins, inlineInvokePlugins, parameterPlugin, nodePlugins, callInlinedMethod, sourceLanguagePositionProvider);

this.providers = providers;
this.graphBuilderConfig = graphBuilderConfig;
this.optimisticOpts = optimisticOpts;
this.allowAssumptions = allowAssumptions;
this.postParsingPhase = postParsingPhase;
this.graphCache = EconomicMap.create();
}

Expand Down Expand Up @@ -128,6 +133,9 @@ private StructuredGraph buildGraph(ResolvedJavaMethod method, MethodSubstitution
GraphBuilderPhase.Instance graphBuilderPhaseInstance = createGraphBuilderPhaseInstance(initialIntrinsicContext);
graphBuilderPhaseInstance.apply(graphToEncode);
new CanonicalizerPhase().apply(graphToEncode, providers);
if (postParsingPhase != null) {
postParsingPhase.apply(graphToEncode, providers);
}
} catch (Throwable ex) {
throw debug.handle(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ enum InlineKind {
* Denotes a call site must not be inlined and the execution should be transferred to
* interpreter in case of an exception.
*/
DO_NOT_INLINE_DEOPTIMIZE_ON_EXCEPTION(false);
DO_NOT_INLINE_DEOPTIMIZE_ON_EXCEPTION(false),

/**
* Denotes a call site must not be inlined and the execution should be speculatively
* transferred to interpreter in case of an exception, unless the speculation has failed.
*/
DO_NOT_INLINE_WITH_SPECULATIVE_EXCEPTION(false);

private final boolean allowsInlining;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@
import org.graalvm.compiler.replacements.PEGraphDecoder;
import org.graalvm.compiler.replacements.ReplacementsImpl;
import org.graalvm.compiler.serviceprovider.GraalServices;
import org.graalvm.compiler.serviceprovider.SpeculationReasonGroup;
import org.graalvm.compiler.truffle.common.CompilableTruffleAST;
import org.graalvm.compiler.truffle.common.TruffleCompilerRuntime;
import org.graalvm.compiler.truffle.common.TruffleCompilerRuntime.InlineKind;
import org.graalvm.compiler.truffle.common.TruffleInliningPlan;
import org.graalvm.compiler.truffle.common.TruffleSourceLanguagePosition;
import org.graalvm.compiler.truffle.compiler.debug.HistogramInlineInvokePlugin;
Expand All @@ -105,6 +107,7 @@
import org.graalvm.compiler.truffle.compiler.phases.InstrumentBranchesPhase;
import org.graalvm.compiler.truffle.compiler.phases.InstrumentPhase;
import org.graalvm.compiler.truffle.compiler.phases.InstrumentTruffleBoundariesPhase;
import org.graalvm.compiler.truffle.compiler.phases.DeoptimizeOnExceptionPhase;
import org.graalvm.compiler.truffle.compiler.phases.VerifyFrameDoesNotEscapePhase;
import org.graalvm.compiler.truffle.compiler.substitutions.KnownTruffleTypes;
import org.graalvm.compiler.truffle.compiler.substitutions.TruffleGraphBuilderPlugins;
Expand All @@ -119,6 +122,7 @@
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.SpeculationLog;
import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;

/**
* Class performing the partial evaluation starting from the root node of an AST.
Expand Down Expand Up @@ -445,10 +449,12 @@ protected PEGraphDecoder createGraphDecoder(StructuredGraph graph, final HighTie
plugins.appendInlineInvokePlugin(new InlineDuringParsingPlugin());
}

DeoptimizeOnExceptionPhase postParsingPhase = new DeoptimizeOnExceptionPhase(
method -> TruffleCompilerRuntime.getRuntime().getInlineKind(method, true) == InlineKind.DO_NOT_INLINE_WITH_SPECULATIVE_EXCEPTION);

Providers compilationUnitProviders = providers.copyWith(new TruffleConstantFieldProvider(providers.getConstantFieldProvider(), providers.getMetaAccess()));
return new CachingPEGraphDecoder(architecture, graph, compilationUnitProviders, newConfig, TruffleCompilerImpl.Optimizations,
AllowAssumptions.ifNonNull(graph.getAssumptions()),
loopExplosionPlugin, decodingInvocationPlugins, inlineInvokePlugins, parameterPlugin, nodePluginList, callInlined, sourceLanguagePositionProvider);
return new CachingPEGraphDecoder(architecture, graph, compilationUnitProviders, newConfig, TruffleCompilerImpl.Optimizations, AllowAssumptions.ifNonNull(graph.getAssumptions()),
loopExplosionPlugin, decodingInvocationPlugins, inlineInvokePlugins, parameterPlugin, nodePluginList, callInlined, sourceLanguagePositionProvider, postParsingPhase);
}

protected void doGraphPE(CompilableTruffleAST compilable, StructuredGraph graph, HighTierContext tierContext, TruffleInliningPlan inliningDecision) {
Expand Down Expand Up @@ -741,6 +747,7 @@ private static InlineInfo asInlineInfo(final TruffleCompilerRuntime.InlineKind i
case DO_NOT_INLINE_NO_EXCEPTION:
return InlineInfo.DO_NOT_INLINE_NO_EXCEPTION;
case DO_NOT_INLINE_WITH_EXCEPTION:
case DO_NOT_INLINE_WITH_SPECULATIVE_EXCEPTION:
return InlineInfo.DO_NOT_INLINE_WITH_EXCEPTION;
case INLINE:
return InlineInfo.createStandardInlineInfo(method);
Expand All @@ -749,6 +756,12 @@ private static InlineInfo asInlineInfo(final TruffleCompilerRuntime.InlineKind i
}
}

private static final SpeculationReasonGroup TRUFFLE_BOUNDARY_EXCEPTION_SPECULATIONS = new SpeculationReasonGroup("TruffleBoundaryWithoutException", ResolvedJavaMethod.class);

public static SpeculationReason createTruffleBoundaryExceptionSpeculation(ResolvedJavaMethod targetMethod) {
return TRUFFLE_BOUNDARY_EXCEPTION_SPECULATIONS.createSpeculationReason(targetMethod);
}

private static final class SourceLanguagePositionImpl implements SourceLanguagePosition {
private final TruffleSourceLanguagePosition delegate;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2019, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 org.graalvm.compiler.truffle.compiler.nodes;

import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;

import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.spi.Canonicalizable;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.DeoptimizeNode;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.truffle.compiler.PartialEvaluator;

import jdk.vm.ci.meta.DeoptimizationAction;
import jdk.vm.ci.meta.DeoptimizationReason;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.SpeculationLog;
import jdk.vm.ci.meta.SpeculationLog.Speculation;
import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;

/**
* A speculation-less node that is inserted into the exception branch of TruffleBoundary calls
* during parsing (graph encoding). During partial evaluation (graph decoding) when a speculation
* log is available, it will speculate that TruffleBoundary method will not throw and either becomes
* a control-flow sink {@link DeoptimizeNode} with the {@link Speculation} in order to off the
* branch, or if the speculation has already failed for this compilation root, disappears.
*/
@NodeInfo(cycles = CYCLES_0, size = SIZE_0)
public final class SpeculativeExceptionAnchorNode extends FixedWithNextNode implements Canonicalizable {

public static final NodeClass<SpeculativeExceptionAnchorNode> TYPE = NodeClass.create(SpeculativeExceptionAnchorNode.class);

private final DeoptimizationReason reason;
private final DeoptimizationAction action;
private final ResolvedJavaMethod targetMethod;

public SpeculativeExceptionAnchorNode(DeoptimizationReason reason, DeoptimizationAction action, ResolvedJavaMethod targetMethod) {
super(TYPE, StampFactory.forVoid());
this.reason = reason;
this.action = action;
this.targetMethod = targetMethod;
}

@Override
public Node canonical(CanonicalizerTool tool) {
SpeculationLog speculationLog = graph().getSpeculationLog();
if (speculationLog != null) {
SpeculationReason speculationReason = PartialEvaluator.createTruffleBoundaryExceptionSpeculation(targetMethod);
if (speculationLog.maySpeculate(speculationReason)) {
Speculation exceptionSpeculation = speculationLog.speculate(speculationReason);
return new DeoptimizeNode(action, reason, exceptionSpeculation);
}
return null;
}
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2019, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 org.graalvm.compiler.truffle.compiler.phases;

import java.util.function.Predicate;

import org.graalvm.compiler.nodes.AbstractBeginNode;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.nodes.Invoke;
import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.phases.Phase;
import org.graalvm.compiler.truffle.compiler.nodes.SpeculativeExceptionAnchorNode;

import jdk.vm.ci.meta.DeoptimizationAction;
import jdk.vm.ci.meta.DeoptimizationReason;
import jdk.vm.ci.meta.ResolvedJavaMethod;

/**
* Instruments the exception edge of TruffleBoundary method calls with a speculative transfer to
* interpreter.
*/
public class DeoptimizeOnExceptionPhase extends Phase {
private final Predicate<ResolvedJavaMethod> deoptimizeOnExceptionPredicate;

public DeoptimizeOnExceptionPhase(Predicate<ResolvedJavaMethod> deoptimizeOnExceptionPredicate) {
this.deoptimizeOnExceptionPredicate = deoptimizeOnExceptionPredicate;
}

@Override
protected void run(StructuredGraph graph) {
for (Invoke invoke : graph.getInvokes()) {
if (invoke instanceof InvokeWithExceptionNode) {
InvokeWithExceptionNode invokeWithException = (InvokeWithExceptionNode) invoke;
ResolvedJavaMethod targetMethod = invokeWithException.callTarget().targetMethod();
if (deoptimizeOnExceptionPredicate.test(targetMethod)) {
// Method has @TruffleBoundary(transferToInterpreterOnException=true)
// Note: Speculation is inserted during PE.
AbstractBeginNode exceptionEdge = invokeWithException.exceptionEdge();
FixedWithNextNode newNode = graph.add(new SpeculativeExceptionAnchorNode(DeoptimizationReason.TransferToInterpreter, DeoptimizationAction.InvalidateRecompile, targetMethod));
graph.addAfterFixed(exceptionEdge, newNode);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -998,16 +998,18 @@ public int getFrameSlotKindTagsCount() {
public InlineKind getInlineKind(ResolvedJavaMethod original, boolean duringPartialEvaluation) {
TruffleBoundary truffleBoundary = getAnnotation(TruffleBoundary.class, original);
if (truffleBoundary != null) {
if (duringPartialEvaluation || !truffleBoundary.allowInlining()) {
if (duringPartialEvaluation) {
// Since this method is invoked by the bytecode parser plugins, which can be invoked
// by the partial evaluator, we want to prevent inlining across the boundary during
// partial evaluation,
// even if the TruffleBoundary allows inlining after partial evaluation.
if (truffleBoundary.transferToInterpreterOnException()) {
return InlineKind.DO_NOT_INLINE_DEOPTIMIZE_ON_EXCEPTION;
return InlineKind.DO_NOT_INLINE_WITH_SPECULATIVE_EXCEPTION;
} else {
return InlineKind.DO_NOT_INLINE_WITH_EXCEPTION;
}
} else if (!truffleBoundary.allowInlining()) {
return InlineKind.DO_NOT_INLINE_WITH_EXCEPTION;
}
} else if (getAnnotation(TruffleCallBoundary.class, original) != null) {
return InlineKind.DO_NOT_INLINE_WITH_EXCEPTION;
Expand Down
Loading

0 comments on commit 2c94ee1

Please sign in to comment.