Search in sources :

Example 1 with Assumption

use of com.oracle.truffle.api.Assumption in project graal by oracle.

the class OptimizedCallTargetTest method testRewriteAssumption.

@Test
public void testRewriteAssumption() {
    String testName = "testRewriteAssumption";
    assertTrue("test only works with inlining enabled", TruffleCompilerOptions.getValue(TruffleFunctionInlining));
    try (TruffleOptionsOverrideScope s = TruffleCompilerOptions.overrideOptions(TruffleCompilerOptions.TruffleCompilationThreshold, 20)) {
        final int compilationThreshold = TruffleCompilerOptions.getValue(TruffleCompilationThreshold);
        assertTrue(compilationThreshold >= 2);
        OptimizedCallTarget innermostCallTarget = (OptimizedCallTarget) runtime.createCallTarget(new RootTestNode(new FrameDescriptor(), testName + 0, new AbstractTestNode() {

            @Child
            private AbstractTestNode child = new ConstantTestNode(42);

            @Child
            private AbstractTestNode dummy = new ConstantTestNode(17);

            @Override
            public int execute(VirtualFrame frame) {
                int k = (int) frame.getArguments()[0];
                if (k > compilationThreshold) {
                    CompilerDirectives.transferToInterpreter();
                    dummy.replace(new ConstantTestNode(k));
                }
                return child.execute(frame);
            }
        }));
        OptimizedCallTarget ct = innermostCallTarget;
        ct = (OptimizedCallTarget) runtime.createCallTarget(new RootTestNode(new FrameDescriptor(), testName + 1, new CallTestNode(ct)));
        ct = (OptimizedCallTarget) runtime.createCallTarget(new RootTestNode(new FrameDescriptor(), testName + 2, new CallTestNode(ct)));
        final OptimizedCallTarget outermostCallTarget = ct;
        assertNull("assumption is initially null", getRewriteAssumption(innermostCallTarget));
        IntStream.range(0, compilationThreshold / 2).parallel().forEach(k -> {
            assertEquals(42, outermostCallTarget.call(k));
            assertNull("assumption stays null in the interpreter", getRewriteAssumption(innermostCallTarget));
        });
        outermostCallTarget.compile();
        assertCompiled(outermostCallTarget);
        Assumption firstRewriteAssumption = getRewriteAssumption(innermostCallTarget);
        assertNotNull("assumption must not be null after compilation", firstRewriteAssumption);
        assertTrue(firstRewriteAssumption.isValid());
        List<Assumption> rewriteAssumptions = IntStream.range(0, 2 * compilationThreshold).parallel().mapToObj(k -> {
            assertEquals(42, outermostCallTarget.call(k));
            Assumption rewriteAssumptionAfter = getRewriteAssumption(innermostCallTarget);
            assertNotNull("assumption must not be null after compilation", rewriteAssumptionAfter);
            return rewriteAssumptionAfter;
        }).collect(Collectors.toList());
        Assumption finalRewriteAssumption = getRewriteAssumption(innermostCallTarget);
        assertNotNull("assumption must not be null after compilation", finalRewriteAssumption);
        assertNotSame(firstRewriteAssumption, finalRewriteAssumption);
        assertFalse(firstRewriteAssumption.isValid());
        assertTrue(finalRewriteAssumption.isValid());
        assertFalse(rewriteAssumptions.stream().filter(a -> a != finalRewriteAssumption).anyMatch(Assumption::isValid));
    }
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) IntStream(java.util.stream.IntStream) AbstractTestNode(org.graalvm.compiler.truffle.test.nodes.AbstractTestNode) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) ConstantTestNode(org.graalvm.compiler.truffle.test.nodes.ConstantTestNode) Assert.assertNotSame(org.junit.Assert.assertNotSame) TruffleCompileOnly(org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleCompileOnly) TruffleOptionsOverrideScope(org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleOptionsOverrideScope) FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) CompilerDirectives(com.oracle.truffle.api.CompilerDirectives) RootTestNode(org.graalvm.compiler.truffle.test.nodes.RootTestNode) LoopNode(com.oracle.truffle.api.nodes.LoopNode) RootNode(com.oracle.truffle.api.nodes.RootNode) TruffleReplaceReprofileCount(org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleReplaceReprofileCount) TruffleOSRCompilationThreshold(org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleOSRCompilationThreshold) RepeatingNode(com.oracle.truffle.api.nodes.RepeatingNode) Node(com.oracle.truffle.api.nodes.Node) TruffleFunctionInlining(org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleFunctionInlining) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Assumption(com.oracle.truffle.api.Assumption) Test(org.junit.Test) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) CallTarget(com.oracle.truffle.api.CallTarget) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) GraalTruffleRuntime(org.graalvm.compiler.truffle.runtime.GraalTruffleRuntime) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) OptimizedOSRLoopNode(org.graalvm.compiler.truffle.runtime.OptimizedOSRLoopNode) TruffleCompilerOptions(org.graalvm.compiler.truffle.common.TruffleCompilerOptions) Truffle(com.oracle.truffle.api.Truffle) TruffleCompilationThreshold(org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleCompilationThreshold) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Util(org.graalvm.compiler.core.common.util.Util) AbstractTestNode(org.graalvm.compiler.truffle.test.nodes.AbstractTestNode) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) Assumption(com.oracle.truffle.api.Assumption) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) ConstantTestNode(org.graalvm.compiler.truffle.test.nodes.ConstantTestNode) TruffleOptionsOverrideScope(org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleOptionsOverrideScope) RootTestNode(org.graalvm.compiler.truffle.test.nodes.RootTestNode) Test(org.junit.Test)

Example 2 with Assumption

use of com.oracle.truffle.api.Assumption in project graal by oracle.

the class OptimizedCompilationProfile method profileDirectCall.

@ExplodeLoop
void profileDirectCall(Object[] args) {
    Assumption typesAssumption = profiledArgumentTypesAssumption;
    if (typesAssumption == null) {
        if (CompilerDirectives.inInterpreter()) {
            initializeProfiledArgumentTypes(args);
        }
    } else {
        Class<?>[] types = profiledArgumentTypes;
        if (types != null) {
            if (types.length != args.length) {
                CompilerDirectives.transferToInterpreterAndInvalidate();
                typesAssumption.invalidate();
                profiledArgumentTypes = null;
            } else if (typesAssumption.isValid()) {
                for (int i = 0; i < types.length; i++) {
                    Class<?> type = types[i];
                    Object value = args[i];
                    if (type != null && (value == null || value.getClass() != type)) {
                        CompilerDirectives.transferToInterpreterAndInvalidate();
                        updateProfiledArgumentTypes(args, types);
                        break;
                    }
                }
            }
        }
    }
}
Also used : Assumption(com.oracle.truffle.api.Assumption) ExplodeLoop(com.oracle.truffle.api.nodes.ExplodeLoop)

Example 3 with Assumption

use of com.oracle.truffle.api.Assumption in project graal by oracle.

the class OptimizedCompilationProfile method profileReturnValue.

final void profileReturnValue(Object result) {
    Assumption returnTypeAssumption = profiledReturnTypeAssumption;
    if (CompilerDirectives.inInterpreter() && returnTypeAssumption == null) {
        // for immediate compiles.
        if (TruffleCompilerOptions.getValue(TruffleReturnTypeSpeculation)) {
            profiledReturnType = classOf(result);
            profiledReturnTypeAssumption = createValidAssumption("Profiled Return Type");
        }
    } else if (profiledReturnType != null) {
        if (result == null || profiledReturnType != result.getClass()) {
            CompilerDirectives.transferToInterpreterAndInvalidate();
            returnTypeAssumption.invalidate();
            profiledReturnType = null;
        }
    }
}
Also used : Assumption(com.oracle.truffle.api.Assumption)

Example 4 with Assumption

use of com.oracle.truffle.api.Assumption in project graal by oracle.

the class OptimizedCompilationProfile method profileIndirectCall.

void profileIndirectCall() {
    Assumption argumentTypesAssumption = profiledArgumentTypesAssumption;
    if (argumentTypesAssumption != null && argumentTypesAssumption.isValid()) {
        // Argument profiling is not possible for targets of indirect calls.
        CompilerDirectives.transferToInterpreter();
        argumentTypesAssumption.invalidate();
        profiledArgumentTypes = null;
    }
}
Also used : Assumption(com.oracle.truffle.api.Assumption)

Example 5 with Assumption

use of com.oracle.truffle.api.Assumption in project graal by oracle.

the class AssumptionPartialEvaluationTest method assumptionBranchCutoff.

/**
 * Tests whether a valid {@link Assumption} cuts off a non-executed branch.
 */
@Test
public void assumptionBranchCutoff() {
    Assumption assumption = Truffle.getRuntime().createAssumption();
    AssumptionCutsBranchTestNode result = new AssumptionCutsBranchTestNode(assumption);
    RootTestNode rootNode = new RootTestNode(new FrameDescriptor(), "cutoffBranch", result);
    OptimizedCallTarget compilable = compileHelper("cutoffBranch", rootNode, new Object[0]);
    for (int i = 0; i < 100000; i++) {
        Assert.assertEquals(0, compilable.call(new Object[0]));
    }
    Assert.assertNull(result.getChildNode());
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) AssumptionCutsBranchTestNode(org.graalvm.compiler.truffle.test.nodes.AssumptionCutsBranchTestNode) RootTestNode(org.graalvm.compiler.truffle.test.nodes.RootTestNode) Assumption(com.oracle.truffle.api.Assumption) OptimizedAssumption(org.graalvm.compiler.truffle.runtime.OptimizedAssumption) Test(org.junit.Test)

Aggregations

Assumption (com.oracle.truffle.api.Assumption)42 Test (org.junit.Test)25 UnionAssumption (com.oracle.truffle.api.utilities.UnionAssumption)8 CallTarget (com.oracle.truffle.api.CallTarget)5 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)5 TestHelper.createCallTarget (com.oracle.truffle.api.dsl.test.TestHelper.createCallTarget)3 Field (java.lang.reflect.Field)3 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)3 RootTestNode (org.graalvm.compiler.truffle.test.nodes.RootTestNode)3 CompilationFinal (com.oracle.truffle.api.CompilerDirectives.CompilationFinal)2 NodeField (com.oracle.truffle.api.dsl.NodeField)2 ExplodeLoop (com.oracle.truffle.api.nodes.ExplodeLoop)2 Node (com.oracle.truffle.api.nodes.Node)2 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)2 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)2 CompilerDirectives (com.oracle.truffle.api.CompilerDirectives)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 Truffle (com.oracle.truffle.api.Truffle)1 TruffleRuntime (com.oracle.truffle.api.TruffleRuntime)1 Cached (com.oracle.truffle.api.dsl.Cached)1