Search in sources :

Example 11 with OptimizedCallTarget

use of org.graalvm.compiler.truffle.runtime.OptimizedCallTarget in project graal by oracle.

the class OptimizedCallTargetTest method testInCompilationRootDirective.

@Ignore
@Test
public void testInCompilationRootDirective() {
    final int compilationThreshold = TruffleCompilerOptions.getValue(TruffleCompilationThreshold);
    int[] outerExecute = { 0 };
    int[] outerMethod = { 0 };
    int[] outerBoundary = { 0 };
    int[] innerExecute = { 0 };
    int[] innerMethod = { 0 };
    int[] innerBoundary = { 0 };
    final OptimizedCallTarget innerTarget = (OptimizedCallTarget) runtime.createCallTarget(new RootNode(null) {

        @Override
        public String toString() {
            return "inner";
        }

        @Override
        public Object execute(VirtualFrame frame) {
            // FALSE
            if (CompilerDirectives.inCompilationRoot()) {
                innerExecute[0]++;
            }
            innerMethod();
            return null;
        }

        @CompilerDirectives.TruffleBoundary
        void innerMethod() {
            // FALSE
            if (CompilerDirectives.inCompilationRoot()) {
                innerMethod[0]++;
            }
        }

        @CompilerDirectives.TruffleBoundary
        void innerBoundary() {
            // FALSE
            if (CompilerDirectives.inCompilationRoot()) {
                innerBoundary[0] = 1;
            }
        }
    });
    final OptimizedCallTarget outerTarget = (OptimizedCallTarget) runtime.createCallTarget(new RootNode(null) {

        @Override
        public String toString() {
            return "outer";
        }

        @Child
        private DirectCallNode child = runtime.createDirectCallNode(innerTarget);

        @Override
        public Object execute(VirtualFrame frame) {
            // TRUE
            if (CompilerDirectives.inCompilationRoot()) {
                outerExecute[0]++;
            }
            outerMethod();
            return child.call(new Object[0]);
        }

        void outerMethod() {
            // TRUE
            if (CompilerDirectives.inCompilationRoot()) {
                outerMethod[0]++;
                outerBoundary();
            }
        }

        @CompilerDirectives.TruffleBoundary
        void outerBoundary() {
            // FALSE
            if (CompilerDirectives.inCompilationRoot()) {
                outerBoundary[0]++;
            }
        }
    });
    for (int i = 0; i < compilationThreshold; i++) {
        outerTarget.call();
    }
    assertCompiled(outerTarget);
    final int executionCount = 10;
    for (int i = 0; i < executionCount; i++) {
        outerTarget.call();
    }
    Assert.assertEquals(executionCount, outerExecute[0]);
    Assert.assertEquals(executionCount, outerMethod[0]);
    Assert.assertEquals(0, outerBoundary[0]);
    Assert.assertEquals(0, innerExecute[0]);
    Assert.assertEquals(0, innerMethod[0]);
    Assert.assertEquals(0, innerBoundary[0]);
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with OptimizedCallTarget

use of org.graalvm.compiler.truffle.runtime.OptimizedCallTarget 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 13 with OptimizedCallTarget

use of org.graalvm.compiler.truffle.runtime.OptimizedCallTarget in project graal by oracle.

the class OptimizedOSRLoopNodeTest method testStackFrameNodes.

/**
 * Test that graal stack frame instances have call nodes associated even when there are OSR
 * frames on the stack.
 */
@Theory
public void testStackFrameNodes(OSRLoopFactory factory) {
    TestOSRStackTraceFromAbove testOSRStackTrace = new TestOSRStackTraceFromAbove();
    TestRootNode rootNode = new TestRootNode(factory, testOSRStackTrace);
    OptimizedCallTarget target = (OptimizedCallTarget) runtime.createCallTarget(rootNode);
    rootNode.forceOSR();
    target.call(1);
}
Also used : OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) Theory(org.junit.experimental.theories.Theory)

Example 14 with OptimizedCallTarget

use of org.graalvm.compiler.truffle.runtime.OptimizedCallTarget in project graal by oracle.

the class OptimizedOSRLoopNodeTest method testOSRAndRewriteDoesNotSuppressTargetCompilation.

@SuppressWarnings("try")
@Theory
public void testOSRAndRewriteDoesNotSuppressTargetCompilation(OSRLoopFactory factory) {
    try (TruffleCompilerOptions.TruffleOptionsOverrideScope s = TruffleCompilerOptions.overrideOptions(TruffleCompilerOptions.TruffleCompilationThreshold, 3)) {
        TestRootNodeWithReplacement rootNode = new TestRootNodeWithReplacement(factory, new TestRepeatingNode());
        OptimizedCallTarget target = runtime.createOptimizedCallTarget(null, rootNode);
        target.call(OSR_THRESHOLD + 1);
        assertCompiled(rootNode.getOSRTarget());
        assertNotCompiled(target);
        target.nodeReplaced(rootNode.toReplace, new TestRepeatingNode(), "test");
        for (int i = 0; i < TruffleCompilerOptions.getValue(TruffleCompilationThreshold) + TruffleCompilerOptions.getValue(TruffleReplaceReprofileCount) - 1; i++) {
            target.call(2);
        }
        assertCompiled(rootNode.getOSRTarget());
        assertCompiled(target);
    }
}
Also used : OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) TruffleCompilerOptions(org.graalvm.compiler.truffle.common.TruffleCompilerOptions) DataPoint(org.junit.experimental.theories.DataPoint) Theory(org.junit.experimental.theories.Theory)

Example 15 with OptimizedCallTarget

use of org.graalvm.compiler.truffle.runtime.OptimizedCallTarget 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

OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)57 Test (org.junit.Test)42 RootNode (com.oracle.truffle.api.nodes.RootNode)18 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)14 RootTestNode (org.graalvm.compiler.truffle.test.nodes.RootTestNode)14 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)13 AbstractTestNode (org.graalvm.compiler.truffle.test.nodes.AbstractTestNode)11 TruffleOptionsOverrideScope (org.graalvm.compiler.truffle.common.TruffleCompilerOptions.TruffleOptionsOverrideScope)8 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)6 TruffleCompilerOptions (org.graalvm.compiler.truffle.common.TruffleCompilerOptions)6 Specialization (com.oracle.truffle.api.dsl.Specialization)4 CompilationIdentifier (org.graalvm.compiler.core.common.CompilationIdentifier)4 GraalTruffleRuntime (org.graalvm.compiler.truffle.runtime.GraalTruffleRuntime)4 OptimizedDirectCallNode (org.graalvm.compiler.truffle.runtime.OptimizedDirectCallNode)4 Theory (org.junit.experimental.theories.Theory)4 Assumption (com.oracle.truffle.api.Assumption)3 LoopNode (com.oracle.truffle.api.nodes.LoopNode)3 Ignore (org.junit.Ignore)3 Node (com.oracle.truffle.api.nodes.Node)2 WeakReference (java.lang.ref.WeakReference)2