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);
}
}
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);
}
}
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;
}
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);
}
}
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);
}
Aggregations