Search in sources :

Example 11 with Assumption

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

the class FlatNodeGenFactory method createFields.

private void createFields(CodeTypeElement clazz) {
    state.declareFields(clazz);
    if (requiresExclude()) {
        exclude.declareFields(clazz);
    }
    for (SpecializationData specialization : reachableSpecializations) {
        List<CodeVariableElement> fields = new ArrayList<>();
        boolean useSpecializationClass = useSpecializationClass(specialization);
        for (CacheExpression cache : specialization.getCaches()) {
            Parameter parameter = cache.getParameter();
            String fieldName = createFieldName(specialization, parameter);
            TypeMirror type = parameter.getType();
            Modifier visibility = useSpecializationClass ? null : Modifier.PRIVATE;
            CodeVariableElement cachedField;
            if (ElementUtils.isAssignable(type, context.getType(NodeInterface.class))) {
                cachedField = createNodeField(visibility, type, fieldName, Child.class);
            } else if (isNodeInterfaceArray(type)) {
                cachedField = createNodeField(visibility, type, fieldName, Children.class);
            } else {
                cachedField = createNodeField(visibility, type, fieldName, null);
                setFieldCompilationFinal(cachedField, parameter.getVariableElement().getAnnotation(Cached.class).dimensions());
            }
            fields.add(cachedField);
        }
        for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
            String fieldName = createAssumptionFieldName(specialization, assumption);
            TypeMirror type;
            int compilationFinalDimensions;
            if (assumption.getExpression().getResolvedType().getKind() == TypeKind.ARRAY) {
                type = context.getType(Assumption[].class);
                compilationFinalDimensions = 1;
            } else {
                type = context.getType(Assumption.class);
                compilationFinalDimensions = -1;
            }
            CodeVariableElement assumptionField;
            if (useSpecializationClass) {
                assumptionField = createNodeField(null, type, fieldName, null);
            } else {
                assumptionField = createNodeField(PRIVATE, type, fieldName, null);
            }
            setFieldCompilationFinal(assumptionField, compilationFinalDimensions);
            fields.add(assumptionField);
        }
        if (useSpecializationClass) {
            TypeMirror baseType;
            boolean useNode = specializationClassIsNode(specialization);
            if (useNode) {
                baseType = context.getType(Node.class);
            } else {
                baseType = context.getType(Object.class);
            }
            CodeTypeElement cacheType = GeneratorUtils.createClass(node, null, modifiers(PRIVATE, FINAL, STATIC), createSpecializationTypeName(specialization), baseType);
            Class<?> annotationType;
            if (useNode) {
                annotationType = Child.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", Child.class));
                }
                CodeExecutableElement getNodeCost = new CodeExecutableElement(modifiers(PUBLIC), context.getType(NodeCost.class), "getCost");
                getNodeCost.createBuilder().startReturn().staticReference(context.getType(NodeCost.class), "NONE").end();
                cacheType.add(getNodeCost);
            } else {
                annotationType = CompilationFinal.class;
                if (specialization.getMaximumNumberOfInstances() > 1) {
                    cacheType.add(createNodeField(null, cacheType.asType(), "next_", annotationType));
                }
            }
            cacheType.add(GeneratorUtils.createConstructorUsingFields(modifiers(), cacheType));
            cacheType.getEnclosedElements().addAll(fields);
            clazz.add(createNodeField(PRIVATE, cacheType.asType(), createSpecializationFieldName(specialization), annotationType));
            clazz.add(cacheType);
            specializationClasses.put(specialization, cacheType);
        } else {
            clazz.getEnclosedElements().addAll(fields);
        }
    }
}
Also used : CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) Node(com.oracle.truffle.api.nodes.Node) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) NodeCost(com.oracle.truffle.api.nodes.NodeCost) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) Assumption(com.oracle.truffle.api.Assumption) DeclaredCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) GeneratedTypeMirror(com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) Cached(com.oracle.truffle.api.dsl.Cached) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) ElementUtils.isObject(com.oracle.truffle.dsl.processor.java.ElementUtils.isObject) Modifier(javax.lang.model.element.Modifier) Child(com.oracle.truffle.api.nodes.Node.Child) NodeInterface(com.oracle.truffle.api.nodes.NodeInterface) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 12 with Assumption

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

the class FrameDescriptorTest method version.

@Test
public void version() {
    FrameDescriptor d = new FrameDescriptor();
    s1 = d.addFrameSlot("v1", "i1", FrameSlotKind.Boolean);
    s2 = d.addFrameSlot("v2", "i2", FrameSlotKind.Float);
    Assumption version;
    version = d.getVersion();
    assertTrue(version.isValid());
    // add slot
    s3 = d.addFrameSlot("v3", "i3", FrameSlotKind.Int);
    assertEquals(3, d.getSize());
    assertFalse(version.isValid());
    version = d.getVersion();
    assertTrue(version.isValid());
    assertSame("1st slot", s1, d.getSlots().get(0));
    assertSame("2nd slot", s2, d.getSlots().get(1));
    assertSame("3rd slot", s3, d.getSlots().get(2));
    // change kind
    s3.setKind(FrameSlotKind.Object);
    assertFalse(version.isValid());
    version = d.getVersion();
    assertTrue(version.isValid());
    // remove slot
    d.removeFrameSlot("v3");
    assertEquals(2, d.getSize());
    assertFalse(version.isValid());
    version = d.getVersion();
    assertTrue(version.isValid());
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) Assumption(com.oracle.truffle.api.Assumption) Test(org.junit.Test)

Example 13 with Assumption

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

the class FrameDescriptorTest method notInFrameAssumption.

@Test
public void notInFrameAssumption() {
    FrameDescriptor d = new FrameDescriptor();
    Assumption[] ass = new Assumption[] { d.getNotInFrameAssumption("v1"), d.getNotInFrameAssumption("v2"), d.getNotInFrameAssumption("v3") };
    assertTrue(ass[0].isValid());
    assertTrue(ass[1].isValid());
    assertTrue(ass[2].isValid());
    s1 = d.addFrameSlot("v1", "i1", FrameSlotKind.Boolean);
    assertFalse(ass[0].isValid());
    assertTrue(ass[1].isValid());
    assertTrue(ass[2].isValid());
    s2 = d.addFrameSlot("v2", "i2", FrameSlotKind.Float);
    assertFalse(ass[0].isValid());
    assertFalse(ass[1].isValid());
    assertTrue(ass[2].isValid());
    s3 = d.addFrameSlot("v3", "i3", FrameSlotKind.Int);
    assertFalse(ass[0].isValid());
    assertFalse(ass[1].isValid());
    assertFalse(ass[2].isValid());
    for (String identifier : new String[] { "v1", "v2", "v3" }) {
        try {
            d.getNotInFrameAssumption(identifier);
            fail("expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
        // expected
        }
    }
    d.getNotInFrameAssumption("v4");
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) Assumption(com.oracle.truffle.api.Assumption) Test(org.junit.Test)

Example 14 with Assumption

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

the class AssumptionInvalidationTest method test.

@Test
public void test() throws InterruptedException {
    TruffleRuntime runtime = Truffle.getRuntime();
    Assumption assumption = runtime.createAssumption("propagated assumption invalidation");
    CountingNode countingNode = new CountingNode(assumption);
    TestRootNode countingRootNode = new TestRootNode(countingNode);
    final CallTarget countingTarget = runtime.createCallTarget(countingRootNode);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            countingTarget.call();
        }
    });
    thread.start();
    // Give a chance for CountingNode.execute to OSR compile
    Thread.sleep(100);
    assumption.invalidate();
    thread.join(100);
    assertEquals("Thread ought to be notified of invalidation in reasonable time.", false, thread.isAlive());
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) TruffleRuntime(com.oracle.truffle.api.TruffleRuntime) Assumption(com.oracle.truffle.api.Assumption) Test(org.junit.Test)

Example 15 with Assumption

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

the class CyclicAssumptionTest method testInvalidate.

@Test
public void testInvalidate() {
    final CyclicAssumption cyclicAssumption = new CyclicAssumption("cyclic-assumption");
    final Assumption firstAssumption = cyclicAssumption.getAssumption();
    assertEquals("cyclic-assumption", firstAssumption.getName());
    assertTrue(firstAssumption.isValid());
    cyclicAssumption.invalidate();
    assertFalse(firstAssumption.isValid());
    final Assumption secondAssumption = cyclicAssumption.getAssumption();
    assertEquals("cyclic-assumption", secondAssumption.getName());
    assertTrue(secondAssumption.isValid());
    cyclicAssumption.invalidate();
    assertFalse(firstAssumption.isValid());
    assertFalse(secondAssumption.isValid());
}
Also used : CyclicAssumption(com.oracle.truffle.api.utilities.CyclicAssumption) Assumption(com.oracle.truffle.api.Assumption) CyclicAssumption(com.oracle.truffle.api.utilities.CyclicAssumption) 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