Search in sources :

Example 1 with CompilationIdentifier

use of org.graalvm.compiler.core.common.CompilationIdentifier in project graal by oracle.

the class ForeignCallStub method getGraph.

/**
 * Creates a graph for this stub.
 * <p>
 * If the stub returns an object, the graph created corresponds to this pseudo code:
 *
 * <pre>
 *     Object foreignFunctionStub(args...) {
 *         foreignFunction(currentThread,  args);
 *         if (clearPendingException(thread())) {
 *             getAndClearObjectResult(thread());
 *             DeoptimizeCallerNode.deopt(InvalidateReprofile, RuntimeConstraint);
 *         }
 *         return verifyObject(getAndClearObjectResult(thread()));
 *     }
 * </pre>
 *
 * If the stub returns a primitive or word, the graph created corresponds to this pseudo code
 * (using {@code int} as the primitive return type):
 *
 * <pre>
 *     int foreignFunctionStub(args...) {
 *         int result = foreignFunction(currentThread,  args);
 *         if (clearPendingException(thread())) {
 *             DeoptimizeCallerNode.deopt(InvalidateReprofile, RuntimeConstraint);
 *         }
 *         return result;
 *     }
 * </pre>
 *
 * If the stub is void, the graph created corresponds to this pseudo code:
 *
 * <pre>
 *     void foreignFunctionStub(args...) {
 *         foreignFunction(currentThread,  args);
 *         if (clearPendingException(thread())) {
 *             DeoptimizeCallerNode.deopt(InvalidateReprofile, RuntimeConstraint);
 *         }
 *     }
 * </pre>
 *
 * In each example above, the {@code currentThread} argument is the C++ JavaThread value (i.e.,
 * %r15 on AMD64) and is only prepended if {@link #prependThread} is true.
 */
@Override
@SuppressWarnings("try")
protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) {
    WordTypes wordTypes = providers.getWordTypes();
    Class<?>[] args = linkage.getDescriptor().getArgumentTypes();
    boolean isObjectResult = !LIRKind.isValue(linkage.getOutgoingCallingConvention().getReturn());
    try {
        ResolvedJavaMethod thisMethod = providers.getMetaAccess().lookupJavaMethod(ForeignCallStub.class.getDeclaredMethod("getGraph", DebugContext.class, CompilationIdentifier.class));
        GraphKit kit = new GraphKit(debug, thisMethod, providers, wordTypes, providers.getGraphBuilderPlugins(), compilationId, toString());
        StructuredGraph graph = kit.getGraph();
        ParameterNode[] params = createParameters(kit, args);
        ReadRegisterNode thread = kit.append(new ReadRegisterNode(providers.getRegisters().getThreadRegister(), wordTypes.getWordKind(), true, false));
        ValueNode result = createTargetCall(kit, params, thread);
        kit.createInvoke(StubUtil.class, "handlePendingException", thread, ConstantNode.forBoolean(isObjectResult, graph));
        if (isObjectResult) {
            InvokeNode object = kit.createInvoke(HotSpotReplacementsUtil.class, "getAndClearObjectResult", thread);
            result = kit.createInvoke(StubUtil.class, "verifyObject", object);
        }
        kit.append(new ReturnNode(linkage.getDescriptor().getResultType() == void.class ? null : result));
        debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Initial stub graph");
        kit.inlineInvokes();
        new RemoveValueProxyPhase().apply(graph);
        debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Stub graph before compilation");
        return graph;
    } catch (Exception e) {
        throw GraalError.shouldNotReachHere(e);
    }
}
Also used : CompilationIdentifier(org.graalvm.compiler.core.common.CompilationIdentifier) RemoveValueProxyPhase(org.graalvm.compiler.phases.common.RemoveValueProxyPhase) WordTypes(org.graalvm.compiler.word.WordTypes) DebugContext(org.graalvm.compiler.debug.DebugContext) ReturnNode(org.graalvm.compiler.nodes.ReturnNode) GraphKit(org.graalvm.compiler.replacements.GraphKit) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) InvokeNode(org.graalvm.compiler.nodes.InvokeNode) ReadRegisterNode(org.graalvm.compiler.replacements.nodes.ReadRegisterNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 2 with CompilationIdentifier

use of org.graalvm.compiler.core.common.CompilationIdentifier in project graal by oracle.

the class TruffleCompilerImpl method doCompile.

@Override
@SuppressWarnings("try")
public void doCompile(DebugContext inDebug, CompilationIdentifier inCompilationId, OptionValues options, CompilableTruffleAST compilable, TruffleInliningPlan inliningPlan, Cancellable cancellable, TruffleCompilerListener listener) {
    CompilationIdentifier compilationId = inCompilationId == null ? getCompilationIdentifier(compilable) : inCompilationId;
    DebugContext debug = inDebug == null ? openDebugContext(options, compilationId, compilable) : inDebug;
    try (DebugContext debugToClose = debug == inDebug ? null : debug;
        DebugContext.Scope s = maybeOpenTruffleScope(compilable, debug)) {
        new TruffleCompilationWrapper(getDebugOutputDirectory(), getCompilationProblemsPerAction(), compilable, cancellable, inliningPlan, compilationId, listener).run(debug);
    } catch (Throwable e) {
        notifyCompilableOfFailure(compilable, e);
    }
}
Also used : CompilationIdentifier(org.graalvm.compiler.core.common.CompilationIdentifier) Scope(org.graalvm.compiler.debug.DebugContext.Scope) DebugContext(org.graalvm.compiler.debug.DebugContext)

Example 3 with CompilationIdentifier

use of org.graalvm.compiler.core.common.CompilationIdentifier in project graal by oracle.

the class Stub method buildCompilationResult.

@SuppressWarnings("try")
private CompilationResult buildCompilationResult(DebugContext debug, final Backend backend) {
    CompilationIdentifier compilationId = getStubCompilationId();
    final StructuredGraph graph = getGraph(debug, compilationId);
    CompilationResult compResult = new CompilationResult(compilationId, toString(), GeneratePIC.getValue(options));
    // Stubs cannot be recompiled so they cannot be compiled with assumptions
    assert graph.getAssumptions() == null;
    if (!(graph.start() instanceof StubStartNode)) {
        StubStartNode newStart = graph.add(new StubStartNode(Stub.this));
        newStart.setStateAfter(graph.start().stateAfter());
        graph.replaceFixed(graph.start(), newStart);
    }
    try (DebugContext.Scope s0 = debug.scope("StubCompilation", graph, providers.getCodeCache())) {
        Suites suites = createSuites();
        emitFrontEnd(providers, backend, graph, providers.getSuites().getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, DefaultProfilingInfo.get(TriState.UNKNOWN), suites);
        LIRSuites lirSuites = createLIRSuites();
        emitBackEnd(graph, Stub.this, getInstalledCodeOwner(), backend, compResult, CompilationResultBuilderFactory.Default, getRegisterConfig(), lirSuites);
        assert checkStubInvariants(compResult);
    } catch (Throwable e) {
        throw debug.handle(e);
    }
    return compResult;
}
Also used : CompilationIdentifier(org.graalvm.compiler.core.common.CompilationIdentifier) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) StubStartNode(org.graalvm.compiler.hotspot.nodes.StubStartNode) LIRSuites(org.graalvm.compiler.lir.phases.LIRSuites) CompilationResult(org.graalvm.compiler.code.CompilationResult) DebugContext(org.graalvm.compiler.debug.DebugContext) LIRSuites(org.graalvm.compiler.lir.phases.LIRSuites) Suites(org.graalvm.compiler.phases.tiers.Suites)

Example 4 with CompilationIdentifier

use of org.graalvm.compiler.core.common.CompilationIdentifier in project graal by oracle.

the class InvokeGraal method compileAndInstallMethod.

/**
 * The simplest way to compile a method, using the default behavior for everything.
 */
@SuppressWarnings("try")
protected InstalledCode compileAndInstallMethod(ResolvedJavaMethod method) {
    /* Create a unique compilation identifier, visible in IGV. */
    CompilationIdentifier compilationId = backend.getCompilationIdentifier(method);
    OptionValues options = getInitialOptions();
    DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER);
    try (DebugContext.Scope s = debug.scope("compileAndInstallMethod", new DebugDumpScope(String.valueOf(compilationId), true))) {
        /*
             * The graph that is compiled. We leave it empty (no nodes added yet). This means that
             * it will be filled according to the graphBuilderSuite defined below. We also specify
             * that we want the compilation to make optimistic assumptions about runtime state such
             * as the loaded class hierarchy.
             */
        StructuredGraph graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.YES).method(method).compilationId(compilationId).build();
        /*
             * The phases used to build the graph. Usually this is just the GraphBuilderPhase. If
             * the graph already contains nodes, it is ignored.
             */
        PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite();
        /*
             * The optimization phases that are applied to the graph. This is the main configuration
             * point for Graal. Add or remove phases to customize your compilation.
             */
        Suites suites = backend.getSuites().getDefaultSuites(options);
        /*
             * The low-level phases that are applied to the low-level representation.
             */
        LIRSuites lirSuites = backend.getSuites().getDefaultLIRSuites(options);
        /*
             * We want Graal to perform all speculative optimistic optimizations, using the
             * profiling information that comes with the method (collected by the interpreter) for
             * speculation.
             */
        OptimisticOptimizations optimisticOpts = OptimisticOptimizations.ALL;
        ProfilingInfo profilingInfo = graph.getProfilingInfo(method);
        /* The default class and configuration for compilation results. */
        CompilationResult compilationResult = new CompilationResult(graph.compilationId());
        CompilationResultBuilderFactory factory = CompilationResultBuilderFactory.Default;
        /* Invoke the whole Graal compilation pipeline. */
        GraalCompiler.compileGraph(graph, method, providers, backend, graphBuilderSuite, optimisticOpts, profilingInfo, suites, lirSuites, compilationResult, factory);
        /*
             * Install the compilation result into the VM, i.e., copy the byte[] array that contains
             * the machine code into an actual executable memory location.
             */
        return backend.addInstalledCode(debug, method, asCompilationRequest(compilationId), compilationResult);
    } catch (Throwable ex) {
        throw debug.handle(ex);
    }
}
Also used : CompilationIdentifier(org.graalvm.compiler.core.common.CompilationIdentifier) OptionValues(org.graalvm.compiler.options.OptionValues) DebugDumpScope(org.graalvm.compiler.debug.DebugDumpScope) ProfilingInfo(jdk.vm.ci.meta.ProfilingInfo) DebugContext(org.graalvm.compiler.debug.DebugContext) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) LIRSuites(org.graalvm.compiler.lir.phases.LIRSuites) HighTierContext(org.graalvm.compiler.phases.tiers.HighTierContext) CompilationResult(org.graalvm.compiler.code.CompilationResult) OptimisticOptimizations(org.graalvm.compiler.phases.OptimisticOptimizations) CompilationResultBuilderFactory(org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory) LIRSuites(org.graalvm.compiler.lir.phases.LIRSuites) Suites(org.graalvm.compiler.phases.tiers.Suites)

Example 5 with CompilationIdentifier

use of org.graalvm.compiler.core.common.CompilationIdentifier in project graal by oracle.

the class GraalCompilerTest method compile.

/**
 * Compiles a given method.
 *
 * @param installedCodeOwner the method the compiled code will be associated with when installed
 * @param graph the graph to be compiled for {@code installedCodeOwner}. If null, a graph will
 *            be obtained from {@code installedCodeOwner} via
 *            {@link #parseForCompile(ResolvedJavaMethod)}.
 */
protected final CompilationResult compile(ResolvedJavaMethod installedCodeOwner, StructuredGraph graph) {
    OptionValues options = graph == null ? getInitialOptions() : graph.getOptions();
    CompilationIdentifier compilationId = getOrCreateCompilationId(installedCodeOwner, graph);
    return compile(installedCodeOwner, graph, new CompilationResult(compilationId), compilationId, options);
}
Also used : CompilationIdentifier(org.graalvm.compiler.core.common.CompilationIdentifier) OptionValues(org.graalvm.compiler.options.OptionValues) CompilationResult(org.graalvm.compiler.code.CompilationResult)

Aggregations

CompilationIdentifier (org.graalvm.compiler.core.common.CompilationIdentifier)16 DebugContext (org.graalvm.compiler.debug.DebugContext)10 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)10 CompilationResult (org.graalvm.compiler.code.CompilationResult)7 OptionValues (org.graalvm.compiler.options.OptionValues)7 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)6 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)4 DebugDumpScope (org.graalvm.compiler.debug.DebugDumpScope)3 InstalledCode (jdk.vm.ci.code.InstalledCode)2 Scope (org.graalvm.compiler.debug.DebugContext.Scope)2 GraalError (org.graalvm.compiler.debug.GraalError)2 LIRSuites (org.graalvm.compiler.lir.phases.LIRSuites)2 RemoveValueProxyPhase (org.graalvm.compiler.phases.common.RemoveValueProxyPhase)2 Suites (org.graalvm.compiler.phases.tiers.Suites)2 TruffleOptionsOverrideScope (org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleOptionsOverrideScope)2 RootNode (com.oracle.truffle.api.nodes.RootNode)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 ServiceConfigurationError (java.util.ServiceConfigurationError)1 BailoutException (jdk.vm.ci.code.BailoutException)1