43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
|
50858
|
24 |
|
|
25 |
|
43972
|
26 |
package org.graalvm.compiler.hotspot.test;
|
|
27 |
|
46344
|
28 |
import java.util.Arrays;
|
43972
|
29 |
|
|
30 |
import org.graalvm.compiler.bytecode.Bytecode;
|
46344
|
31 |
import org.graalvm.compiler.bytecode.BytecodeDisassembler;
|
43972
|
32 |
import org.graalvm.compiler.bytecode.BytecodeStream;
|
|
33 |
import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode;
|
49873
|
34 |
import org.graalvm.compiler.core.GraalCompilerOptions;
|
43972
|
35 |
import org.graalvm.compiler.core.target.Backend;
|
|
36 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
46640
|
37 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
38 |
import org.graalvm.compiler.debug.GraalError;
|
46344
|
39 |
import org.graalvm.compiler.debug.TTY;
|
43972
|
40 |
import org.graalvm.compiler.hotspot.CompilationTask;
|
47798
|
41 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
|
43972
|
42 |
import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
|
47798
|
43 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
|
43972
|
44 |
import org.graalvm.compiler.java.BciBlockMapping;
|
|
45 |
import org.graalvm.compiler.java.BciBlockMapping.BciBlock;
|
46640
|
46 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
43972
|
47 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
46344
|
48 |
import org.graalvm.compiler.options.OptionValues;
|
|
49 |
import org.junit.Assert;
|
43972
|
50 |
|
|
51 |
import jdk.vm.ci.code.Architecture;
|
|
52 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
|
|
53 |
import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
|
|
54 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
|
|
55 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
|
|
56 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
57 |
|
|
58 |
public abstract class GraalOSRTestBase extends GraalCompilerTest {
|
|
59 |
|
46344
|
60 |
protected void testOSR(OptionValues options, String methodName) {
|
|
61 |
testOSR(options, methodName, null);
|
43972
|
62 |
}
|
|
63 |
|
46344
|
64 |
protected void testOSR(OptionValues options, String methodName, Object receiver, Object... args) {
|
|
65 |
ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
|
|
66 |
testOSR(options, method, receiver, args);
|
|
67 |
}
|
|
68 |
|
|
69 |
protected void testOSR(OptionValues options, ResolvedJavaMethod method, Object receiver, Object... args) {
|
43972
|
70 |
// invalidate any existing compiled code
|
|
71 |
method.reprofile();
|
46344
|
72 |
compileOSR(options, method);
|
|
73 |
Result result = executeExpected(method, receiver, args);
|
43972
|
74 |
checkResult(result);
|
|
75 |
}
|
|
76 |
|
46640
|
77 |
protected static void compile(DebugContext debug, ResolvedJavaMethod method, int bci) {
|
50858
|
78 |
HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
|
43972
|
79 |
long jvmciEnv = 0L;
|
|
80 |
HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) method, bci, jvmciEnv);
|
|
81 |
HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
|
54601
|
82 |
CompilationTask task = new CompilationTask(runtime, compiler, request, true, true);
|
47798
|
83 |
if (method instanceof HotSpotResolvedJavaMethod) {
|
|
84 |
HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime();
|
|
85 |
GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
|
|
86 |
if (((HotSpotResolvedJavaMethod) method).hasCodeAtLevel(bci, config.compilationLevelFullOptimization)) {
|
|
87 |
return;
|
|
88 |
}
|
|
89 |
}
|
46640
|
90 |
HotSpotCompilationRequestResult result = task.runCompilation(debug);
|
43972
|
91 |
if (result.getFailure() != null) {
|
|
92 |
throw new GraalError(result.getFailureMessage());
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Returns the target BCI of the first bytecode backedge. This is where HotSpot triggers
|
|
98 |
* on-stack-replacement in case the backedge counter overflows.
|
|
99 |
*/
|
49873
|
100 |
static int getBackedgeBCI(DebugContext debug, ResolvedJavaMethod method) {
|
43972
|
101 |
Bytecode code = new ResolvedJavaMethodBytecode(method);
|
|
102 |
BytecodeStream stream = new BytecodeStream(code.getCode());
|
46640
|
103 |
OptionValues options = debug.getOptions();
|
|
104 |
BciBlockMapping bciBlockMapping = BciBlockMapping.create(stream, code, options, debug);
|
43972
|
105 |
|
|
106 |
for (BciBlock block : bciBlockMapping.getBlocks()) {
|
49873
|
107 |
if (block.getStartBci() != -1) {
|
|
108 |
int bci = block.getEndBci();
|
46344
|
109 |
for (BciBlock succ : block.getSuccessors()) {
|
49873
|
110 |
if (succ.getStartBci() != -1) {
|
|
111 |
int succBci = succ.getStartBci();
|
46344
|
112 |
if (succBci < bci) {
|
|
113 |
// back edge
|
|
114 |
return succBci;
|
|
115 |
}
|
|
116 |
}
|
43972
|
117 |
}
|
|
118 |
}
|
|
119 |
}
|
46344
|
120 |
TTY.println("Cannot find loop back edge with bytecode loops at:%s", Arrays.toString(bciBlockMapping.getLoopHeaders()));
|
|
121 |
TTY.println(new BytecodeDisassembler().disassemble(code));
|
43972
|
122 |
return -1;
|
|
123 |
}
|
|
124 |
|
49873
|
125 |
protected static void checkResult(Result result) {
|
43972
|
126 |
Assert.assertNull("Unexpected exception", result.exception);
|
|
127 |
Assert.assertNotNull(result.returnValue);
|
|
128 |
Assert.assertTrue(result.returnValue instanceof ReturnValue);
|
|
129 |
Assert.assertEquals(ReturnValue.SUCCESS, result.returnValue);
|
|
130 |
}
|
|
131 |
|
49873
|
132 |
protected void compileOSR(OptionValues options, ResolvedJavaMethod method) {
|
|
133 |
OptionValues goptions = options;
|
|
134 |
// Silence diagnostics for permanent bailout errors as they
|
|
135 |
// are expected for some OSR tests.
|
54204
|
136 |
if (!GraalCompilerOptions.CompilationBailoutAsFailure.hasBeenSet(options)) {
|
|
137 |
goptions = new OptionValues(options, GraalCompilerOptions.CompilationBailoutAsFailure, false);
|
49873
|
138 |
}
|
46344
|
139 |
// ensure eager resolving
|
49873
|
140 |
StructuredGraph graph = parseEager(method, AllowAssumptions.YES, goptions);
|
46640
|
141 |
DebugContext debug = graph.getDebug();
|
|
142 |
int bci = getBackedgeBCI(debug, method);
|
43972
|
143 |
assert bci != -1;
|
46640
|
144 |
compile(debug, method, bci);
|
43972
|
145 |
}
|
|
146 |
|
|
147 |
protected enum ReturnValue {
|
|
148 |
SUCCESS,
|
46344
|
149 |
FAILURE,
|
|
150 |
SIDE
|
43972
|
151 |
}
|
|
152 |
|
|
153 |
public GraalOSRTestBase() {
|
|
154 |
super();
|
|
155 |
}
|
|
156 |
|
|
157 |
public GraalOSRTestBase(Class<? extends Architecture> arch) {
|
|
158 |
super(arch);
|
|
159 |
}
|
|
160 |
|
|
161 |
public GraalOSRTestBase(Backend backend) {
|
|
162 |
super(backend);
|
|
163 |
}
|
|
164 |
|
|
165 |
}
|