Search in sources :

Example 56 with OptimizedCallTarget

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

the class AllocationReporterPartialEvaluationTest method testConsistentAssertions.

@Test
public void testConsistentAssertions() {
    // Test that onEnter()/onReturnValue() are not broken
    // when only one of them is compiled with PE.
    Context context = Context.newBuilder(AllocationReporterLanguage.ID).build();
    context.initialize(AllocationReporterLanguage.ID);
    final TestAllocationReporter tester = context.getEngine().getInstruments().get(TestAllocationReporter.ID).lookup(TestAllocationReporter.class);
    assertNotNull(tester);
    final AllocationReporter reporter = (AllocationReporter) context.getPolyglotBindings().getMember(AllocationReporter.class.getSimpleName()).asHostObject();
    final Long[] value = new Long[] { 1L };
    OptimizedCallTarget enterTarget = (OptimizedCallTarget) runtime.createCallTarget(new RootNode(null) {

        @Override
        public Object execute(VirtualFrame frame) {
            reporter.onEnter(value[0], 4, 8);
            return null;
        }
    });
    OptimizedCallTarget returnTarget = (OptimizedCallTarget) runtime.createCallTarget(new RootNode(null) {

        @Override
        public Object execute(VirtualFrame frame) {
            reporter.onReturnValue(value[0], 4, 8);
            return null;
        }
    });
    // Interpret both:
    assertNotCompiled(enterTarget);
    enterTarget.call();
    assertNotCompiled(returnTarget);
    returnTarget.call();
    value[0]++;
    enterTarget.compile();
    returnTarget.compile();
    assertCompiled(enterTarget);
    assertCompiled(returnTarget);
    long expectedCounters = allocCounter(value[0]);
    assertEquals(expectedCounters, tester.getEnterCount());
    assertEquals(expectedCounters, tester.getReturnCount());
    for (int j = 0; j < 2; j++) {
        // Compile both:
        for (int i = 0; i < 5; i++) {
            assertCompiled(enterTarget);
            enterTarget.call();
            assertCompiled(returnTarget);
            returnTarget.call();
            value[0]++;
        }
        expectedCounters = allocCounter(value[0]);
        assertEquals(expectedCounters, tester.getEnterCount());
        assertEquals(expectedCounters, tester.getReturnCount());
        // Deoptimize enter:
        enterTarget.invalidate(this, "test");
        assertNotCompiled(enterTarget);
        enterTarget.call();
        assertCompiled(returnTarget);
        returnTarget.call();
        value[0]++;
        enterTarget.compile();
        returnTarget.compile();
        assertCompiled(enterTarget);
        assertCompiled(returnTarget);
        // Deoptimize return:
        returnTarget.invalidate(this, "test");
        assertCompiled(enterTarget);
        enterTarget.call();
        assertNotCompiled(returnTarget);
        returnTarget.call();
        value[0]++;
        enterTarget.compile();
        returnTarget.compile();
        assertCompiled(enterTarget);
        assertCompiled(returnTarget);
        // Deoptimize both:
        enterTarget.invalidate(this, "test");
        returnTarget.invalidate(this, "test");
        assertNotCompiled(enterTarget);
        enterTarget.call();
        assertNotCompiled(returnTarget);
        returnTarget.call();
        value[0]++;
        enterTarget.compile();
        returnTarget.compile();
        assertCompiled(enterTarget);
        assertCompiled(returnTarget);
    }
    // Check that the allocation calls happened:
    expectedCounters = allocCounter(value[0]);
    assertEquals(expectedCounters, tester.getEnterCount());
    assertEquals(expectedCounters, tester.getReturnCount());
    assertCompiled(enterTarget);
    assertCompiled(returnTarget);
    // Verify that the assertions work in the compiled code:
    value[0] = null;
    boolean expectedFailure = true;
    // Deoptimize for assertions to be active
    enterTarget.invalidate(this, "test");
    try {
        enterTarget.call();
        expectedFailure = false;
    } catch (AssertionError err) {
    // O.K.
    }
    assertTrue("onEnter(null) did not fail!", expectedFailure);
    // Deoptimize for assertions to be active
    returnTarget.invalidate(this, "test");
    value[0] = Long.MIN_VALUE;
    try {
        returnTarget.call();
        expectedFailure = false;
    } catch (AssertionError err) {
    // O.K.
    }
    assertTrue("onReturn(<unseen value>) did not fail!", expectedFailure);
}
Also used : Context(org.graalvm.polyglot.Context) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) AllocationReporter(com.oracle.truffle.api.instrumentation.AllocationReporter) Test(org.junit.Test)

Example 57 with OptimizedCallTarget

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

the class AssumptionPartialEvaluationTest method constantValue.

@Test
public void constantValue() {
    Assumption assumption = Truffle.getRuntime().createAssumption();
    AbstractTestNode result = new ConstantWithAssumptionTestNode(assumption, 42);
    RootTestNode rootNode = new RootTestNode(new FrameDescriptor(), "constantValue", result);
    OptimizedCallTarget callTarget = assertPartialEvalEquals("constant42", rootNode);
    Assert.assertTrue(callTarget.isValid());
    assertDeepEquals(42, callTarget.call());
    Assert.assertTrue(callTarget.isValid());
    try {
        assumption.check();
    } catch (InvalidAssumptionException e) {
        Assert.fail("Assumption must not have been invalidated.");
    }
    assumption.invalidate();
    try {
        assumption.check();
        Assert.fail("Assumption must have been invalidated.");
    } catch (InvalidAssumptionException e) {
    }
    Assert.assertFalse(callTarget.isValid());
    assertDeepEquals(43, callTarget.call());
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) ConstantWithAssumptionTestNode(org.graalvm.compiler.truffle.test.nodes.ConstantWithAssumptionTestNode) AbstractTestNode(org.graalvm.compiler.truffle.test.nodes.AbstractTestNode) InvalidAssumptionException(com.oracle.truffle.api.nodes.InvalidAssumptionException) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) 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