Search in sources :

Example 36 with InstalledCode

use of jdk.vm.ci.code.InstalledCode in project graal by oracle.

the class GraalCompilerTest method getCode.

/**
 * Gets installed code for a given method and graph, compiling it first if necessary.
 *
 * @param installedCodeOwner the method the compiled code will be associated with when installed
 * @param graph the graph to be compiled. If null, a graph will be obtained from
 *            {@code installedCodeOwner} via {@link #parseForCompile(ResolvedJavaMethod)}.
 * @param forceCompile specifies whether to ignore any previous code cached for the (method,
 *            key) pair
 * @param installAsDefault specifies whether to install as the default implementation
 * @param options the options that will be used in {@link #parseForCompile(ResolvedJavaMethod)}
 */
@SuppressWarnings("try")
protected InstalledCode getCode(final ResolvedJavaMethod installedCodeOwner, StructuredGraph graph, boolean forceCompile, boolean installAsDefault, OptionValues options) {
    boolean useCache = !forceCompile && getArgumentToBind() == null;
    if (useCache && graph == null) {
        InstalledCode cached = cache.get(installedCodeOwner);
        if (cached != null) {
            if (cached.isValid()) {
                return cached;
            }
        }
    }
    // loop for retrying compilation
    for (int retry = 0; retry <= BAILOUT_RETRY_LIMIT; retry++) {
        final CompilationIdentifier id = getOrCreateCompilationId(installedCodeOwner, graph);
        InstalledCode installedCode = null;
        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))) {
            CompilationPrinter printer = CompilationPrinter.begin(options, id, installedCodeOwner, INVOCATION_ENTRY_BCI);
            CompilationResult compResult = compile(installedCodeOwner, graphToCompile, new CompilationResult(graphToCompile.compilationId()), id, options);
            printer.finish(compResult);
            try (DebugContext.Scope s = debug.scope("CodeInstall", getCodeCache(), installedCodeOwner, compResult);
                DebugContext.Activation a = debug.activate()) {
                try {
                    if (installAsDefault) {
                        installedCode = addDefaultMethod(debug, installedCodeOwner, compResult);
                    } else {
                        installedCode = addMethod(debug, installedCodeOwner, compResult);
                    }
                    if (installedCode == null) {
                        throw new GraalError("Could not install code for " + installedCodeOwner.format("%H.%n(%p)"));
                    }
                } catch (BailoutException e) {
                    if (retry < BAILOUT_RETRY_LIMIT && graph == null && !e.isPermanent()) {
                        // retry (if there is no predefined graph)
                        TTY.println(String.format("Restart compilation %s (%s) due to a non-permanent bailout!", installedCodeOwner, id));
                        continue;
                    }
                    throw e;
                }
            } catch (Throwable e) {
                throw debug.handle(e);
            }
        } catch (Throwable e) {
            throw debug.handle(e);
        }
        if (useCache) {
            cache.put(installedCodeOwner, installedCode);
        }
        return installedCode;
    }
    throw GraalError.shouldNotReachHere();
}
Also used : CompilationIdentifier(org.graalvm.compiler.core.common.CompilationIdentifier) DebugDumpScope(org.graalvm.compiler.debug.DebugDumpScope) DebugContext(org.graalvm.compiler.debug.DebugContext) BailoutException(jdk.vm.ci.code.BailoutException) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) GraalError(org.graalvm.compiler.debug.GraalError) CompilationPrinter(org.graalvm.compiler.core.CompilationPrinter) InstalledCode(jdk.vm.ci.code.InstalledCode) CompilationResult(org.graalvm.compiler.code.CompilationResult)

Example 37 with InstalledCode

use of jdk.vm.ci.code.InstalledCode in project graal by oracle.

the class DeoptimizeOnVolatileReadTest method test1.

@Test
public void test1() {
    ResolvedJavaMethod method = getResolvedJavaMethod("test1Snippet");
    Dummy dummy = new Dummy();
    Result expected = executeExpected(method, null, dummy);
    assertEquals(new Result(0, null), expected);
    dummy.f1 = true;
    InstalledCode code = getCode(method);
    Result actual;
    try {
        actual = new Result(code.executeVarargs(dummy), null);
    } catch (Exception e) {
        actual = new Result(null, e);
    }
    // The code should get deoptimized, and resume execution at the beginning of the method.
    // Therefore it does not re-enter the branch as inCompiledCode() is false.
    assertEquals(new Result(0, null), actual);
    assertFalse(code.isValid());
}
Also used : InstalledCode(jdk.vm.ci.code.InstalledCode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) Test(org.junit.Test) GraalCompilerTest(org.graalvm.compiler.core.test.GraalCompilerTest)

Example 38 with InstalledCode

use of jdk.vm.ci.code.InstalledCode in project graal by oracle.

the class CompiledMethodTest method test4.

@Test
public void test4() {
    final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethodVirtual");
    InstalledCode compiledMethod = getCode(javaMethod);
    try {
        f1 = "0";
        Object result = compiledMethod.executeVarargs(this, "1", "2", "3");
        Assert.assertEquals("0 1 2 3", result);
    } catch (InvalidInstalledCodeException t) {
        Assert.fail("method invalidated");
    }
}
Also used : InvalidInstalledCodeException(jdk.vm.ci.code.InvalidInstalledCodeException) InstalledCode(jdk.vm.ci.code.InstalledCode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) Test(org.junit.Test) GraalCompilerTest(org.graalvm.compiler.core.test.GraalCompilerTest)

Example 39 with InstalledCode

use of jdk.vm.ci.code.InstalledCode in project graal by oracle.

the class CompiledMethodTest method test1.

/**
 * Usages of the constant {@code " "} are replaced with the constant {@code "-"} and it is
 * verified that executing the compiled code produces a result that the preserves the node
 * replacement unless deoptimization occurs (e.g., due to -Xcomp causing profiles to be
 * missing).
 */
@Test
public void test1() {
    final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
    final StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.NO);
    new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
    new DeadCodeEliminationPhase().apply(graph);
    for (ConstantNode node : ConstantNode.getConstantNodes(graph)) {
        if (node.getStackKind() == JavaKind.Object && " ".equals(getSnippetReflection().asObject(String.class, node.asJavaConstant()))) {
            node.replace(graph, ConstantNode.forConstant(getSnippetReflection().forObject("-"), getMetaAccess(), graph));
        }
    }
    InstalledCode compiledMethod = getCode(javaMethod, graph);
    try {
        Object result = compiledMethod.executeVarargs("1", "2", "3");
        if (!"1-2-3".equals(result)) {
            // Deoptimization probably occurred
            Assert.assertEquals("interpreter", result);
        }
    } catch (InvalidInstalledCodeException t) {
        Assert.fail("method invalidated");
    }
}
Also used : PhaseContext(org.graalvm.compiler.phases.tiers.PhaseContext) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) InvalidInstalledCodeException(jdk.vm.ci.code.InvalidInstalledCodeException) InstalledCode(jdk.vm.ci.code.InstalledCode) CanonicalizerPhase(org.graalvm.compiler.phases.common.CanonicalizerPhase) DeadCodeEliminationPhase(org.graalvm.compiler.phases.common.DeadCodeEliminationPhase) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) Test(org.junit.Test) GraalCompilerTest(org.graalvm.compiler.core.test.GraalCompilerTest)

Example 40 with InstalledCode

use of jdk.vm.ci.code.InstalledCode in project graal by oracle.

the class CFGPrinterObserver method dumpSandboxed.

public void dumpSandboxed(DebugContext debug, Object object, String message) {
    OptionValues options = debug.getOptions();
    boolean dumpFrontend = PrintCFG.getValue(options);
    if (!dumpFrontend && isFrontendObject(object)) {
        return;
    }
    if (cfgPrinter == null) {
        try {
            Path dumpFile = debug.getDumpPath(".cfg", false);
            cfgFile = dumpFile.toFile();
            OutputStream out = new BufferedOutputStream(new FileOutputStream(cfgFile));
            cfgPrinter = new CFGPrinter(out);
        } catch (IOException e) {
            throw (GraalError) new GraalError("Could not open %s", cfgFile == null ? "[null]" : cfgFile.getAbsolutePath()).initCause(e);
        }
    }
    if (!checkMethodScope(debug)) {
        return;
    }
    try {
        if (curMethod instanceof ResolvedJavaMethod) {
            cfgPrinter.method = (ResolvedJavaMethod) curMethod;
        }
        if (object instanceof LIR) {
            cfgPrinter.lir = (LIR) object;
        } else {
            cfgPrinter.lir = debug.contextLookup(LIR.class);
        }
        cfgPrinter.nodeLirGenerator = debug.contextLookup(NodeLIRBuilder.class);
        cfgPrinter.livenessInfo = debug.contextLookup(GlobalLivenessInfo.class);
        cfgPrinter.res = debug.contextLookup(LIRGenerationResult.class);
        if (cfgPrinter.nodeLirGenerator != null) {
            cfgPrinter.target = cfgPrinter.nodeLirGenerator.getLIRGeneratorTool().target();
        }
        if (cfgPrinter.lir != null && cfgPrinter.lir.getControlFlowGraph() instanceof ControlFlowGraph) {
            cfgPrinter.cfg = (ControlFlowGraph) cfgPrinter.lir.getControlFlowGraph();
        }
        CodeCacheProvider codeCache = debug.contextLookup(CodeCacheProvider.class);
        if (codeCache != null) {
            cfgPrinter.target = codeCache.getTarget();
        }
        if (object instanceof BciBlockMapping) {
            BciBlockMapping blockMap = (BciBlockMapping) object;
            cfgPrinter.printCFG(message, blockMap);
            if (blockMap.code.getCode() != null) {
                cfgPrinter.printBytecodes(new BytecodeDisassembler(false).disassemble(blockMap.code));
            }
        } else if (object instanceof LIR) {
            // Currently no node printing for lir
            cfgPrinter.printCFG(message, cfgPrinter.lir.codeEmittingOrder(), false);
            lastLIR = (LIR) object;
            if (delayedIntervals != null) {
                cfgPrinter.printIntervals(message, delayedIntervals);
                delayedIntervals = null;
            }
        } else if (object instanceof ScheduleResult) {
            cfgPrinter.printSchedule(message, (ScheduleResult) object);
        } else if (object instanceof StructuredGraph) {
            if (cfgPrinter.cfg == null) {
                StructuredGraph graph = (StructuredGraph) object;
                cfgPrinter.cfg = ControlFlowGraph.compute(graph, true, true, true, false);
                cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
            } else {
                cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
            }
        } else if (object instanceof CompilationResult) {
            final CompilationResult compResult = (CompilationResult) object;
            cfgPrinter.printMachineCode(disassemble(codeCache, compResult, null), message);
        } else if (object instanceof InstalledCode) {
            CompilationResult compResult = debug.contextLookup(CompilationResult.class);
            if (compResult != null) {
                cfgPrinter.printMachineCode(disassemble(codeCache, compResult, (InstalledCode) object), message);
            }
        } else if (object instanceof IntervalDumper) {
            if (lastLIR == cfgPrinter.lir) {
                cfgPrinter.printIntervals(message, (IntervalDumper) object);
            } else {
                if (delayedIntervals != null) {
                    debug.log("Some delayed intervals were dropped (%s)", delayedIntervals);
                }
                delayedIntervals = (IntervalDumper) object;
            }
        } else if (object instanceof AbstractBlockBase<?>[]) {
            cfgPrinter.printCFG(message, (AbstractBlockBase<?>[]) object, false);
        } else if (object instanceof Trace) {
            cfgPrinter.printCFG(message, ((Trace) object).getBlocks(), false);
        } else if (object instanceof TraceBuilderResult) {
            cfgPrinter.printTraces(message, (TraceBuilderResult) object);
        }
    } finally {
        cfgPrinter.target = null;
        cfgPrinter.lir = null;
        cfgPrinter.res = null;
        cfgPrinter.nodeLirGenerator = null;
        cfgPrinter.livenessInfo = null;
        cfgPrinter.cfg = null;
        cfgPrinter.flush();
    }
}
Also used : BytecodeDisassembler(org.graalvm.compiler.bytecode.BytecodeDisassembler) IntervalDumper(org.graalvm.compiler.lir.debug.IntervalDumper) ScheduleResult(org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult) OptionValues(org.graalvm.compiler.options.OptionValues) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CodeCacheProvider(jdk.vm.ci.code.CodeCacheProvider) AbstractBlockBase(org.graalvm.compiler.core.common.cfg.AbstractBlockBase) LIRGenerationResult(org.graalvm.compiler.lir.gen.LIRGenerationResult) GraalError(org.graalvm.compiler.debug.GraalError) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) InstalledCode(jdk.vm.ci.code.InstalledCode) TraceBuilderResult(org.graalvm.compiler.core.common.alloc.TraceBuilderResult) BufferedOutputStream(java.io.BufferedOutputStream) Path(java.nio.file.Path) BciBlockMapping(org.graalvm.compiler.java.BciBlockMapping) GlobalLivenessInfo(org.graalvm.compiler.lir.alloc.trace.GlobalLivenessInfo) IOException(java.io.IOException) Trace(org.graalvm.compiler.core.common.alloc.Trace) LIR(org.graalvm.compiler.lir.LIR) FileOutputStream(java.io.FileOutputStream) ControlFlowGraph(org.graalvm.compiler.nodes.cfg.ControlFlowGraph) NodeLIRBuilder(org.graalvm.compiler.core.gen.NodeLIRBuilder) CompilationResult(org.graalvm.compiler.code.CompilationResult) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

InstalledCode (jdk.vm.ci.code.InstalledCode)40 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)25 Test (org.junit.Test)17 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)13 GraalCompilerTest (org.graalvm.compiler.core.test.GraalCompilerTest)10 CompilationResult (org.graalvm.compiler.code.CompilationResult)8 InvalidInstalledCodeException (jdk.vm.ci.code.InvalidInstalledCodeException)6 DebugContext (org.graalvm.compiler.debug.DebugContext)5 OptionValues (org.graalvm.compiler.options.OptionValues)4 CanonicalizerPhase (org.graalvm.compiler.phases.common.CanonicalizerPhase)3 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)2 CompilationIdentifier (org.graalvm.compiler.core.common.CompilationIdentifier)2 GraalError (org.graalvm.compiler.debug.GraalError)2 ScheduleResult (org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 ByteBuffer (java.nio.ByteBuffer)1