Search in sources :

Example 11 with SpecializationData

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

the class FlatNodeGenFactory method accessesCachedState.

private static boolean accessesCachedState(List<SpecializationData> specializations) {
    final AtomicBoolean needsState = new AtomicBoolean(false);
    for (final SpecializationData specialization : specializations) {
        if (!specialization.getAssumptionExpressions().isEmpty()) {
            needsState.set(true);
            break;
        }
        for (GuardExpression expression : specialization.getGuards()) {
            expression.getExpression().accept(new DSLExpressionVisitor() {

                public void visitVariable(Variable binary) {
                    if (!needsState.get() && isVariableAccessMember(binary)) {
                        needsState.set(true);
                    }
                }

                private boolean isVariableAccessMember(Variable variable) {
                    if (variable.getName().equals("null") && variable.getReceiver() == null) {
                        return false;
                    }
                    Parameter p = specialization.findByVariable(variable.getResolvedVariable());
                    if (p == null && !variable.getResolvedVariable().getModifiers().contains(STATIC)) {
                        DSLExpression receiver = variable.getReceiver();
                        if (receiver instanceof Variable) {
                            return isVariableAccessMember((Variable) receiver);
                        } else if (receiver instanceof Call) {
                            return isMethodAccessMember((Call) receiver);
                        }
                        return true;
                    } else if (p != null && p.getSpecification().isCached()) {
                        return true;
                    }
                    return false;
                }

                public void visitBooleanLiteral(BooleanLiteral binary) {
                }

                public void visitNegate(Negate negate) {
                }

                public void visitIntLiteral(IntLiteral binary) {
                }

                private boolean isMethodAccessMember(Call call) {
                    if (!call.getResolvedMethod().getModifiers().contains(STATIC)) {
                        DSLExpression receiver = call.getReceiver();
                        if (receiver instanceof Variable) {
                            return isVariableAccessMember((Variable) receiver);
                        } else if (receiver instanceof Call) {
                            return isMethodAccessMember((Call) receiver);
                        }
                        return true;
                    }
                    return false;
                }

                public void visitCall(Call call) {
                    if (!needsState.get() && isMethodAccessMember(call)) {
                        needsState.set(true);
                    }
                }

                public void visitBinary(Binary binary) {
                }
            });
        }
    }
    boolean needsStat = needsState.get();
    return needsStat;
}
Also used : Call(com.oracle.truffle.dsl.processor.expression.DSLExpression.Call) Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) BooleanLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) IntLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.IntLiteral) Negate(com.oracle.truffle.dsl.processor.expression.DSLExpression.Negate) Binary(com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary) DSLExpressionVisitor(com.oracle.truffle.dsl.processor.expression.DSLExpression.DSLExpressionVisitor)

Example 12 with SpecializationData

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

the class NodeParser method initializeFallbackReachability.

private static void initializeFallbackReachability(NodeData node) {
    List<SpecializationData> specializations = node.getSpecializations();
    SpecializationData fallback = null;
    for (int i = specializations.size() - 1; i >= 0; i--) {
        SpecializationData specialization = specializations.get(i);
        if (specialization.isFallback() && specialization.getMethod() != null) {
            fallback = specialization;
            break;
        }
    }
    if (fallback == null) {
        // no need to compute reachability
        return;
    }
    for (int index = 0; index < specializations.size(); index++) {
        SpecializationData specialization = specializations.get(index);
        SpecializationData lastReachable = specialization;
        for (int searchIndex = index + 1; searchIndex < specializations.size(); searchIndex++) {
            SpecializationData search = specializations.get(searchIndex);
            if (search == fallback) {
                // reached the end of the specialization
                break;
            }
            assert lastReachable != search;
            if (!lastReachable.isReachableAfter(search)) {
                lastReachable = search;
            } else if (search.getReplaces().contains(specialization)) {
                lastReachable = search;
            }
        }
        specialization.setReachesFallback(lastReachable == specialization);
        List<SpecializationData> failedSpecializations = null;
        if (specialization.isReachesFallback() && !specialization.getCaches().isEmpty() && !specialization.getGuards().isEmpty()) {
            boolean guardBoundByCache = false;
            for (GuardExpression guard : specialization.getGuards()) {
                if (specialization.isGuardBoundWithCache(guard)) {
                    guardBoundByCache = true;
                    break;
                }
            }
            if (guardBoundByCache && specialization.getMaximumNumberOfInstances() > 1) {
                if (failedSpecializations == null) {
                    failedSpecializations = new ArrayList<>();
                }
                failedSpecializations.add(specialization);
            }
        }
        if (failedSpecializations != null) {
            List<String> specializationIds = failedSpecializations.stream().map((e) -> e.getId()).collect(Collectors.toList());
            fallback.addError("Some guards for the following specializations could not be negated for the @%s specialization: %s. " + "Guards cannot be negated for the @%s when they bind @%s parameters and the specialization may consist of multiple instances. " + "To fix this limit the number of instances to '1' or " + "introduce a more generic specialization declared between this specialization and the fallback. " + "Alternatively the use of @%s can be avoided by declaring a @%s with manually specified negated guards.", Fallback.class.getSimpleName(), specializationIds, Fallback.class.getSimpleName(), Cached.class.getSimpleName(), Fallback.class.getSimpleName(), Specialization.class.getSimpleName());
        }
    }
}
Also used : Arrays(java.util.Arrays) Children(com.oracle.truffle.api.nodes.Node.Children) ListIterator(java.util.ListIterator) Modifier(javax.lang.model.element.Modifier) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) Specialization(com.oracle.truffle.api.dsl.Specialization) AssumptionExpression(com.oracle.truffle.dsl.processor.model.AssumptionExpression) MethodSpec(com.oracle.truffle.dsl.processor.model.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) NodeInterface(com.oracle.truffle.api.nodes.NodeInterface) Executed(com.oracle.truffle.api.dsl.Executed) NodeFieldData(com.oracle.truffle.dsl.processor.model.NodeFieldData) NodeExecutionData(com.oracle.truffle.dsl.processor.model.NodeExecutionData) NodeField(com.oracle.truffle.api.dsl.NodeField) Map(java.util.Map) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) Log(com.oracle.truffle.dsl.processor.Log) ArrayType(javax.lang.model.type.ArrayType) NodeData(com.oracle.truffle.dsl.processor.model.NodeData) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) GenerateNodeFactory(com.oracle.truffle.api.dsl.GenerateNodeFactory) Collection(java.util.Collection) Set(java.util.Set) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) List(java.util.List) CreateCast(com.oracle.truffle.api.dsl.CreateCast) TemplateMethod(com.oracle.truffle.dsl.processor.model.TemplateMethod) ParameterSpec(com.oracle.truffle.dsl.processor.model.ParameterSpec) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) Annotation(java.lang.annotation.Annotation) Cardinality(com.oracle.truffle.dsl.processor.model.NodeChildData.Cardinality) AnnotationValue(javax.lang.model.element.AnnotationValue) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) Cached(com.oracle.truffle.api.dsl.Cached) Child(com.oracle.truffle.api.nodes.Node.Child) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) TypeSystemData(com.oracle.truffle.dsl.processor.model.TypeSystemData) VariableElement(javax.lang.model.element.VariableElement) Fallback(com.oracle.truffle.api.dsl.Fallback) GeneratedBy(com.oracle.truffle.api.dsl.GeneratedBy) SpecializationKind(com.oracle.truffle.dsl.processor.model.SpecializationData.SpecializationKind) HashMap(java.util.HashMap) NodeFields(com.oracle.truffle.api.dsl.NodeFields) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Kind(javax.tools.Diagnostic.Kind) CompilerFactory(com.oracle.truffle.dsl.processor.java.compiler.CompilerFactory) CacheExpression(com.oracle.truffle.dsl.processor.model.CacheExpression) SpecializationThrowsData(com.oracle.truffle.dsl.processor.model.SpecializationThrowsData) DeclaredType(javax.lang.model.type.DeclaredType) InvalidExpressionException(com.oracle.truffle.dsl.processor.expression.InvalidExpressionException) ElementFilter(javax.lang.model.util.ElementFilter) ImportStatic(com.oracle.truffle.api.dsl.ImportStatic) ElementUtils(com.oracle.truffle.dsl.processor.java.ElementUtils) NodeChildren(com.oracle.truffle.api.dsl.NodeChildren) Iterator(java.util.Iterator) ElementKind(javax.lang.model.element.ElementKind) TypeSystemReference(com.oracle.truffle.api.dsl.TypeSystemReference) CompileErrorException(com.oracle.truffle.dsl.processor.CompileErrorException) ExecutableElement(javax.lang.model.element.ExecutableElement) NodeChild(com.oracle.truffle.api.dsl.NodeChild) Assumption(com.oracle.truffle.api.Assumption) ProcessorContext(com.oracle.truffle.dsl.processor.ProcessorContext) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) Frame(com.oracle.truffle.api.frame.Frame) Comparator(java.util.Comparator) ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) Collections(java.util.Collections) Introspectable(com.oracle.truffle.api.dsl.Introspectable) ReportPolymorphism(com.oracle.truffle.api.dsl.ReportPolymorphism) DSLExpressionResolver(com.oracle.truffle.dsl.processor.expression.DSLExpressionResolver) Specialization(com.oracle.truffle.api.dsl.Specialization) GuardExpression(com.oracle.truffle.dsl.processor.model.GuardExpression) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) Fallback(com.oracle.truffle.api.dsl.Fallback) Cached(com.oracle.truffle.api.dsl.Cached)

Example 13 with SpecializationData

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

the class NodeParser method collectIncludes.

private void collectIncludes(SpecializationData specialization, Set<SpecializationData> found, Set<SpecializationData> visited) {
    if (visited.contains(specialization)) {
        // circle found
        specialization.addError("Circular replaced specialization '%s' found.", specialization.createReferenceName());
        return;
    }
    visited.add(specialization);
    for (SpecializationData included : specialization.getReplaces()) {
        collectIncludes(included, found, new HashSet<>(visited));
        found.add(included);
    }
}
Also used : SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData)

Example 14 with SpecializationData

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

the class NodeParser method initializeReplaces.

private static void initializeReplaces(NodeData node) {
    for (SpecializationData specialization : node.getSpecializations()) {
        Set<SpecializationData> resolvedSpecializations = specialization.getReplaces();
        resolvedSpecializations.clear();
        Set<String> includeNames = specialization.getReplacesNames();
        for (String includeName : includeNames) {
            // TODO reduce complexity of this lookup.
            SpecializationData foundSpecialization = lookupSpecialization(node, includeName);
            AnnotationValue value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "replaces");
            if (value == null) {
                // TODO remove if deprecated api was removed.
                value = ElementUtils.getAnnotationValue(specialization.getMarkerAnnotation(), "contains");
            }
            if (foundSpecialization == null) {
                specialization.addError(value, "The referenced specialization '%s' could not be found.", includeName);
            } else {
                if (foundSpecialization.compareTo(specialization) > 0) {
                    if (foundSpecialization.compareTo(specialization) > 0) {
                        specialization.addError(value, "The replaced specialization '%s' must be declared before the replacing specialization.", includeName);
                    }
                }
                resolvedSpecializations.add(foundSpecialization);
            }
        }
    }
}
Also used : SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 15 with SpecializationData

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

the class NodeParser method createGenericSpecialization.

private SpecializationData createGenericSpecialization(final NodeData node) {
    FallbackParser parser = new FallbackParser(context, node);
    MethodSpec specification = parser.createDefaultMethodSpec(node.getSpecializations().iterator().next().getMethod(), null, true, null);
    List<VariableElement> parameterTypes = new ArrayList<>();
    int signatureIndex = 1;
    for (ParameterSpec spec : specification.getRequired()) {
        parameterTypes.add(new CodeVariableElement(createGenericType(node, spec), "arg" + signatureIndex));
        if (spec.isSignature()) {
            signatureIndex++;
        }
    }
    TypeMirror returnType = createGenericType(node, specification.getReturnType());
    SpecializationData generic = parser.create("Generic", TemplateMethod.NO_NATURAL_ORDER, null, null, returnType, parameterTypes);
    if (generic == null) {
        throw new RuntimeException("Unable to create generic signature for node " + node.getNodeId() + " with " + parameterTypes + ". Specification " + specification + ".");
    }
    return generic;
}
Also used : MethodSpec(com.oracle.truffle.dsl.processor.model.MethodSpec) ParameterSpec(com.oracle.truffle.dsl.processor.model.ParameterSpec) ArrayCodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)35 ArrayList (java.util.ArrayList)22 TypeMirror (javax.lang.model.type.TypeMirror)17 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)16 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)12 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)11 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)11 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)11 Parameter (com.oracle.truffle.dsl.processor.model.Parameter)9 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)8 ExecutableTypeData (com.oracle.truffle.dsl.processor.model.ExecutableTypeData)7 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)6 HashSet (java.util.HashSet)6 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)5 VariableElement (javax.lang.model.element.VariableElement)5 AnnotationValue (javax.lang.model.element.AnnotationValue)4 ExecutableElement (javax.lang.model.element.ExecutableElement)4 Child (com.oracle.truffle.api.nodes.Node.Child)3 DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)3 ElementUtils.isObject (com.oracle.truffle.dsl.processor.java.ElementUtils.isObject)3