Skip to content

Commit 8f98ffb

Browse files
committed
Compiler changes for prepareForCompilation. (to-backport)
1 parent 9db8fab commit 8f98ffb

File tree

13 files changed

+242
-83
lines changed

13 files changed

+242
-83
lines changed

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/truffle/test/AgnosticInliningPhaseTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ public String toString(Verbosity verbosity) {
6363
return "";
6464
}
6565
};
66-
final TruffleTierContext context = new TruffleTierContext(partialEvaluator,
66+
final TruffleTierContext context = TruffleTierContext.createInitialContext(
67+
partialEvaluator,
6768
compiler.getOrCreateCompilerOptions(callTarget),
68-
getDebugContext(), callTarget, partialEvaluator.rootForCallTarget(callTarget),
69+
getDebugContext(), callTarget,
6970
compilationIdentifier, getSpeculationLog(),
7071
new TruffleCompilationTask() {
7172

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/truffle/test/PartialEvaluationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,9 @@ private StructuredGraph partialEval(OptimizedCallTarget compilable, Object[] arg
299299
final PartialEvaluator partialEvaluator = compiler.getPartialEvaluator();
300300
try (PerformanceInformationHandler handler = PerformanceInformationHandler.install(
301301
compiler.getConfig().runtime(), compiler.getOrCreateCompilerOptions(compilable))) {
302-
final TruffleTierContext context = new TruffleTierContext(partialEvaluator,
302+
final TruffleTierContext context = TruffleTierContext.createInitialContext(partialEvaluator,
303303
compiler.getOrCreateCompilerOptions(compilable),
304304
debug, compilable,
305-
partialEvaluator.rootForCallTarget(compilable),
306305
compilation.getCompilationId(), speculationLog,
307306
task,
308307
handler);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/libgraal/truffle/HSTruffleCompilable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public long engineId() {
8787
}
8888

8989
@Override
90-
public void prepareForCompilation() {
90+
public boolean prepareForCompilation(boolean rootCompilation, int compilationTier, boolean lastTier) {
9191
try {
92-
HANDLES.prepareForCompilation.invoke(hsHandle);
92+
return (boolean) HANDLES.prepareForCompilation.invoke(hsHandle, rootCompilation, compilationTier, lastTier);
9393
} catch (Throwable t) {
9494
throw handleException(t);
9595
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/TruffleCompilerImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,10 @@ private StructuredGraph truffleTier(TruffleCompilationWrapper wrapper, DebugCont
538538
* TODO GR-37097 Merge TruffleTierConfiguration and TruffleCompilationWrapper so that
539539
* there is one place where compilation data lives
540540
*/
541-
TruffleTierContext context = new TruffleTierContext(partialEvaluator,
541+
TruffleTierContext context = TruffleTierContext.createInitialContext(partialEvaluator,
542542
wrapper.compilerOptions,
543543
debug,
544544
wrapper.compilable,
545-
partialEvaluator.rootForCallTarget(wrapper.compilable),
546545
wrapper.compilationId, TruffleTierContext.getSpeculationLog(wrapper), wrapper.task,
547546
handler);
548547

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/TruffleTierContext.java

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626

2727
import java.util.Objects;
2828

29+
import com.oracle.truffle.compiler.TruffleCompilable;
30+
import com.oracle.truffle.compiler.TruffleCompilationTask;
31+
import com.oracle.truffle.compiler.TruffleCompilerRuntime;
32+
2933
import jdk.graal.compiler.core.common.CompilationIdentifier;
34+
import jdk.graal.compiler.core.common.RetryableBailoutException;
3035
import jdk.graal.compiler.debug.DebugContext;
3136
import jdk.graal.compiler.debug.GraalError;
3237
import jdk.graal.compiler.nodes.Cancellable;
@@ -37,11 +42,6 @@
3742
import jdk.graal.compiler.phases.tiers.HighTierContext;
3843
import jdk.graal.compiler.phases.util.Providers;
3944
import jdk.graal.compiler.truffle.nodes.TruffleAssumption;
40-
41-
import com.oracle.truffle.compiler.TruffleCompilable;
42-
import com.oracle.truffle.compiler.TruffleCompilationTask;
43-
import com.oracle.truffle.compiler.TruffleCompilerRuntime;
44-
4545
import jdk.vm.ci.meta.JavaConstant;
4646
import jdk.vm.ci.meta.ResolvedJavaField;
4747
import jdk.vm.ci.meta.ResolvedJavaMethod;
@@ -62,12 +62,13 @@ public final class TruffleTierContext extends HighTierContext {
6262
public final StructuredGraph graph;
6363
public final PerformanceInformationHandler handler;
6464

65-
public TruffleTierContext(PartialEvaluator partialEvaluator,
65+
private TruffleTierContext(PartialEvaluator partialEvaluator,
6666
OptionValues compilerOptions,
6767
DebugContext debug,
68-
TruffleCompilable compilable, ResolvedJavaMethod method,
68+
TruffleCompilable compilable,
6969
CompilationIdentifier compilationId, SpeculationLog log,
70-
TruffleCompilationTask task, PerformanceInformationHandler handler) {
70+
TruffleCompilationTask task, PerformanceInformationHandler handler,
71+
ResolvedJavaMethod initialMethod) {
7172
super(partialEvaluator.getProviders(), new PhaseSuite<>(), OptimisticOptimizations.NONE);
7273
Objects.requireNonNull(debug);
7374
Objects.requireNonNull(compilable);
@@ -82,12 +83,71 @@ public TruffleTierContext(PartialEvaluator partialEvaluator,
8283
this.log = log;
8384
this.task = task;
8485
this.handler = handler;
85-
this.graph = createInitialGraph(method);
86+
this.graph = createInitialGraph(initialMethod);
8687
}
8788

88-
private StructuredGraph createInitialGraph(ResolvedJavaMethod method) {
89-
compilable.prepareForCompilation();
89+
private TruffleTierContext(TruffleTierContext parent,
90+
TruffleCompilable compilable,
91+
ResolvedJavaMethod initialMethod) {
92+
this(parent.partialEvaluator,
93+
parent.compilerOptions,
94+
parent.debug,
95+
compilable,
96+
parent.compilationId,
97+
parent.log,
98+
parent.task,
99+
parent.handler,
100+
initialMethod);
101+
}
102+
103+
public static TruffleTierContext createInitialContext(PartialEvaluator partialEvaluator,
104+
OptionValues compilerOptions,
105+
DebugContext debug,
106+
TruffleCompilable compilable,
107+
CompilationIdentifier compilationId, SpeculationLog log,
108+
TruffleCompilationTask task, PerformanceInformationHandler handler) {
109+
110+
boolean readyForCompilation = compilable.prepareForCompilation(true, task.tier(), task.isLastTier());
111+
if (!readyForCompilation) {
112+
/*
113+
* If the root node not ready for compilation we throw a retryable bailout for root
114+
* compilations. This will have the effect that the method will be called again in the
115+
* interpreter to reprofile.
116+
*/
117+
throw new RetryableBailoutException("Compilable not ready for compilation.");
118+
}
119+
ResolvedJavaMethod method = partialEvaluator.rootForCallTarget(compilable);
120+
TruffleTierContext context = new TruffleTierContext(partialEvaluator, compilerOptions, debug, compilable, compilationId, log, task, handler, method);
121+
context.recordStabilityAssumptions();
122+
return context;
123+
}
124+
125+
public TruffleTierContext createInlineContext(TruffleCompilable inlinedCompilable) {
126+
boolean readyForCompilation = inlinedCompilable.prepareForCompilation(false, task.tier(), task.isLastTier());
127+
if (!readyForCompilation) {
128+
/*
129+
* If the root node not ready for compilation we throw a retryable bailout. For inlining
130+
* this bailout triggers the Bailout state in the inlining call tree which forces the
131+
* call to a materialize and not inline.
132+
*/
133+
throw new RetryableBailoutException("Compilable not ready for compilation.");
134+
}
135+
TruffleTierContext context = new TruffleTierContext(this, inlinedCompilable,
136+
partialEvaluator.inlineRootForCallTarget(compilable));
137+
context.recordStabilityAssumptions();
138+
return context;
139+
}
90140

141+
/**
142+
* This creates a compilation context for call site finalization during inlining. We PE all
143+
* candidates up to callInlined, then when we are done with inlining we finalize the graph by PE
144+
* from callDirect to the call boundary for call sites not inlined.
145+
*/
146+
public TruffleTierContext createFinalizationContext(TruffleCompilable inlinedCompilable) {
147+
return new TruffleTierContext(this, inlinedCompilable, partialEvaluator.getCallDirect());
148+
}
149+
150+
private StructuredGraph createInitialGraph(ResolvedJavaMethod method) {
91151
// @formatter:off
92152
StructuredGraph.Builder builder = new StructuredGraph.Builder(this.debug.getOptions(), this.debug, StructuredGraph.AllowAssumptions.YES).
93153
name(this.compilable.getName()).
@@ -97,11 +157,12 @@ private StructuredGraph createInitialGraph(ResolvedJavaMethod method) {
97157
trackNodeSourcePosition(partialEvaluator.graphBuilderConfigForParsing.trackNodeSourcePosition()).
98158
cancellable(new CancellableTask(this.task));
99159
// @formatter:on
100-
builder = partialEvaluator.customizeStructuredGraphBuilder(builder);
101-
StructuredGraph g = builder.build();
102-
g.getAssumptions().record(new TruffleAssumption(getValidRootAssumption(partialEvaluator.getProviders())));
103-
g.getAssumptions().record(new TruffleAssumption(getNodeRewritingAssumption(partialEvaluator.getProviders())));
104-
return g;
160+
return partialEvaluator.customizeStructuredGraphBuilder(builder).build();
161+
}
162+
163+
private void recordStabilityAssumptions() {
164+
graph.getAssumptions().record(new TruffleAssumption(getValidRootAssumption(partialEvaluator.getProviders())));
165+
graph.getAssumptions().record(new TruffleAssumption(getNodeRewritingAssumption(partialEvaluator.getProviders())));
105166
}
106167

107168
public PartialEvaluator getPartialEvaluator() {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/phases/inlining/CallNode.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import com.oracle.truffle.compiler.TruffleCompilable;
3838
import com.oracle.truffle.compiler.TruffleCompilationTask;
3939

40-
import jdk.graal.compiler.core.common.PermanentBailoutException;
40+
import jdk.graal.compiler.core.common.GraalBailoutException;
4141
import jdk.graal.compiler.debug.Assertions;
4242
import jdk.graal.compiler.debug.GraalError;
4343
import jdk.graal.compiler.graph.Node;
@@ -278,6 +278,13 @@ private void addIndirectChildren(GraphManager.Entry entry) {
278278
}
279279

280280
public void expand() {
281+
if (state == State.Expanded) {
282+
/*
283+
* The CallNode may be already expanded for trivial call nodes which are expanded in the
284+
* afterExpand method.
285+
*/
286+
return;
287+
}
281288
assert state == State.Cutoff : "Cannot expand a non-cutoff node. Node is " + state;
282289
assert getParent() != null;
283290
state = State.Expanded;
@@ -288,7 +295,7 @@ public void expand() {
288295
GraphManager manager = getCallTree().getGraphManager();
289296
try {
290297
entry = manager.pe(directCallTarget);
291-
} catch (PermanentBailoutException e) {
298+
} catch (GraalBailoutException e) {
292299
state = State.BailedOut;
293300
return;
294301
}
@@ -466,17 +473,25 @@ public int compareTo(CallNode o) {
466473
}
467474

468475
public void finalizeGraph() {
469-
if (state == State.Inlined) {
470-
for (CallNode child : children) {
471-
child.finalizeGraph();
472-
}
473-
}
474-
if (state == State.Cutoff || state == State.Expanded || state == State.BailedOut) {
475-
if (invoke.isAlive()) {
476-
getCallTree().getGraphManager().finalizeGraph(invoke, directCallTarget);
477-
} else {
478-
state = State.Removed;
479-
}
476+
switch (state) {
477+
case Inlined:
478+
for (CallNode child : children) {
479+
child.finalizeGraph();
480+
}
481+
break;
482+
case Cutoff:
483+
case Expanded:
484+
case BailedOut:
485+
if (invoke.isAlive()) {
486+
getCallTree().getGraphManager().finalizeGraph(invoke, directCallTarget);
487+
} else {
488+
state = State.Removed;
489+
}
490+
break;
491+
case Indirect:
492+
case Removed:
493+
// nothing to finalize
494+
break;
480495
}
481496
}
482497

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/phases/inlining/DefaultInliningPolicy.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,17 @@ private void analyse(CallNode node) {
9090
if (node.getState() == CallNode.State.Expanded) {
9191
data.callDiff = -1 * node.getRootRelativeFrequency();
9292
for (CallNode child : node.getChildren()) {
93-
if (child.getState() != CallNode.State.Indirect && child.getState() != CallNode.State.Removed && child.getState() != CallNode.State.BailedOut) {
94-
data.callDiff += data(child).callDiff;
93+
switch (child.getState()) {
94+
case Indirect:
95+
case Removed:
96+
case BailedOut:
97+
// nothing to do
98+
break;
99+
case Cutoff:
100+
case Inlined:
101+
case Expanded:
102+
data.callDiff += data(child).callDiff;
103+
break;
95104
}
96105
}
97106
if (data.callDiff > 0) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/phases/inlining/GraphManager.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Entry pe(TruffleCompilable truffleAST) {
7979
// the guest scope represents the guest language method of truffle
8080
try (AutoCloseable guestScope = rootContext.debug.scope("Truffle", new TruffleDebugJavaMethod(rootContext.task, truffleAST))) {
8181
final PEAgnosticInlineInvokePlugin plugin = newPlugin();
82-
final TruffleTierContext context = newContext(truffleAST, false);
82+
final TruffleTierContext context = rootContext.createInlineContext(truffleAST);
8383
try (Scope hostScope = context.debug.scope("CreateGraph", context.graph);
8484
Indent indent = context.debug.logAndIndent("evaluate %s", context.graph);) {
8585
partialEvaluator.doGraphPE(context, plugin, graphCacheForInlining);
@@ -99,19 +99,6 @@ Entry pe(TruffleCompilable truffleAST) {
9999
return entry;
100100
}
101101

102-
private TruffleTierContext newContext(TruffleCompilable truffleAST, boolean finalize) {
103-
return new TruffleTierContext(
104-
partialEvaluator,
105-
rootContext.compilerOptions,
106-
rootContext.debug,
107-
truffleAST,
108-
finalize ? partialEvaluator.getCallDirect() : partialEvaluator.inlineRootForCallTarget(truffleAST),
109-
rootContext.compilationId,
110-
rootContext.log,
111-
rootContext.task,
112-
rootContext.handler);
113-
}
114-
115102
private PEAgnosticInlineInvokePlugin newPlugin() {
116103
return new PEAgnosticInlineInvokePlugin(partialEvaluator);
117104
}
@@ -135,7 +122,7 @@ UnmodifiableEconomicMap<Node, Node> doInline(Invoke invoke, StructuredGraph ir,
135122
}
136123

137124
void finalizeGraph(Invoke invoke, TruffleCompilable truffleAST) {
138-
final TruffleTierContext context = newContext(truffleAST, true);
125+
final TruffleTierContext context = rootContext.createFinalizationContext(truffleAST);
139126
partialEvaluator.doGraphPE(context, new InlineInvokePlugin() {
140127
@Override
141128
public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {

substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/FromLibGraalCalls.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public String toString() {
9595
}
9696
}
9797

98+
JClass getPeer() {
99+
return peer;
100+
}
101+
102+
JNICalls getJNICalls() {
103+
return hotSpotCalls;
104+
}
105+
98106
public final void callVoid(JNIEnv env, T id, JValue args) {
99107
JNIMethodImpl<T> method = getJNIMethod(env, id, void.class);
100108
hotSpotCalls.callStaticVoid(env, peer, method, args);

substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/TruffleFromLibGraalCalls.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
*/
2525
package com.oracle.svm.graal.hotspot.libgraal;
2626

27-
import com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id;
27+
import static org.graalvm.jniutils.JNIUtil.NewGlobalRef;
28+
2829
import org.graalvm.jniutils.JNI.JClass;
2930
import org.graalvm.jniutils.JNI.JNIEnv;
3031
import org.graalvm.jniutils.JNI.JObject;
3132
import org.graalvm.jniutils.JNIUtil;
3233

33-
import static org.graalvm.jniutils.JNIUtil.NewGlobalRef;
34+
import com.oracle.truffle.compiler.hotspot.libgraal.TruffleFromLibGraal.Id;
3435

3536
final class TruffleFromLibGraalCalls extends FromLibGraalCalls<Id> {
3637

0 commit comments

Comments
 (0)