43972
|
1 |
/*
|
51436
|
2 |
* Copyright (c) 2011, 2018, 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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.core.test;
|
|
26 |
|
49451
|
27 |
import static java.lang.reflect.Modifier.isStatic;
|
46680
|
28 |
import static jdk.vm.ci.runtime.JVMCICompiler.INVOCATION_ENTRY_BCI;
|
43972
|
29 |
import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes;
|
|
30 |
import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.DO_NOT_INLINE_NO_EXCEPTION;
|
|
31 |
import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.DO_NOT_INLINE_WITH_EXCEPTION;
|
|
32 |
|
|
33 |
import java.lang.annotation.ElementType;
|
|
34 |
import java.lang.annotation.Retention;
|
|
35 |
import java.lang.annotation.RetentionPolicy;
|
|
36 |
import java.lang.annotation.Target;
|
46551
|
37 |
import java.lang.reflect.Constructor;
|
|
38 |
import java.lang.reflect.Executable;
|
43972
|
39 |
import java.lang.reflect.InvocationTargetException;
|
|
40 |
import java.lang.reflect.Method;
|
|
41 |
import java.util.ArrayList;
|
|
42 |
import java.util.Arrays;
|
46640
|
43 |
import java.util.Collection;
|
43972
|
44 |
import java.util.Collections;
|
|
45 |
import java.util.EnumMap;
|
58299
|
46 |
import java.util.HashMap;
|
43972
|
47 |
import java.util.List;
|
|
48 |
import java.util.ListIterator;
|
|
49 |
import java.util.Map;
|
|
50 |
import java.util.Set;
|
50609
|
51 |
import java.util.concurrent.ConcurrentHashMap;
|
43972
|
52 |
import java.util.function.Supplier;
|
|
53 |
|
|
54 |
import org.graalvm.compiler.api.directives.GraalDirectives;
|
|
55 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
56 |
import org.graalvm.compiler.api.test.Graal;
|
|
57 |
import org.graalvm.compiler.code.CompilationResult;
|
46680
|
58 |
import org.graalvm.compiler.core.CompilationPrinter;
|
43972
|
59 |
import org.graalvm.compiler.core.GraalCompiler;
|
|
60 |
import org.graalvm.compiler.core.GraalCompiler.Request;
|
|
61 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
|
62 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
63 |
import org.graalvm.compiler.core.target.Backend;
|
46640
|
64 |
import org.graalvm.compiler.debug.DebugContext;
|
|
65 |
import org.graalvm.compiler.debug.DebugDumpHandler;
|
46680
|
66 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
43972
|
67 |
import org.graalvm.compiler.debug.GraalError;
|
|
68 |
import org.graalvm.compiler.debug.TTY;
|
|
69 |
import org.graalvm.compiler.graph.Node;
|
|
70 |
import org.graalvm.compiler.graph.NodeClass;
|
|
71 |
import org.graalvm.compiler.graph.NodeMap;
|
|
72 |
import org.graalvm.compiler.java.BytecodeParser;
|
|
73 |
import org.graalvm.compiler.java.ComputeLoopFrequenciesClosure;
|
|
74 |
import org.graalvm.compiler.java.GraphBuilderPhase;
|
|
75 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
|
|
76 |
import org.graalvm.compiler.lir.phases.LIRSuites;
|
58299
|
77 |
import org.graalvm.compiler.loop.phases.ConvertDeoptimizeToGuardPhase;
|
43972
|
78 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
79 |
import org.graalvm.compiler.nodeinfo.NodeSize;
|
|
80 |
import org.graalvm.compiler.nodeinfo.Verbosity;
|
|
81 |
import org.graalvm.compiler.nodes.BreakpointNode;
|
46459
|
82 |
import org.graalvm.compiler.nodes.Cancellable;
|
43972
|
83 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
84 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
|
85 |
import org.graalvm.compiler.nodes.FrameState;
|
|
86 |
import org.graalvm.compiler.nodes.FullInfopointNode;
|
52910
|
87 |
import org.graalvm.compiler.nodes.Invoke;
|
43972
|
88 |
import org.graalvm.compiler.nodes.InvokeNode;
|
|
89 |
import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
|
46371
|
90 |
import org.graalvm.compiler.nodes.ParameterNode;
|
43972
|
91 |
import org.graalvm.compiler.nodes.ProxyNode;
|
|
92 |
import org.graalvm.compiler.nodes.ReturnNode;
|
|
93 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
94 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
46640
|
95 |
import org.graalvm.compiler.nodes.StructuredGraph.Builder;
|
43972
|
96 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
|
97 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
98 |
import org.graalvm.compiler.nodes.cfg.Block;
|
|
99 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
|
|
100 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
|
|
101 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
|
102 |
import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
|
|
103 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
|
|
104 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
|
46371
|
105 |
import org.graalvm.compiler.nodes.java.AccessFieldNode;
|
43972
|
106 |
import org.graalvm.compiler.nodes.spi.LoweringProvider;
|
|
107 |
import org.graalvm.compiler.nodes.spi.Replacements;
|
|
108 |
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
|
46344
|
109 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
110 |
import org.graalvm.compiler.phases.BasePhase;
|
|
111 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
112 |
import org.graalvm.compiler.phases.Phase;
|
|
113 |
import org.graalvm.compiler.phases.PhaseSuite;
|
|
114 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
52910
|
115 |
import org.graalvm.compiler.phases.common.inlining.InliningPhase;
|
|
116 |
import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
|
|
117 |
import org.graalvm.compiler.phases.common.inlining.policy.GreedyInliningPolicy;
|
43972
|
118 |
import org.graalvm.compiler.phases.schedule.SchedulePhase;
|
|
119 |
import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
|
|
120 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
46762
|
121 |
import org.graalvm.compiler.phases.tiers.MidTierContext;
|
43972
|
122 |
import org.graalvm.compiler.phases.tiers.Suites;
|
|
123 |
import org.graalvm.compiler.phases.tiers.TargetProvider;
|
|
124 |
import org.graalvm.compiler.phases.util.Providers;
|
46640
|
125 |
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
|
43972
|
126 |
import org.graalvm.compiler.runtime.RuntimeProvider;
|
46344
|
127 |
import org.graalvm.compiler.test.AddExports;
|
43972
|
128 |
import org.graalvm.compiler.test.GraalTest;
|
58299
|
129 |
import org.graalvm.compiler.test.ModuleSupport;
|
46344
|
130 |
import org.junit.After;
|
|
131 |
import org.junit.Assert;
|
58299
|
132 |
import org.junit.BeforeClass;
|
46344
|
133 |
import org.junit.Test;
|
|
134 |
import org.junit.internal.AssumptionViolatedException;
|
43972
|
135 |
|
|
136 |
import jdk.vm.ci.code.Architecture;
|
46344
|
137 |
import jdk.vm.ci.code.BailoutException;
|
43972
|
138 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
139 |
import jdk.vm.ci.code.InstalledCode;
|
|
140 |
import jdk.vm.ci.code.TargetDescription;
|
46371
|
141 |
import jdk.vm.ci.meta.Assumptions.Assumption;
|
43972
|
142 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
143 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
50609
|
144 |
import jdk.vm.ci.meta.JavaConstant;
|
43972
|
145 |
import jdk.vm.ci.meta.JavaKind;
|
|
146 |
import jdk.vm.ci.meta.JavaType;
|
|
147 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
148 |
import jdk.vm.ci.meta.ProfilingInfo;
|
|
149 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
150 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
151 |
import jdk.vm.ci.meta.SpeculationLog;
|
|
152 |
|
|
153 |
/**
|
55509
|
154 |
* Base class for compiler unit tests.
|
43972
|
155 |
* <p>
|
55509
|
156 |
* White box tests for compiler transformations use this pattern:
|
43972
|
157 |
* <ol>
|
46344
|
158 |
* <li>Create a graph by {@linkplain #parseEager parsing} a method.</li>
|
43972
|
159 |
* <li>Manually modify the graph (e.g. replace a parameter node with a constant).</li>
|
|
160 |
* <li>Apply a transformation to the graph.</li>
|
|
161 |
* <li>Assert that the transformed graph is equal to an expected graph.</li>
|
|
162 |
* </ol>
|
|
163 |
* <p>
|
|
164 |
* See {@link InvokeHintsTest} as an example of a white box test.
|
|
165 |
* <p>
|
|
166 |
* Black box tests use the {@link #test(String, Object...)} or
|
|
167 |
* {@link #testN(int, String, Object...)} to execute some method in the interpreter and compare its
|
|
168 |
* result against that produced by a Graal compiled version of the method.
|
|
169 |
* <p>
|
|
170 |
* These tests will be run by the {@code mx unittest} command.
|
|
171 |
*/
|
46459
|
172 |
@AddExports({"java.base/jdk.internal.org.objectweb.asm", "java.base/jdk.internal.org.objectweb.asm.tree"})
|
43972
|
173 |
public abstract class GraalCompilerTest extends GraalTest {
|
|
174 |
|
46344
|
175 |
/**
|
|
176 |
* Gets the initial option values provided by the Graal runtime. These are option values
|
|
177 |
* typically parsed from the command line.
|
|
178 |
*/
|
|
179 |
public static OptionValues getInitialOptions() {
|
|
180 |
return Graal.getRequiredCapability(OptionValues.class);
|
|
181 |
}
|
|
182 |
|
|
183 |
private static final int BAILOUT_RETRY_LIMIT = 1;
|
43972
|
184 |
private final Providers providers;
|
|
185 |
private final Backend backend;
|
46344
|
186 |
|
|
187 |
/**
|
|
188 |
* Representative class for the {@code java.base} module.
|
|
189 |
*/
|
|
190 |
public static final Class<?> JAVA_BASE = Class.class;
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Exports the package named {@code packageName} declared in {@code moduleMember}'s module to
|
|
194 |
* this object's module. This must be called before accessing packages that are no longer public
|
|
195 |
* as of JDK 9.
|
|
196 |
*/
|
|
197 |
protected final void exportPackage(Class<?> moduleMember, String packageName) {
|
58299
|
198 |
ModuleSupport.exportPackageTo(moduleMember, packageName, getClass());
|
46344
|
199 |
}
|
43972
|
200 |
|
|
201 |
/**
|
|
202 |
* Denotes a test method that must be inlined by the {@link BytecodeParser}.
|
|
203 |
*/
|
|
204 |
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
|
|
205 |
@Retention(RetentionPolicy.RUNTIME)
|
|
206 |
public @interface BytecodeParserForceInline {
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Denotes a test method that must never be inlined by the {@link BytecodeParser}.
|
|
211 |
*/
|
|
212 |
@Retention(RetentionPolicy.RUNTIME)
|
|
213 |
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
|
|
214 |
public @interface BytecodeParserNeverInline {
|
|
215 |
/**
|
|
216 |
* Specifies if the call should be implemented with {@link InvokeWithExceptionNode} instead
|
|
217 |
* of {@link InvokeNode}.
|
|
218 |
*/
|
|
219 |
boolean invokeWithException() default false;
|
|
220 |
}
|
|
221 |
|
|
222 |
/**
|
|
223 |
* Can be overridden by unit tests to verify properties of the graph.
|
|
224 |
*
|
|
225 |
* @param graph the graph at the end of HighTier
|
55509
|
226 |
* @throws AssertionError if the verification fails
|
43972
|
227 |
*/
|
55509
|
228 |
protected void checkHighTierGraph(StructuredGraph graph) {
|
43972
|
229 |
}
|
|
230 |
|
|
231 |
/**
|
|
232 |
* Can be overridden by unit tests to verify properties of the graph.
|
|
233 |
*
|
|
234 |
* @param graph the graph at the end of MidTier
|
55509
|
235 |
* @throws AssertionError if the verification fails
|
43972
|
236 |
*/
|
55509
|
237 |
protected void checkMidTierGraph(StructuredGraph graph) {
|
43972
|
238 |
}
|
|
239 |
|
|
240 |
/**
|
|
241 |
* Can be overridden by unit tests to verify properties of the graph.
|
|
242 |
*
|
|
243 |
* @param graph the graph at the end of LowTier
|
55509
|
244 |
* @throws AssertionError if the verification fails
|
43972
|
245 |
*/
|
55509
|
246 |
protected void checkLowTierGraph(StructuredGraph graph) {
|
43972
|
247 |
}
|
|
248 |
|
|
249 |
protected static void breakpoint() {
|
|
250 |
}
|
|
251 |
|
|
252 |
@SuppressWarnings("unused")
|
|
253 |
protected static void breakpoint(int arg0) {
|
|
254 |
}
|
|
255 |
|
|
256 |
protected static void shouldBeOptimizedAway() {
|
|
257 |
}
|
|
258 |
|
46344
|
259 |
protected Suites createSuites(OptionValues opts) {
|
|
260 |
Suites ret = backend.getSuites().getDefaultSuites(opts).copy();
|
43972
|
261 |
ListIterator<BasePhase<? super HighTierContext>> iter = ret.getHighTier().findPhase(ConvertDeoptimizeToGuardPhase.class, true);
|
|
262 |
if (iter == null) {
|
|
263 |
/*
|
|
264 |
* in the economy configuration, we don't have the ConvertDeoptimizeToGuard phase, so we
|
|
265 |
* just select the first CanonicalizerPhase in HighTier
|
|
266 |
*/
|
|
267 |
iter = ret.getHighTier().findPhase(CanonicalizerPhase.class);
|
|
268 |
}
|
|
269 |
iter.add(new Phase() {
|
|
270 |
|
|
271 |
@Override
|
|
272 |
protected void run(StructuredGraph graph) {
|
|
273 |
ComputeLoopFrequenciesClosure.compute(graph);
|
|
274 |
}
|
|
275 |
|
|
276 |
@Override
|
|
277 |
public float codeSizeIncrease() {
|
|
278 |
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
|
|
279 |
}
|
|
280 |
|
|
281 |
@Override
|
|
282 |
protected CharSequence getName() {
|
|
283 |
return "ComputeLoopFrequenciesPhase";
|
|
284 |
}
|
|
285 |
});
|
|
286 |
ret.getHighTier().appendPhase(new Phase() {
|
|
287 |
|
|
288 |
@Override
|
|
289 |
protected void run(StructuredGraph graph) {
|
55509
|
290 |
checkHighTierGraph(graph);
|
43972
|
291 |
}
|
|
292 |
|
|
293 |
@Override
|
|
294 |
public float codeSizeIncrease() {
|
|
295 |
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
|
|
296 |
}
|
|
297 |
|
|
298 |
@Override
|
|
299 |
protected CharSequence getName() {
|
|
300 |
return "CheckGraphPhase";
|
|
301 |
}
|
|
302 |
});
|
|
303 |
ret.getMidTier().appendPhase(new Phase() {
|
|
304 |
|
|
305 |
@Override
|
|
306 |
protected void run(StructuredGraph graph) {
|
55509
|
307 |
checkMidTierGraph(graph);
|
43972
|
308 |
}
|
|
309 |
|
|
310 |
@Override
|
|
311 |
public float codeSizeIncrease() {
|
|
312 |
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
|
|
313 |
}
|
|
314 |
|
|
315 |
@Override
|
|
316 |
protected CharSequence getName() {
|
|
317 |
return "CheckGraphPhase";
|
|
318 |
}
|
|
319 |
});
|
|
320 |
ret.getLowTier().appendPhase(new Phase() {
|
|
321 |
|
|
322 |
@Override
|
|
323 |
protected void run(StructuredGraph graph) {
|
55509
|
324 |
checkLowTierGraph(graph);
|
43972
|
325 |
}
|
|
326 |
|
|
327 |
@Override
|
|
328 |
public float codeSizeIncrease() {
|
|
329 |
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
|
|
330 |
}
|
|
331 |
|
|
332 |
@Override
|
|
333 |
protected CharSequence getName() {
|
|
334 |
return "CheckGraphPhase";
|
|
335 |
}
|
|
336 |
});
|
|
337 |
return ret;
|
|
338 |
}
|
|
339 |
|
46344
|
340 |
protected LIRSuites createLIRSuites(OptionValues opts) {
|
|
341 |
LIRSuites ret = backend.getSuites().getDefaultLIRSuites(opts).copy();
|
43972
|
342 |
return ret;
|
|
343 |
}
|
|
344 |
|
58299
|
345 |
private static final ThreadLocal<HashMap<ResolvedJavaMethod, InstalledCode>> cache = ThreadLocal.withInitial(HashMap::new);
|
|
346 |
|
|
347 |
@BeforeClass
|
|
348 |
public static void resetCache() {
|
|
349 |
cache.get().clear();
|
|
350 |
}
|
|
351 |
|
43972
|
352 |
public GraalCompilerTest() {
|
|
353 |
this.backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend();
|
|
354 |
this.providers = getBackend().getProviders();
|
|
355 |
}
|
|
356 |
|
|
357 |
/**
|
|
358 |
* Set up a test for a non-default backend. The test should check (via {@link #getBackend()} )
|
|
359 |
* whether the desired backend is available.
|
|
360 |
*
|
|
361 |
* @param arch the name of the desired backend architecture
|
|
362 |
*/
|
|
363 |
public GraalCompilerTest(Class<? extends Architecture> arch) {
|
|
364 |
RuntimeProvider runtime = Graal.getRequiredCapability(RuntimeProvider.class);
|
|
365 |
Backend b = runtime.getBackend(arch);
|
|
366 |
if (b != null) {
|
|
367 |
this.backend = b;
|
|
368 |
} else {
|
|
369 |
// Fall back to the default/host backend
|
|
370 |
this.backend = runtime.getHostBackend();
|
|
371 |
}
|
|
372 |
this.providers = backend.getProviders();
|
|
373 |
}
|
|
374 |
|
|
375 |
/**
|
|
376 |
* Set up a test for a non-default backend.
|
|
377 |
*
|
|
378 |
* @param backend the desired backend
|
|
379 |
*/
|
|
380 |
public GraalCompilerTest(Backend backend) {
|
|
381 |
this.backend = backend;
|
|
382 |
this.providers = backend.getProviders();
|
|
383 |
}
|
|
384 |
|
46640
|
385 |
@Override
|
43972
|
386 |
@After
|
|
387 |
public void afterTest() {
|
46459
|
388 |
if (invocationPluginExtensions != null) {
|
|
389 |
synchronized (this) {
|
|
390 |
if (invocationPluginExtensions != null) {
|
|
391 |
extendedInvocationPlugins.removeTestPlugins(invocationPluginExtensions);
|
|
392 |
extendedInvocationPlugins = null;
|
|
393 |
invocationPluginExtensions = null;
|
|
394 |
}
|
|
395 |
}
|
|
396 |
}
|
46640
|
397 |
super.afterTest();
|
|
398 |
}
|
|
399 |
|
|
400 |
/**
|
|
401 |
* Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if
|
|
402 |
* none currently exists. Debug contexts created by this method will have their
|
|
403 |
* {@link DebugDumpHandler}s closed in {@link #afterTest()}.
|
|
404 |
*/
|
|
405 |
protected DebugContext getDebugContext() {
|
47798
|
406 |
return getDebugContext(getInitialOptions(), null, null);
|
46640
|
407 |
}
|
|
408 |
|
|
409 |
@Override
|
|
410 |
protected Collection<DebugHandlersFactory> getDebugHandlersFactories() {
|
|
411 |
return Collections.singletonList(new GraalDebugHandlersFactory(getSnippetReflection()));
|
43972
|
412 |
}
|
|
413 |
|
|
414 |
protected void assertEquals(StructuredGraph expected, StructuredGraph graph) {
|
|
415 |
assertEquals(expected, graph, false, true);
|
|
416 |
}
|
|
417 |
|
|
418 |
protected int countUnusedConstants(StructuredGraph graph) {
|
|
419 |
int total = 0;
|
|
420 |
for (ConstantNode node : getConstantNodes(graph)) {
|
|
421 |
if (node.hasNoUsages()) {
|
|
422 |
total++;
|
|
423 |
}
|
|
424 |
}
|
|
425 |
return total;
|
|
426 |
}
|
|
427 |
|
|
428 |
protected int getNodeCountExcludingUnusedConstants(StructuredGraph graph) {
|
|
429 |
return graph.getNodeCount() - countUnusedConstants(graph);
|
|
430 |
}
|
|
431 |
|
|
432 |
protected void assertEquals(StructuredGraph expected, StructuredGraph graph, boolean excludeVirtual, boolean checkConstants) {
|
|
433 |
String expectedString = getCanonicalGraphString(expected, excludeVirtual, checkConstants);
|
|
434 |
String actualString = getCanonicalGraphString(graph, excludeVirtual, checkConstants);
|
|
435 |
String mismatchString = compareGraphStrings(expected, expectedString, graph, actualString);
|
|
436 |
|
|
437 |
if (!excludeVirtual && getNodeCountExcludingUnusedConstants(expected) != getNodeCountExcludingUnusedConstants(graph)) {
|
46640
|
438 |
expected.getDebug().dump(DebugContext.BASIC_LEVEL, expected, "Node count not matching - expected");
|
|
439 |
graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "Node count not matching - actual");
|
43972
|
440 |
Assert.fail("Graphs do not have the same number of nodes: " + expected.getNodeCount() + " vs. " + graph.getNodeCount() + "\n" + mismatchString);
|
|
441 |
}
|
|
442 |
if (!expectedString.equals(actualString)) {
|
46640
|
443 |
expected.getDebug().dump(DebugContext.BASIC_LEVEL, expected, "mismatching graphs - expected");
|
|
444 |
graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "mismatching graphs - actual");
|
43972
|
445 |
Assert.fail(mismatchString);
|
|
446 |
}
|
|
447 |
}
|
|
448 |
|
|
449 |
private static String compareGraphStrings(StructuredGraph expectedGraph, String expectedString, StructuredGraph actualGraph, String actualString) {
|
|
450 |
if (!expectedString.equals(actualString)) {
|
|
451 |
String[] expectedLines = expectedString.split("\n");
|
|
452 |
String[] actualLines = actualString.split("\n");
|
|
453 |
int diffIndex = -1;
|
|
454 |
int limit = Math.min(actualLines.length, expectedLines.length);
|
|
455 |
String marker = " <<<";
|
|
456 |
for (int i = 0; i < limit; i++) {
|
|
457 |
if (!expectedLines[i].equals(actualLines[i])) {
|
|
458 |
diffIndex = i;
|
|
459 |
break;
|
|
460 |
}
|
|
461 |
}
|
|
462 |
if (diffIndex == -1) {
|
|
463 |
// Prefix is the same so add some space after the prefix
|
|
464 |
diffIndex = limit;
|
|
465 |
if (actualLines.length == limit) {
|
|
466 |
actualLines = Arrays.copyOf(actualLines, limit + 1);
|
|
467 |
actualLines[diffIndex] = "";
|
|
468 |
} else {
|
|
469 |
assert expectedLines.length == limit;
|
|
470 |
expectedLines = Arrays.copyOf(expectedLines, limit + 1);
|
|
471 |
expectedLines[diffIndex] = "";
|
|
472 |
}
|
|
473 |
}
|
|
474 |
// Place a marker next to the first line that differs
|
|
475 |
expectedLines[diffIndex] = expectedLines[diffIndex] + marker;
|
|
476 |
actualLines[diffIndex] = actualLines[diffIndex] + marker;
|
|
477 |
String ediff = String.join("\n", expectedLines);
|
|
478 |
String adiff = String.join("\n", actualLines);
|
|
479 |
return "mismatch in graphs:\n========= expected (" + expectedGraph + ") =========\n" + ediff + "\n\n========= actual (" + actualGraph + ") =========\n" + adiff;
|
|
480 |
} else {
|
|
481 |
return "mismatch in graphs";
|
|
482 |
}
|
|
483 |
}
|
|
484 |
|
|
485 |
protected void assertOptimizedAway(StructuredGraph g) {
|
|
486 |
Assert.assertEquals(0, g.getNodes().filter(NotOptimizedNode.class).count());
|
|
487 |
}
|
|
488 |
|
|
489 |
protected void assertConstantReturn(StructuredGraph graph, int value) {
|
|
490 |
String graphString = getCanonicalGraphString(graph, false, true);
|
|
491 |
Assert.assertEquals("unexpected number of ReturnNodes: " + graphString, graph.getNodes(ReturnNode.TYPE).count(), 1);
|
|
492 |
ValueNode result = graph.getNodes(ReturnNode.TYPE).first().result();
|
|
493 |
Assert.assertTrue("unexpected ReturnNode result node: " + graphString, result.isConstant());
|
|
494 |
Assert.assertEquals("unexpected ReturnNode result kind: " + graphString, result.asJavaConstant().getJavaKind(), JavaKind.Int);
|
|
495 |
Assert.assertEquals("unexpected ReturnNode result: " + graphString, result.asJavaConstant().asInt(), value);
|
|
496 |
}
|
|
497 |
|
|
498 |
protected static String getCanonicalGraphString(StructuredGraph graph, boolean excludeVirtual, boolean checkConstants) {
|
|
499 |
SchedulePhase schedule = new SchedulePhase(SchedulingStrategy.EARLIEST);
|
|
500 |
schedule.apply(graph);
|
|
501 |
ScheduleResult scheduleResult = graph.getLastSchedule();
|
|
502 |
|
|
503 |
NodeMap<Integer> canonicalId = graph.createNodeMap();
|
|
504 |
int nextId = 0;
|
|
505 |
|
|
506 |
List<String> constantsLines = new ArrayList<>();
|
|
507 |
|
|
508 |
StringBuilder result = new StringBuilder();
|
|
509 |
for (Block block : scheduleResult.getCFG().getBlocks()) {
|
46371
|
510 |
result.append("Block ").append(block).append(' ');
|
43972
|
511 |
if (block == scheduleResult.getCFG().getStartBlock()) {
|
|
512 |
result.append("* ");
|
|
513 |
}
|
|
514 |
result.append("-> ");
|
|
515 |
for (Block succ : block.getSuccessors()) {
|
46371
|
516 |
result.append(succ).append(' ');
|
43972
|
517 |
}
|
46371
|
518 |
result.append('\n');
|
43972
|
519 |
for (Node node : scheduleResult.getBlockToNodesMap().get(block)) {
|
|
520 |
if (node instanceof ValueNode && node.isAlive()) {
|
46371
|
521 |
if (!excludeVirtual || !(node instanceof VirtualObjectNode || node instanceof ProxyNode || node instanceof FullInfopointNode || node instanceof ParameterNode)) {
|
43972
|
522 |
if (node instanceof ConstantNode) {
|
|
523 |
String name = checkConstants ? node.toString(Verbosity.Name) : node.getClass().getSimpleName();
|
46371
|
524 |
if (excludeVirtual) {
|
|
525 |
constantsLines.add(name);
|
|
526 |
} else {
|
|
527 |
constantsLines.add(name + " (" + filteredUsageCount(node) + ")");
|
|
528 |
}
|
43972
|
529 |
} else {
|
|
530 |
int id;
|
|
531 |
if (canonicalId.get(node) != null) {
|
|
532 |
id = canonicalId.get(node);
|
|
533 |
} else {
|
|
534 |
id = nextId++;
|
|
535 |
canonicalId.set(node, id);
|
|
536 |
}
|
|
537 |
String name = node.getClass().getSimpleName();
|
46371
|
538 |
result.append(" ").append(id).append('|').append(name);
|
|
539 |
if (node instanceof AccessFieldNode) {
|
|
540 |
result.append('#');
|
|
541 |
result.append(((AccessFieldNode) node).field());
|
|
542 |
}
|
|
543 |
if (!excludeVirtual) {
|
|
544 |
result.append(" (");
|
|
545 |
result.append(filteredUsageCount(node));
|
|
546 |
result.append(')');
|
|
547 |
}
|
|
548 |
result.append('\n');
|
43972
|
549 |
}
|
|
550 |
}
|
|
551 |
}
|
|
552 |
}
|
|
553 |
}
|
|
554 |
|
|
555 |
StringBuilder constantsLinesResult = new StringBuilder();
|
46371
|
556 |
constantsLinesResult.append(constantsLines.size()).append(" constants:\n");
|
43972
|
557 |
Collections.sort(constantsLines);
|
|
558 |
for (String s : constantsLines) {
|
|
559 |
constantsLinesResult.append(s);
|
46371
|
560 |
constantsLinesResult.append('\n');
|
43972
|
561 |
}
|
|
562 |
|
46371
|
563 |
return constantsLinesResult.toString() + result.toString();
|
43972
|
564 |
}
|
|
565 |
|
|
566 |
/**
|
|
567 |
* @return usage count excluding {@link FrameState} usages
|
|
568 |
*/
|
|
569 |
private static int filteredUsageCount(Node node) {
|
|
570 |
return node.usages().filter(n -> !(n instanceof FrameState)).count();
|
|
571 |
}
|
|
572 |
|
|
573 |
/**
|
|
574 |
* @param graph
|
|
575 |
* @return a scheduled textual dump of {@code graph} .
|
|
576 |
*/
|
|
577 |
protected static String getScheduledGraphString(StructuredGraph graph) {
|
48861
|
578 |
SchedulePhase schedule = new SchedulePhase(SchedulingStrategy.EARLIEST_WITH_GUARD_ORDER);
|
43972
|
579 |
schedule.apply(graph);
|
|
580 |
ScheduleResult scheduleResult = graph.getLastSchedule();
|
|
581 |
|
|
582 |
StringBuilder result = new StringBuilder();
|
|
583 |
Block[] blocks = scheduleResult.getCFG().getBlocks();
|
|
584 |
for (Block block : blocks) {
|
46371
|
585 |
result.append("Block ").append(block).append(' ');
|
43972
|
586 |
if (block == scheduleResult.getCFG().getStartBlock()) {
|
|
587 |
result.append("* ");
|
|
588 |
}
|
|
589 |
result.append("-> ");
|
|
590 |
for (Block succ : block.getSuccessors()) {
|
46371
|
591 |
result.append(succ).append(' ');
|
43972
|
592 |
}
|
46371
|
593 |
result.append('\n');
|
43972
|
594 |
for (Node node : scheduleResult.getBlockToNodesMap().get(block)) {
|
|
595 |
result.append(String.format("%1S\n", node));
|
|
596 |
}
|
|
597 |
}
|
|
598 |
return result.toString();
|
|
599 |
}
|
|
600 |
|
|
601 |
protected Backend getBackend() {
|
|
602 |
return backend;
|
|
603 |
}
|
|
604 |
|
|
605 |
protected final Providers getProviders() {
|
|
606 |
return providers;
|
|
607 |
}
|
|
608 |
|
|
609 |
protected HighTierContext getDefaultHighTierContext() {
|
52910
|
610 |
return new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), getOptimisticOptimizations());
|
43972
|
611 |
}
|
|
612 |
|
46762
|
613 |
protected MidTierContext getDefaultMidTierContext() {
|
52910
|
614 |
return new MidTierContext(getProviders(), getTargetProvider(), getOptimisticOptimizations(), null);
|
46762
|
615 |
}
|
|
616 |
|
43972
|
617 |
protected SnippetReflectionProvider getSnippetReflection() {
|
|
618 |
return Graal.getRequiredCapability(SnippetReflectionProvider.class);
|
|
619 |
}
|
|
620 |
|
|
621 |
protected TargetDescription getTarget() {
|
|
622 |
return getTargetProvider().getTarget();
|
|
623 |
}
|
|
624 |
|
|
625 |
protected TargetProvider getTargetProvider() {
|
|
626 |
return getBackend();
|
|
627 |
}
|
|
628 |
|
|
629 |
protected CodeCacheProvider getCodeCache() {
|
|
630 |
return getProviders().getCodeCache();
|
|
631 |
}
|
|
632 |
|
|
633 |
protected ConstantReflectionProvider getConstantReflection() {
|
|
634 |
return getProviders().getConstantReflection();
|
|
635 |
}
|
|
636 |
|
|
637 |
protected MetaAccessProvider getMetaAccess() {
|
|
638 |
return getProviders().getMetaAccess();
|
|
639 |
}
|
|
640 |
|
|
641 |
protected LoweringProvider getLowerer() {
|
|
642 |
return getProviders().getLowerer();
|
|
643 |
}
|
|
644 |
|
52910
|
645 |
protected final BasePhase<HighTierContext> createInliningPhase() {
|
|
646 |
return createInliningPhase(new CanonicalizerPhase());
|
|
647 |
}
|
|
648 |
|
|
649 |
protected BasePhase<HighTierContext> createInliningPhase(CanonicalizerPhase canonicalizer) {
|
|
650 |
return createInliningPhase(null, canonicalizer);
|
|
651 |
}
|
|
652 |
|
|
653 |
static class GreedyTestInliningPolicy extends GreedyInliningPolicy {
|
|
654 |
GreedyTestInliningPolicy(Map<Invoke, Double> hints) {
|
|
655 |
super(hints);
|
|
656 |
}
|
|
657 |
|
|
658 |
@Override
|
|
659 |
protected int previousLowLevelGraphSize(InlineInfo info) {
|
|
660 |
// Ignore previous compiles for tests
|
|
661 |
return 0;
|
|
662 |
}
|
|
663 |
}
|
|
664 |
|
|
665 |
protected BasePhase<HighTierContext> createInliningPhase(Map<Invoke, Double> hints, CanonicalizerPhase canonicalizer) {
|
|
666 |
return new InliningPhase(new GreedyTestInliningPolicy(hints), canonicalizer);
|
|
667 |
}
|
|
668 |
|
43972
|
669 |
protected CompilationIdentifier getCompilationId(ResolvedJavaMethod method) {
|
|
670 |
return getBackend().getCompilationIdentifier(method);
|
|
671 |
}
|
|
672 |
|
|
673 |
protected CompilationIdentifier getOrCreateCompilationId(final ResolvedJavaMethod installedCodeOwner, StructuredGraph graph) {
|
|
674 |
if (graph != null) {
|
|
675 |
return graph.compilationId();
|
|
676 |
}
|
|
677 |
return getCompilationId(installedCodeOwner);
|
|
678 |
}
|
|
679 |
|
|
680 |
protected void testN(int n, final String name, final Object... args) {
|
|
681 |
final List<Throwable> errors = new ArrayList<>(n);
|
|
682 |
Thread[] threads = new Thread[n];
|
|
683 |
for (int i = 0; i < n; i++) {
|
|
684 |
Thread t = new Thread(i + ":" + name) {
|
|
685 |
|
|
686 |
@Override
|
|
687 |
public void run() {
|
|
688 |
try {
|
|
689 |
test(name, args);
|
|
690 |
} catch (Throwable e) {
|
|
691 |
errors.add(e);
|
|
692 |
}
|
|
693 |
}
|
|
694 |
};
|
|
695 |
threads[i] = t;
|
|
696 |
t.start();
|
|
697 |
}
|
|
698 |
for (int i = 0; i < n; i++) {
|
|
699 |
try {
|
|
700 |
threads[i].join();
|
|
701 |
} catch (InterruptedException e) {
|
|
702 |
errors.add(e);
|
|
703 |
}
|
|
704 |
}
|
|
705 |
if (!errors.isEmpty()) {
|
|
706 |
throw new MultiCauseAssertionError(errors.size() + " failures", errors.toArray(new Throwable[errors.size()]));
|
|
707 |
}
|
|
708 |
}
|
|
709 |
|
46551
|
710 |
protected Object referenceInvoke(ResolvedJavaMethod method, Object receiver, Object... args)
|
|
711 |
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
|
43972
|
712 |
return invoke(method, receiver, args);
|
|
713 |
}
|
|
714 |
|
46344
|
715 |
public static class Result {
|
43972
|
716 |
|
|
717 |
public final Object returnValue;
|
|
718 |
public final Throwable exception;
|
|
719 |
|
|
720 |
public Result(Object returnValue, Throwable exception) {
|
|
721 |
this.returnValue = returnValue;
|
|
722 |
this.exception = exception;
|
|
723 |
}
|
|
724 |
|
|
725 |
@Override
|
|
726 |
public String toString() {
|
|
727 |
return exception == null ? returnValue == null ? "null" : returnValue.toString() : "!" + exception;
|
|
728 |
}
|
|
729 |
}
|
|
730 |
|
|
731 |
/**
|
|
732 |
* Called before a test is executed.
|
|
733 |
*/
|
|
734 |
protected void before(@SuppressWarnings("unused") ResolvedJavaMethod method) {
|
|
735 |
}
|
|
736 |
|
|
737 |
/**
|
|
738 |
* Called after a test is executed.
|
|
739 |
*/
|
|
740 |
protected void after() {
|
|
741 |
}
|
|
742 |
|
|
743 |
protected Result executeExpected(ResolvedJavaMethod method, Object receiver, Object... args) {
|
|
744 |
before(method);
|
|
745 |
try {
|
|
746 |
// This gives us both the expected return value as well as ensuring that the method to
|
|
747 |
// be compiled is fully resolved
|
|
748 |
return new Result(referenceInvoke(method, receiver, args), null);
|
|
749 |
} catch (InvocationTargetException e) {
|
|
750 |
return new Result(null, e.getTargetException());
|
|
751 |
} catch (Exception e) {
|
|
752 |
throw new RuntimeException(e);
|
|
753 |
} finally {
|
|
754 |
after();
|
|
755 |
}
|
|
756 |
}
|
|
757 |
|
|
758 |
protected Result executeActual(ResolvedJavaMethod method, Object receiver, Object... args) {
|
46344
|
759 |
return executeActual(getInitialOptions(), method, receiver, args);
|
|
760 |
}
|
|
761 |
|
|
762 |
protected Result executeActual(OptionValues options, ResolvedJavaMethod method, Object receiver, Object... args) {
|
43972
|
763 |
before(method);
|
|
764 |
Object[] executeArgs = argsWithReceiver(receiver, args);
|
|
765 |
|
|
766 |
checkArgs(method, executeArgs);
|
|
767 |
|
46344
|
768 |
InstalledCode compiledMethod = getCode(method, options);
|
43972
|
769 |
try {
|
|
770 |
return new Result(compiledMethod.executeVarargs(executeArgs), null);
|
|
771 |
} catch (Throwable e) {
|
|
772 |
return new Result(null, e);
|
|
773 |
} finally {
|
|
774 |
after();
|
|
775 |
}
|
|
776 |
}
|
|
777 |
|
|
778 |
protected void checkArgs(ResolvedJavaMethod method, Object[] args) {
|
|
779 |
JavaType[] sig = method.toParameterTypes();
|
|
780 |
Assert.assertEquals(sig.length, args.length);
|
|
781 |
for (int i = 0; i < args.length; i++) {
|
|
782 |
JavaType javaType = sig[i];
|
|
783 |
JavaKind kind = javaType.getJavaKind();
|
|
784 |
Object arg = args[i];
|
|
785 |
if (kind == JavaKind.Object) {
|
|
786 |
if (arg != null && javaType instanceof ResolvedJavaType) {
|
|
787 |
ResolvedJavaType resolvedJavaType = (ResolvedJavaType) javaType;
|
|
788 |
Assert.assertTrue(resolvedJavaType + " from " + getMetaAccess().lookupJavaType(arg.getClass()), resolvedJavaType.isAssignableFrom(getMetaAccess().lookupJavaType(arg.getClass())));
|
|
789 |
}
|
|
790 |
} else {
|
|
791 |
Assert.assertNotNull(arg);
|
|
792 |
Assert.assertEquals(kind.toBoxedJavaClass(), arg.getClass());
|
|
793 |
}
|
|
794 |
}
|
|
795 |
}
|
|
796 |
|
|
797 |
/**
|
|
798 |
* Prepends a non-null receiver argument to a given list or args.
|
|
799 |
*
|
|
800 |
* @param receiver the receiver argument to prepend if it is non-null
|
|
801 |
*/
|
|
802 |
protected Object[] argsWithReceiver(Object receiver, Object... args) {
|
|
803 |
Object[] executeArgs;
|
|
804 |
if (receiver == null) {
|
|
805 |
executeArgs = args;
|
|
806 |
} else {
|
|
807 |
executeArgs = new Object[args.length + 1];
|
|
808 |
executeArgs[0] = receiver;
|
|
809 |
for (int i = 0; i < args.length; i++) {
|
|
810 |
executeArgs[i + 1] = args[i];
|
|
811 |
}
|
|
812 |
}
|
|
813 |
return applyArgSuppliers(executeArgs);
|
|
814 |
}
|
|
815 |
|
46344
|
816 |
protected final Result test(String name, Object... args) {
|
|
817 |
return test(getInitialOptions(), name, args);
|
|
818 |
}
|
|
819 |
|
|
820 |
protected final Result test(OptionValues options, String name, Object... args) {
|
43972
|
821 |
try {
|
|
822 |
ResolvedJavaMethod method = getResolvedJavaMethod(name);
|
|
823 |
Object receiver = method.isStatic() ? null : this;
|
46344
|
824 |
return test(options, method, receiver, args);
|
43972
|
825 |
} catch (AssumptionViolatedException e) {
|
|
826 |
// Suppress so that subsequent calls to this method within the
|
|
827 |
// same Junit @Test annotated method can proceed.
|
46344
|
828 |
return null;
|
43972
|
829 |
}
|
|
830 |
}
|
|
831 |
|
|
832 |
/**
|
|
833 |
* Type denoting a lambda that supplies a fresh value each time it is called. This is useful
|
|
834 |
* when supplying an argument to {@link GraalCompilerTest#test(String, Object...)} where the
|
|
835 |
* test modifies the state of the argument (e.g., updates a field).
|
|
836 |
*/
|
|
837 |
@FunctionalInterface
|
|
838 |
public interface ArgSupplier extends Supplier<Object> {
|
|
839 |
}
|
|
840 |
|
|
841 |
/**
|
|
842 |
* Convenience method for using an {@link ArgSupplier} lambda in a varargs list.
|
|
843 |
*/
|
|
844 |
public static Object supply(ArgSupplier supplier) {
|
|
845 |
return supplier;
|
|
846 |
}
|
|
847 |
|
46344
|
848 |
protected Result test(ResolvedJavaMethod method, Object receiver, Object... args) {
|
|
849 |
return test(getInitialOptions(), method, receiver, args);
|
|
850 |
}
|
|
851 |
|
|
852 |
protected Result test(OptionValues options, ResolvedJavaMethod method, Object receiver, Object... args) {
|
43972
|
853 |
Result expect = executeExpected(method, receiver, args);
|
46344
|
854 |
if (getCodeCache() != null) {
|
|
855 |
testAgainstExpected(options, method, expect, receiver, args);
|
43972
|
856 |
}
|
46344
|
857 |
return expect;
|
43972
|
858 |
}
|
|
859 |
|
|
860 |
/**
|
|
861 |
* Process a given set of arguments, converting any {@link ArgSupplier} argument to the argument
|
|
862 |
* it supplies.
|
|
863 |
*/
|
|
864 |
protected Object[] applyArgSuppliers(Object... args) {
|
|
865 |
Object[] res = args;
|
|
866 |
for (int i = 0; i < args.length; i++) {
|
|
867 |
if (args[i] instanceof ArgSupplier) {
|
|
868 |
if (res == args) {
|
|
869 |
res = args.clone();
|
|
870 |
}
|
|
871 |
res[i] = ((ArgSupplier) args[i]).get();
|
|
872 |
}
|
|
873 |
}
|
|
874 |
return res;
|
|
875 |
}
|
|
876 |
|
46344
|
877 |
protected final void testAgainstExpected(ResolvedJavaMethod method, Result expect, Object receiver, Object... args) {
|
|
878 |
testAgainstExpected(getInitialOptions(), method, expect, Collections.<DeoptimizationReason> emptySet(), receiver, args);
|
|
879 |
}
|
|
880 |
|
|
881 |
protected void testAgainstExpected(ResolvedJavaMethod method, Result expect, Set<DeoptimizationReason> shouldNotDeopt, Object receiver, Object... args) {
|
|
882 |
testAgainstExpected(getInitialOptions(), method, expect, shouldNotDeopt, receiver, args);
|
43972
|
883 |
}
|
|
884 |
|
46344
|
885 |
protected final void testAgainstExpected(OptionValues options, ResolvedJavaMethod method, Result expect, Object receiver, Object... args) {
|
|
886 |
testAgainstExpected(options, method, expect, Collections.<DeoptimizationReason> emptySet(), receiver, args);
|
|
887 |
}
|
|
888 |
|
|
889 |
protected void testAgainstExpected(OptionValues options, ResolvedJavaMethod method, Result expect, Set<DeoptimizationReason> shouldNotDeopt, Object receiver, Object... args) {
|
|
890 |
Result actual = executeActualCheckDeopt(options, method, shouldNotDeopt, receiver, args);
|
|
891 |
assertEquals(expect, actual);
|
|
892 |
}
|
|
893 |
|
|
894 |
protected Result executeActualCheckDeopt(OptionValues options, ResolvedJavaMethod method, Set<DeoptimizationReason> shouldNotDeopt, Object receiver, Object... args) {
|
43972
|
895 |
Map<DeoptimizationReason, Integer> deoptCounts = new EnumMap<>(DeoptimizationReason.class);
|
|
896 |
ProfilingInfo profile = method.getProfilingInfo();
|
|
897 |
for (DeoptimizationReason reason : shouldNotDeopt) {
|
|
898 |
deoptCounts.put(reason, profile.getDeoptimizationCount(reason));
|
|
899 |
}
|
46344
|
900 |
Result actual = executeActual(options, method, receiver, args);
|
43972
|
901 |
profile = method.getProfilingInfo(); // profile can change after execution
|
|
902 |
for (DeoptimizationReason reason : shouldNotDeopt) {
|
47798
|
903 |
Assert.assertEquals("wrong number of deopt counts for " + reason, (int) deoptCounts.get(reason), profile.getDeoptimizationCount(reason));
|
43972
|
904 |
}
|
|
905 |
return actual;
|
|
906 |
}
|
|
907 |
|
54084
|
908 |
private static final List<Class<?>> C2_OMIT_STACK_TRACE_IN_FAST_THROW_EXCEPTIONS = Arrays.asList(
|
|
909 |
ArithmeticException.class,
|
|
910 |
ArrayIndexOutOfBoundsException.class,
|
|
911 |
ArrayStoreException.class,
|
|
912 |
ClassCastException.class,
|
|
913 |
NullPointerException.class);
|
|
914 |
|
43972
|
915 |
protected void assertEquals(Result expect, Result actual) {
|
|
916 |
if (expect.exception != null) {
|
|
917 |
Assert.assertTrue("expected " + expect.exception, actual.exception != null);
|
|
918 |
Assert.assertEquals("Exception class", expect.exception.getClass(), actual.exception.getClass());
|
54084
|
919 |
// C2 can optimize out the stack trace and message in some cases
|
|
920 |
if (expect.exception.getMessage() != null || !C2_OMIT_STACK_TRACE_IN_FAST_THROW_EXCEPTIONS.contains(expect.exception.getClass())) {
|
|
921 |
Assert.assertEquals("Exception message", expect.exception.getMessage(), actual.exception.getMessage());
|
|
922 |
}
|
43972
|
923 |
} else {
|
|
924 |
if (actual.exception != null) {
|
|
925 |
throw new AssertionError("expected " + expect.returnValue + " but got an exception", actual.exception);
|
|
926 |
}
|
|
927 |
assertDeepEquals(expect.returnValue, actual.returnValue);
|
|
928 |
}
|
|
929 |
}
|
|
930 |
|
|
931 |
/**
|
|
932 |
* Gets installed code for a given method, compiling it first if necessary. The graph is parsed
|
46344
|
933 |
* {@link #parseEager eagerly}.
|
43972
|
934 |
*/
|
46344
|
935 |
protected final InstalledCode getCode(ResolvedJavaMethod method) {
|
|
936 |
return getCode(method, null, false, false, getInitialOptions());
|
|
937 |
}
|
|
938 |
|
|
939 |
protected final InstalledCode getCode(ResolvedJavaMethod method, OptionValues options) {
|
|
940 |
return getCode(method, null, false, false, options);
|
43972
|
941 |
}
|
|
942 |
|
|
943 |
/**
|
|
944 |
* Gets installed code for a given method, compiling it first if necessary.
|
|
945 |
*
|
|
946 |
* @param installedCodeOwner the method the compiled code will be associated with when installed
|
|
947 |
* @param graph the graph to be compiled. If null, a graph will be obtained from
|
|
948 |
* {@code installedCodeOwner} via {@link #parseForCompile(ResolvedJavaMethod)}.
|
|
949 |
*/
|
46344
|
950 |
protected final InstalledCode getCode(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph) {
|
|
951 |
return getCode(installedCodeOwner, graph, false, false, graph == null ? getInitialOptions() : graph.getOptions());
|
43972
|
952 |
}
|
|
953 |
|
46344
|
954 |
/**
|
|
955 |
* Gets installed code for a given method and graph, compiling it first if necessary.
|
|
956 |
*
|
|
957 |
* @param installedCodeOwner the method the compiled code will be associated with when installed
|
|
958 |
* @param graph the graph to be compiled. If null, a graph will be obtained from
|
|
959 |
* {@code installedCodeOwner} via {@link #parseForCompile(ResolvedJavaMethod)}.
|
|
960 |
* @param forceCompile specifies whether to ignore any previous code cached for the (method,
|
|
961 |
* key) pair
|
|
962 |
*/
|
|
963 |
protected final InstalledCode getCode(final ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, boolean forceCompile) {
|
|
964 |
return getCode(installedCodeOwner, graph, forceCompile, false, graph == null ? getInitialOptions() : graph.getOptions());
|
43972
|
965 |
}
|
|
966 |
|
|
967 |
/**
|
|
968 |
* Gets installed code for a given method and graph, compiling it first if necessary.
|
|
969 |
*
|
|
970 |
* @param installedCodeOwner the method the compiled code will be associated with when installed
|
|
971 |
* @param graph the graph to be compiled. If null, a graph will be obtained from
|
|
972 |
* {@code installedCodeOwner} via {@link #parseForCompile(ResolvedJavaMethod)}.
|
|
973 |
* @param forceCompile specifies whether to ignore any previous code cached for the (method,
|
|
974 |
* key) pair
|
46344
|
975 |
* @param installAsDefault specifies whether to install as the default implementation
|
|
976 |
* @param options the options that will be used in {@link #parseForCompile(ResolvedJavaMethod)}
|
43972
|
977 |
*/
|
|
978 |
@SuppressWarnings("try")
|
46344
|
979 |
protected InstalledCode getCode(final ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, boolean forceCompile, boolean installAsDefault, OptionValues options) {
|
49451
|
980 |
boolean useCache = !forceCompile && getArgumentToBind() == null;
|
|
981 |
if (useCache && graph == null) {
|
58299
|
982 |
InstalledCode cached = cache.get().get(installedCodeOwner);
|
43972
|
983 |
if (cached != null) {
|
|
984 |
if (cached.isValid()) {
|
|
985 |
return cached;
|
|
986 |
}
|
|
987 |
}
|
|
988 |
}
|
46344
|
989 |
// loop for retrying compilation
|
|
990 |
for (int retry = 0; retry <= BAILOUT_RETRY_LIMIT; retry++) {
|
|
991 |
final CompilationIdentifier id = getOrCreateCompilationId(installedCodeOwner, graph);
|
43972
|
992 |
|
46344
|
993 |
InstalledCode installedCode = null;
|
46640
|
994 |
StructuredGraph graphToCompile = graph == null ? parseForCompile(installedCodeOwner, id, options) : graph;
|
|
995 |
DebugContext debug = graphToCompile.getDebug();
|
46807
|
996 |
|
58299
|
997 |
try (AllocSpy spy = AllocSpy.open(installedCodeOwner); DebugContext.Scope ds = debug.scope("Compiling", graph)) {
|
46680
|
998 |
CompilationPrinter printer = CompilationPrinter.begin(options, id, installedCodeOwner, INVOCATION_ENTRY_BCI);
|
47084
|
999 |
CompilationResult compResult = compile(installedCodeOwner, graphToCompile, new CompilationResult(graphToCompile.compilationId()), id, options);
|
46680
|
1000 |
printer.finish(compResult);
|
43972
|
1001 |
|
46640
|
1002 |
try (DebugContext.Scope s = debug.scope("CodeInstall", getCodeCache(), installedCodeOwner, compResult);
|
|
1003 |
DebugContext.Activation a = debug.activate()) {
|
46344
|
1004 |
try {
|
|
1005 |
if (installAsDefault) {
|
46640
|
1006 |
installedCode = addDefaultMethod(debug, installedCodeOwner, compResult);
|
46344
|
1007 |
} else {
|
46640
|
1008 |
installedCode = addMethod(debug, installedCodeOwner, compResult);
|
46344
|
1009 |
}
|
|
1010 |
if (installedCode == null) {
|
|
1011 |
throw new GraalError("Could not install code for " + installedCodeOwner.format("%H.%n(%p)"));
|
|
1012 |
}
|
|
1013 |
} catch (BailoutException e) {
|
49451
|
1014 |
if (retry < BAILOUT_RETRY_LIMIT && graph == null && !e.isPermanent()) {
|
46344
|
1015 |
// retry (if there is no predefined graph)
|
|
1016 |
TTY.println(String.format("Restart compilation %s (%s) due to a non-permanent bailout!", installedCodeOwner, id));
|
|
1017 |
continue;
|
|
1018 |
}
|
|
1019 |
throw e;
|
|
1020 |
}
|
|
1021 |
} catch (Throwable e) {
|
46640
|
1022 |
throw debug.handle(e);
|
43972
|
1023 |
}
|
|
1024 |
} catch (Throwable e) {
|
46640
|
1025 |
throw debug.handle(e);
|
43972
|
1026 |
}
|
46344
|
1027 |
|
49451
|
1028 |
if (useCache) {
|
58299
|
1029 |
cache.get().put(installedCodeOwner, installedCode);
|
46344
|
1030 |
}
|
|
1031 |
return installedCode;
|
43972
|
1032 |
}
|
46344
|
1033 |
throw GraalError.shouldNotReachHere();
|
43972
|
1034 |
}
|
|
1035 |
|
|
1036 |
/**
|
|
1037 |
* Used to produce a graph for a method about to be compiled by
|
|
1038 |
* {@link #compile(ResolvedJavaMethod, StructuredGraph)} if the second parameter to that method
|
|
1039 |
* is null.
|
|
1040 |
*
|
46344
|
1041 |
* The default implementation in {@link GraalCompilerTest} is to call {@link #parseEager}.
|
43972
|
1042 |
*/
|
46344
|
1043 |
protected StructuredGraph parseForCompile(ResolvedJavaMethod method, OptionValues options) {
|
|
1044 |
return parseEager(method, AllowAssumptions.YES, getCompilationId(method), options);
|
43972
|
1045 |
}
|
|
1046 |
|
46640
|
1047 |
protected final StructuredGraph parseForCompile(ResolvedJavaMethod method, DebugContext debug) {
|
|
1048 |
return parseEager(method, AllowAssumptions.YES, debug);
|
|
1049 |
}
|
|
1050 |
|
46344
|
1051 |
protected final StructuredGraph parseForCompile(ResolvedJavaMethod method) {
|
|
1052 |
return parseEager(method, AllowAssumptions.YES, getCompilationId(method), getInitialOptions());
|
|
1053 |
}
|
|
1054 |
|
|
1055 |
protected StructuredGraph parseForCompile(ResolvedJavaMethod method, CompilationIdentifier compilationId, OptionValues options) {
|
|
1056 |
return parseEager(method, AllowAssumptions.YES, compilationId, options);
|
43972
|
1057 |
}
|
|
1058 |
|
|
1059 |
/**
|
|
1060 |
* Compiles a given method.
|
|
1061 |
*
|
|
1062 |
* @param installedCodeOwner the method the compiled code will be associated with when installed
|
|
1063 |
* @param graph the graph to be compiled for {@code installedCodeOwner}. If null, a graph will
|
|
1064 |
* be obtained from {@code installedCodeOwner} via
|
|
1065 |
* {@link #parseForCompile(ResolvedJavaMethod)}.
|
|
1066 |
*/
|
|
1067 |
protected final CompilationResult compile(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph) {
|
46344
|
1068 |
OptionValues options = graph == null ? getInitialOptions() : graph.getOptions();
|
47084
|
1069 |
CompilationIdentifier compilationId = getOrCreateCompilationId(installedCodeOwner, graph);
|
|
1070 |
return compile(installedCodeOwner, graph, new CompilationResult(compilationId), compilationId, options);
|
43972
|
1071 |
}
|
|
1072 |
|
46344
|
1073 |
protected final CompilationResult compile(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, CompilationIdentifier compilationId) {
|
|
1074 |
OptionValues options = graph == null ? getInitialOptions() : graph.getOptions();
|
47084
|
1075 |
return compile(installedCodeOwner, graph, new CompilationResult(compilationId), compilationId, options);
|
46344
|
1076 |
}
|
|
1077 |
|
|
1078 |
protected final CompilationResult compile(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, OptionValues options) {
|
|
1079 |
assert graph == null || graph.getOptions() == options;
|
47084
|
1080 |
CompilationIdentifier compilationId = getOrCreateCompilationId(installedCodeOwner, graph);
|
|
1081 |
return compile(installedCodeOwner, graph, new CompilationResult(compilationId), compilationId, options);
|
43972
|
1082 |
}
|
|
1083 |
|
52910
|
1084 |
protected OptimisticOptimizations getOptimisticOptimizations() {
|
|
1085 |
return OptimisticOptimizations.ALL;
|
|
1086 |
}
|
|
1087 |
|
43972
|
1088 |
/**
|
|
1089 |
* Compiles a given method.
|
|
1090 |
*
|
|
1091 |
* @param installedCodeOwner the method the compiled code will be associated with when installed
|
|
1092 |
* @param graph the graph to be compiled for {@code installedCodeOwner}. If null, a graph will
|
|
1093 |
* be obtained from {@code installedCodeOwner} via
|
|
1094 |
* {@link #parseForCompile(ResolvedJavaMethod)}.
|
|
1095 |
* @param compilationId
|
|
1096 |
*/
|
|
1097 |
@SuppressWarnings("try")
|
46344
|
1098 |
protected CompilationResult compile(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, CompilationResult compilationResult, CompilationIdentifier compilationId, OptionValues options) {
|
|
1099 |
StructuredGraph graphToCompile = graph == null ? parseForCompile(installedCodeOwner, compilationId, options) : graph;
|
43972
|
1100 |
lastCompiledGraph = graphToCompile;
|
46640
|
1101 |
DebugContext debug = graphToCompile.getDebug();
|
|
1102 |
try (DebugContext.Scope s = debug.scope("Compile", graphToCompile)) {
|
46344
|
1103 |
assert options != null;
|
52910
|
1104 |
Request<CompilationResult> request = new Request<>(graphToCompile, installedCodeOwner, getProviders(), getBackend(), getDefaultGraphBuilderSuite(), getOptimisticOptimizations(),
|
50330
|
1105 |
graphToCompile.getProfilingInfo(), createSuites(options), createLIRSuites(options), compilationResult, CompilationResultBuilderFactory.Default, true);
|
43972
|
1106 |
return GraalCompiler.compile(request);
|
|
1107 |
} catch (Throwable e) {
|
46640
|
1108 |
throw debug.handle(e);
|
43972
|
1109 |
}
|
|
1110 |
}
|
|
1111 |
|
51436
|
1112 |
protected StructuredGraph getFinalGraph(String method) {
|
|
1113 |
return getFinalGraph(getResolvedJavaMethod(method));
|
|
1114 |
}
|
|
1115 |
|
|
1116 |
protected StructuredGraph getFinalGraph(ResolvedJavaMethod method) {
|
|
1117 |
StructuredGraph graph = parseForCompile(method);
|
|
1118 |
applyFrontEnd(graph);
|
|
1119 |
return graph;
|
|
1120 |
}
|
|
1121 |
|
57537
|
1122 |
@SuppressWarnings("try")
|
51436
|
1123 |
protected void applyFrontEnd(StructuredGraph graph) {
|
57537
|
1124 |
DebugContext debug = graph.getDebug();
|
|
1125 |
try (DebugContext.Scope s = debug.scope("FrontEnd", graph)) {
|
|
1126 |
GraalCompiler.emitFrontEnd(getProviders(), getBackend(), graph, getDefaultGraphBuilderSuite(), getOptimisticOptimizations(), graph.getProfilingInfo(), createSuites(graph.getOptions()));
|
|
1127 |
} catch (Throwable e) {
|
|
1128 |
throw debug.handle(e);
|
|
1129 |
}
|
51436
|
1130 |
}
|
|
1131 |
|
43972
|
1132 |
protected StructuredGraph lastCompiledGraph;
|
|
1133 |
|
|
1134 |
protected SpeculationLog getSpeculationLog() {
|
|
1135 |
return null;
|
|
1136 |
}
|
|
1137 |
|
46640
|
1138 |
protected InstalledCode addMethod(DebugContext debug, final ResolvedJavaMethod method, final CompilationResult compilationResult) {
|
|
1139 |
return backend.addInstalledCode(debug, method, null, compilationResult);
|
43972
|
1140 |
}
|
|
1141 |
|
46640
|
1142 |
protected InstalledCode addDefaultMethod(DebugContext debug, final ResolvedJavaMethod method, final CompilationResult compilationResult) {
|
|
1143 |
return backend.createDefaultInstalledCode(debug, method, compilationResult);
|
43972
|
1144 |
}
|
|
1145 |
|
50609
|
1146 |
private final Map<ResolvedJavaMethod, Executable> methodMap = new ConcurrentHashMap<>();
|
43972
|
1147 |
|
|
1148 |
/**
|
|
1149 |
* Converts a reflection {@link Method} to a {@link ResolvedJavaMethod}.
|
|
1150 |
*/
|
46551
|
1151 |
protected ResolvedJavaMethod asResolvedJavaMethod(Executable method) {
|
43972
|
1152 |
ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(method);
|
|
1153 |
methodMap.put(javaMethod, method);
|
|
1154 |
return javaMethod;
|
|
1155 |
}
|
|
1156 |
|
|
1157 |
protected ResolvedJavaMethod getResolvedJavaMethod(String methodName) {
|
|
1158 |
return asResolvedJavaMethod(getMethod(methodName));
|
|
1159 |
}
|
|
1160 |
|
|
1161 |
protected ResolvedJavaMethod getResolvedJavaMethod(Class<?> clazz, String methodName) {
|
|
1162 |
return asResolvedJavaMethod(getMethod(clazz, methodName));
|
|
1163 |
}
|
|
1164 |
|
|
1165 |
protected ResolvedJavaMethod getResolvedJavaMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
|
|
1166 |
return asResolvedJavaMethod(getMethod(clazz, methodName, parameterTypes));
|
|
1167 |
}
|
|
1168 |
|
|
1169 |
/**
|
|
1170 |
* Gets the reflection {@link Method} from which a given {@link ResolvedJavaMethod} was created
|
|
1171 |
* or null if {@code javaMethod} does not correspond to a reflection method.
|
|
1172 |
*/
|
46551
|
1173 |
protected Executable lookupMethod(ResolvedJavaMethod javaMethod) {
|
43972
|
1174 |
return methodMap.get(javaMethod);
|
|
1175 |
}
|
|
1176 |
|
51436
|
1177 |
@SuppressWarnings("deprecation")
|
46551
|
1178 |
protected Object invoke(ResolvedJavaMethod javaMethod, Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
|
|
1179 |
Executable method = lookupMethod(javaMethod);
|
43972
|
1180 |
Assert.assertTrue(method != null);
|
|
1181 |
if (!method.isAccessible()) {
|
|
1182 |
method.setAccessible(true);
|
|
1183 |
}
|
46551
|
1184 |
if (method instanceof Method) {
|
|
1185 |
return ((Method) method).invoke(receiver, applyArgSuppliers(args));
|
|
1186 |
}
|
|
1187 |
assert receiver == null : "no receiver for constructor invokes";
|
|
1188 |
return ((Constructor<?>) method).newInstance(applyArgSuppliers(args));
|
43972
|
1189 |
}
|
|
1190 |
|
|
1191 |
/**
|
|
1192 |
* Parses a Java method in {@linkplain GraphBuilderConfiguration#getDefault default} mode to
|
|
1193 |
* produce a graph.
|
|
1194 |
*
|
|
1195 |
* @param methodName the name of the method in {@code this.getClass()} to be parsed
|
46344
|
1196 |
* @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph
|
43972
|
1197 |
*/
|
46344
|
1198 |
protected final StructuredGraph parseProfiled(String methodName, AllowAssumptions allowAssumptions) {
|
46640
|
1199 |
ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
|
|
1200 |
return parse(builder(method, allowAssumptions), getDefaultGraphBuilderSuite());
|
43972
|
1201 |
}
|
|
1202 |
|
|
1203 |
/**
|
|
1204 |
* Parses a Java method in {@linkplain GraphBuilderConfiguration#getDefault default} mode to
|
|
1205 |
* produce a graph.
|
46344
|
1206 |
*
|
|
1207 |
* @param method the method to be parsed
|
|
1208 |
* @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph
|
43972
|
1209 |
*/
|
46344
|
1210 |
protected final StructuredGraph parseProfiled(ResolvedJavaMethod method, AllowAssumptions allowAssumptions) {
|
46640
|
1211 |
return parse(builder(method, allowAssumptions), getDefaultGraphBuilderSuite());
|
43972
|
1212 |
}
|
|
1213 |
|
46344
|
1214 |
/**
|
|
1215 |
* Parses a Java method with {@linkplain GraphBuilderConfiguration#withEagerResolving(boolean)}
|
|
1216 |
* set to true to produce a graph.
|
|
1217 |
*
|
|
1218 |
* @param methodName the name of the method in {@code this.getClass()} to be parsed
|
|
1219 |
* @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph
|
|
1220 |
*/
|
|
1221 |
protected final StructuredGraph parseEager(String methodName, AllowAssumptions allowAssumptions) {
|
46640
|
1222 |
ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
|
|
1223 |
return parse(builder(method, allowAssumptions), getEagerGraphBuilderSuite());
|
43972
|
1224 |
}
|
|
1225 |
|
|
1226 |
/**
|
|
1227 |
* Parses a Java method with {@linkplain GraphBuilderConfiguration#withEagerResolving(boolean)}
|
|
1228 |
* set to true to produce a graph.
|
|
1229 |
*
|
|
1230 |
* @param methodName the name of the method in {@code this.getClass()} to be parsed
|
46344
|
1231 |
* @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph
|
|
1232 |
* @param options the option values to be used when compiling the graph
|
43972
|
1233 |
*/
|
46344
|
1234 |
protected final StructuredGraph parseEager(String methodName, AllowAssumptions allowAssumptions, OptionValues options) {
|
46640
|
1235 |
ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
|
|
1236 |
return parse(builder(method, allowAssumptions, options), getEagerGraphBuilderSuite());
|
|
1237 |
}
|
|
1238 |
|
|
1239 |
protected final StructuredGraph parseEager(String methodName, AllowAssumptions allowAssumptions, DebugContext debug) {
|
|
1240 |
ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
|
|
1241 |
return parse(builder(method, allowAssumptions, debug), getEagerGraphBuilderSuite());
|
46344
|
1242 |
}
|
|
1243 |
|
|
1244 |
/**
|
|
1245 |
* Parses a Java method with {@linkplain GraphBuilderConfiguration#withEagerResolving(boolean)}
|
|
1246 |
* set to true to produce a graph.
|
|
1247 |
*
|
|
1248 |
* @param method the method to be parsed
|
|
1249 |
* @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph
|
|
1250 |
*/
|
|
1251 |
protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions) {
|
46640
|
1252 |
return parse(builder(method, allowAssumptions), getEagerGraphBuilderSuite());
|
|
1253 |
}
|
|
1254 |
|
|
1255 |
protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, DebugContext debug) {
|
|
1256 |
return parse(builder(method, allowAssumptions, debug), getEagerGraphBuilderSuite());
|
43972
|
1257 |
}
|
|
1258 |
|
|
1259 |
/**
|
|
1260 |
* Parses a Java method with {@linkplain GraphBuilderConfiguration#withEagerResolving(boolean)}
|
|
1261 |
* set to true to produce a graph.
|
46344
|
1262 |
*
|
|
1263 |
* @param method the method to be parsed
|
|
1264 |
* @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph
|
|
1265 |
* @param options the option values to be used when compiling the graph
|
43972
|
1266 |
*/
|
46344
|
1267 |
protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, OptionValues options) {
|
46640
|
1268 |
return parse(builder(method, allowAssumptions, options), getEagerGraphBuilderSuite());
|
43972
|
1269 |
}
|
|
1270 |
|
46344
|
1271 |
/**
|
|
1272 |
* Parses a Java method with {@linkplain GraphBuilderConfiguration#withEagerResolving(boolean)}
|
|
1273 |
* set to true to produce a graph.
|
|
1274 |
*
|
|
1275 |
* @param method the method to be parsed
|
|
1276 |
* @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph
|
|
1277 |
* @param compilationId the compilation identifier to be associated with the graph
|
|
1278 |
* @param options the option values to be used when compiling the graph
|
|
1279 |
*/
|
46640
|
1280 |
protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, OptionValues options) {
|
|
1281 |
return parse(builder(method, allowAssumptions, compilationId, options), getEagerGraphBuilderSuite());
|
|
1282 |
}
|
|
1283 |
|
|
1284 |
protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, DebugContext debug) {
|
|
1285 |
OptionValues options = debug.getOptions();
|
|
1286 |
return new Builder(options, debug, allowAssumptions).method(method).compilationId(getCompilationId(method));
|
43972
|
1287 |
}
|
|
1288 |
|
46640
|
1289 |
protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions) {
|
|
1290 |
OptionValues options = getInitialOptions();
|
47798
|
1291 |
return new Builder(options, getDebugContext(options, null, method), allowAssumptions).method(method).compilationId(getCompilationId(method));
|
46640
|
1292 |
}
|
|
1293 |
|
|
1294 |
protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, OptionValues options) {
|
47798
|
1295 |
return new Builder(options, getDebugContext(options, compilationId.toString(CompilationIdentifier.Verbosity.ID), method), allowAssumptions).method(method).compilationId(compilationId);
|
46640
|
1296 |
}
|
|
1297 |
|
|
1298 |
protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, OptionValues options) {
|
47798
|
1299 |
return new Builder(options, getDebugContext(options, null, method), allowAssumptions).method(method).compilationId(getCompilationId(method));
|
46640
|
1300 |
}
|
|
1301 |
|
|
1302 |
protected PhaseSuite<HighTierContext> getDebugGraphBuilderSuite() {
|
|
1303 |
return getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withFullInfopoints(true));
|
43972
|
1304 |
}
|
|
1305 |
|
|
1306 |
@SuppressWarnings("try")
|
46640
|
1307 |
protected StructuredGraph parse(StructuredGraph.Builder builder, PhaseSuite<HighTierContext> graphBuilderSuite) {
|
|
1308 |
ResolvedJavaMethod javaMethod = builder.getMethod();
|
47798
|
1309 |
builder.speculationLog(getSpeculationLog());
|
46640
|
1310 |
if (builder.getCancellable() == null) {
|
|
1311 |
builder.cancellable(getCancellable(javaMethod));
|
|
1312 |
}
|
43972
|
1313 |
assert javaMethod.getAnnotation(Test.class) == null : "shouldn't parse method with @Test annotation: " + javaMethod;
|
46640
|
1314 |
StructuredGraph graph = builder.build();
|
|
1315 |
DebugContext debug = graph.getDebug();
|
|
1316 |
try (DebugContext.Scope ds = debug.scope("Parsing", javaMethod, graph)) {
|
43972
|
1317 |
graphBuilderSuite.apply(graph, getDefaultHighTierContext());
|
49451
|
1318 |
Object[] args = getArgumentToBind();
|
|
1319 |
if (args != null) {
|
|
1320 |
bindArguments(graph, args);
|
|
1321 |
}
|
43972
|
1322 |
return graph;
|
|
1323 |
} catch (Throwable e) {
|
46640
|
1324 |
throw debug.handle(e);
|
43972
|
1325 |
}
|
|
1326 |
}
|
|
1327 |
|
58299
|
1328 |
protected static final Object NO_BIND = new Object();
|
|
1329 |
|
49451
|
1330 |
protected void bindArguments(StructuredGraph graph, Object[] argsToBind) {
|
|
1331 |
ResolvedJavaMethod m = graph.method();
|
|
1332 |
Object receiver = isStatic(m.getModifiers()) ? null : this;
|
|
1333 |
Object[] args = argsWithReceiver(receiver, argsToBind);
|
|
1334 |
JavaType[] parameterTypes = m.toParameterTypes();
|
|
1335 |
assert parameterTypes.length == args.length;
|
|
1336 |
for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) {
|
58299
|
1337 |
Object arg = args[param.index()];
|
|
1338 |
if (arg != NO_BIND) {
|
|
1339 |
JavaConstant c = getSnippetReflection().forBoxed(parameterTypes[param.index()].getJavaKind(), arg);
|
|
1340 |
ConstantNode replacement = ConstantNode.forConstant(c, getMetaAccess(), graph);
|
|
1341 |
param.replaceAtUsages(replacement);
|
|
1342 |
}
|
49451
|
1343 |
}
|
|
1344 |
}
|
|
1345 |
|
|
1346 |
protected Object[] getArgumentToBind() {
|
|
1347 |
return null;
|
|
1348 |
}
|
|
1349 |
|
46640
|
1350 |
protected PhaseSuite<HighTierContext> getEagerGraphBuilderSuite() {
|
47667
|
1351 |
return getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withEagerResolving(true).withUnresolvedIsError(true));
|
46640
|
1352 |
}
|
|
1353 |
|
46459
|
1354 |
/**
|
|
1355 |
* Gets the cancellable that should be associated with a graph being created by any of the
|
|
1356 |
* {@code parse...()} methods.
|
|
1357 |
*
|
|
1358 |
* @param method the method being parsed into a graph
|
|
1359 |
*/
|
|
1360 |
protected Cancellable getCancellable(ResolvedJavaMethod method) {
|
|
1361 |
return null;
|
|
1362 |
}
|
|
1363 |
|
43972
|
1364 |
protected Plugins getDefaultGraphBuilderPlugins() {
|
|
1365 |
PhaseSuite<HighTierContext> suite = backend.getSuites().getDefaultGraphBuilderSuite();
|
|
1366 |
Plugins defaultPlugins = ((GraphBuilderPhase) suite.findPhase(GraphBuilderPhase.class).previous()).getGraphBuilderConfig().getPlugins();
|
|
1367 |
// defensive copying
|
|
1368 |
return new Plugins(defaultPlugins);
|
|
1369 |
}
|
|
1370 |
|
|
1371 |
protected PhaseSuite<HighTierContext> getDefaultGraphBuilderSuite() {
|
|
1372 |
// defensive copying
|
|
1373 |
return backend.getSuites().getDefaultGraphBuilderSuite().copy();
|
|
1374 |
}
|
|
1375 |
|
46459
|
1376 |
/**
|
|
1377 |
* Registers extra invocation plugins for this test. The extra plugins are removed in the
|
|
1378 |
* {@link #afterTest()} method.
|
|
1379 |
*
|
|
1380 |
* Subclasses overriding this method should always call the same method on the super class in
|
|
1381 |
* case it wants to register plugins.
|
|
1382 |
*
|
|
1383 |
* @param invocationPlugins
|
|
1384 |
*/
|
|
1385 |
protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
|
43972
|
1386 |
invocationPlugins.register(new InvocationPlugin() {
|
|
1387 |
@Override
|
|
1388 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
|
|
1389 |
b.add(new BreakpointNode());
|
|
1390 |
return true;
|
|
1391 |
}
|
|
1392 |
}, GraalCompilerTest.class, "breakpoint");
|
|
1393 |
invocationPlugins.register(new InvocationPlugin() {
|
|
1394 |
@Override
|
|
1395 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode arg0) {
|
|
1396 |
b.add(new BreakpointNode(arg0));
|
|
1397 |
return true;
|
|
1398 |
}
|
|
1399 |
}, GraalCompilerTest.class, "breakpoint", int.class);
|
|
1400 |
invocationPlugins.register(new InvocationPlugin() {
|
|
1401 |
@Override
|
|
1402 |
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
|
|
1403 |
b.add(new NotOptimizedNode());
|
|
1404 |
return true;
|
|
1405 |
}
|
|
1406 |
}, GraalCompilerTest.class, "shouldBeOptimizedAway");
|
46459
|
1407 |
}
|
43972
|
1408 |
|
46459
|
1409 |
/**
|
|
1410 |
* The {@link #testN(int, String, Object...)} method means multiple threads trying to initialize
|
|
1411 |
* this field.
|
|
1412 |
*/
|
|
1413 |
private volatile InvocationPlugins invocationPluginExtensions;
|
|
1414 |
|
|
1415 |
private InvocationPlugins extendedInvocationPlugins;
|
|
1416 |
|
|
1417 |
protected PhaseSuite<HighTierContext> getCustomGraphBuilderSuite(GraphBuilderConfiguration gbConf) {
|
|
1418 |
PhaseSuite<HighTierContext> suite = getDefaultGraphBuilderSuite();
|
|
1419 |
ListIterator<BasePhase<? super HighTierContext>> iterator = suite.findPhase(GraphBuilderPhase.class);
|
|
1420 |
initializeInvocationPluginExtensions();
|
|
1421 |
GraphBuilderConfiguration gbConfCopy = editGraphBuilderConfiguration(gbConf.copy());
|
|
1422 |
iterator.remove();
|
|
1423 |
iterator.add(new GraphBuilderPhase(gbConfCopy));
|
|
1424 |
return suite;
|
|
1425 |
}
|
|
1426 |
|
|
1427 |
private void initializeInvocationPluginExtensions() {
|
|
1428 |
if (invocationPluginExtensions == null) {
|
|
1429 |
synchronized (this) {
|
|
1430 |
if (invocationPluginExtensions == null) {
|
|
1431 |
InvocationPlugins invocationPlugins = new InvocationPlugins();
|
|
1432 |
registerInvocationPlugins(invocationPlugins);
|
|
1433 |
extendedInvocationPlugins = getReplacements().getGraphBuilderPlugins().getInvocationPlugins();
|
|
1434 |
extendedInvocationPlugins.addTestPlugins(invocationPlugins, null);
|
|
1435 |
invocationPluginExtensions = invocationPlugins;
|
|
1436 |
}
|
|
1437 |
}
|
|
1438 |
}
|
|
1439 |
}
|
|
1440 |
|
|
1441 |
protected GraphBuilderConfiguration editGraphBuilderConfiguration(GraphBuilderConfiguration conf) {
|
43972
|
1442 |
conf.getPlugins().prependInlineInvokePlugin(new InlineInvokePlugin() {
|
|
1443 |
|
|
1444 |
@Override
|
|
1445 |
public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
|
|
1446 |
BytecodeParserNeverInline neverInline = method.getAnnotation(BytecodeParserNeverInline.class);
|
|
1447 |
if (neverInline != null) {
|
|
1448 |
return neverInline.invokeWithException() ? DO_NOT_INLINE_WITH_EXCEPTION : DO_NOT_INLINE_NO_EXCEPTION;
|
|
1449 |
}
|
|
1450 |
if (method.getAnnotation(BytecodeParserForceInline.class) != null) {
|
|
1451 |
return InlineInfo.createStandardInlineInfo(method);
|
|
1452 |
}
|
|
1453 |
return bytecodeParserShouldInlineInvoke(b, method, args);
|
|
1454 |
}
|
|
1455 |
});
|
|
1456 |
return conf;
|
|
1457 |
}
|
|
1458 |
|
|
1459 |
/**
|
|
1460 |
* Supplements {@link BytecodeParserForceInline} and {@link BytecodeParserNeverInline} in terms
|
|
1461 |
* of allowing a test to influence the inlining decision made during bytecode parsing.
|
|
1462 |
*
|
|
1463 |
* @see InlineInvokePlugin#shouldInlineInvoke(GraphBuilderContext, ResolvedJavaMethod,
|
|
1464 |
* ValueNode[])
|
|
1465 |
*/
|
|
1466 |
@SuppressWarnings("unused")
|
|
1467 |
protected InlineInvokePlugin.InlineInfo bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
|
|
1468 |
return null;
|
|
1469 |
}
|
|
1470 |
|
|
1471 |
@NodeInfo
|
|
1472 |
public static class NotOptimizedNode extends FixedWithNextNode {
|
|
1473 |
private static final NodeClass<NotOptimizedNode> TYPE = NodeClass.create(NotOptimizedNode.class);
|
|
1474 |
|
|
1475 |
protected NotOptimizedNode() {
|
|
1476 |
super(TYPE, StampFactory.forVoid());
|
|
1477 |
}
|
|
1478 |
|
|
1479 |
}
|
|
1480 |
|
|
1481 |
protected Replacements getReplacements() {
|
|
1482 |
return getProviders().getReplacements();
|
|
1483 |
}
|
|
1484 |
|
|
1485 |
/**
|
|
1486 |
* Inject a probability for a branch condition into the profiling information of this test case.
|
|
1487 |
*
|
|
1488 |
* @param p the probability that cond is true
|
|
1489 |
* @param cond the condition of the branch
|
|
1490 |
* @return cond
|
|
1491 |
*/
|
|
1492 |
protected static boolean branchProbability(double p, boolean cond) {
|
|
1493 |
return GraalDirectives.injectBranchProbability(p, cond);
|
|
1494 |
}
|
|
1495 |
|
|
1496 |
/**
|
|
1497 |
* Inject an iteration count for a loop condition into the profiling information of this test
|
|
1498 |
* case.
|
|
1499 |
*
|
|
1500 |
* @param i the iteration count of the loop
|
|
1501 |
* @param cond the condition of the loop
|
|
1502 |
* @return cond
|
|
1503 |
*/
|
|
1504 |
protected static boolean iterationCount(double i, boolean cond) {
|
|
1505 |
return GraalDirectives.injectIterationCount(i, cond);
|
|
1506 |
}
|
|
1507 |
|
|
1508 |
/**
|
|
1509 |
* Test if the current test runs on the given platform. The name must match the name given in
|
|
1510 |
* the {@link Architecture#getName()}.
|
|
1511 |
*
|
|
1512 |
* @param name The name to test
|
|
1513 |
* @return true if we run on the architecture given by name
|
|
1514 |
*/
|
|
1515 |
protected boolean isArchitecture(String name) {
|
|
1516 |
return name.equals(backend.getTarget().arch.getName());
|
|
1517 |
}
|
|
1518 |
}
|