Search in sources :

Example 51 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Loop method cleanPragmas.

/**
 * Clean up extra pragma that have no more sense after transformation.
 *
 * @param node     Do statement that will be removed.
 * @param previous List of pragma to be removed before the do statement.
 * @param next     List of pragmas to be removed after the do statement.
 */
private static void cleanPragmas(Xnode node, String[] previous, String[] next) {
    if (!Xnode.isOfCode(node, Xcode.F_DO_STATEMENT)) {
        return;
    }
    Xnode doStatement = node;
    while (Xnode.isOfCode(node.prevSibling(), Xcode.F_PRAGMA_STATEMENT)) {
        String pragma = node.prevSibling().value().toLowerCase();
        Xnode toDelete = null;
        for (String p : previous) {
            if (!pragma.startsWith(CompilerDirective.CLAW.getPrefix()) && pragma.contains(p)) {
                toDelete = node.prevSibling();
                break;
            }
        }
        node = node.prevSibling();
        XnodeUtil.safeDelete(toDelete);
    }
    // Reset node to the initial position.
    node = doStatement;
    while (Xnode.isOfCode(node.nextSibling(), Xcode.F_PRAGMA_STATEMENT)) {
        String pragma = node.nextSibling().value().toLowerCase();
        Xnode toDelete = null;
        for (String n : next) {
            if (!pragma.startsWith(CompilerDirective.CLAW.getPrefix()) && pragma.contains(n)) {
                toDelete = node.nextSibling();
                break;
            }
        }
        node = node.nextSibling();
        XnodeUtil.safeDelete(toDelete);
    }
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode)

Example 52 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Loop method createIfStatementForLowerBound.

/**
 * Create an IF statement surrounding the entire most inner do statement body.
 * Condition if made from the lower bound (if(induction_var >= lower_bound).
 *
 * @param xcodeml Current XcodeML program
 * @param g       The group of do statements.
 */
private static void createIfStatementForLowerBound(XcodeML xcodeml, HoistedNestedDoStatement g) {
    Xnode ifStmt = xcodeml.createNode(Xcode.F_IF_STATEMENT);
    Xnode condition = xcodeml.createNode(Xcode.CONDITION);
    Xnode thenBlock = xcodeml.createNode(Xcode.THEN);
    g.getOuterStatement().copyEnhancedInfo(ifStmt);
    Xnode cond = xcodeml.createNode(Xcode.LOG_GE_EXPR);
    Xnode inductionVar = g.getOuterStatement().matchDirectDescendant(Xcode.VAR);
    cond.append(inductionVar, true);
    cond.append(g.getOuterStatement().matchDirectDescendant(Xcode.INDEX_RANGE).matchDirectDescendant(Xcode.LOWER_BOUND).child(0), true);
    ifStmt.append(condition);
    ifStmt.append(thenBlock);
    condition.append(cond);
    thenBlock.append(g.getInnerStatement().body(), true);
    g.getInnerStatement().body().delete();
    Xnode body = xcodeml.createNode(Xcode.BODY);
    body.append(ifStmt);
    g.getInnerStatement().append(body);
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode)

Example 53 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Directive method insertPragmas.

/**
 * Generate corresponding pragmas to surround the code with a parallel
 * accelerated region.
 *
 * @param xcodeml        Current XcodeML program unit. representation in which
 *                       the pragmas will be generated.
 * @param startStmt      Start reference statement.
 * @param endStmt        End reference statement.
 * @param startDirective String value of the start directive.
 * @param endDirective   String value of the end directive.
 * @return Last stmt inserted or null if nothing is inserted.
 */
private static Xnode insertPragmas(XcodeProgram xcodeml, Xnode startStmt, Xnode endStmt, String[] startDirective, String[] endDirective) {
    if (xcodeml.context().getGenerator().getDirectiveLanguage() == CompilerDirective.NONE) {
        return null;
    }
    Xnode begin = addPragmasBefore(xcodeml, startDirective, startStmt);
    Xnode end = addPragmaAfter(xcodeml, endDirective, endStmt);
    return end != null ? end : begin;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode)

Example 54 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Directive method findParallelRegionStart.

/**
 * Skip elements in preamble and find the first element that will be included in
 * the parallel region.
 *
 * @param functionDefinition Function definition in which body checked.
 * @param from               Optional element to start from. If null, starts
 *                           from first element in function's body.
 * @return First element for the parallel region.
 */
public static Xnode findParallelRegionStart(Context context, Xnode functionDefinition, Xnode from) {
    DirectiveGenerator dg = context.getGenerator();
    if (!Xnode.isOfCode(functionDefinition, Xcode.F_FUNCTION_DEFINITION)) {
        return null;
    }
    Xnode first = functionDefinition.body().firstChild();
    if (first == null) {
        return null;
    }
    if (from != null) {
        // Start from given element
        first = from;
    }
    if (dg.getSkippedStatementsInPreamble().isEmpty()) {
        return first;
    } else {
        while (first.nextSibling() != null && ((dg.getSkippedStatementsInPreamble().contains(first.opcode())) || (isClawDirective(first) && !first.value().contains("nodep")))) {
            if (Xnode.isOfCode(first, Xcode.F_IF_STATEMENT)) {
                Xnode then = first.matchDescendant(Xcode.THEN);
                if (then != null && then.hasBody()) {
                    for (Xnode child : then.body().children()) {
                        if (!dg.getSkippedStatementsInPreamble().contains(child.opcode())) {
                            return first;
                        }
                    }
                }
            } else if (first.hasBody()) {
                for (Xnode child : first.body().children()) {
                    if (!dg.getSkippedStatementsInPreamble().contains(child.opcode())) {
                        return first;
                    }
                }
            }
            first = first.nextSibling();
        }
    }
    return first;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) DirectiveGenerator(claw.tatsu.directive.generator.DirectiveGenerator)

Example 55 with Xnode

use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.

the class Directive method generateLoopSeq.

/**
 * Generate loop seq directives on the top of loops in the given function
 * definition.
 *
 * @param xcodeml               Object representation of the current XcodeML
 *                              representation in which the pragmas will be
 *                              generated.
 * @param fctDef                Function definition in which do statements will
 *                              be decorated.
 * @param noDependencyDirective Directive string used to flag a loop as no
 *                              dependency loop.
 * @return Number of independent flagged loop.
 */
public static int generateLoopSeq(XcodeProgram xcodeml, Xnode tree, String noDependencyDirective) {
    if (xcodeml.context().getCompilerDirective() == CompilerDirective.NONE) {
        return 0;
    }
    int nodepCounter = 0;
    List<Xnode> doStmts = tree.matchAll(Xcode.F_DO_STATEMENT);
    for (Xnode doStmt : doStmts) {
        // Check if the nodep directive decorates the loop
        Xnode noDependency = isDecoratedWith(doStmt, noDependencyDirective);
        if (noDependency == null) {
            addPragmasBefore(xcodeml, xcodeml.context().getGenerator().getStartLoopDirective(NO_COLLAPSE, true, true, ""), doStmt);
        } else {
            ++nodepCounter;
        }
        XnodeUtil.safeDelete(noDependency);
        // Debug logging
        Message.debug(xcodeml.context(), String.format("%s generated loop %s directive for loop at line: %d", xcodeml.context().getGenerator().getPrefix(), (noDependency == null) ? "seq" : "", doStmt.lineNo()));
    }
    return xcodeml.context().getAcceleratorConfig().hasCollapseStrategy() ? nodepCounter : 0;
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode)

Aggregations

Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)124 Context (claw.tatsu.common.Context)29 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)24 Test (org.junit.Test)24 XcodeProgram (claw.tatsu.xcodeml.xnode.common.XcodeProgram)20 TestContext (helper.Utils.TestContext)20 ArrayList (java.util.ArrayList)18 FfunctionDefinition (claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)17 Xblock (claw.tatsu.xcodeml.abstraction.Xblock)9 FunctionCall (claw.tatsu.xcodeml.abstraction.FunctionCall)8 Xid (claw.tatsu.xcodeml.xnode.common.Xid)8 FbasicType (claw.tatsu.xcodeml.xnode.fortran.FbasicType)8 HashSet (java.util.HashSet)7 PromotionInfo (claw.tatsu.xcodeml.abstraction.PromotionInfo)6 FfunctionType (claw.tatsu.xcodeml.xnode.fortran.FfunctionType)6 NestedDoStatement (claw.tatsu.xcodeml.abstraction.NestedDoStatement)5 ClawPragma (claw.wani.language.ClawPragma)5 ClawTranslator (claw.wani.x2t.translator.ClawTranslator)5 NodeList (org.w3c.dom.NodeList)5 DirectiveGenerator (claw.tatsu.directive.generator.DirectiveGenerator)4