Skip to content

Commit ac25ff1

Browse files
committed
[GR-42162] Add support for tracking inlining during inlinebeforeanalysis.
PullRequest: graal/15097
2 parents e1322a1 + c5e1c7b commit ac25ff1

File tree

11 files changed

+101
-14
lines changed

11 files changed

+101
-14
lines changed

compiler/src/jdk.internal.vm.compiler/src/org/graalvm/compiler/replacements/GraphKit.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public GraphKit(DebugContext debug, ResolvedJavaMethod stubMethod, Providers pro
119119
// Set up a default value that everything constructed by GraphKit will use.
120120
graph.withNodeSourcePosition(NodeSourcePosition.substitution(stubMethod));
121121
}
122+
graph.recordMethod(stubMethod);
122123
this.wordTypes = wordTypes;
123124
this.graphBuilderPlugins = graphBuilderPlugins;
124125
this.lastFixedNode = graph.start();

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/api/HostVM.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ public StructuredGraph.AllowAssumptions allowAssumptions(AnalysisMethod method)
292292
return StructuredGraph.AllowAssumptions.NO;
293293
}
294294

295+
/**
296+
* @return Whether which methods were inlined should be recorded.
297+
*/
298+
@SuppressWarnings("unused")
299+
public boolean recordInlinedMethods(AnalysisMethod method) {
300+
return false;
301+
}
302+
295303
public void initializeProviders(HostedProviders newProviders) {
296304
AnalysisError.guarantee(providers == null, "can only initialize providers once");
297305
providers = newProviders;

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisMethod.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ public StructuredGraph decodeAnalyzedGraph(DebugContext debug, Iterable<EncodedN
937937
}
938938

939939
var allowAssumptions = getUniverse().hostVM().allowAssumptions(this);
940+
// Note we never record inlined methods. This is correct even for runtime compiled methods
940941
StructuredGraph result = new StructuredGraph.Builder(debug.getOptions(), debug, allowAssumptions).method(this).recordInlinedMethods(false).trackNodeSourcePosition(
941942
analyzedGraph.trackNodeSourcePosition()).build();
942943
GraphDecoder decoder = new GraphDecoder(AnalysisParsedGraph.HOST_ARCHITECTURE, result);
@@ -960,6 +961,10 @@ public void setAnalyzedGraph(EncodedGraph analyzedGraph) {
960961
this.analyzedGraph = analyzedGraph;
961962
}
962963

964+
public EncodedGraph getAnalyzedGraph() {
965+
return analyzedGraph;
966+
}
967+
963968
@Override
964969
public MultiMethodKey getMultiMethodKey() {
965970
return multiMethodKey;

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/phases/InlineBeforeAnalysis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static StructuredGraph decodeGraph(BigBang bb, AnalysisMethod method, Ana
6565

6666
StructuredGraph result = new StructuredGraph.Builder(bb.getOptions(), debug, bb.getHostVM().allowAssumptions(method))
6767
.method(method)
68-
.recordInlinedMethods(false)
68+
.recordInlinedMethods(bb.getHostVM().recordInlinedMethods(method))
6969
.trackNodeSourcePosition(analysisParsedGraph.getEncodedGraph().trackNodeSourcePosition())
7070
.build();
7171

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/phases/InlineBeforeAnalysisGraphDecoder.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
*/
2525
package com.oracle.graal.pointsto.phases;
2626

27+
import static com.oracle.graal.pointsto.phases.InlineBeforeAnalysisGraphDecoder.InlineBeforeAnalysisMethodScope.recordInlined;
28+
2729
import java.util.ArrayDeque;
2830
import java.util.Deque;
2931
import java.util.concurrent.ConcurrentHashMap;
3032

33+
import org.graalvm.collections.EconomicSet;
3134
import org.graalvm.compiler.bytecode.BytecodeProvider;
3235
import org.graalvm.compiler.debug.GraalError;
3336
import org.graalvm.compiler.graph.Node;
@@ -64,6 +67,12 @@ public class InlineBeforeAnalysisMethodScope extends PEMethodScope {
6467

6568
private boolean inliningAborted;
6669

70+
/*
71+
* We temporarily track all graphs actually encoded (i.e., not aborted) so that all
72+
* recording can be performed afterwards.
73+
*/
74+
private final EconomicSet<EncodedGraph> encodedGraphs;
75+
6776
InlineBeforeAnalysisMethodScope(StructuredGraph targetGraph, PEMethodScope caller, LoopScope callerLoopScope, EncodedGraph encodedGraph, ResolvedJavaMethod method,
6877
InvokeData invokeData, int inliningDepth, ValueNode[] arguments) {
6978
super(targetGraph, caller, callerLoopScope, encodedGraph, method, invokeData, inliningDepth, arguments);
@@ -83,6 +92,16 @@ public class InlineBeforeAnalysisMethodScope extends PEMethodScope {
8392
graph.getDebug().logv(" ".repeat(inliningDepth) + "openCalleeScope for " + method.format("%H.%n(%p)") + ": " + policyScope);
8493
}
8594
}
95+
encodedGraphs = EconomicSet.create();
96+
}
97+
98+
static void recordInlined(InlineBeforeAnalysisMethodScope callerScope, InlineBeforeAnalysisMethodScope calleeScope) {
99+
/*
100+
* Update caller's encoded graphs
101+
*/
102+
var callerEncodedGraphs = callerScope.encodedGraphs;
103+
callerEncodedGraphs.addAll(calleeScope.encodedGraphs);
104+
callerEncodedGraphs.add(calleeScope.encodedGraph);
86105
}
87106
}
88107

@@ -111,6 +130,18 @@ protected InvocationPlugin getInvocationPlugin(ResolvedJavaMethod targetMethod)
111130
return null;
112131
}
113132

133+
@Override
134+
protected void cleanupGraph(MethodScope ms) {
135+
super.cleanupGraph(ms);
136+
137+
// at the very end we record all inlining
138+
var methodScope = cast(ms);
139+
methodScope.encodedGraphs.add(methodScope.encodedGraph);
140+
for (var encodedGraph : methodScope.encodedGraphs) {
141+
super.recordGraphElements(encodedGraph);
142+
}
143+
}
144+
114145
@Override
115146
protected PEMethodScope createMethodScope(StructuredGraph targetGraph, PEMethodScope caller, LoopScope callerLoopScope, EncodedGraph encodedGraph, ResolvedJavaMethod method, InvokeData invokeData,
116147
int inliningDepth, ValueNode[] arguments) {
@@ -195,6 +226,14 @@ protected LoopScope processNextNode(MethodScope ms, LoopScope loopScope) {
195226
return super.processNextNode(methodScope, loopScope);
196227
}
197228

229+
@Override
230+
protected void recordGraphElements(EncodedGraph encodedGraph) {
231+
/*
232+
* We temporarily delay recording graph elements, as at this point it is possible inlining
233+
* will be aborted.
234+
*/
235+
}
236+
198237
@Override
199238
protected void finishInlining(MethodScope is) {
200239
InlineBeforeAnalysisMethodScope inlineScope = cast(is);
@@ -235,6 +274,8 @@ protected void finishInlining(MethodScope is) {
235274
callerScope.policyScope.commitCalleeScope(inlineScope.policyScope);
236275
}
237276

277+
recordInlined(callerScope, inlineScope);
278+
238279
NodeSourcePosition callerBytecodePosition = callerScope.getCallerNodeSourcePosition();
239280
Object reason = callerBytecodePosition != null ? callerBytecodePosition : callerScope.method;
240281
reason = reason == null ? graph.method() : reason;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/replacements/SubstrateGraphKit.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ private static boolean trackNodeSourcePosition(boolean forceTrackNodeSourcePosit
107107

108108
@SuppressWarnings("this-escape")
109109
public SubstrateGraphKit(DebugContext debug, ResolvedJavaMethod stubMethod, Providers providers, WordTypes wordTypes,
110-
GraphBuilderConfiguration.Plugins graphBuilderPlugins, CompilationIdentifier compilationId, boolean forceTrackNodeSourcePosition) {
111-
super(debug, stubMethod, providers, wordTypes, graphBuilderPlugins, compilationId, null, trackNodeSourcePosition(forceTrackNodeSourcePosition), false);
110+
GraphBuilderConfiguration.Plugins graphBuilderPlugins, CompilationIdentifier compilationId, boolean forceTrackNodeSourcePosition, boolean recordInlinedMethods) {
111+
super(debug, stubMethod, providers, wordTypes, graphBuilderPlugins, compilationId, null, trackNodeSourcePosition(forceTrackNodeSourcePosition), recordInlinedMethods);
112112
assert wordTypes != null : "Support for Word types is mandatory";
113113
frameState = new FrameStateBuilder(this, stubMethod, graph);
114114
frameState.disableKindVerification();
@@ -117,6 +117,11 @@ public SubstrateGraphKit(DebugContext debug, ResolvedJavaMethod stubMethod, Prov
117117
graph.start().setStateAfter(frameState.create(bci(), graph.start()));
118118
}
119119

120+
public SubstrateGraphKit(DebugContext debug, ResolvedJavaMethod stubMethod, Providers providers, WordTypes wordTypes,
121+
GraphBuilderConfiguration.Plugins graphBuilderPlugins, CompilationIdentifier compilationId, boolean forceTrackNodeSourcePosition) {
122+
this(debug, stubMethod, providers, wordTypes, graphBuilderPlugins, compilationId, forceTrackNodeSourcePosition, false);
123+
}
124+
120125
@Override
121126
protected MethodCallTargetNode createMethodCallTarget(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, StampPair returnStamp, int bci) {
122127
return new SubstrateMethodCallTargetNode(invokeKind, targetMethod, args, returnStamp, null, null, null);

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/ParseOnceDeoptTestFeature.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public boolean allowAssumptions(AnalysisMethod method) {
133133
return false;
134134
}
135135

136+
@Override
137+
public boolean recordInlinedMethods(AnalysisMethod method) {
138+
return false;
139+
}
140+
136141
@Override
137142
public HostedProviders getHostedProviders(MultiMethod.MultiMethodKey key) {
138143
/* The buildtime providers are always used. */

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/ParseOnceRuntimeCompilationFeature.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.concurrent.atomic.AtomicLong;
4545
import java.util.function.Function;
4646
import java.util.function.Supplier;
47+
import java.util.stream.Collectors;
4748

4849
import org.graalvm.collections.EconomicMap;
4950
import org.graalvm.compiler.core.common.PermanentBailoutException;
@@ -214,9 +215,11 @@ public int hashCode() {
214215

215216
static final class RuntimeCompiledMethodImpl implements RuntimeCompiledMethod {
216217
final AnalysisMethod method;
218+
final Collection<ResolvedJavaMethod> inlinedMethods;
217219

218-
private RuntimeCompiledMethodImpl(AnalysisMethod method) {
220+
private RuntimeCompiledMethodImpl(AnalysisMethod method, Collection<ResolvedJavaMethod> inlinedMethods) {
219221
this.method = method;
222+
this.inlinedMethods = inlinedMethods;
220223
}
221224

222225
@Override
@@ -226,10 +229,7 @@ public AnalysisMethod getMethod() {
226229

227230
@Override
228231
public Collection<ResolvedJavaMethod> getInlinedMethods() {
229-
/*
230-
* Currently no inlining is performed when ParseOnceJIT is enabled.
231-
*/
232-
return List.of();
232+
return inlinedMethods;
233233
}
234234

235235
@Override
@@ -399,8 +399,15 @@ public void afterAnalysis(AfterAnalysisAccess access) {
399399
for (var method : impl.getUniverse().getMethods()) {
400400
var rMethod = method.getMultiMethod(RUNTIME_COMPILED_METHOD);
401401
if (rMethod != null && rMethod.isReachable() && !invalidForRuntimeCompilation.containsKey(rMethod)) {
402-
boolean added = runtimeCompilations.add(new RuntimeCompiledMethodImpl(method));
403-
assert !added || runtimeCompiledMethodCallTree.containsKey(method);
402+
var runtimeInlinedMethods = rMethod.getAnalyzedGraph().getInlinedMethods();
403+
var inlinedMethods = runtimeInlinedMethods.stream().map(inlinedMethod -> {
404+
ResolvedJavaMethod orig = ((AnalysisMethod) inlinedMethod).getMultiMethod(ORIGINAL_METHOD);
405+
assert orig != null;
406+
return orig;
407+
}).collect(Collectors.toUnmodifiableSet());
408+
boolean added = runtimeCompilations.add(new RuntimeCompiledMethodImpl(method, inlinedMethods));
409+
assert added;
410+
assert runtimeCompiledMethodCallTree.containsKey(method);
404411
}
405412
}
406413

@@ -763,6 +770,11 @@ public boolean allowAssumptions(AnalysisMethod method) {
763770
return method.getMultiMethodKey() == RUNTIME_COMPILED_METHOD;
764771
}
765772

773+
@Override
774+
public boolean recordInlinedMethods(AnalysisMethod method) {
775+
return method.getMultiMethodKey() == RUNTIME_COMPILED_METHOD;
776+
}
777+
766778
@Override
767779
public Object parseGraph(BigBang bb, DebugContext debug, AnalysisMethod method) {
768780
// want to have a couple more checks here that are in DeoptimizationUtils
@@ -811,9 +823,7 @@ private Object parseRuntimeCompiledMethod(BigBang bb, DebugContext debug, Analys
811823
if (parsed) {
812824
// enable this logging to get log output in compilation passes
813825
try (Indent indent2 = debug.logAndIndent("parse graph phases")) {
814-
RuntimeGraphBuilderPhase
815-
.createRuntimeGraphBuilderPhase(bb, analysisProviders, graphBuilderConfig, optimisticOpts)
816-
.apply(graph);
826+
RuntimeGraphBuilderPhase.createRuntimeGraphBuilderPhase(bb, analysisProviders, graphBuilderConfig, optimisticOpts).apply(graph);
817827
} catch (PermanentBailoutException ex) {
818828
bb.getUnsupportedFeatures().addMessage(method.format("%H.%n(%p)"), method, ex.getLocalizedMessage(), null, ex);
819829
recordFailed(method);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,14 @@ public StructuredGraph.AllowAssumptions allowAssumptions(AnalysisMethod method)
901901
return super.allowAssumptions(method);
902902
}
903903

904+
@Override
905+
public boolean recordInlinedMethods(AnalysisMethod method) {
906+
if (parsingSupport != null) {
907+
return parsingSupport.recordInlinedMethods(method);
908+
}
909+
return super.recordInlinedMethods(method);
910+
}
911+
904912
@Override
905913
public HostedProviders getProviders(MultiMethod.MultiMethodKey key) {
906914
if (parsingSupport != null) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/analysis/SVMParsingSupport.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public interface SVMParsingSupport {
5656

5757
boolean allowAssumptions(AnalysisMethod method);
5858

59+
boolean recordInlinedMethods(AnalysisMethod method);
60+
5961
HostedProviders getHostedProviders(MultiMethod.MultiMethodKey key);
6062

6163
void initializeInlineBeforeAnalysisPolicy(SVMHost svmHost, InlineBeforeAnalysisPolicyUtils inliningUtils);

0 commit comments

Comments
 (0)