46344
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.core.test.inlining;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Optional;
|
|
26 |
|
|
27 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
|
28 |
import org.graalvm.compiler.debug.Debug;
|
|
29 |
import org.graalvm.compiler.debug.TTY;
|
|
30 |
import org.graalvm.compiler.graph.Node;
|
|
31 |
import org.graalvm.compiler.nodes.Invoke;
|
|
32 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
33 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
34 |
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
|
|
35 |
import org.graalvm.compiler.phases.BasePhase;
|
|
36 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
37 |
import org.graalvm.compiler.phases.PhaseSuite;
|
|
38 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
39 |
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
|
|
40 |
import org.graalvm.compiler.phases.common.inlining.InliningUtil;
|
|
41 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
42 |
import org.graalvm.compiler.phases.tiers.PhaseContext;
|
|
43 |
import org.graalvm.compiler.virtual.phases.ea.EarlyReadEliminationPhase;
|
|
44 |
import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
|
|
45 |
import org.graalvm.util.EconomicSet;
|
|
46 |
import org.junit.Test;
|
|
47 |
|
|
48 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
49 |
import sun.misc.Unsafe;
|
|
50 |
|
|
51 |
public class NestedLoopEffectsPhaseComplexityTest extends GraalCompilerTest {
|
|
52 |
|
|
53 |
public static int IntSideEffect;
|
|
54 |
public static int[] Memory = new int[]{0};
|
|
55 |
|
|
56 |
public static void recursiveLoopMethodUnsafeLoad(int a) {
|
|
57 |
if (UNSAFE.getInt(Memory, (long) Unsafe.ARRAY_INT_BASE_OFFSET) == 0) {
|
|
58 |
return;
|
|
59 |
}
|
|
60 |
for (int i = 0; i < a; i++) {
|
|
61 |
recursiveLoopMethodUnsafeLoad(i);
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
public static void recursiveLoopMethodFieldLoad(int a) {
|
|
66 |
if (IntSideEffect == 0) {
|
|
67 |
return;
|
|
68 |
}
|
|
69 |
for (int i = 0; i < a; i++) {
|
|
70 |
recursiveLoopMethodFieldLoad(i);
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
|
74 |
public static void recursiveLoopMethod(int a) {
|
|
75 |
if (a == 0) {
|
|
76 |
return;
|
|
77 |
}
|
|
78 |
for (int i = 0; i < a; i++) {
|
|
79 |
recursiveLoopMethod(i);
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
private static final boolean LOG_PHASE_TIMINGS = false;
|
|
84 |
private static int InliningCountLowerBound = 1;
|
|
85 |
private static int InliningCountUpperBound = 32;
|
|
86 |
|
|
87 |
@Test(timeout = 120_000)
|
|
88 |
public void inlineDirectRecursiveLoopCallUnsafeLoad() {
|
|
89 |
testAndTime("recursiveLoopMethodUnsafeLoad");
|
|
90 |
}
|
|
91 |
|
|
92 |
@Test(timeout = 120_000)
|
|
93 |
public void inlineDirectRecursiveLoopCallFieldLoad() {
|
|
94 |
testAndTime("recursiveLoopMethodFieldLoad");
|
|
95 |
}
|
|
96 |
|
|
97 |
@Test(timeout = 120_000)
|
|
98 |
public void inlineDirectRecursiveLoopCallNoReads() {
|
|
99 |
testAndTime("recursiveLoopMethod");
|
|
100 |
}
|
|
101 |
|
|
102 |
private void testAndTime(String snippet) {
|
|
103 |
initializeForTimeout();
|
|
104 |
for (int i = InliningCountLowerBound; i < InliningCountUpperBound; i++) {
|
|
105 |
StructuredGraph g1 = prepareGraph(snippet, i);
|
|
106 |
StructuredGraph g2 = (StructuredGraph) g1.copy();
|
|
107 |
ResolvedJavaMethod method = g1.method();
|
|
108 |
long elapsedRE = runAndTimePhase(g1, new EarlyReadEliminationPhase(new CanonicalizerPhase()));
|
|
109 |
long elapsedPEA = runAndTimePhase(g2, new PartialEscapePhase(true, new CanonicalizerPhase(), g1.getOptions()));
|
|
110 |
if (LOG_PHASE_TIMINGS) {
|
|
111 |
TTY.printf("Needed %dms to run early partial escape analysis on a graph with %d nested loops compiling method %s\n", elapsedPEA, i, method);
|
|
112 |
}
|
|
113 |
if (LOG_PHASE_TIMINGS) {
|
|
114 |
TTY.printf("Needed %dms to run early read elimination on a graph with %d nested loops compiling method %s\n", elapsedRE, i, method);
|
|
115 |
}
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
private long runAndTimePhase(StructuredGraph g, BasePhase<? super PhaseContext> phase) {
|
|
120 |
HighTierContext context = getDefaultHighTierContext();
|
|
121 |
long start = System.currentTimeMillis();
|
|
122 |
phase.apply(g, context);
|
|
123 |
long end = System.currentTimeMillis();
|
46371
|
124 |
Debug.dump(Debug.DETAILED_LEVEL, g, "After %s", phase.contractorName());
|
46344
|
125 |
return end - start;
|
|
126 |
}
|
|
127 |
|
|
128 |
private StructuredGraph prepareGraph(String snippet, int inliningCount) {
|
|
129 |
ResolvedJavaMethod callerMethod = getResolvedJavaMethod(snippet);
|
|
130 |
StructuredGraph callerGraph = parseEager(callerMethod, AllowAssumptions.YES);
|
|
131 |
PhaseSuite<HighTierContext> graphBuilderSuite = getDefaultGraphBuilderSuite();
|
|
132 |
HighTierContext context = new HighTierContext(getProviders(), graphBuilderSuite, OptimisticOptimizations.ALL);
|
|
133 |
CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
|
|
134 |
Invoke next = callerGraph.getNodes(MethodCallTargetNode.TYPE).first().invoke();
|
|
135 |
StructuredGraph calleeGraph = parseBytecodes(next.callTarget().targetMethod(), context, canonicalizer);
|
|
136 |
ResolvedJavaMethod calleeMethod = next.callTarget().targetMethod();
|
|
137 |
for (int i = 0; i < inliningCount; i++) {
|
|
138 |
next = callerGraph.getNodes(MethodCallTargetNode.TYPE).first().invoke();
|
|
139 |
EconomicSet<Node> canonicalizeNodes = InliningUtil.inlineForCanonicalization(next, calleeGraph, false, calleeMethod);
|
|
140 |
canonicalizer.applyIncremental(callerGraph, context, canonicalizeNodes);
|
46371
|
141 |
Debug.dump(Debug.DETAILED_LEVEL, callerGraph, "After inlining %s into %s iteration %d", calleeMethod, callerMethod, i);
|
46344
|
142 |
}
|
|
143 |
return callerGraph;
|
|
144 |
}
|
|
145 |
|
|
146 |
private static StructuredGraph parseBytecodes(ResolvedJavaMethod method, HighTierContext context, CanonicalizerPhase canonicalizer) {
|
|
147 |
StructuredGraph newGraph = new StructuredGraph.Builder(getInitialOptions(), AllowAssumptions.NO).method(method).build();
|
|
148 |
context.getGraphBuilderSuite().apply(newGraph, context);
|
|
149 |
new DeadCodeEliminationPhase(Optional).apply(newGraph);
|
|
150 |
canonicalizer.apply(newGraph, context);
|
|
151 |
return newGraph;
|
|
152 |
}
|
|
153 |
|
|
154 |
}
|