Search in sources :

Example 1 with CompilationContext

use of org.graalvm.compiler.hotspot.CompilationContext in project graal by oracle.

the class TruffleToLibGraalEntryPoints method doCompile.

@TruffleToLibGraal(DoCompile)
@SuppressWarnings({ "unused", "try" })
@CEntryPoint(name = "Java_org_graalvm_compiler_truffle_runtime_hotspot_libgraal_TruffleToLibGraalCalls_doCompile")
public static void doCompile(JNIEnv env, JClass hsClazz, @CEntryPoint.IsolateThreadContext long isolateThreadId, long compilerHandle, long debugContextHandle, long compilationHandle, JByteArray hsOptions, JObject hsTask, JObject hsListener) {
    try (JNIMethodScope scope = LibGraalUtil.openScope(TruffleToLibGraalEntryPoints.class, DoCompile, env)) {
        TruffleCompilationIdentifier compilation = LibGraalObjectHandles.resolve(compilationHandle, TruffleCompilationIdentifier.class);
        try (CompilationContext hotSpotObjectConstantScope = HotSpotGraalServices.openLocalCompilationContext(compilation)) {
            HotSpotTruffleCompilerImpl compiler = LibGraalObjectHandles.resolve(compilerHandle, HotSpotTruffleCompilerImpl.class);
            TruffleDebugContext debugContext = LibGraalObjectHandles.resolve(debugContextHandle, TruffleDebugContext.class);
            Map<String, Object> options = decodeOptions(env, hsOptions);
            TruffleCompilationTask task = hsTask.isNull() ? null : new HSTruffleCompilationTask(hsTask);
            TruffleCompilerListener listener = hsListener.isNull() ? null : new HSTruffleCompilerListener(scope, hsListener);
            compiler.doCompile(debugContext, compilation, options, task, listener);
        }
    } catch (Throwable t) {
        JNIExceptionWrapper.throwInHotSpot(env, t);
    }
}
Also used : TruffleCompilationIdentifier(org.graalvm.compiler.truffle.compiler.TruffleCompilationIdentifier) JNIMethodScope(org.graalvm.jniutils.JNIMethodScope) TruffleDebugContext(org.graalvm.compiler.truffle.common.TruffleDebugContext) JNIUtil.createString(org.graalvm.jniutils.JNIUtil.createString) JString(org.graalvm.jniutils.JNI.JString) GetSuppliedString(org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal.Id.GetSuppliedString) JNIUtil.createHSString(org.graalvm.jniutils.JNIUtil.createHSString) TruffleCompilationTask(org.graalvm.compiler.truffle.common.TruffleCompilationTask) HotSpotTruffleCompilerImpl(org.graalvm.compiler.truffle.compiler.hotspot.HotSpotTruffleCompilerImpl) CompilationContext(org.graalvm.compiler.hotspot.CompilationContext) HSObject(org.graalvm.jniutils.HSObject) JObject(org.graalvm.jniutils.JNI.JObject) TruffleCompilerListener(org.graalvm.compiler.truffle.common.TruffleCompilerListener) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) TruffleToLibGraal(org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal)

Example 2 with CompilationContext

use of org.graalvm.compiler.hotspot.CompilationContext in project graal by oracle.

the class LibGraalEntryPoints method compileMethod.

/**
 * The implementation of
 * {@code org.graalvm.compiler.hotspot.test.CompileTheWorld.compileMethodInLibgraal()}.
 *
 * @param methodHandle the method to be compiled. This is a handle to a
 *            {@link HotSpotResolvedJavaMethod} in HotSpot's heap. A value of 0L can be passed
 *            to use this method for the side effect of initializing a
 *            {@link HotSpotGraalCompiler} instance without doing any compilation.
 * @param useProfilingInfo specifies if profiling info should be used during the compilation
 * @param installAsDefault specifies if the compiled code should be installed for the
 *            {@code Method*} associated with {@code methodHandle}
 * @param printMetrics specifies if global metrics should be printed and reset
 * @param optionsAddress native byte buffer storing a serialized {@link OptionValues} object
 * @param optionsSize the number of bytes in the buffer
 * @param optionsHash hash code of bytes in the buffer (computed with
 *            {@link Arrays#hashCode(byte[])})
 * @param stackTraceAddress a native buffer in which a serialized stack trace can be returned.
 *            The caller will only read from this buffer if this method returns 0. A returned
 *            serialized stack trace is returned in this buffer with the following format:
 *
 *            <pre>
 *            struct {
 *                int   length;
 *                byte  data[length]; // Bytes from a stack trace printed to a ByteArrayOutputStream.
 *            }
 *            </pre>
 *
 *            where {@code length} truncated to {@code stackTraceCapacity - 4} if necessary
 *
 * @param stackTraceCapacity the size of the stack trace buffer
 * @return a handle to a {@link InstalledCode} in HotSpot's heap or 0 if compilation failed
 */
@SuppressWarnings({ "unused", "try" })
@CEntryPoint(name = "Java_org_graalvm_compiler_hotspot_test_CompileTheWorld_compileMethodInLibgraal", include = LibGraalFeature.IsEnabled.class)
private static long compileMethod(PointerBase jniEnv, PointerBase jclass, @CEntryPoint.IsolateThreadContext long isolateThread, long methodHandle, boolean useProfilingInfo, boolean installAsDefault, boolean printMetrics, long optionsAddress, int optionsSize, int optionsHash, long stackTraceAddress, int stackTraceCapacity) {
    try {
        HotSpotJVMCIRuntime runtime = runtime();
        HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
        if (methodHandle == 0L) {
            return 0L;
        }
        HotSpotResolvedJavaMethod method = LibGraal.unhand(HotSpotResolvedJavaMethod.class, methodHandle);
        int entryBCI = JVMCICompiler.INVOCATION_ENTRY_BCI;
        HotSpotCompilationRequest request = new HotSpotCompilationRequest(method, entryBCI, 0L);
        try (CompilationContext scope = HotSpotGraalServices.openLocalCompilationContext(request)) {
            OptionValues options = decodeOptions(optionsAddress, optionsSize, optionsHash);
            CompilationTask task = new CompilationTask(runtime, compiler, request, useProfilingInfo, installAsDefault);
            task.runCompilation(options);
            HotSpotInstalledCode installedCode = task.getInstalledCode();
            if (printMetrics) {
                GlobalMetrics metricValues = ((HotSpotGraalRuntime) compiler.getGraalRuntime()).getMetricValues();
                metricValues.print(options);
                metricValues.clear();
            }
            return LibGraal.translate(installedCode);
        }
    } catch (Throwable t) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        t.printStackTrace(new PrintStream(baos));
        byte[] stackTrace = baos.toByteArray();
        int length = Math.min(stackTraceCapacity - Integer.BYTES, stackTrace.length);
        UNSAFE.putInt(stackTraceAddress, length);
        UNSAFE.copyMemory(stackTrace, ARRAY_BYTE_BASE_OFFSET, null, stackTraceAddress + Integer.BYTES, length);
        return 0L;
    } finally {
        /*
             * libgraal doesn't use a dedicated reference handler thread, so we trigger the
             * reference handling manually when a compilation finishes.
             */
        Heap.getHeap().doReferenceHandling();
    }
}
Also used : HotSpotGraalCompiler(org.graalvm.compiler.hotspot.HotSpotGraalCompiler) PrintStream(java.io.PrintStream) HotSpotResolvedJavaMethod(jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod) OptionValues(org.graalvm.compiler.options.OptionValues) HotSpotJVMCIRuntime(jdk.vm.ci.hotspot.HotSpotJVMCIRuntime) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HotSpotCompilationRequest(jdk.vm.ci.hotspot.HotSpotCompilationRequest) GlobalMetrics(org.graalvm.compiler.debug.GlobalMetrics) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CompilationTask(org.graalvm.compiler.hotspot.CompilationTask) CompilationContext(org.graalvm.compiler.hotspot.CompilationContext) HotSpotGraalRuntime(org.graalvm.compiler.hotspot.HotSpotGraalRuntime) HotSpotInstalledCode(jdk.vm.ci.hotspot.HotSpotInstalledCode) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Aggregations

CompilationContext (org.graalvm.compiler.hotspot.CompilationContext)2 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 HotSpotCompilationRequest (jdk.vm.ci.hotspot.HotSpotCompilationRequest)1 HotSpotInstalledCode (jdk.vm.ci.hotspot.HotSpotInstalledCode)1 HotSpotJVMCIRuntime (jdk.vm.ci.hotspot.HotSpotJVMCIRuntime)1 HotSpotResolvedJavaMethod (jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod)1 GlobalMetrics (org.graalvm.compiler.debug.GlobalMetrics)1 CompilationTask (org.graalvm.compiler.hotspot.CompilationTask)1 HotSpotGraalCompiler (org.graalvm.compiler.hotspot.HotSpotGraalCompiler)1 HotSpotGraalRuntime (org.graalvm.compiler.hotspot.HotSpotGraalRuntime)1 OptionValues (org.graalvm.compiler.options.OptionValues)1 TruffleCompilationTask (org.graalvm.compiler.truffle.common.TruffleCompilationTask)1 TruffleCompilerListener (org.graalvm.compiler.truffle.common.TruffleCompilerListener)1 TruffleDebugContext (org.graalvm.compiler.truffle.common.TruffleDebugContext)1 TruffleToLibGraal (org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal)1 GetSuppliedString (org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal.Id.GetSuppliedString)1 TruffleCompilationIdentifier (org.graalvm.compiler.truffle.compiler.TruffleCompilationIdentifier)1 HotSpotTruffleCompilerImpl (org.graalvm.compiler.truffle.compiler.hotspot.HotSpotTruffleCompilerImpl)1