Skip to content

Commit 5145191

Browse files
author
Christian Wimmer
committed
[GR-38180] [GR-38568] Encode compilation graphs to reduce memory footprint of the image generator.
PullRequest: graal/11701
2 parents db9711f + 957b2f1 commit 5145191

File tree

8 files changed

+374
-177
lines changed

8 files changed

+374
-177
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/nodes/CFunctionEpilogueNode.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,18 @@ public final class CFunctionEpilogueNode extends AbstractStateSplit implements L
5555
public CFunctionEpilogueNode(int oldThreadStatus) {
5656
super(TYPE, StampFactory.forVoid());
5757
this.oldThreadStatus = oldThreadStatus;
58-
marker = new CFunctionEpilogueMarker();
5958
}
6059

6160
@Override
6261
protected void afterClone(Node other) {
6362
super.afterClone(other);
64-
marker = new CFunctionEpilogueMarker();
63+
assert marker == null : "Marker must be unique";
6564
}
6665

6766
public CFunctionEpilogueMarker getMarker() {
67+
if (marker == null) {
68+
marker = new CFunctionEpilogueMarker();
69+
}
6870
return marker;
6971
}
7072

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/nodes/CFunctionPrologueNode.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,32 @@ public final class CFunctionPrologueNode extends FixedWithNextNode implements Lo
6262

6363
private final int newThreadStatus;
6464
/**
65-
* The marker object prevents value numbering of the node. This means that the marker must be a
66-
* unique object per node, even after node cloning (e.g., because of method inlining).
67-
* Therefore, {@link #afterClone} properly re-initializes the field to a new marker instance.
68-
*
69-
* The marker is also used for LIR frame state verification, to ensure we have a proper matching
70-
* of prologue and epilogue and no unexpected machine code while the thread is in Native state.
65+
* The marker is used for LIR frame state verification, to ensure we have a proper matching of
66+
* prologue and epilogue and no unexpected machine code while the thread is in Native state.
7167
*/
7268
private CFunctionPrologueMarker marker;
7369

7470
public CFunctionPrologueNode(int newThreadStatus) {
7571
super(TYPE, StampFactory.forVoid());
7672
this.newThreadStatus = newThreadStatus;
77-
marker = new CFunctionPrologueMarker(newThreadStatus);
7873
}
7974

8075
@Override
8176
protected void afterClone(Node other) {
8277
super.afterClone(other);
83-
marker = new CFunctionPrologueMarker(newThreadStatus);
78+
/*
79+
* Note that this method is invoked by the regular method inlining, but not by the
80+
* PEGraphDecoder. So the method inlining before analysis, as well as the trivial method
81+
* inlining before compilation, do not invoke this method. So it is only suitable for
82+
* assertion checking.
83+
*/
84+
assert marker == null : "Marker must be unique";
8485
}
8586

8687
public CFunctionPrologueMarker getMarker() {
88+
if (marker == null) {
89+
marker = new CFunctionPrologueMarker(newThreadStatus);
90+
}
8791
return marker;
8892
}
8993

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.hosted.code;
26+
27+
import java.util.Collections;
28+
import java.util.HashSet;
29+
import java.util.Set;
30+
31+
import org.graalvm.compiler.graph.NodeSourcePosition;
32+
import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
33+
import org.graalvm.compiler.nodes.EncodedGraph;
34+
import org.graalvm.compiler.nodes.GraphEncoder;
35+
import org.graalvm.compiler.nodes.StructuredGraph;
36+
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
37+
38+
import com.oracle.graal.pointsto.flow.AnalysisParsedGraph;
39+
import com.oracle.svm.hosted.meta.HostedMethod;
40+
41+
/**
42+
* A holder for an {@link EncodedGraph} while doing the AOT compilation in {@link CompileQueue}.
43+
* Encoding the graph is important to reduce the memory footprint of the image generator. But a few
44+
* properties of the graph need to remain accessible, for example to guide inlining decisions. All
45+
* such information is stored in separate fields of this class. This ensures that an encoded graph
46+
* and its extra information are always in sync.
47+
*/
48+
public final class CompilationGraph {
49+
50+
/** Information about an invocation in the encoded graph. */
51+
public static class InvokeInfo {
52+
private final InvokeKind invokeKind;
53+
private final HostedMethod targetMethod;
54+
private final HostedMethod directCaller;
55+
private final NodeSourcePosition nodeSourcePosition;
56+
57+
InvokeInfo(InvokeKind invokeKind, HostedMethod targetMethod, HostedMethod directCaller, NodeSourcePosition nodeSourcePosition) {
58+
this.invokeKind = invokeKind;
59+
this.targetMethod = targetMethod;
60+
this.directCaller = directCaller;
61+
this.nodeSourcePosition = nodeSourcePosition;
62+
}
63+
64+
public InvokeKind getInvokeKind() {
65+
return invokeKind;
66+
}
67+
68+
public HostedMethod getTargetMethod() {
69+
return targetMethod;
70+
}
71+
72+
public HostedMethod getDirectCaller() {
73+
return directCaller;
74+
}
75+
76+
public NodeSourcePosition getNodeSourcePosition() {
77+
return nodeSourcePosition;
78+
}
79+
}
80+
81+
/** Information about an allocation in the encoded graph. */
82+
public static class AllocationInfo {
83+
private final NodeSourcePosition nodeSourcePosition;
84+
85+
AllocationInfo(NodeSourcePosition nodeSourcePosition) {
86+
this.nodeSourcePosition = nodeSourcePosition;
87+
}
88+
89+
public NodeSourcePosition getNodeSourcePosition() {
90+
return nodeSourcePosition;
91+
}
92+
}
93+
94+
private final EncodedGraph encodedGraph;
95+
private final int nodeCount;
96+
private final Set<InvokeInfo> invokeInfos;
97+
private final Set<AllocationInfo> allocationInfos;
98+
99+
private CompilationGraph(EncodedGraph encodedGraph, int nodeCount, Set<InvokeInfo> invokeInfos, Set<AllocationInfo> allocationInfos) {
100+
this.encodedGraph = encodedGraph;
101+
this.nodeCount = nodeCount;
102+
this.invokeInfos = invokeInfos;
103+
this.allocationInfos = allocationInfos;
104+
}
105+
106+
static CompilationGraph encode(StructuredGraph graph) {
107+
Set<InvokeInfo> invokeInfos = new HashSet<>();
108+
Set<AllocationInfo> allocationInfos = new HashSet<>();
109+
for (var n : graph.getNodes()) {
110+
if (n instanceof MethodCallTargetNode) {
111+
MethodCallTargetNode node = (MethodCallTargetNode) n;
112+
invokeInfos.add(new InvokeInfo(
113+
node.invokeKind(),
114+
(HostedMethod) node.targetMethod(),
115+
(HostedMethod) node.invoke().stateAfter().getMethod(),
116+
node.getNodeSourcePosition()));
117+
}
118+
if (UninterruptibleAnnotationChecker.isAllocationNode(n)) {
119+
allocationInfos.add(new AllocationInfo(n.getNodeSourcePosition()));
120+
}
121+
}
122+
123+
return new CompilationGraph(
124+
GraphEncoder.encodeSingleGraph(graph, AnalysisParsedGraph.HOST_ARCHITECTURE),
125+
graph.getNodeCount(),
126+
invokeInfos.isEmpty() ? Collections.emptySet() : invokeInfos,
127+
allocationInfos.isEmpty() ? Collections.emptySet() : allocationInfos);
128+
}
129+
130+
public EncodedGraph getEncodedGraph() {
131+
return encodedGraph;
132+
}
133+
134+
public int getNodeCount() {
135+
return nodeCount;
136+
}
137+
138+
public Set<InvokeInfo> getInvokeInfos() {
139+
return invokeInfos;
140+
}
141+
142+
public Set<AllocationInfo> getAllocationInfos() {
143+
return allocationInfos;
144+
}
145+
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/CompilationInfo.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
import java.util.concurrent.atomic.AtomicBoolean;
2828
import java.util.concurrent.atomic.AtomicLong;
2929

30+
import org.graalvm.compiler.core.common.CompilationIdentifier;
31+
import org.graalvm.compiler.debug.DebugContext;
3032
import org.graalvm.compiler.nodes.ConstantNode;
33+
import org.graalvm.compiler.nodes.GraphDecoder;
3134
import org.graalvm.compiler.nodes.StructuredGraph;
35+
import org.graalvm.compiler.options.OptionValues;
3236

33-
import com.oracle.svm.core.SubstrateOptions;
37+
import com.oracle.graal.pointsto.flow.AnalysisParsedGraph;
3438
import com.oracle.svm.core.annotate.DeoptTest;
3539
import com.oracle.svm.core.annotate.Specialize;
3640
import com.oracle.svm.hosted.code.CompileQueue.CompileFunction;
@@ -48,14 +52,13 @@ public class CompilationInfo {
4852
*/
4953
protected boolean inCompileQueue;
5054

51-
protected volatile StructuredGraph graph;
55+
private volatile CompilationGraph compilationGraph;
56+
private OptionValues compileOptions;
5257

5358
protected boolean isTrivialMethod;
5459

5560
protected boolean canDeoptForTesting;
5661

57-
protected boolean modifiesSpecialRegisters;
58-
5962
/**
6063
* The constant arguments for a {@link DeoptTest} method called by a {@link Specialize} method.
6164
* Note: this is only used for testing.
@@ -126,22 +129,41 @@ public HostedMethod getDeoptTargetMethod() {
126129
return deoptTarget;
127130
}
128131

129-
public void setGraph(StructuredGraph graph) {
130-
this.graph = graph;
132+
public CompilationGraph getCompilationGraph() {
133+
return compilationGraph;
131134
}
132135

133-
public void clear() {
134-
graph = null;
135-
specializedArguments = null;
136-
}
136+
@SuppressWarnings("try")
137+
public StructuredGraph createGraph(DebugContext debug, CompilationIdentifier compilationId, boolean decode) {
138+
var graph = new StructuredGraph.Builder(compileOptions, debug)
139+
.method(method)
140+
.recordInlinedMethods(false)
141+
.trackNodeSourcePosition(getCompilationGraph().getEncodedGraph().trackNodeSourcePosition())
142+
.compilationId(compilationId)
143+
.build();
137144

138-
public StructuredGraph getGraph() {
145+
if (decode) {
146+
try (var s = debug.scope("CreateGraph", graph, method)) {
147+
var decoder = new GraphDecoder(AnalysisParsedGraph.HOST_ARCHITECTURE, graph);
148+
decoder.decode(getCompilationGraph().getEncodedGraph());
149+
} catch (Throwable ex) {
150+
throw debug.handle(ex);
151+
}
152+
}
139153
return graph;
140154
}
141155

142-
public boolean modifiesSpecialRegisters() {
143-
assert SubstrateOptions.useLLVMBackend();
144-
return modifiesSpecialRegisters;
156+
void encodeGraph(StructuredGraph graph) {
157+
compilationGraph = CompilationGraph.encode(graph);
158+
}
159+
160+
public void setCompileOptions(OptionValues compileOptions) {
161+
this.compileOptions = compileOptions;
162+
}
163+
164+
public void clear() {
165+
compilationGraph = null;
166+
specializedArguments = null;
145167
}
146168

147169
public boolean isTrivialMethod() {

0 commit comments

Comments
 (0)