Search in sources :

Example 26 with SpecializationData

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

the class NodeParser method initializeUninitialized.

private static void initializeUninitialized(final NodeData node) {
    SpecializationData generic = node.getGenericSpecialization();
    if (generic == null) {
        return;
    }
    TemplateMethod uninializedMethod = new TemplateMethod("Uninitialized", -1, node, generic.getSpecification(), null, null, generic.getReturnType(), generic.getParameters());
    // should not use messages from generic specialization
    uninializedMethod.getMessages().clear();
    node.getSpecializations().add(new SpecializationData(node, uninializedMethod, SpecializationKind.UNINITIALIZED));
}
Also used : TemplateMethod(com.oracle.truffle.dsl.processor.model.TemplateMethod) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData)

Example 27 with SpecializationData

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

the class FlatNodeGenFactory method filterExecutableTypes.

private List<ExecutableTypeData> filterExecutableTypes(List<ExecutableTypeData> executableTypes, List<SpecializationData> specializations) {
    Set<TypeMirror> specializedReturnTypes = new HashSet<>();
    for (SpecializationData specialization : specializations) {
        specializedReturnTypes.add(specialization.getReturnType().getType());
    }
    List<ExecutableTypeData> filteredTypes = new ArrayList<>();
    outer: for (ExecutableTypeData executable : executableTypes) {
        if (executable.getMethod() == null) {
            continue;
        }
        if (executable.isAbstract()) {
            filteredTypes.add(executable);
            continue;
        }
        if (executable.isFinal()) {
            // no way to implement that
            continue;
        }
        if (!executable.hasUnexpectedValue(context)) {
            filteredTypes.add(executable);
            continue;
        } else {
            TypeMirror returnType = executable.getReturnType();
            if (boxingEliminationEnabled && (isVoid(returnType) || ElementUtils.isPrimitive(returnType))) {
                for (TypeMirror specializedReturnType : specializedReturnTypes) {
                    if (isSubtypeBoxed(context, specializedReturnType, returnType)) {
                        filteredTypes.add(executable);
                        continue outer;
                    }
                }
            }
        }
    }
    Collections.sort(filteredTypes);
    return filteredTypes;
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) 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) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 28 with SpecializationData

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

the class FlatNodeGenFactory method createExecuteDelegation.

private ExecuteDelegationResult createExecuteDelegation(CodeTreeBuilder parent, FrameState frameState, ExecutableTypeData type, List<ExecutableTypeData> delegateableTypes, final List<SpecializationData> compatibleSpecializations, List<SpecializationData> implementedSpecializations) {
    CodeTreeBuilder builder = parent.create();
    List<SpecializationData> notImplemented = new ArrayList<>(compatibleSpecializations);
    for (SpecializationData specialization : implementedSpecializations) {
        notImplemented.remove(specialization);
    }
    if (notImplemented.isEmpty()) {
        throw new AssertionError();
    }
    List<ExecutableTypeData> compatibleDelegateTypes = filterCompatibleExecutableTypes(type, delegateableTypes);
    List<ExecutableTypeData> delegatedDelegateTypes = new ArrayList<>();
    CodeTreeBuilder delegateBuilder = builder.create();
    boolean elseIf = false;
    boolean coversAllSpecializations = false;
    if (boxingEliminationEnabled) {
        Set<TypeMirror> optimizeTypes = new HashSet<>();
        for (SpecializationData specialization : reachableSpecializations) {
            TypeMirror returnType = specialization.getReturnType().getType();
            if (ElementUtils.isPrimitive(returnType)) {
                optimizeTypes.add(returnType);
            }
        }
        for (TypeMirror optimizedType : ElementUtils.uniqueSortedTypes(optimizeTypes, true)) {
            ExecutableTypeData delegateType = null;
            for (ExecutableTypeData compatibleType : compatibleDelegateTypes) {
                if (ElementUtils.typeEquals(compatibleType.getReturnType(), optimizedType)) {
                    delegateType = compatibleType;
                    break;
                }
            }
            if (delegateType != null) {
                List<SpecializationData> delegateSpecializations = filterImplementedSpecializations(delegateType, filterCompatibleSpecializations(delegateType, reachableSpecializations));
                coversAllSpecializations = delegateSpecializations.size() == reachableSpecializations.size();
                if (!coversAllSpecializations) {
                    builder.tree(state.createLoad(frameState));
                    elseIf = delegateBuilder.startIf(elseIf);
                    delegateBuilder.startGroup();
                    delegateBuilder.tree(state.createContainsOnly(frameState, 0, -1, delegateSpecializations.toArray(), reachableSpecializationsArray)).end();
                    delegateBuilder.string(" && ");
                    delegateBuilder.tree(state.createIsNotAny(frameState, reachableSpecializationsArray));
                    delegateBuilder.end();
                    delegateBuilder.end();
                    delegateBuilder.startBlock();
                }
                delegatedDelegateTypes.add(delegateType);
                delegateBuilder.tree(createCallExecute(type, delegateType, frameState));
                if (!coversAllSpecializations) {
                    delegateBuilder.end();
                }
                if (coversAllSpecializations) {
                    break;
                }
            }
        }
    }
    if (!compatibleDelegateTypes.isEmpty() && !coversAllSpecializations) {
        ExecutableTypeData delegateType = compatibleDelegateTypes.get(0);
        coversAllSpecializations = notImplemented.size() == reachableSpecializations.size();
        if (!coversAllSpecializations) {
            builder.tree(state.createLoad(frameState));
            elseIf = delegateBuilder.startIf(elseIf);
            delegateBuilder.tree(state.createContains(frameState, notImplemented.toArray())).end();
            delegateBuilder.startBlock();
        }
        delegatedDelegateTypes.add(delegateType);
        delegateBuilder.tree(createCallExecute(type, delegateType, frameState));
        if (!coversAllSpecializations) {
            delegateBuilder.end();
        }
    }
    boolean hasUnexpected = false;
    for (ExecutableTypeData delegateType : delegatedDelegateTypes) {
        if (needsUnexpectedResultException(delegateType)) {
            hasUnexpected = true;
            break;
        }
    }
    if (hasUnexpected) {
        builder.startTryBlock();
        builder.tree(delegateBuilder.build());
        builder.end().startCatchBlock(context.getType(UnexpectedResultException.class), "ex");
        if (isVoid(type.getReturnType())) {
            builder.returnStatement();
        } else {
            builder.startReturn();
            builder.tree(expectOrCast(getType(Object.class), type, CodeTreeBuilder.singleString("ex")));
            builder.end();
        }
        builder.end();
    } else {
        builder.tree(delegateBuilder.build());
    }
    return new ExecuteDelegationResult(builder.build(), !coversAllSpecializations);
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) UnexpectedResultException(com.oracle.truffle.api.nodes.UnexpectedResultException) 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) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 29 with SpecializationData

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

the class FlatNodeGenFactory method parameterBoxingElimination.

/*
     * It duplicates a group into small subgroups of specializations that don't need boxing when
     * executing the children.
     */
private List<BoxingSplit> parameterBoxingElimination(SpecializationGroup group, int evaluatedcount) {
    if (!boxingEliminationEnabled) {
        return Collections.emptyList();
    }
    List<SpecializationData> allSpecializations = group.collectSpecializations();
    List<Set<TypeGuard>> signatures = new ArrayList<>();
    List<List<SpecializationData>> signatureSpecializations = new ArrayList<>();
    for (SpecializationData specialization : allSpecializations) {
        int index = -1;
        List<TypeGuard> guards = new ArrayList<>();
        for (Parameter p : specialization.getSignatureParameters()) {
            index++;
            if (!ElementUtils.isPrimitive(p.getType())) {
                continue;
            } else if (index < evaluatedcount) {
                continue;
            } else {
                NodeChildData child = p.getSpecification().getExecution().getChild();
                if (child != null && child.findExecutableType(p.getType()) == null) {
                    // type cannot be executed so it cannot be eliminated
                    continue;
                }
            }
            guards.add(new TypeGuard(p.getType(), index));
        }
        if (!guards.isEmpty()) {
            boolean directFound = false;
            for (int i = 0; i < signatures.size(); i++) {
                if (guards.containsAll(signatures.get(i))) {
                    if (signatures.get(i).containsAll(guards)) {
                        directFound = true;
                    }
                    signatureSpecializations.get(i).add(specialization);
                }
            }
            if (!directFound) {
                signatures.add(new LinkedHashSet<>(guards));
                List<SpecializationData> specializations = new ArrayList<>();
                specializations.add(specialization);
                signatureSpecializations.add(specializations);
            }
        }
    }
    List<BoxingSplit> groups = new ArrayList<>();
    for (int i = 0; i < signatureSpecializations.size(); i++) {
        List<SpecializationData> groupedSpecialization = signatureSpecializations.get(i);
        if (allSpecializations.size() == groupedSpecialization.size()) {
            // contains all specializations does not make sense to group
            continue;
        }
        Set<TypeGuard> signature = signatures.get(i);
        TypeMirror[] signatureMirrors = new TypeMirror[signature.size()];
        int index = 0;
        for (TypeGuard typeGuard : signature) {
            signatureMirrors[index] = typeGuard.getType();
            index++;
        }
        groups.add(new BoxingSplit(SpecializationGroup.create(groupedSpecialization), signatureMirrors));
    }
    Collections.sort(groups, new Comparator<BoxingSplit>() {

        public int compare(BoxingSplit o1, BoxingSplit o2) {
            return Integer.compare(o2.primitiveSignature.length, o1.primitiveSignature.length);
        }
    });
    return groups;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) NodeChildData(com.oracle.truffle.dsl.processor.model.NodeChildData) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) 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) Parameter(com.oracle.truffle.dsl.processor.model.Parameter) List(java.util.List) ArrayList(java.util.ArrayList) TypeGuard(com.oracle.truffle.dsl.processor.parser.SpecializationGroup.TypeGuard)

Example 30 with SpecializationData

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

the class FlatNodeGenFactory method createExecuteAndSpecializeType.

private ExecutableTypeData createExecuteAndSpecializeType() {
    SpecializationData polymorphicSpecialization = node.getPolymorphicSpecialization();
    TypeMirror polymorphicType = polymorphicSpecialization.getReturnType().getType();
    List<TypeMirror> parameters = new ArrayList<>();
    for (Parameter param : polymorphicSpecialization.getSignatureParameters()) {
        parameters.add(param.getType());
    }
    return new ExecutableTypeData(node, polymorphicType, "executeAndSpecialize", node.getFrameType(), parameters);
}
Also used : ExecutableTypeData(com.oracle.truffle.dsl.processor.model.ExecutableTypeData) 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) SpecializationData(com.oracle.truffle.dsl.processor.model.SpecializationData) ArrayList(java.util.ArrayList) Parameter(com.oracle.truffle.dsl.processor.model.Parameter)

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