Search in sources :

Example 1 with AssumptionExpression

use of com.oracle.truffle.dsl.processor.model.AssumptionExpression in project graal by oracle.

the class FlatNodeGenFactory method persistAssumptions.

private List<IfTriple> persistAssumptions(FrameState frameState, SpecializationData specialization) {
    List<IfTriple> triples = new ArrayList<>();
    for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
        LocalVariable var = frameState.get(assumption.getId());
        String name = createAssumptionFieldName(specialization, assumption);
        CodeTreeBuilder builder = new CodeTreeBuilder(null);
        builder.startStatement();
        builder.tree(createSpecializationFieldReference(frameState, specialization, name)).string(" = ").tree(var.createReference());
        builder.end();
        triples.add(new IfTriple(builder.build(), null, null));
    }
    return triples;
}
Also used : ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 2 with AssumptionExpression

use of com.oracle.truffle.dsl.processor.model.AssumptionExpression in project graal by oracle.

the class FlatNodeGenFactory method createAssumptionCheckTriples.

private List<IfTriple> createAssumptionCheckTriples(FrameState frameState, SpecializationData specialization) {
    if (specialization == null || specialization.getAssumptionExpressions().isEmpty()) {
        return Collections.emptyList();
    }
    List<IfTriple> triples = new ArrayList<>();
    List<AssumptionExpression> assumptions = specialization.getAssumptionExpressions();
    for (AssumptionExpression assumption : assumptions) {
        CodeTree assumptionReference = createAssumptionReference(frameState, specialization, assumption);
        CodeTree assumptionGuard = createAssumptionGuard(assumption, assumptionReference);
        CodeTreeBuilder builder = new CodeTreeBuilder(null);
        builder.string("(");
        builder.tree(assumptionReference).string(" == null || ");
        builder.tree(assumptionGuard);
        builder.string(")");
        triples.add(new IfTriple(null, builder.build(), null));
    }
    return triples;
}
Also used : CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 3 with AssumptionExpression

use of com.oracle.truffle.dsl.processor.model.AssumptionExpression in project graal by oracle.

the class FlatNodeGenFactory method createFastPathAssumptionCheck.

private CodeTree createFastPathAssumptionCheck(CodeTreeBuilder parent, SpecializationData specialization, ExecutableTypeData forType, FrameState frameState) throws AssertionError {
    CodeTreeBuilder builder = parent.create();
    builder.startIf();
    String sep = "";
    for (AssumptionExpression assumption : specialization.getAssumptionExpressions()) {
        builder.string(sep);
        builder.string("!");
        builder.startCall("isValid_").tree(createAssumptionReference(frameState, specialization, assumption)).end();
        isValidSignatures.put(ElementUtils.getQualifiedName(assumption.getExpression().getResolvedType()), assumption.getExpression().getResolvedType());
        sep = " || ";
    }
    builder.end().startBlock();
    builder.tree(createTransferToInterpreterAndInvalidate());
    builder.tree(createRemoveThis(builder, frameState, forType, specialization));
    builder.end();
    return builder.build();
}
Also used : CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 4 with AssumptionExpression

use of com.oracle.truffle.dsl.processor.model.AssumptionExpression in project graal by oracle.

the class FlatNodeGenFactory method visitSpecializationGroup.

private CodeTree visitSpecializationGroup(CodeTreeBuilder parent, SpecializationGroup group, ExecutableTypeData forType, FrameState frameState, List<SpecializationData> allowedSpecializations, NodeExecutionMode mode) {
    CodeTreeBuilder builder = parent.create();
    boolean hasFallthrough = false;
    boolean hasImplicitCast = false;
    List<IfTriple> cachedTriples = new ArrayList<>();
    for (TypeGuard guard : group.getTypeGuards()) {
        IfTriple triple = createTypeCheckOrCast(frameState, group, guard, mode, false, true);
        if (triple != null) {
            cachedTriples.add(triple);
        }
        hasImplicitCast = hasImplicitCast || node.getTypeSystem().hasImplicitSourceTypes(guard.getType());
        if (!mode.isGuardFallback()) {
            triple = createTypeCheckOrCast(frameState, group, guard, mode, true, true);
            if (triple != null) {
                cachedTriples.add(triple);
            }
        }
    }
    SpecializationData specialization = group.getSpecialization();
    SpecializationData[] specializations = group.collectSpecializations().toArray(new SpecializationData[0]);
    List<GuardExpression> guardExpressions = new ArrayList<>(group.getGuards());
    // out of the loop.
    if (specialization != null && specialization.hasMultipleInstances()) {
        List<GuardExpression> unboundGuards = new ArrayList<>();
        for (GuardExpression guard : guardExpressions) {
            if (!specialization.isGuardBoundWithCache(guard)) {
                unboundGuards.add(guard);
            } else {
                // we need to stop as we need to ensure guard execution order
                break;
            }
        }
        cachedTriples.addAll(createMethodGuardCheck(frameState, group, unboundGuards, mode));
        guardExpressions.removeAll(unboundGuards);
    }
    boolean useSpecializationClass = specialization != null && useSpecializationClass(specialization);
    if (mode.isFastPath()) {
        int ifCount = 0;
        final boolean stateGuaranteed = group.isLast() && allowedSpecializations != null && allowedSpecializations.size() == 1 && group.getAllSpecializations().size() == allowedSpecializations.size();
        if ((!group.isEmpty() || specialization != null)) {
            CodeTree stateCheck = state.createContains(frameState, specializations);
            CodeTree stateGuard = null;
            CodeTree assertCheck = null;
            if (stateGuaranteed) {
                assertCheck = CodeTreeBuilder.createBuilder().startAssert().tree(stateCheck).end().build();
            } else {
                stateGuard = stateCheck;
            }
            cachedTriples.add(0, new IfTriple(null, stateGuard, assertCheck));
        }
        ifCount += IfTriple.materialize(builder, IfTriple.optimize(cachedTriples), false);
        // reset current triples
        cachedTriples = new ArrayList<>();
        String specializationLocalName = null;
        if (useSpecializationClass) {
            specializationLocalName = createSpecializationLocalName(specialization);
            builder.tree(loadSpecializationClass(frameState, specialization));
            if (specialization.getMaximumNumberOfInstances() > 1) {
                builder.startWhile();
            } else {
                builder.startIf();
            }
            builder.string(specializationLocalName, " != null");
            builder.end();
            builder.startBlock();
            ifCount++;
        }
        if (specialization != null) {
            if (!specialization.getAssumptionExpressions().isEmpty()) {
                builder.tree(createFastPathAssumptionCheck(builder, specialization, forType, frameState));
            }
        }
        cachedTriples = createMethodGuardCheck(frameState, group, guardExpressions, mode);
        int innerIfCount = IfTriple.materialize(builder, IfTriple.optimize(cachedTriples), false);
        SpecializationGroup prev = null;
        for (SpecializationGroup child : group.getChildren()) {
            if (prev != null && !prev.hasFallthrough()) {
                break;
            }
            builder.tree(visitSpecializationGroup(builder, child, forType, frameState.copy(), allowedSpecializations, mode));
        }
        if (specialization != null && (prev == null || prev.hasFallthrough())) {
            builder.tree(createFastPathExecute(builder, forType, specialization, frameState));
        }
        builder.end(innerIfCount);
        hasFallthrough |= innerIfCount > 0;
        if (useSpecializationClass && specialization.getMaximumNumberOfInstances() > 1) {
            String name = createSpecializationLocalName(specialization);
            builder.startStatement().string(name, " = ", name, ".next_").end();
        }
        builder.end(ifCount);
        hasFallthrough |= ifCount > 0;
    } else if (mode.isSlowPath()) {
        if (specialization != null && mayBeExcluded(specialization)) {
            CodeTree excludeCheck = exclude.createNotContains(frameState, specializations);
            cachedTriples.add(0, new IfTriple(null, excludeCheck, null));
        }
        int outerIfCount = 0;
        if (specialization == null) {
            cachedTriples.addAll(createMethodGuardCheck(frameState, group, guardExpressions, mode));
            outerIfCount += IfTriple.materialize(builder, IfTriple.optimize(cachedTriples), false);
            SpecializationGroup prev = null;
            for (SpecializationGroup child : group.getChildren()) {
                if (prev != null && !prev.hasFallthrough()) {
                    break;
                }
                builder.tree(visitSpecializationGroup(builder, child, forType, frameState.copy(), allowedSpecializations, mode));
                prev = child;
            }
        } else {
            outerIfCount += IfTriple.materialize(builder, IfTriple.optimize(cachedTriples), false);
            String countName = specialization != null ? "count" + specialization.getIndex() + "_" : null;
            boolean needsDuplicationCheck = specialization.isGuardBindsCache() || specialization.hasMultipleInstances();
            boolean useDuplicateFlag = specialization.isGuardBindsCache() && !specialization.hasMultipleInstances();
            String duplicateFoundName = specialization.getId() + "_duplicateFound_";
            int innerIfCount = 0;
            String specializationLocalName = createSpecializationLocalName(specialization);
            if (needsDuplicationCheck) {
                builder.tree(createDuplicationCheck(builder, frameState, group, guardExpressions, useDuplicateFlag, countName, duplicateFoundName, specializationLocalName));
                builder.startIf();
                if (useDuplicateFlag) {
                    // we reuse the specialization class local name instead of a duplicate found
                    // name
                    builder.string("!", duplicateFoundName);
                } else {
                    builder.string(createSpecializationLocalName(specialization), " == null");
                }
                builder.end().startBlock();
                innerIfCount++;
            }
            List<IfTriple> innerTripples = createMethodGuardCheck(frameState, group, guardExpressions, mode);
            List<AssumptionExpression> assumptions = specialization.getAssumptionExpressions();
            if (!assumptions.isEmpty()) {
                for (AssumptionExpression assumption : assumptions) {
                    innerTripples.addAll(createAssumptionSlowPathTriples(frameState, group, assumption));
                }
            }
            if (specialization.hasMultipleInstances()) {
                DSLExpression limit = specialization.getLimitExpression();
                innerTripples.addAll(initializeCaches(frameState, group, specialization.getBoundCaches(limit), NodeExecutionMode.SLOW_PATH, true, false));
                CodeTree limitExpression = DSLExpressionGenerator.write(limit, null, castBoundTypes(bindExpressionValues(frameState, limit, specialization)));
                CodeTree limitCondition = CodeTreeBuilder.createBuilder().string(countName).string(" < ").tree(limitExpression).build();
                innerTripples.add(new IfTriple(null, limitCondition, null));
                // assert that specialization is not initialized
                // otherwise we have been inserting invalid instances
                assertSpecializationClassNotInitialized(frameState, specialization);
            } else if (needsDuplicationCheck) {
                innerTripples.add(new IfTriple(null, state.createNotContains(frameState, new Object[] { specialization }), null));
            }
            innerIfCount += IfTriple.materialize(builder, IfTriple.optimize(innerTripples), false);
            builder.tree(createSpecialize(builder, frameState, group, specialization));
            if (needsDuplicationCheck) {
                hasFallthrough = true;
                if (useDuplicateFlag) {
                    builder.startStatement().string(duplicateFoundName, " = true").end();
                }
                builder.end(innerIfCount);
                // need to ensure that we update the implicit cast specializations on duplicates
                CodeTree updateImplicitCast = createUpdateImplicitCastState(builder, frameState, specialization);
                if (updateImplicitCast != null) {
                    builder.startElseBlock();
                    builder.tree(createUpdateImplicitCastState(builder, frameState, specialization));
                    builder.tree(state.createSet(frameState, new Object[] { specialization }, true, true));
                    builder.end();
                }
                builder.startIf();
                if (useDuplicateFlag) {
                    builder.string(duplicateFoundName);
                } else {
                    builder.string(createSpecializationLocalName(specialization), " != null");
                }
                builder.end().startBlock();
                builder.tree(createExecute(builder, frameState, executeAndSpecializeType, specialization, mode));
                builder.end();
            } else {
                builder.tree(createExecute(builder, frameState, executeAndSpecializeType, specialization, mode));
                builder.end(innerIfCount);
                hasFallthrough |= innerIfCount > 0;
            }
        }
        builder.end(outerIfCount);
        hasFallthrough |= outerIfCount > 0;
    } else if (mode.isGuardFallback()) {
        int ifCount = 0;
        if (specialization != null && specialization.getMaximumNumberOfInstances() > 1) {
            throw new AssertionError("unsupported path. should be caught by parser..");
        }
        int innerIfCount = 0;
        cachedTriples.addAll(createMethodGuardCheck(frameState, group, guardExpressions, mode));
        cachedTriples.addAll(createAssumptionCheckTriples(frameState, specialization));
        cachedTriples = IfTriple.optimize(cachedTriples);
        if (specialization != null && !hasImplicitCast) {
            IfTriple singleCondition = null;
            if (cachedTriples.size() == 1) {
                singleCondition = cachedTriples.get(0);
            }
            if (singleCondition != null) {
                int index = cachedTriples.indexOf(singleCondition);
                CodeTree stateCheck = state.createNotContains(frameState, specializations);
                cachedTriples.set(index, new IfTriple(singleCondition.prepare, combineTrees(" && ", stateCheck, singleCondition.condition), singleCondition.statements));
                fallbackNeedsState = true;
            }
        }
        innerIfCount += IfTriple.materialize(builder, cachedTriples, false);
        SpecializationGroup prev = null;
        for (SpecializationGroup child : group.getChildren()) {
            if (prev != null && !prev.hasFallthrough()) {
                break;
            }
            builder.tree(visitSpecializationGroup(builder, child, forType, frameState.copy(), allowedSpecializations, mode));
            prev = child;
        }
        if (specialization != null) {
            builder.returnFalse();
        }
        builder.end(innerIfCount);
        builder.end(ifCount);
        hasFallthrough |= ifCount > 0 || innerIfCount > 0;
    } else {
        throw new AssertionError("unexpected path");
    }
    group.setFallthrough(hasFallthrough);
    return builder.build();
}
Also used : GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) ArrayList(java.util.ArrayList) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) SpecializationGroup(com.oracle.truffle.dsl.processor.parser.SpecializationGroup) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) CodeTree(com.oracle.truffle.dsl.processor.java.model.CodeTree) List(java.util.List) ArrayList(java.util.ArrayList) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) TypeGuard(com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression)

Example 5 with AssumptionExpression

use of com.oracle.truffle.dsl.processor.model.AssumptionExpression 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)

Aggregations

AssumptionExpression (com.oracle.truffle.dsl.processor.model.AssumptionExpression)6 ArrayList (java.util.ArrayList)5 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)4 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)2 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)2 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)2 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Assumption (com.oracle.truffle.api.Assumption)1 Cached (com.oracle.truffle.api.dsl.Cached)1 Node (com.oracle.truffle.api.nodes.Node)1 Child (com.oracle.truffle.api.nodes.Node.Child)1 NodeCost (com.oracle.truffle.api.nodes.NodeCost)1 NodeInterface (com.oracle.truffle.api.nodes.NodeInterface)1 InvalidExpressionException (com.oracle.truffle.dsl.processor.expression.InvalidExpressionException)1 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)1 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)1 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)1 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)1 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)1