Skip to content

Commit 8251c3d

Browse files
committed
[GR-66216] [GR-66435] Improve loop explosion performance during partial evaluation
PullRequest: graal/21159
2 parents ff024fb + 7f1e61b commit 8251c3d

File tree

7 files changed

+582
-109
lines changed

7 files changed

+582
-109
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/graph/Graph.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -84,6 +84,12 @@ private enum FreezeState {
8484
public final boolean verifyGraphs;
8585
public final boolean verifyGraphEdges;
8686

87+
/**
88+
* Cached actual value of {@link GraalOptions#TrackNodeInsertion} to avoid expensive map lookup
89+
* every time a node is registered.
90+
*/
91+
public final boolean trackNodeInsertion;
92+
8793
/**
8894
* The set of nodes in the graph, ordered by {@linkplain #register(Node) registration} time.
8995
*/
@@ -329,6 +335,8 @@ public Graph(String name, OptionValues options, DebugContext debug, boolean trac
329335

330336
verifyGraphs = Options.VerifyGraalGraphs.getValue(options);
331337
verifyGraphEdges = Options.VerifyGraalGraphEdges.getValue(options);
338+
339+
trackNodeInsertion = TrackNodeInsertion.getValue(options);
332340
}
333341

334342
int extractOriginalNodeId(Node node) {
@@ -1329,7 +1337,7 @@ void register(Node node) {
13291337
if (currentNodeSourcePosition != null && trackNodeSourcePosition()) {
13301338
node.setNodeSourcePosition(currentNodeSourcePosition);
13311339
}
1332-
if (TrackNodeInsertion.getValue(getOptions()) && node.getInsertionPosition() == null) {
1340+
if (trackNodeInsertion && node.getInsertionPosition() == null) {
13331341
node.setInsertionPosition(new NodeInsertionStackTrace());
13341342
}
13351343

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/GraphDecoder.java

Lines changed: 447 additions & 92 deletions
Large diffs are not rendered by default.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/SimplifyingGraphDecoder.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,7 @@
3838
import jdk.graal.compiler.debug.TimerKey;
3939
import jdk.graal.compiler.graph.Edges;
4040
import jdk.graal.compiler.graph.Node;
41+
import jdk.graal.compiler.graph.NodeBitMap;
4142
import jdk.graal.compiler.graph.NodeClass;
4243
import jdk.graal.compiler.nodeinfo.InputType;
4344
import jdk.graal.compiler.nodeinfo.NodeInfo;
@@ -149,22 +150,30 @@ protected void cleanupGraph(MethodScope methodScope) {
149150
GraphUtil.normalizeLoops(graph);
150151
super.cleanupGraph(methodScope);
151152

153+
/*
154+
* To avoid calling tryKillUnused for each individual floating node we remove during
155+
* cleanup, we maintain unused nodes in a bitmap and kill them after we finish iterating the
156+
* new nodes in the graph.
157+
*/
158+
final NodeBitMap unusedNodes = new NodeBitMap(graph);
159+
152160
for (Node node : graph.getNewNodes(methodScope.methodStartMark)) {
153-
if (node instanceof MergeNode) {
154-
MergeNode mergeNode = (MergeNode) node;
161+
if (node instanceof MergeNode mergeNode) {
155162
if (mergeNode.forwardEndCount() == 1) {
156-
graph.reduceTrivialMerge(mergeNode);
163+
graph.reduceTrivialMerge(mergeNode, false, unusedNodes);
157164
}
158165
} else if (node instanceof BeginNode) {
159166
if (!(node.predecessor() instanceof ControlSplitNode) && node.hasNoUsages()) {
160167
GraphUtil.unlinkFixedNode((AbstractBeginNode) node);
161168
node.safeDelete();
162169
}
170+
} else if (GraphUtil.shouldKillUnused(node)) {
171+
unusedNodes.mark(node);
163172
}
164173
}
165174

166-
for (Node node : graph.getNewNodes(methodScope.methodStartMark)) {
167-
GraphUtil.tryKillUnused(node);
175+
if (unusedNodes.isNotEmpty()) {
176+
GraphUtil.killAllWithUnusedFloatingInputs(unusedNodes, false);
168177
}
169178
}
170179

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/StructuredGraph.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
import jdk.graal.compiler.debug.TTY;
5353
import jdk.graal.compiler.graph.Graph;
5454
import jdk.graal.compiler.graph.Node;
55+
import jdk.graal.compiler.graph.NodeBitMap;
5556
import jdk.graal.compiler.graph.NodeMap;
5657
import jdk.graal.compiler.graph.NodeSourcePosition;
58+
import jdk.graal.compiler.graph.iterators.NodeIterable;
5759
import jdk.graal.compiler.nodes.GraphState.StageFlag;
5860
import jdk.graal.compiler.nodes.calc.FloatingNode;
5961
import jdk.graal.compiler.nodes.cfg.ControlFlowGraph;
@@ -1079,8 +1081,21 @@ public void reduceTrivialMerge(AbstractMergeNode merge) {
10791081
reduceTrivialMerge(merge, false);
10801082
}
10811083

1082-
@SuppressWarnings("static-method")
10831084
public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG) {
1085+
reduceTrivialMerge(merge, forKillCFG, null);
1086+
}
1087+
1088+
/**
1089+
* Removes merges with phis that have a single input value.
1090+
*
1091+
* @param unusedNodes A set to mark unused nodes in the graph that should be killed later by the
1092+
* caller. The set should be used when calling this method for multiple merges. The
1093+
* resulting set must be killed with
1094+
* {@link GraphUtil#killAllWithUnusedFloatingInputs(NodeIterable, boolean)}. If this
1095+
* set is null, the nodes are killed immediately.
1096+
*/
1097+
@SuppressWarnings("static-method")
1098+
public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG, NodeBitMap unusedNodes) {
10841099
assert merge.forwardEndCount() == 1 : Assertions.errorMessageContext("merge", merge);
10851100
assert !(merge instanceof LoopBeginNode) || ((LoopBeginNode) merge).loopEnds().isEmpty();
10861101
for (PhiNode phi : merge.phis().snapshot()) {
@@ -1091,7 +1106,11 @@ public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG) {
10911106
} else {
10921107
phi.safeDelete();
10931108
if (singleValue != null) {
1094-
GraphUtil.tryKillUnused(singleValue);
1109+
if (unusedNodes == null) {
1110+
GraphUtil.tryKillUnused(singleValue);
1111+
} else if (GraphUtil.shouldKillUnused(singleValue)) {
1112+
unusedNodes.mark(singleValue);
1113+
}
10951114
}
10961115
}
10971116
}
@@ -1106,7 +1125,11 @@ public void reduceTrivialMerge(AbstractMergeNode merge, boolean forKillCFG) {
11061125
merge.prepareDelete((FixedNode) singleEnd.predecessor());
11071126
merge.safeDelete();
11081127
if (stateAfter != null) {
1109-
GraphUtil.tryKillUnused(stateAfter);
1128+
if (unusedNodes == null) {
1129+
GraphUtil.tryKillUnused(stateAfter);
1130+
} else if (GraphUtil.shouldKillUnused(stateAfter)) {
1131+
unusedNodes.mark(stateAfter);
1132+
}
11101133
}
11111134
if (sux == null) {
11121135
singleEnd.replaceAtPredecessor(null);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/util/GraphUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1076,13 +1076,17 @@ private static ValueNode originalValueForComplicatedPhi(ValueNode value, PhiNode
10761076
}
10771077

10781078
public static boolean tryKillUnused(Node node) {
1079-
if (node.isAlive() && isFloatingNode(node) && node.hasNoUsages() && !(node instanceof GuardNode)) {
1079+
if (shouldKillUnused(node)) {
10801080
killWithUnusedFloatingInputs(node);
10811081
return true;
10821082
}
10831083
return false;
10841084
}
10851085

1086+
public static boolean shouldKillUnused(Node node) {
1087+
return node.isAlive() && isFloatingNode(node) && node.hasNoUsages() && !(node instanceof GuardNode);
1088+
}
1089+
10861090
/**
10871091
* Returns an iterator that will return the given node followed by all its predecessors, up
10881092
* until the point where {@link Node#predecessor()} returns null.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/PEGraphDecoder.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1279,9 +1279,7 @@ protected LoopScope doInline(PEMethodScope methodScope, LoopScope loopScope, Inv
12791279
* ParameterNodes.
12801280
*/
12811281
int firstArgumentNodeId = inlineScope.maxFixedNodeOrderId + 1;
1282-
for (int i = 0; i < arguments.length; i++) {
1283-
inlineLoopScope.createdNodes[firstArgumentNodeId + i] = arguments[i];
1284-
}
1282+
inlineLoopScope.setNodes(firstArgumentNodeId, arguments);
12851283

12861284
// Copy inlined methods from inlinee to caller
12871285
recordGraphElements(graphToInline);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2025, 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 jdk.graal.compiler.util;
26+
27+
import java.util.Arrays;
28+
29+
/**
30+
* Utility class for building dynamically sized int arrays.
31+
*/
32+
public class IntArrayBuilder {
33+
private static final int[] EMPTY_INT_ARRAY = {};
34+
35+
private int[] data;
36+
private int size;
37+
38+
public IntArrayBuilder() {
39+
this.data = new int[1];
40+
this.size = 0;
41+
}
42+
43+
private void ensureCapacity(int expectedSize) {
44+
if (expectedSize >= data.length) {
45+
final int nSize = Integer.highestOneBit(expectedSize) << 1;
46+
final int[] nData = new int[nSize];
47+
System.arraycopy(data, 0, nData, 0, size);
48+
data = nData;
49+
}
50+
}
51+
52+
public void set(int index, int value) {
53+
ensureCapacity(index);
54+
data[index] = value;
55+
size = Math.max(size, index + 1);
56+
}
57+
58+
public void add(int value) {
59+
ensureCapacity(size);
60+
data[size] = value;
61+
size++;
62+
}
63+
64+
/**
65+
* @return The int array generated by the builder. May return the underlying int array or a
66+
* correctly sized copy.
67+
*/
68+
public int[] toArray() {
69+
if (size == 0) {
70+
return EMPTY_INT_ARRAY;
71+
} else if (size == data.length) {
72+
return data;
73+
}
74+
return Arrays.copyOf(data, size);
75+
}
76+
}

0 commit comments

Comments
 (0)