48190
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 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.test.SubprocessUtil.getVMCommandLine;
|
|
26 |
import static org.graalvm.compiler.test.SubprocessUtil.java;
|
|
27 |
import static org.graalvm.compiler.test.SubprocessUtil.withoutDebuggerArguments;
|
|
28 |
|
|
29 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
30 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
|
31 |
import org.graalvm.compiler.debug.DebugContext;
|
|
32 |
import org.graalvm.compiler.debug.DebugDumpScope;
|
|
33 |
import org.graalvm.compiler.graph.Node;
|
|
34 |
import org.graalvm.compiler.nodes.DeoptimizeNode;
|
|
35 |
import org.graalvm.compiler.nodes.InvokeNode;
|
|
36 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
37 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
38 |
import org.graalvm.compiler.nodes.StructuredGraph.Builder;
|
|
39 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
|
|
40 |
import org.graalvm.compiler.nodes.java.TypeSwitchNode;
|
|
41 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
42 |
import org.graalvm.compiler.phases.PhaseSuite;
|
|
43 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
44 |
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
|
|
45 |
import org.graalvm.compiler.phases.common.inlining.InliningPhase;
|
|
46 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
47 |
import org.graalvm.compiler.test.SubprocessUtil;
|
|
48 |
import org.junit.Assert;
|
|
49 |
import org.junit.Test;
|
|
50 |
|
|
51 |
import java.io.IOException;
|
|
52 |
import java.util.List;
|
|
53 |
|
|
54 |
public class PolymorphicInliningTest extends GraalCompilerTest {
|
|
55 |
|
|
56 |
@Test
|
|
57 |
public void testInSubprocess() throws InterruptedException, IOException {
|
|
58 |
String recursionPropName = getClass().getName() + ".recursion";
|
|
59 |
if (Boolean.getBoolean(recursionPropName)) {
|
|
60 |
testPolymorphicInlining();
|
|
61 |
testPolymorphicNotInlining();
|
|
62 |
testMegamorphicInlining();
|
|
63 |
testMegamorphicNotInlining();
|
|
64 |
} else {
|
|
65 |
List<String> vmArgs = withoutDebuggerArguments(getVMCommandLine());
|
|
66 |
NotInlinableSubClass.class.getCanonicalName();
|
|
67 |
vmArgs.add("-XX:CompileCommand=dontinline,org/graalvm/compiler/core/test/inlining/PolymorphicInliningTest$NotInlinableSubClass.publicOverriddenMethod");
|
|
68 |
vmArgs.add("-D" + recursionPropName + "=true");
|
|
69 |
SubprocessUtil.Subprocess proc = java(vmArgs, "com.oracle.mxtool.junit.MxJUnitWrapper", getClass().getName());
|
|
70 |
if (proc.exitCode != 0) {
|
|
71 |
Assert.fail(String.format("non-zero exit code %d for command:%n%s", proc.exitCode, proc));
|
|
72 |
}
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
public int polymorphicCallsite(SuperClass receiver) {
|
|
77 |
return receiver.publicOverriddenMethod();
|
|
78 |
}
|
|
79 |
|
|
80 |
public void testPolymorphicInlining() {
|
|
81 |
for (int i = 0; i < 10000; i++) {
|
|
82 |
if (i % 2 == 0) {
|
|
83 |
polymorphicCallsite(Receivers.subClassA);
|
|
84 |
} else {
|
|
85 |
polymorphicCallsite(Receivers.subClassB);
|
|
86 |
}
|
|
87 |
}
|
|
88 |
StructuredGraph graph = getGraph("polymorphicCallsite", false);
|
|
89 |
// This callsite should be inlined with a TypeCheckedInliningViolated deoptimization.
|
|
90 |
assertTrue(getNodeCount(graph, InvokeNode.class) == 0);
|
|
91 |
assertTrue(getNodeCount(graph, TypeSwitchNode.class) == 1);
|
|
92 |
assertTrue(getNodeCount(graph, DeoptimizeNode.class) >= 1);
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* This snippet is identical to {@link #polymorphicCallsite(SuperClass)}, and is for avoiding
|
|
97 |
* interference of the receiver type profile from different unit tests.
|
|
98 |
*/
|
|
99 |
public int polymorphicCallsite1(SuperClass receiver) {
|
|
100 |
return receiver.publicOverriddenMethod();
|
|
101 |
}
|
|
102 |
|
|
103 |
public void testPolymorphicNotInlining() {
|
|
104 |
for (int i = 0; i < 10000; i++) {
|
|
105 |
if (i % 2 == 0) {
|
|
106 |
polymorphicCallsite1(Receivers.subClassA);
|
|
107 |
} else {
|
|
108 |
polymorphicCallsite1(Receivers.notInlinableSubClass);
|
|
109 |
}
|
|
110 |
}
|
|
111 |
StructuredGraph graph = getGraph("polymorphicCallsite1", false);
|
|
112 |
// This callsite should not be inlined due to one of the potential callee method is not
|
|
113 |
// inlinable.
|
|
114 |
assertTrue(getNodeCount(graph, InvokeNode.class) == 1);
|
|
115 |
assertTrue(getNodeCount(graph, TypeSwitchNode.class) == 0);
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* This snippet is identical to {@link #polymorphicCallsite(SuperClass)}, and is for avoiding
|
|
120 |
* interference of the receiver type profile from different unit tests.
|
|
121 |
*/
|
|
122 |
public int polymorphicCallsite2(SuperClass receiver) {
|
|
123 |
return receiver.publicOverriddenMethod();
|
|
124 |
}
|
|
125 |
|
|
126 |
public void testMegamorphicInlining() {
|
|
127 |
// Construct a receiver type profile that exceeds the max type width (by default 8 in JVMCI,
|
|
128 |
// specified by -XX:TypeProfileWidth).
|
|
129 |
for (int i = 0; i < 2000; i++) {
|
|
130 |
// Ensure the following receiver type is within the type profile.
|
|
131 |
polymorphicCallsite2(Receivers.subClassA);
|
|
132 |
}
|
|
133 |
for (int i = 0; i < 10000; i++) {
|
|
134 |
switch (i % 20) {
|
|
135 |
case 0:
|
|
136 |
case 1:
|
|
137 |
case 2:
|
|
138 |
case 3:
|
|
139 |
case 4:
|
|
140 |
case 5:
|
|
141 |
case 6:
|
|
142 |
case 7:
|
|
143 |
// Probability: 40%
|
|
144 |
// Ensure the probability is greater than
|
|
145 |
// GraalOptions.MegamorphicInliningMinMethodProbability (by default 0.33D);
|
|
146 |
polymorphicCallsite2(Receivers.subClassA);
|
|
147 |
break;
|
|
148 |
case 8:
|
|
149 |
polymorphicCallsite2(Receivers.subClassB);
|
|
150 |
break;
|
|
151 |
case 9:
|
|
152 |
polymorphicCallsite2(Receivers.subClassC);
|
|
153 |
break;
|
|
154 |
case 10:
|
|
155 |
polymorphicCallsite2(Receivers.subClassD);
|
|
156 |
break;
|
|
157 |
case 11:
|
|
158 |
polymorphicCallsite2(Receivers.subClassE);
|
|
159 |
break;
|
|
160 |
case 12:
|
|
161 |
polymorphicCallsite2(Receivers.subClassF);
|
|
162 |
break;
|
|
163 |
case 13:
|
|
164 |
polymorphicCallsite2(Receivers.subClassG);
|
|
165 |
break;
|
|
166 |
case 14:
|
|
167 |
polymorphicCallsite2(Receivers.subClassH);
|
|
168 |
break;
|
|
169 |
default:
|
|
170 |
// Probability: 25%
|
|
171 |
polymorphicCallsite2(Receivers.notInlinableSubClass);
|
|
172 |
break;
|
|
173 |
}
|
|
174 |
}
|
|
175 |
StructuredGraph graph = getGraph("polymorphicCallsite2", false);
|
|
176 |
// This callsite should be inlined with a fallback invocation.
|
|
177 |
assertTrue(getNodeCount(graph, InvokeNode.class) == 1);
|
|
178 |
assertTrue(getNodeCount(graph, TypeSwitchNode.class) == 1);
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* This snippet is identical to {@link #polymorphicCallsite(SuperClass)}, and is for avoiding
|
|
183 |
* interference of the receiver type profile from different unit tests.
|
|
184 |
*/
|
|
185 |
public int polymorphicCallsite3(SuperClass receiver) {
|
|
186 |
return receiver.publicOverriddenMethod();
|
|
187 |
}
|
|
188 |
|
|
189 |
public void testMegamorphicNotInlining() {
|
|
190 |
for (int i = 0; i < 10000; i++) {
|
|
191 |
switch (i % 10) {
|
|
192 |
case 0:
|
|
193 |
case 1:
|
|
194 |
polymorphicCallsite3(Receivers.subClassA);
|
|
195 |
break;
|
|
196 |
case 2:
|
|
197 |
polymorphicCallsite3(Receivers.subClassB);
|
|
198 |
break;
|
|
199 |
case 3:
|
|
200 |
polymorphicCallsite3(Receivers.subClassC);
|
|
201 |
break;
|
|
202 |
case 4:
|
|
203 |
polymorphicCallsite3(Receivers.subClassD);
|
|
204 |
break;
|
|
205 |
case 5:
|
|
206 |
polymorphicCallsite3(Receivers.subClassE);
|
|
207 |
break;
|
|
208 |
case 6:
|
|
209 |
polymorphicCallsite3(Receivers.subClassF);
|
|
210 |
break;
|
|
211 |
case 7:
|
|
212 |
polymorphicCallsite3(Receivers.subClassG);
|
|
213 |
break;
|
|
214 |
case 8:
|
|
215 |
polymorphicCallsite3(Receivers.subClassH);
|
|
216 |
break;
|
|
217 |
default:
|
|
218 |
polymorphicCallsite3(Receivers.notInlinableSubClass);
|
|
219 |
break;
|
|
220 |
}
|
|
221 |
}
|
|
222 |
StructuredGraph graph = getGraph("polymorphicCallsite3", false);
|
|
223 |
// This callsite should not be inlined due to non of the potential callee method exceeds the
|
|
224 |
// probability specified by GraalOptions.MegamorphicInliningMinMethodProbability.
|
|
225 |
assertTrue(getNodeCount(graph, InvokeNode.class) == 1);
|
|
226 |
assertTrue(getNodeCount(graph, TypeSwitchNode.class) == 0);
|
|
227 |
}
|
|
228 |
|
|
229 |
@SuppressWarnings("try")
|
|
230 |
private StructuredGraph getGraph(final String snippet, final boolean eagerInfopointMode) {
|
|
231 |
DebugContext debug = getDebugContext();
|
|
232 |
try (DebugContext.Scope s = debug.scope("InliningTest", new DebugDumpScope(snippet, true))) {
|
|
233 |
ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
|
|
234 |
Builder builder = builder(method, AllowAssumptions.YES, debug);
|
|
235 |
StructuredGraph graph = eagerInfopointMode ? parse(builder, getDebugGraphBuilderSuite()) : parse(builder, getEagerGraphBuilderSuite());
|
|
236 |
try (DebugContext.Scope s2 = debug.scope("Inlining", graph)) {
|
|
237 |
PhaseSuite<HighTierContext> graphBuilderSuite = eagerInfopointMode
|
|
238 |
? getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withFullInfopoints(true))
|
|
239 |
: getDefaultGraphBuilderSuite();
|
|
240 |
HighTierContext context = new HighTierContext(getProviders(), graphBuilderSuite, OptimisticOptimizations.ALL);
|
|
241 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
|
|
242 |
new CanonicalizerPhase().apply(graph, context);
|
|
243 |
new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
|
|
244 |
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
|
|
245 |
new CanonicalizerPhase().apply(graph, context);
|
|
246 |
new DeadCodeEliminationPhase().apply(graph);
|
|
247 |
return graph;
|
|
248 |
}
|
|
249 |
} catch (Throwable e) {
|
|
250 |
throw debug.handle(e);
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
private static int getNodeCount(StructuredGraph graph, Class<? extends Node> nodeClass) {
|
|
255 |
return graph.getNodes().filter(nodeClass).count();
|
|
256 |
}
|
|
257 |
|
|
258 |
private static final class Receivers {
|
|
259 |
static final SubClassA subClassA = new SubClassA();
|
|
260 |
static final SubClassB subClassB = new SubClassB();
|
|
261 |
static final SubClassC subClassC = new SubClassC();
|
|
262 |
static final SubClassD subClassD = new SubClassD();
|
|
263 |
static final SubClassE subClassE = new SubClassE();
|
|
264 |
static final SubClassF subClassF = new SubClassF();
|
|
265 |
static final SubClassG subClassG = new SubClassG();
|
|
266 |
static final SubClassH subClassH = new SubClassH();
|
|
267 |
|
|
268 |
static final NotInlinableSubClass notInlinableSubClass = new NotInlinableSubClass();
|
|
269 |
}
|
|
270 |
|
|
271 |
private abstract static class SuperClass {
|
|
272 |
|
|
273 |
public abstract int publicOverriddenMethod();
|
|
274 |
|
|
275 |
}
|
|
276 |
|
|
277 |
private static class SubClassA extends SuperClass {
|
|
278 |
|
|
279 |
@Override
|
|
280 |
public int publicOverriddenMethod() {
|
|
281 |
return 'A';
|
|
282 |
}
|
|
283 |
|
|
284 |
}
|
|
285 |
|
|
286 |
private static class SubClassB extends SuperClass {
|
|
287 |
|
|
288 |
@Override
|
|
289 |
public int publicOverriddenMethod() {
|
|
290 |
return 'B';
|
|
291 |
}
|
|
292 |
|
|
293 |
}
|
|
294 |
|
|
295 |
private static class SubClassC extends SuperClass {
|
|
296 |
|
|
297 |
@Override
|
|
298 |
public int publicOverriddenMethod() {
|
|
299 |
return 'C';
|
|
300 |
}
|
|
301 |
|
|
302 |
}
|
|
303 |
|
|
304 |
private static class SubClassD extends SuperClass {
|
|
305 |
|
|
306 |
@Override
|
|
307 |
public int publicOverriddenMethod() {
|
|
308 |
return 'D';
|
|
309 |
}
|
|
310 |
|
|
311 |
}
|
|
312 |
|
|
313 |
private static class SubClassE extends SuperClass {
|
|
314 |
|
|
315 |
@Override
|
|
316 |
public int publicOverriddenMethod() {
|
|
317 |
return 'E';
|
|
318 |
}
|
|
319 |
|
|
320 |
}
|
|
321 |
|
|
322 |
private static class SubClassF extends SuperClass {
|
|
323 |
|
|
324 |
@Override
|
|
325 |
public int publicOverriddenMethod() {
|
|
326 |
return 'F';
|
|
327 |
}
|
|
328 |
|
|
329 |
}
|
|
330 |
|
|
331 |
private static class SubClassG extends SuperClass {
|
|
332 |
|
|
333 |
@Override
|
|
334 |
public int publicOverriddenMethod() {
|
|
335 |
return 'G';
|
|
336 |
}
|
|
337 |
|
|
338 |
}
|
|
339 |
|
|
340 |
private static class SubClassH extends SuperClass {
|
|
341 |
|
|
342 |
@Override
|
|
343 |
public int publicOverriddenMethod() {
|
|
344 |
return 'H';
|
|
345 |
}
|
|
346 |
|
|
347 |
}
|
|
348 |
|
|
349 |
private static final class NotInlinableSubClass extends SuperClass {
|
|
350 |
|
|
351 |
@Override
|
|
352 |
public int publicOverriddenMethod() {
|
|
353 |
return 'X';
|
|
354 |
}
|
|
355 |
|
|
356 |
}
|
|
357 |
|
|
358 |
}
|