Search in sources :

Example 11 with InstalledCode

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

the class GraalTutorial method testIntrinsicIntegerReverseBytes.

@Test
public void testIntrinsicIntegerReverseBytes() throws InvalidInstalledCodeException {
    int input = 0x12345678;
    int expected = intrinsicIntegerReverseBytes(input);
    InstalledCode compiledMethod = compileAndInstallMethod(findMethod(GraalTutorial.class, "intrinsicIntegerReverseBytes"));
    int actual = (int) compiledMethod.executeVarargs(input);
    Assert.assertEquals(expected, actual);
}
Also used : InstalledCode(jdk.vm.ci.code.InstalledCode) Test(org.junit.Test)

Example 12 with InstalledCode

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

the class GraalTutorial method testSpeculativeOptimization.

@Test
public void testSpeculativeOptimization() throws InvalidInstalledCodeException {
    for (int i = 0; i < 10000; i++) {
        /* Execute several times so that enough profiling information gets collected. */
        speculativeOptimization(false);
    }
    /*
         * Warmup to collect profiling information is done, now we compile the method. Since the
         * value of "flag" was always false during the warmup, the compiled code speculates that the
         * value remains false.
         */
    InstalledCode compiledMethod = compileAndInstallMethod(findMethod(GraalTutorial.class, "speculativeOptimization"));
    f1 = 0;
    f2 = 0;
    compiledMethod.executeVarargs(this, true);
    Assert.assertEquals(41, f1);
    Assert.assertEquals(42, f2);
    /*
         * We executed the compiled method with a "flag" value that triggered deoptimization (since
         * the warmup always used the different "flag" value). The interpreter updated the profiling
         * information, so the second compilation does not perform the speculative optimization.
         */
    compiledMethod = compileAndInstallMethod(findMethod(GraalTutorial.class, "speculativeOptimization"));
    f1 = 0;
    f2 = 0;
    compiledMethod.executeVarargs(this, false);
    Assert.assertEquals(41, f1);
    Assert.assertEquals(43, f2);
}
Also used : InstalledCode(jdk.vm.ci.code.InstalledCode) Test(org.junit.Test)

Example 13 with InstalledCode

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

the class GraalTutorial method testInstanceOfUsage.

@Test
public void testInstanceOfUsage() throws InvalidInstalledCodeException {
    /*
         * Collect profiling information by running the method in the interpreter.
         */
    A a = new A();
    /* Allocate an (unused) instance of B so that the class B gets loaded. */
    @SuppressWarnings("unused") B b = new B();
    int expectedResult = instanceOfUsage(a);
    for (int i = 0; i < 10000; i++) {
        /* Execute several times so that enough profiling information gets collected. */
        instanceOfUsage(a);
    }
    /* Warmup to collect profiling information is done, now compile the method. */
    InstalledCode compiledMethod = compileAndInstallMethod(findMethod(GraalTutorial.class, "instanceOfUsage"));
    int result = (int) compiledMethod.executeVarargs(a);
    Assert.assertEquals(expectedResult, result);
}
Also used : InstalledCode(jdk.vm.ci.code.InstalledCode) Test(org.junit.Test)

Example 14 with InstalledCode

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

the class GraalTutorial method testIdentityHashCodeUsage.

@Test
public void testIdentityHashCodeUsage() throws InvalidInstalledCodeException {
    Object a = new Object();
    int expectedResult = identityHashCodeUsage(a);
    InstalledCode compiledMethod = compileAndInstallMethod(findMethod(GraalTutorial.class, "identityHashCodeUsage"));
    int result = (int) compiledMethod.executeVarargs(a);
    Assert.assertEquals(expectedResult, result);
}
Also used : InstalledCode(jdk.vm.ci.code.InstalledCode) Test(org.junit.Test)

Example 15 with InstalledCode

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

the class Backend method createInstalledCode.

/**
 * Installs code based on a given compilation result.
 *
 * @param method the method compiled to produce {@code compiledCode} or {@code null} if the
 *            input to {@code compResult} was not a {@link ResolvedJavaMethod}
 * @param compilationRequest the compilation request or {@code null}
 * @param compilationResult the code to be installed
 * @param predefinedInstalledCode a pre-allocated {@link InstalledCode} object to use as a
 *            reference to the installed code. If {@code null}, a new {@link InstalledCode}
 *            object will be created.
 * @param speculationLog the speculation log to be used
 * @param isDefault specifies if the installed code should be made the default implementation of
 *            {@code compRequest.getMethod()}. The default implementation for a method is the
 *            code executed for standard calls to the method. This argument is ignored if
 *            {@code compRequest == null}.
 * @param context a custom debug context to use for the code installation
 * @return a reference to the compiled and ready-to-run installed code
 * @throws BailoutException if the code installation failed
 */
@SuppressWarnings("try")
public InstalledCode createInstalledCode(DebugContext debug, ResolvedJavaMethod method, CompilationRequest compilationRequest, CompilationResult compilationResult, SpeculationLog speculationLog, InstalledCode predefinedInstalledCode, boolean isDefault, Object[] context) {
    Object[] debugContext = context != null ? context : new Object[] { getProviders().getCodeCache(), method, compilationResult };
    CodeInstallationTask[] tasks;
    synchronized (this) {
        tasks = new CodeInstallationTask[codeInstallationTaskFactories.size()];
        for (int i = 0; i < codeInstallationTaskFactories.size(); i++) {
            tasks[i] = codeInstallationTaskFactories.get(i).create();
        }
    }
    try (DebugContext.Scope s2 = debug.scope("CodeInstall", debugContext);
        DebugContext.Activation a = debug.activate()) {
        InstalledCode installedCode;
        try {
            preCodeInstallationTasks(tasks, compilationResult, predefinedInstalledCode);
            CompiledCode compiledCode = createCompiledCode(method, compilationRequest, compilationResult);
            installedCode = getProviders().getCodeCache().installCode(method, compiledCode, predefinedInstalledCode, speculationLog, isDefault);
            assert predefinedInstalledCode == null || installedCode == predefinedInstalledCode;
        } catch (Throwable t) {
            failCodeInstallationTasks(tasks, t);
            throw t;
        }
        postCodeInstallationTasks(tasks, installedCode);
        return installedCode;
    } catch (Throwable e) {
        throw debug.handle(e);
    }
}
Also used : InstalledCode(jdk.vm.ci.code.InstalledCode) CompiledCode(jdk.vm.ci.code.CompiledCode) DebugContext(org.graalvm.compiler.debug.DebugContext)

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