Search in sources :

Example 1 with ClawReshapeInfo

use of cx2x.translator.language.common.ClawReshapeInfo in project claw-compiler by C2SM-RCM.

the class ClawLanguageTest method loopHoistTest.

/**
   * Test various input for the CLAW loop-hoist directive.
   */
@Test
public void loopHoistTest() {
    // Valid directives
    analyzeValidLoopHoist("claw loop-hoist(i,j)", Arrays.asList("i", "j"), false, null, false, null, null, false, null, 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) interchange", Arrays.asList("i", "j"), true, null, false, null, null, false, null, 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) interchange(j,i)", Arrays.asList("i", "j"), true, Arrays.asList("j", "i"), false, null, null, false, null, 0);
    List<Integer> empty = Collections.emptyList();
    ClawReshapeInfo info1 = new ClawReshapeInfo("zmd", 0, empty);
    ClawReshapeInfo info2 = new ClawReshapeInfo("zsediflux", 1, Collections.singletonList(2));
    analyzeValidLoopHoist("claw loop-hoist(i,j) reshape(zmd(0), zsediflux(1,2))", Arrays.asList("i", "j"), false, null, true, Arrays.asList(info1, info2), null, false, null, 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) target(cpu) interchange", Arrays.asList("i", "j"), true, null, false, null, Collections.singletonList(Target.CPU), false, null, 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) interchange target(cpu, gpu)", Arrays.asList("i", "j"), true, null, false, null, Arrays.asList(Target.CPU, Target.GPU), false, null, 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) target(mic)", Arrays.asList("i", "j"), false, null, false, null, Collections.singletonList(Target.MIC), false, null, 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) fusion", Arrays.asList("i", "j"), false, null, false, null, null, true, null, 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) fusion group(j1)", Arrays.asList("i", "j"), false, null, false, null, null, true, "j1", 0);
    analyzeValidLoopHoist("claw loop-hoist(i,j) fusion collapse(2)", Arrays.asList("i", "j"), false, null, false, null, null, true, null, 2);
    // Unvalid directives
    analyzeUnvalidClawLanguage("claw loop-hoist");
    analyzeUnvalidClawLanguage("claw loop-hoist()");
    analyzeUnvalidClawLanguage("claw loop-hoist(i,j) interchange()");
    analyzeValidSimpleClaw("claw end loop-hoist", ClawDirective.LOOP_HOIST, true, null);
    analyzeValidSimpleClaw("claw   end   loop-hoist  ", ClawDirective.LOOP_HOIST, true, null);
}
Also used : ClawReshapeInfo(cx2x.translator.language.common.ClawReshapeInfo) Test(org.junit.Test)

Example 2 with ClawReshapeInfo

use of cx2x.translator.language.common.ClawReshapeInfo in project claw-compiler by C2SM-RCM.

the class TransformationHelper method applyReshapeClause.

/**
   * Apply the reshape clause transformation.
   *
   * @param claw    The claw language object holding the reshape information.
   * @param xcodeml The current XcodeML program.
   * @throws IllegalTransformationException when reshape information are not
   *                                        valid or an new element cannot be
   *                                        created.
   */
public static void applyReshapeClause(ClawLanguage claw, XcodeProgram xcodeml) throws IllegalTransformationException {
    if (!claw.hasReshapeClause()) {
        return;
    }
    XfunctionDefinition fctDef = XnodeUtil.findParentFunction(claw.getPragma());
    if (fctDef == null) {
        throw new IllegalTransformationException("Cannot apply reshape clause." + "Parent function definition not found.", claw.getPragma().lineNo());
    }
    for (ClawReshapeInfo reshapeInfo : claw.getReshapeClauseValues()) {
        Xid id = getIdInNestedFctDef(fctDef, reshapeInfo.getArrayName());
        Xdecl decl = getDeclInNestedFctDef(fctDef, reshapeInfo.getArrayName());
        if (id == null || decl == null) {
            throw new IllegalTransformationException("Cannot apply reshape clause." + "Variable " + reshapeInfo.getArrayName() + " not found in " + "declaration table.", claw.getPragma().lineNo());
        }
        String crtTypeHash = id.getType();
        Xtype rawType = xcodeml.getTypeTable().get(crtTypeHash);
        if (!(rawType instanceof XbasicType)) {
            throw new IllegalTransformationException(String.format("Reshape variable %s is not a basic type.", reshapeInfo.getArrayName()), claw.getPragma().lineNo());
        }
        XbasicType crtType = (XbasicType) rawType;
        // Check dimension
        if (crtType.getDimensions() < reshapeInfo.getTargetDimension()) {
            throw new IllegalTransformationException(String.format("Reshape variable %s has smaller dimension than requested.", reshapeInfo.getArrayName()), claw.getPragma().lineNo());
        }
        // Create new type
        XbasicType newType = crtType.cloneNode();
        newType.setType(xcodeml.getTypeTable().generateRealTypeHash());
        if (reshapeInfo.getTargetDimension() == 0) {
            // Demote to scalar
            newType.resetDimension();
        } else {
            if (crtType.getDimensions() - reshapeInfo.getKeptDimensions().size() != reshapeInfo.getTargetDimension()) {
                throw new IllegalTransformationException(String.format("Reshape information for %s not valid. " + "Target dimension and kept dimension mismatch.", reshapeInfo.getArrayName()));
            }
            newType.removeDimension(reshapeInfo.getKeptDimensions());
        }
        xcodeml.getTypeTable().add(newType);
        // Update symbol & declaration
        id.setType(newType.getType());
        decl.matchSeq(Xcode.NAME).setAttribute(Xattr.TYPE, newType.getType());
        // Update array references
        List<Xnode> refs = XnodeUtil.getAllArrayReferences(fctDef.body(), reshapeInfo.getArrayName());
        for (Xnode ref : refs) {
            if (reshapeInfo.getTargetDimension() == 0) {
                XnodeUtil.demoteToScalar(ref);
            } else {
                XnodeUtil.demote(ref, reshapeInfo.getKeptDimensions());
            }
        }
    }
}
Also used : IllegalTransformationException(cx2x.xcodeml.exception.IllegalTransformationException) ClawReshapeInfo(cx2x.translator.language.common.ClawReshapeInfo)

Example 3 with ClawReshapeInfo

use of cx2x.translator.language.common.ClawReshapeInfo in project claw-compiler by C2SM-RCM.

the class LoopHoist method analyze.

/**
   * @see Transformation#analyze(XcodeProgram, Transformer)
   */
@Override
public boolean analyze(XcodeProgram xcodeml, Transformer transformer) {
    int _pragmaDepthLevel = _clawStart.getPragma().depth();
    _nestedLevel = _clawStart.getHoistInductionVars().size();
    // Find all the group of nested loops that can be part of the hoisting
    List<Xnode> statements = XnodeUtil.findDoStatement(_clawStart.getPragma(), _clawEnd.getPragma(), _clawStart.getHoistInductionVars());
    if (statements.size() == 0) {
        xcodeml.addError("No do statement group meets the criteria of hoisting.", _clawStart.getPragma().lineNo());
        return false;
    }
    for (int i = 0; i < statements.size(); i++) {
        Xnode[] group = new Xnode[_nestedLevel];
        LoopHoistDoStmtGroup g = new LoopHoistDoStmtGroup(group);
        try {
            reloadDoStmts(g, statements.get(i));
        } catch (IllegalTransformationException e) {
            xcodeml.addError("Group " + i + " of do statements do not meet the" + " criteria of loop hoisting (Group index starts at 0).", _clawStart.getPragma().lineNo());
            return false;
        }
        LoopHoistDoStmtGroup crtGroup = new LoopHoistDoStmtGroup(group);
        int depth = group[0].depth();
        if (depth != _pragmaDepthLevel) {
            Xnode tmpIf = group[0].matchAncestor(Xcode.FIFSTATEMENT);
            Xnode tmpSelect = group[0].matchAncestor(Xcode.FSELECTCASESTATEMENT);
            Xnode tmpDo = group[0].matchAncestor(Xcode.FDOSTATEMENT);
            if (tmpIf == null && tmpSelect == null && tmpDo == null) {
                xcodeml.addError("Group " + i + " is nested in an unsupported " + "statement for loop hoisting (Group index starts at 0).", _clawStart.getPragma().lineNo());
                return false;
            }
            int ifDepth = (tmpIf != null) ? tmpIf.depth() : Xnode.UNDEF_DEPTH;
            int selectDepth = (tmpSelect != null) ? tmpSelect.depth() : Xnode.UNDEF_DEPTH;
            int doDepth = (tmpDo != null) ? tmpDo.depth() : Xnode.UNDEF_DEPTH;
            if ((_pragmaDepthLevel <= ifDepth || _pragmaDepthLevel <= selectDepth || _pragmaDepthLevel <= doDepth) && (ifDepth < depth || selectDepth < depth || doDepth < depth)) {
                crtGroup.setExtraction();
            } else {
                xcodeml.addError("Group " + i + " is nested in an unsupported " + "statement for loop hoisting or depth is too high " + "(Group index starts at 0).", _clawStart.getPragma().lineNo());
                return false;
            }
        }
        _doGroup.add(crtGroup);
    }
    LoopHoistDoStmtGroup master = _doGroup.get(0);
    for (int i = 1; i < _doGroup.size(); ++i) {
        LoopHoistDoStmtGroup next = _doGroup.get(i);
        for (int j = 0; j < master.getDoStmts().length; ++j) {
            // Iteration range are identical, just merge
            if (j == 0 && (!XnodeUtil.hasSameIndexRange(master.getDoStmts()[j], next.getDoStmts()[j]) && XnodeUtil.hasSameIndexRangeBesidesLower(master.getDoStmts()[j], next.getDoStmts()[j]))) {
                // Iteration range are identical besides lower-bound, if creation
                next.setIfStatement();
            } else if (!XnodeUtil.hasSameIndexRange(master.getDoStmts()[j], next.getDoStmts()[j])) {
                // Iteration range are too different, stop analysis
                xcodeml.addError("Iteration range of do statements group " + i + " differs from group 0. Loop hoisting aborted.", _clawStart.getPragma().lineNo());
                return false;
            }
        }
    }
    // Check reshape mandatory points
    if (_clawStart.hasReshapeClause()) {
        XfunctionDefinition fctDef = XnodeUtil.findParentFunction(_clawStart.getPragma());
        if (fctDef == null) {
            xcodeml.addError("Unable to matchSeq the function/subroutine/module " + "definition including the current directive", _clawStart.getPragma().lineNo());
            return false;
        }
        for (ClawReshapeInfo r : _clawStart.getReshapeClauseValues()) {
            if (!fctDef.getSymbolTable().contains(r.getArrayName()) || !fctDef.getDeclarationTable().contains(r.getArrayName())) {
                // Check in the parent def if present
                if (!checkUpperDefinition(fctDef, r.getArrayName())) {
                    xcodeml.addError(String.format("Reshape variable %s not found in " + "the definition of %s", r.getArrayName(), fctDef.getName().value()), _clawStart.getPragma().lineNo());
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Xnode(cx2x.xcodeml.xnode.Xnode) IllegalTransformationException(cx2x.xcodeml.exception.IllegalTransformationException) ClawReshapeInfo(cx2x.translator.language.common.ClawReshapeInfo) XfunctionDefinition(cx2x.xcodeml.xnode.XfunctionDefinition)

Aggregations

ClawReshapeInfo (cx2x.translator.language.common.ClawReshapeInfo)3 IllegalTransformationException (cx2x.xcodeml.exception.IllegalTransformationException)2 XfunctionDefinition (cx2x.xcodeml.xnode.XfunctionDefinition)1 Xnode (cx2x.xcodeml.xnode.Xnode)1 Test (org.junit.Test)1