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