Skip to content

Commit 3041baf

Browse files
author
Christian Wimmer
committed
Encode compilation graphs to reduce memory footprint of the image generator
1 parent f8c21ae commit 3041baf

File tree

6 files changed

+320
-165
lines changed

6 files changed

+320
-165
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
public final class CompilationGraph {
42+
43+
public static class InvokeInfo {
44+
public final InvokeKind invokeKind;
45+
public final HostedMethod targetMethod;
46+
public final HostedMethod directCaller;
47+
public final NodeSourcePosition nodeSourcePosition;
48+
49+
InvokeInfo(InvokeKind invokeKind, HostedMethod targetMethod, HostedMethod directCaller, NodeSourcePosition nodeSourcePosition) {
50+
this.invokeKind = invokeKind;
51+
this.targetMethod = targetMethod;
52+
this.directCaller = directCaller;
53+
this.nodeSourcePosition = nodeSourcePosition;
54+
}
55+
}
56+
57+
public static class AllocationInfo {
58+
public final NodeSourcePosition nodeSourcePosition;
59+
60+
AllocationInfo(NodeSourcePosition nodeSourcePosition) {
61+
this.nodeSourcePosition = nodeSourcePosition;
62+
}
63+
}
64+
65+
private final EncodedGraph encodedGraph;
66+
private final int nodeCount;
67+
private final Set<InvokeInfo> invokeInfos;
68+
private final Set<AllocationInfo> allocationInfos;
69+
70+
private CompilationGraph(EncodedGraph encodedGraph, int nodeCount, Set<InvokeInfo> invokeInfos, Set<AllocationInfo> allocationInfos) {
71+
this.encodedGraph = encodedGraph;
72+
this.nodeCount = nodeCount;
73+
this.invokeInfos = invokeInfos;
74+
this.allocationInfos = allocationInfos;
75+
}
76+
77+
static CompilationGraph encode(StructuredGraph graph) {
78+
Set<InvokeInfo> invokeInfos = new HashSet<>();
79+
Set<AllocationInfo> allocationInfos = new HashSet<>();
80+
for (var n : graph.getNodes()) {
81+
if (n instanceof MethodCallTargetNode) {
82+
MethodCallTargetNode node = (MethodCallTargetNode) n;
83+
invokeInfos.add(new InvokeInfo(
84+
node.invokeKind(),
85+
(HostedMethod) node.targetMethod(),
86+
(HostedMethod) node.invoke().stateAfter().getMethod(),
87+
node.getNodeSourcePosition()));
88+
}
89+
if (UninterruptibleAnnotationChecker.isAllocationNode(n)) {
90+
allocationInfos.add(new AllocationInfo(n.getNodeSourcePosition()));
91+
}
92+
}
93+
94+
return new CompilationGraph(
95+
GraphEncoder.encodeSingleGraph(graph, AnalysisParsedGraph.HOST_ARCHITECTURE),
96+
graph.getNodeCount(),
97+
invokeInfos.isEmpty() ? Collections.emptySet() : invokeInfos,
98+
allocationInfos.isEmpty() ? Collections.emptySet() : allocationInfos);
99+
}
100+
101+
public EncodedGraph getEncodedGraph() {
102+
return encodedGraph;
103+
}
104+
105+
public int getNodeCount() {
106+
return nodeCount;
107+
}
108+
109+
public Set<InvokeInfo> getInvokeInfos() {
110+
return invokeInfos;
111+
}
112+
113+
public Set<AllocationInfo> getAllocationInfos() {
114+
return allocationInfos;
115+
}
116+
}

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)