Skip to content

Commit

Permalink
[GR-37550] Reduce memory footprint of graphs.
Browse files Browse the repository at this point in the history
PullRequest: graal/11435
  • Loading branch information
dougxc committed Apr 8, 2022
2 parents 5d35927 + 65a7232 commit 7b55044
Show file tree
Hide file tree
Showing 36 changed files with 759 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public static void runTest(InvariantsTool tool) {
verifiers.add(new VerifyUsageWithEquals(ArithmeticOpTable.class));
verifiers.add(new VerifyUsageWithEquals(ArithmeticOpTable.Op.class));

verifiers.add(new VerifySharedConstantEmptyArray());
verifiers.add(new VerifyDebugUsage());
verifiers.add(new VerifyVirtualizableUsage());
verifiers.add(new VerifyUpdateUsages());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.compiler.core.test;

import java.util.Set;

import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeInputList;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
import org.graalvm.compiler.nodes.java.NewArrayNode;
import org.graalvm.compiler.nodes.java.StoreFieldNode;
import org.graalvm.compiler.nodes.spi.CoreProviders;
import org.graalvm.compiler.nodes.util.GraphUtil;

import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;

/**
* Verifies that if a shared 0-length array constant is available for some type (e.g.
* {@link ValueNode#EMPTY_ARRAY}), then it is used everywhere such a value is needed.
*/
public class VerifySharedConstantEmptyArray extends VerifyStringFormatterUsage {

/**
* Names of static final fields in the Graal code base that defined shared 0-length arrays.
*/
private static final Set<String> NAMES = Set.of(
"EMPTY_ARRAY",
"EMPTY_PATTERNS",
"NO_NODES");

@Override
protected void verify(StructuredGraph graph, CoreProviders context) {
for (NewArrayNode t : graph.getNodes().filter(NewArrayNode.class)) {
checkNewArrayNode(graph, t);
}
}

private static void checkNewArrayNode(StructuredGraph graph, NewArrayNode t) {
ResolvedJavaMethod method = graph.method();
if (t.length().isDefaultConstant()) {
ResolvedJavaType elementType = t.elementType();
if (!checkSharedConstantDefinition(t, method, elementType)) {
for (ResolvedJavaField field : elementType.getStaticFields()) {
if (field.isFinal() && field.getType().isArray() && field.getType().getElementalType().equals(elementType) && NAMES.contains(field.getName())) {
if (isUsageVarargsParameter(t)) {
return;
}
throw new VerificationError("In %s use %s instead of `new %s[0]`%s", method.format("%H.%n(%p)"), field.format("%H.%n"), elementType.toJavaName(false), approxLocation(t));
}
}
}
}
}

/**
* Determines if {@code newZeroLengthArray} has a usage as a varargs parameter.
*/
private static boolean isUsageVarargsParameter(NewArrayNode newZeroLengthArray) {
for (Node usage : newZeroLengthArray.usages()) {
if (usage instanceof MethodCallTargetNode) {
MethodCallTargetNode target = (MethodCallTargetNode) usage;
ResolvedJavaMethod m = target.targetMethod();
if (m.isVarArgs()) {
NodeInputList<ValueNode> margs = target.arguments();
if (margs.last() == newZeroLengthArray) {
return true;
}
}
}
}
return false;
}

/**
* Checks if {@code newZeroLengthArray} in {@code method} is defining a shared constant and if
* so, that its name is in {@link #NAMES}.
*
* @return {@code true} if it is a shared constant definition
*/
private static boolean checkSharedConstantDefinition(NewArrayNode newZeroLengthArray, ResolvedJavaMethod method, ResolvedJavaType elementType) {
if (method.getDeclaringClass().equals(elementType) && method.getName().equals("<clinit>")) {
for (Node usage : newZeroLengthArray.usages()) {
if (usage instanceof StoreFieldNode) {
StoreFieldNode store = (StoreFieldNode) usage;
ResolvedJavaField f = store.field();
if (f.isStatic() && f.isFinal() && store.value() == newZeroLengthArray) {
if (!NAMES.contains(f.getName())) {
throw new VerificationError("%s appears to be a shared 0-length array constant - rename to EMPTY_ARRAY or add its name to %s.NAMES%s", f.format("%H.%n"),
VerifySharedConstantEmptyArray.class.getName(), approxLocation(newZeroLengthArray));
}
return true;
}
}
}
}
return false;
}

private static String approxLocation(NewArrayNode n) {
String loc = GraphUtil.approxSourceLocation(n);
return loc == null ? "" : String.format(" [approx location: %s]", loc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public String toString() {
*/
private final boolean ignoresSideEffects;

private static final MatchPattern[] EMPTY_PATTERNS = new MatchPattern[0];
private static final MatchPattern[] EMPTY_PATTERNS = {};

public MatchPattern(String name, boolean singleUser, boolean consumable, boolean ignoresSideEffects) {
this(null, name, singleUser, consumable, ignoresSideEffects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,12 +1061,12 @@ protected boolean compress(boolean minimizeSize) {

if (minimizeSize) {
/* Trim the array of all alive nodes itself. */
nodes = trimArrayToNewSize(nodes, nextId, NodeList.EMPTY_NODE_ARRAY);
nodes = trimArrayToNewSize(nodes, nextId, Node.EMPTY_ARRAY);
/* Remove deleted nodes from the linked list of Node.typeCacheNext. */
recomputeIterableNodeLists();
/* Trim node arrays used within each node. */
for (Node node : nodes) {
node.extraUsages = trimArrayToNewSize(node.extraUsages, node.extraUsagesCount, NodeList.EMPTY_NODE_ARRAY);
node.extraUsages = trimArrayToNewSize(node.extraUsages, node.extraUsagesCount, Node.EMPTY_ARRAY);
node.getNodeClass().getInputEdges().minimizeSize(node);
node.getNodeClass().getSuccessorEdges().minimizeSize(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public interface IndirectCanonicalization {
Node typeCacheNext;

static final int INLINE_USAGE_COUNT = 2;
private static final Node[] NO_NODES = {};
static final Node[] EMPTY_ARRAY = {};

/**
* Head of usage list (i.e. list of nodes that have {@code this} as an input). Note that each
Expand Down Expand Up @@ -315,7 +315,7 @@ final void init(NodeClass<? extends Node> c) {
assert c.getJavaClass() == this.getClass();
this.nodeClass = c;
id = INITIAL_ID;
extraUsages = NO_NODES;
extraUsages = EMPTY_ARRAY;
if (TRACK_CREATION_POSITION) {
setCreationPosition(new NodeCreationStackTrace());
}
Expand Down Expand Up @@ -937,7 +937,7 @@ public final void replaceAtAllUsages(Node replacement, boolean forDeletion) {
Node usage = extraUsages[i];
replaceAtUsage(replacement, forDeletion, usage);
}
this.extraUsages = NO_NODES;
this.extraUsages = EMPTY_ARRAY;
this.extraUsagesCount = 0;
}

Expand Down Expand Up @@ -1337,7 +1337,7 @@ final Node clone(Graph into, EnumSet<Edges.Type> edgesToCopy) {
if (into != null) {
into.register(newNode);
}
newNode.extraUsages = NO_NODES;
newNode.extraUsages = EMPTY_ARRAY;

if (into != null && useIntoLeafNodeCache) {
into.putNodeIntoCache(newNode);
Expand Down Expand Up @@ -1490,7 +1490,13 @@ public final Map<Object, Object> getDebugProperties() {
public Map<Object, Object> getDebugProperties(Map<Object, Object> map) {
Fields properties = getNodeClass().getData();
for (int i = 0; i < properties.getCount(); i++) {
map.put(properties.getName(i), properties.get(this, i));
Object value = properties.get(this, i);
if (properties.getType(i) == Character.TYPE) {
// Convert a char to an int as chars are not guaranteed to printable/viewable
char ch = (char) value;
value = Integer.valueOf(ch);
}
map.put(properties.getName(i), value);
}
NodeSourcePosition pos = getNodeSourcePosition();
if (pos != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public abstract class NodeList<T extends Node> extends AbstractList<T> implement
*/
private static final int MAX_ENTRIES = 65536;

protected static final Node[] EMPTY_NODE_ARRAY = new Node[0];

protected final Node self;
/**
* The array that stores the contents of this node list. We over-allocate when adding nodes, and
Expand All @@ -67,7 +65,7 @@ public abstract class NodeList<T extends Node> extends AbstractList<T> implement

protected NodeList(Node self) {
this.self = self;
this.nodes = EMPTY_NODE_ARRAY;
this.nodes = Node.EMPTY_ARRAY;
this.initialSize = 0;
}

Expand All @@ -77,7 +75,7 @@ protected NodeList(Node self, int initialSize) {
this.size = initialSize;
this.initialSize = initialSize;
if (initialSize == 0) {
this.nodes = EMPTY_NODE_ARRAY;
this.nodes = Node.EMPTY_ARRAY;
} else {
this.nodes = new Node[initialSize];
}
Expand All @@ -87,7 +85,7 @@ protected NodeList(Node self, T[] elements) {
this.self = self;
if (elements == null || elements.length == 0) {
this.size = 0;
this.nodes = EMPTY_NODE_ARRAY;
this.nodes = Node.EMPTY_ARRAY;
this.initialSize = 0;
} else {
checkMaxSize(elements.length);
Expand All @@ -105,7 +103,7 @@ protected NodeList(Node self, List<? extends T> elements) {
this.self = self;
if (elements == null || elements.isEmpty()) {
this.size = 0;
this.nodes = EMPTY_NODE_ARRAY;
this.nodes = Node.EMPTY_ARRAY;
this.initialSize = 0;
} else {
int newSize = elements.size();
Expand Down Expand Up @@ -238,7 +236,7 @@ void copy(NodeList<? extends Node> other) {
incModCount();
Node[] newNodes;
if (other.size == 0) {
newNodes = EMPTY_NODE_ARRAY;
newNodes = Node.EMPTY_ARRAY;
} else {
newNodes = new Node[other.size];
System.arraycopy(other.nodes, 0, newNodes, 0, newNodes.length);
Expand Down Expand Up @@ -284,12 +282,12 @@ public void clear() {
}

void clearWithoutUpdate() {
nodes = EMPTY_NODE_ARRAY;
nodes = Node.EMPTY_ARRAY;
size = 0;
}

void minimizeSize() {
nodes = Graph.trimArrayToNewSize(nodes, size, EMPTY_NODE_ARRAY);
nodes = Graph.trimArrayToNewSize(nodes, size, Node.EMPTY_ARRAY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ protected void finalizeGraph(StructuredGraph graph) {
if (substitutedMethod.equals(target.targetMethod())) {
// Replace call to original method with a placeholder
PartialIntrinsicCallTargetNode partial = graph.add(
new PartialIntrinsicCallTargetNode(target.invokeKind(), substitutedMethod, target.returnStamp(), target.arguments().toArray(new ValueNode[0])));
new PartialIntrinsicCallTargetNode(target.invokeKind(), substitutedMethod, target.returnStamp(), target.arguments().toArray(ValueNode.EMPTY_ARRAY)));
target.replaceAndDelete(partial);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ private void checkBalancedMonitors(StructuredGraph graph, LoweringTool tool) {
// Only insert the nodes if this is the first monitorenter being lowered.
JavaType returnType = initCounter.getMethod().getSignature().getReturnType(initCounter.getMethod().getDeclaringClass());
StampPair returnStamp = StampFactory.forDeclaredType(graph.getAssumptions(), returnType, false);
MethodCallTargetNode callTarget = graph.add(new MethodCallTargetNode(InvokeKind.Static, initCounter.getMethod(), new ValueNode[0], returnStamp, null));
MethodCallTargetNode callTarget = graph.add(new MethodCallTargetNode(InvokeKind.Static, initCounter.getMethod(), ValueNode.EMPTY_ARRAY, returnStamp, null));
InvokeNode invoke = graph.add(new InvokeNode(callTarget, 0));
invoke.setStateAfter(graph.start().stateAfter());
graph.addAfterFixed(graph.start(), invoke);
Expand All @@ -899,7 +899,8 @@ private void checkBalancedMonitors(StructuredGraph graph, LoweringTool tool) {
callTarget = graph.add(new MethodCallTargetNode(InvokeKind.Static, checkCounter.getMethod(), new ValueNode[]{errMsg}, returnStamp, null));
invoke = graph.add(new InvokeNode(callTarget, 0));
Bytecode code = new ResolvedJavaMethodBytecode(graph.method());
FrameState stateAfter = new FrameState(null, code, BytecodeFrame.AFTER_BCI, new ValueNode[0], new ValueNode[0], 0, null, null, new ValueNode[0], null, false, false);
FrameState stateAfter = new FrameState(null, code, BytecodeFrame.AFTER_BCI, ValueNode.EMPTY_ARRAY, ValueNode.EMPTY_ARRAY, 0, null, null, ValueNode.EMPTY_ARRAY, null, false,
false);
invoke.setStateAfter(graph.add(stateAfter));
graph.addBeforeFixed(ret, invoke);

Expand Down
Loading

0 comments on commit 7b55044

Please sign in to comment.