--- old/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/GraalCompilerTest.java 2017-07-07 09:29:31.000000000 -0700 +++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/GraalCompilerTest.java 2017-07-07 09:29:31.000000000 -0700 @@ -37,6 +37,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.EnumMap; import java.util.HashMap; @@ -55,10 +56,10 @@ import org.graalvm.compiler.core.common.CompilationIdentifier; import org.graalvm.compiler.core.common.type.StampFactory; import org.graalvm.compiler.core.target.Backend; -import org.graalvm.compiler.debug.Debug; -import org.graalvm.compiler.debug.Debug.Scope; +import org.graalvm.compiler.debug.DebugHandlersFactory; +import org.graalvm.compiler.debug.DebugContext; +import org.graalvm.compiler.debug.DebugDumpHandler; import org.graalvm.compiler.debug.DebugDumpScope; -import org.graalvm.compiler.debug.DebugEnvironment; import org.graalvm.compiler.debug.GraalError; import org.graalvm.compiler.debug.TTY; import org.graalvm.compiler.graph.Node; @@ -85,6 +86,7 @@ import org.graalvm.compiler.nodes.ReturnNode; import org.graalvm.compiler.nodes.StructuredGraph; import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; +import org.graalvm.compiler.nodes.StructuredGraph.Builder; import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; import org.graalvm.compiler.nodes.ValueNode; import org.graalvm.compiler.nodes.cfg.Block; @@ -111,13 +113,13 @@ import org.graalvm.compiler.phases.tiers.Suites; import org.graalvm.compiler.phases.tiers.TargetProvider; import org.graalvm.compiler.phases.util.Providers; +import org.graalvm.compiler.printer.GraalDebugHandlersFactory; import org.graalvm.compiler.runtime.RuntimeProvider; import org.graalvm.compiler.test.AddExports; import org.graalvm.compiler.test.GraalTest; import org.graalvm.compiler.test.JLModule; import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.internal.AssumptionViolatedException; @@ -364,14 +366,7 @@ this.providers = backend.getProviders(); } - private Scope debugScope; - - @Before - public void beforeTest() { - assert debugScope == null; - debugScope = Debug.scope(getClass()); - } - + @Override @After public void afterTest() { if (invocationPluginExtensions != null) { @@ -383,10 +378,21 @@ } } } - if (debugScope != null) { - debugScope.close(); - } - debugScope = null; + super.afterTest(); + } + + /** + * Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if + * none currently exists. Debug contexts created by this method will have their + * {@link DebugDumpHandler}s closed in {@link #afterTest()}. + */ + protected DebugContext getDebugContext() { + return getDebugContext(getInitialOptions()); + } + + @Override + protected Collection getDebugHandlersFactories() { + return Collections.singletonList(new GraalDebugHandlersFactory(getSnippetReflection())); } protected void assertEquals(StructuredGraph expected, StructuredGraph graph) { @@ -413,13 +419,13 @@ String mismatchString = compareGraphStrings(expected, expectedString, graph, actualString); if (!excludeVirtual && getNodeCountExcludingUnusedConstants(expected) != getNodeCountExcludingUnusedConstants(graph)) { - Debug.dump(Debug.BASIC_LEVEL, expected, "Node count not matching - expected"); - Debug.dump(Debug.BASIC_LEVEL, graph, "Node count not matching - actual"); + expected.getDebug().dump(DebugContext.BASIC_LEVEL, expected, "Node count not matching - expected"); + graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "Node count not matching - actual"); Assert.fail("Graphs do not have the same number of nodes: " + expected.getNodeCount() + " vs. " + graph.getNodeCount() + "\n" + mismatchString); } if (!expectedString.equals(actualString)) { - Debug.dump(Debug.BASIC_LEVEL, expected, "mismatching graphs - expected"); - Debug.dump(Debug.BASIC_LEVEL, graph, "mismatching graphs - actual"); + expected.getDebug().dump(DebugContext.BASIC_LEVEL, expected, "mismatching graphs - expected"); + graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "mismatching graphs - actual"); Assert.fail(mismatchString); } } @@ -932,24 +938,27 @@ final CompilationIdentifier id = getOrCreateCompilationId(installedCodeOwner, graph); InstalledCode installedCode = null; - try (AllocSpy spy = AllocSpy.open(installedCodeOwner); Scope ds = Debug.scope("Compiling", new DebugDumpScope(id.toString(CompilationIdentifier.Verbosity.ID), true))) { + StructuredGraph graphToCompile = graph == null ? parseForCompile(installedCodeOwner, id, options) : graph; + DebugContext debug = graphToCompile.getDebug(); + try (AllocSpy spy = AllocSpy.open(installedCodeOwner); DebugContext.Scope ds = debug.scope("Compiling", new DebugDumpScope(id.toString(CompilationIdentifier.Verbosity.ID), true))) { final boolean printCompilation = PrintCompilation.getValue(options) && !TTY.isSuppressed(); if (printCompilation) { TTY.println(String.format("@%-6s Graal %-70s %-45s %-50s ...", id, installedCodeOwner.getDeclaringClass().getName(), installedCodeOwner.getName(), installedCodeOwner.getSignature())); } long start = System.currentTimeMillis(); - CompilationResult compResult = compile(installedCodeOwner, graph, new CompilationResult(), id, options); + CompilationResult compResult = compile(installedCodeOwner, graphToCompile, new CompilationResult(), id, options); if (printCompilation) { TTY.println(String.format("@%-6s Graal %-70s %-45s %-50s | %4dms %5dB", id, "", "", "", System.currentTimeMillis() - start, compResult.getTargetCodeSize())); } - try (Scope s = Debug.scope("CodeInstall", getCodeCache(), installedCodeOwner, compResult)) { + try (DebugContext.Scope s = debug.scope("CodeInstall", getCodeCache(), installedCodeOwner, compResult); + DebugContext.Activation a = debug.activate()) { try { if (installAsDefault) { - installedCode = addDefaultMethod(installedCodeOwner, compResult); + installedCode = addDefaultMethod(debug, installedCodeOwner, compResult); } else { - installedCode = addMethod(installedCodeOwner, compResult); + installedCode = addMethod(debug, installedCodeOwner, compResult); } if (installedCode == null) { throw new GraalError("Could not install code for " + installedCodeOwner.format("%H.%n(%p)")); @@ -963,10 +972,10 @@ throw e; } } catch (Throwable e) { - throw Debug.handle(e); + throw debug.handle(e); } } catch (Throwable e) { - throw Debug.handle(e); + throw debug.handle(e); } if (!forceCompile) { @@ -988,6 +997,10 @@ return parseEager(method, AllowAssumptions.YES, getCompilationId(method), options); } + protected final StructuredGraph parseForCompile(ResolvedJavaMethod method, DebugContext debug) { + return parseEager(method, AllowAssumptions.YES, debug); + } + protected final StructuredGraph parseForCompile(ResolvedJavaMethod method) { return parseEager(method, AllowAssumptions.YES, getCompilationId(method), getInitialOptions()); } @@ -1032,13 +1045,14 @@ protected CompilationResult compile(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, CompilationResult compilationResult, CompilationIdentifier compilationId, OptionValues options) { StructuredGraph graphToCompile = graph == null ? parseForCompile(installedCodeOwner, compilationId, options) : graph; lastCompiledGraph = graphToCompile; - try (Scope s = Debug.scope("Compile", graphToCompile)) { + DebugContext debug = graphToCompile.getDebug(); + try (DebugContext.Scope s = debug.scope("Compile", graphToCompile)) { assert options != null; Request request = new Request<>(graphToCompile, installedCodeOwner, getProviders(), getBackend(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, graphToCompile.getProfilingInfo(), createSuites(options), createLIRSuites(options), compilationResult, CompilationResultBuilderFactory.Default); return GraalCompiler.compile(request); } catch (Throwable e) { - throw Debug.handle(e); + throw debug.handle(e); } } @@ -1048,12 +1062,12 @@ return null; } - protected InstalledCode addMethod(final ResolvedJavaMethod method, final CompilationResult compilationResult) { - return backend.addInstalledCode(method, null, compilationResult); + protected InstalledCode addMethod(DebugContext debug, final ResolvedJavaMethod method, final CompilationResult compilationResult) { + return backend.addInstalledCode(debug, method, null, compilationResult); } - protected InstalledCode addDefaultMethod(final ResolvedJavaMethod method, final CompilationResult compilationResult) { - return backend.createDefaultInstalledCode(method, compilationResult); + protected InstalledCode addDefaultMethod(DebugContext debug, final ResolvedJavaMethod method, final CompilationResult compilationResult) { + return backend.createDefaultInstalledCode(debug, method, compilationResult); } private final Map methodMap = new HashMap<>(); @@ -1108,7 +1122,8 @@ * @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph */ protected final StructuredGraph parseProfiled(String methodName, AllowAssumptions allowAssumptions) { - return parseProfiled(getResolvedJavaMethod(methodName), allowAssumptions); + ResolvedJavaMethod method = getResolvedJavaMethod(methodName); + return parse(builder(method, allowAssumptions), getDefaultGraphBuilderSuite()); } /** @@ -1119,7 +1134,7 @@ * @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph */ protected final StructuredGraph parseProfiled(ResolvedJavaMethod method, AllowAssumptions allowAssumptions) { - return parse1(method, getDefaultGraphBuilderSuite(), allowAssumptions, getCompilationId(method), getInitialOptions()); + return parse(builder(method, allowAssumptions), getDefaultGraphBuilderSuite()); } /** @@ -1130,7 +1145,8 @@ * @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph */ protected final StructuredGraph parseEager(String methodName, AllowAssumptions allowAssumptions) { - return parseEager(getResolvedJavaMethod(methodName), allowAssumptions, getInitialOptions()); + ResolvedJavaMethod method = getResolvedJavaMethod(methodName); + return parse(builder(method, allowAssumptions), getEagerGraphBuilderSuite()); } /** @@ -1142,7 +1158,13 @@ * @param options the option values to be used when compiling the graph */ protected final StructuredGraph parseEager(String methodName, AllowAssumptions allowAssumptions, OptionValues options) { - return parseEager(getResolvedJavaMethod(methodName), allowAssumptions, options); + ResolvedJavaMethod method = getResolvedJavaMethod(methodName); + return parse(builder(method, allowAssumptions, options), getEagerGraphBuilderSuite()); + } + + protected final StructuredGraph parseEager(String methodName, AllowAssumptions allowAssumptions, DebugContext debug) { + ResolvedJavaMethod method = getResolvedJavaMethod(methodName); + return parse(builder(method, allowAssumptions, debug), getEagerGraphBuilderSuite()); } /** @@ -1153,7 +1175,11 @@ * @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph */ protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions) { - return parseEager(method, allowAssumptions, getCompilationId(method), getInitialOptions()); + return parse(builder(method, allowAssumptions), getEagerGraphBuilderSuite()); + } + + protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, DebugContext debug) { + return parse(builder(method, allowAssumptions, debug), getEagerGraphBuilderSuite()); } /** @@ -1165,7 +1191,7 @@ * @param options the option values to be used when compiling the graph */ protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, OptionValues options) { - return parseEager(method, allowAssumptions, getCompilationId(method), options); + return parse(builder(method, allowAssumptions, options), getEagerGraphBuilderSuite()); } /** @@ -1177,43 +1203,53 @@ * @param compilationId the compilation identifier to be associated with the graph * @param options the option values to be used when compiling the graph */ - protected StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, OptionValues options) { - return parse1(method, getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withEagerResolving(true)), allowAssumptions, compilationId, options); + protected final StructuredGraph parseEager(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, OptionValues options) { + return parse(builder(method, allowAssumptions, compilationId, options), getEagerGraphBuilderSuite()); } - /** - * Parses a Java method using {@linkplain GraphBuilderConfiguration#withFullInfopoints(boolean) - * full debug} set to true to produce a graph. - * - * @param method the method to be parsed - * @param allowAssumptions specifies if {@link Assumption}s can be made compiling the graph - */ - protected StructuredGraph parseDebug(ResolvedJavaMethod method, AllowAssumptions allowAssumptions) { - return parse1(method, getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withFullInfopoints(true)), allowAssumptions, getCompilationId(method), - getInitialOptions()); + protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, DebugContext debug) { + OptionValues options = debug.getOptions(); + return new Builder(options, debug, allowAssumptions).method(method).compilationId(getCompilationId(method)); + } + + protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions) { + OptionValues options = getInitialOptions(); + return new Builder(options, getDebugContext(options), allowAssumptions).method(method).compilationId(getCompilationId(method)); + } + + protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, OptionValues options) { + return new Builder(options, getDebugContext(options), allowAssumptions).method(method).compilationId(compilationId); + } + + protected final Builder builder(ResolvedJavaMethod method, AllowAssumptions allowAssumptions, OptionValues options) { + return new Builder(options, getDebugContext(options), allowAssumptions).method(method).compilationId(getCompilationId(method)); + } + + protected PhaseSuite getDebugGraphBuilderSuite() { + return getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withFullInfopoints(true)); } @SuppressWarnings("try") - private StructuredGraph parse1(ResolvedJavaMethod javaMethod, PhaseSuite graphBuilderSuite, AllowAssumptions allowAssumptions, CompilationIdentifier compilationId, - OptionValues options) { + protected StructuredGraph parse(StructuredGraph.Builder builder, PhaseSuite graphBuilderSuite) { + ResolvedJavaMethod javaMethod = builder.getMethod(); + if (builder.getCancellable() == null) { + builder.cancellable(getCancellable(javaMethod)); + } assert javaMethod.getAnnotation(Test.class) == null : "shouldn't parse method with @Test annotation: " + javaMethod; - // @formatter:off - StructuredGraph graph = new StructuredGraph.Builder(options, allowAssumptions). - method(javaMethod). - speculationLog(getSpeculationLog()). - useProfilingInfo(true). - compilationId(compilationId). - cancellable(getCancellable(javaMethod)). - build(); - // @formatter:on - try (Scope ds = Debug.scope("Parsing", javaMethod, graph)) { + StructuredGraph graph = builder.build(); + DebugContext debug = graph.getDebug(); + try (DebugContext.Scope ds = debug.scope("Parsing", javaMethod, graph)) { graphBuilderSuite.apply(graph, getDefaultHighTierContext()); return graph; } catch (Throwable e) { - throw Debug.handle(e); + throw debug.handle(e); } } + protected PhaseSuite getEagerGraphBuilderSuite() { + return getCustomGraphBuilderSuite(GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withEagerResolving(true)); + } + /** * Gets the cancellable that should be associated with a graph being created by any of the * {@code parse...()} methods. @@ -1385,6 +1421,6 @@ public static void initializeForTimeout() { // timeout tests run in a separate thread which needs the DebugEnvironment to be // initialized - DebugEnvironment.ensureInitialized(getInitialOptions()); + // DebugEnvironment.ensureInitialized(getInitialOptions()); } }