Search in sources :

Example 6 with IllegalTransformationException

use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.

the class LoopHoist method reloadDoStmts.

/**
   * Relocated nested do statement inside a group of do statement.
   *
   * @param g        The group of do statement.
   * @param newStart The new outer do statement.
   * @throws IllegalTransformationException If the nested group doesn't match
   *                                        the correct size.
   */
private void reloadDoStmts(LoopHoistDoStmtGroup g, Xnode newStart) throws IllegalTransformationException {
    g.getDoStmts()[0] = newStart;
    for (int j = 1; j < g.getDoStmts().length; ++j) {
        Xnode next = g.getDoStmts()[j - 1].body().matchDirectDescendant(Xcode.FDOSTATEMENT);
        if (next == null) {
            throw new IllegalTransformationException("Unable to matchSeq enough nested do statements", _clawStart.getPragma().lineNo());
        }
        g.getDoStmts()[j] = next;
    }
}
Also used : Xnode(cx2x.xcodeml.xnode.Xnode) IllegalTransformationException(cx2x.xcodeml.exception.IllegalTransformationException)

Example 7 with IllegalTransformationException

use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.

the class ClawXcodeMlTranslator method transform.

/**
   * Apply all the transformation in the pipeline.
   */
public void transform() {
    try {
        if (!_canTransform) {
            _program.write(_xcodemlOutputFile, ClawConstant.INDENT_OUTPUT);
            return;
        }
        reorderTransformations();
        for (Map.Entry<Class, TransformationGroup> entry : _transformer.getGroups().entrySet()) {
            if (XmOption.isDebugOutput()) {
                System.out.println("Apply transformation: " + entry.getValue().transformationName() + " - " + entry.getValue().count());
            }
            try {
                entry.getValue().applyTranslations(_program, _transformer);
                displayWarnings();
            } catch (IllegalTransformationException itex) {
                _program.addError(itex.getMessage(), itex.getStartLine());
                abort();
            } catch (Exception ex) {
                ex.printStackTrace();
                _program.addError("Unexpected error: " + ex.getMessage(), 0);
                if (XmOption.isDebugOutput()) {
                    StringWriter errors = new StringWriter();
                    ex.printStackTrace(new PrintWriter(errors));
                    _program.addError(errors.toString(), 0);
                }
                abort();
            }
        }
        // Write transformed IR to file
        _program.write(_xcodemlOutputFile, ClawConstant.INDENT_OUTPUT);
    } catch (Exception ex) {
        System.err.println("Transformation exception: " + ex.getMessage());
    }
}
Also used : StringWriter(java.io.StringWriter) IllegalTransformationException(cx2x.xcodeml.exception.IllegalTransformationException) TransformationGroup(cx2x.xcodeml.transformation.TransformationGroup) IllegalDirectiveException(cx2x.xcodeml.exception.IllegalDirectiveException) IllegalTransformationException(cx2x.xcodeml.exception.IllegalTransformationException) PrintWriter(java.io.PrintWriter)

Example 8 with IllegalTransformationException

use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.

the class Parallelize method transform.

@Override
public void transform(XcodeProgram xcodeml, Transformer transformer, Transformation other) throws Exception {
    // Handle PURE function / subroutine
    ClawTransformer trans = (ClawTransformer) transformer;
    boolean pureRemoved = XnodeUtil.removePure(_fctDef, _fctType);
    if (trans.getConfiguration().isForcePure() && pureRemoved) {
        throw new IllegalTransformationException("PURE specifier cannot be removed", _fctDef.lineNo());
    } else if (pureRemoved) {
        String fctName = _fctDef.matchDirectDescendant(Xcode.NAME).value();
        System.out.println("Warning: PURE specifier removed from function " + fctName + " at line " + _fctDef.lineNo() + ". Transformation and " + "code generation applied to it.");
    }
    // Prepare the array index that will be added to the array references.
    prepareArrayIndexes(xcodeml);
    // Insert the declarations of variables to iterate over the new dimensions.
    insertVariableToIterateOverDimension(xcodeml);
    // Promote all array fields with new dimensions.
    promoteFields(xcodeml);
    // Adapt array references.
    if (_claw.hasOverDataClause()) {
        for (int i = 0; i < _claw.getOverDataClauseValues().size(); ++i) {
            TransformationHelper.adaptArrayReferences(_claw.getOverDataClauseValues().get(i), i, _fctDef.body(), _promotions, _beforeCrt, _inMiddle, _afterCrt, xcodeml);
        }
    } else {
        TransformationHelper.adaptArrayReferences(_arrayFieldsInOut, DEFAULT_OVER, _fctDef.body(), _promotions, _beforeCrt, _inMiddle, _afterCrt, xcodeml);
    }
    // Delete the pragma
    _claw.getPragma().delete();
    // Apply specific target transformation
    if (_claw.getTarget() == Target.GPU) {
        transformForGPU(xcodeml);
    } else if (_claw.getTarget() == Target.CPU) {
        transformForCPU(xcodeml);
    }
    if (!_fctType.getBooleanAttribute(Xattr.IS_PRIVATE)) {
        XmoduleDefinition modDef = _fctDef.findParentModule();
        if (modDef != null) {
            TransformationHelper.updateModuleSignature(xcodeml, _fctDef, _fctType, modDef, _claw, transformer, false);
        }
    }
}
Also used : IllegalTransformationException(cx2x.xcodeml.exception.IllegalTransformationException) ClawTransformer(cx2x.translator.transformer.ClawTransformer)

Example 9 with IllegalTransformationException

use of cx2x.xcodeml.exception.IllegalTransformationException 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 10 with IllegalTransformationException

use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.

the class ParallelizeForward method transformStd.

/**
   * Do the standard transformation for the forward directive. This
   * transformation adapt the function call and replicates any necessary changes
   * to the containing subroutine.
   *
   * @param xcodeml     Current XcodeML file unit.
   * @param transformer Current transformer.
   * @throws Exception If something goes wrong.
   */
private void transformStd(XcodeProgram xcodeml, Transformer transformer) throws Exception {
    XfunctionDefinition fDef = XnodeUtil.findParentFunction(_claw.getPragma());
    if (fDef == null) {
        throw new IllegalTransformationException("Parallelize directive is not " + "nested in a function/subroutine.", _claw.getPragma().lineNo());
    }
    _parentFctType = (XfunctionType) xcodeml.getTypeTable().get(fDef.getName().getAttribute(Xattr.TYPE));
    List<Xnode> params = _fctType.getParams().getAll();
    /* Compute the position of the first new arguments. In the case of a
     * type-bound procedure call, the first parameter declared in the procedure
     * is not actually passed as an argument. In this case, we add an offset of
     * one to the starting arguments.
     * TODO the check might be change to fit with the XcodeML/F2008 specs. The
     * TODO cont: attribute data_ref will probably be gone and replaced by a
     * TODO cont: FmemberRef element
     */
    int argOffset = 0;
    if (params.get(0).getAttribute(Xattr.TYPE).startsWith(Xtype.PREFIX_STRUCT) && _fctCall.firstChild().opcode().equals(Xcode.FMEMBERREF)) {
        argOffset = 1;
    }
    // 1. Adapt function call with potential new arguments
    for (int i = 0; i < params.size(); i++) {
        Xnode p = params.get(i);
        String var = p.value();
        String type;
        XbasicType paramType = (XbasicType) xcodeml.getTypeTable().get(p.getAttribute(Xattr.TYPE));
        if (!p.getBooleanAttribute(ClawAttr.IS_CLAW.toString())) {
            continue;
        }
        if (!fDef.getSymbolTable().contains(var)) {
            if (_flatten && !paramType.getBooleanAttribute(Xattr.IS_OPTIONAL)) {
                throw new IllegalTransformationException("Variable " + var + " must" + " be locally defined where the last call to parallelize if made.", _claw.getPragma().lineNo());
            }
            // Size variable have to be declared
            XbasicType intTypeIntentIn = xcodeml.createBasicType(xcodeml.getTypeTable().generateIntegerTypeHash(), Xname.TYPE_F_INT, Xintent.IN);
            xcodeml.getTypeTable().add(intTypeIntentIn);
            xcodeml.createIdAndDecl(var, intTypeIntentIn.getType(), Xname.SCLASS_F_PARAM, fDef);
            type = intTypeIntentIn.getType();
            Xnode param = xcodeml.createAndAddParam(var, type, _parentFctType);
            param.setAttribute(ClawAttr.IS_CLAW.toString(), Xname.TRUE);
        } else {
            // Var exists already. Add to the parameters if not here.
            type = fDef.getSymbolTable().get(var).getType();
            /* If flatten mode, we do not add extra parameters to the function
         * definition */
            if (!_flatten) {
                Xnode param = xcodeml.createAndAddParamIfNotExists(var, type, _parentFctType);
                if (param != null) {
                    param.setAttribute(ClawAttr.IS_CLAW.toString(), Xname.TRUE);
                }
            }
        }
        // Add variable in the function call before the optional parameters
        Xnode arg = xcodeml.createNamedValue(var);
        Xnode namedValVar = xcodeml.createVar(type, var, Xscope.LOCAL);
        arg.append(namedValVar, false);
        Xnode arguments = _fctCall.matchSeq(Xcode.ARGUMENTS);
        Xnode hook = arguments.child((i - 1) - argOffset);
        hook.insertAfter(arg);
    }
    // In flatten mode, arguments are demoted if needed.
    if (_flatten) {
        Xnode arguments = _fctCall.matchSeq(Xcode.ARGUMENTS);
        for (Xnode arg : arguments.children()) {
            if (arg.opcode() == Xcode.FARRAYREF && arg.matchDirectDescendant(Arrays.asList(Xcode.INDEXRANGE, Xcode.ARRAYINDEX)) != null) {
                Xnode var = arg.matchSeq(Xcode.VARREF, Xcode.VAR);
                if (var != null) {
                    arg.insertAfter(var.cloneNode());
                    arg.delete();
                }
            }
        }
    } else {
        // 2. Adapt function/subroutine in which the function call is nested
        for (Xnode pBase : _fctType.getParams().getAll()) {
            String original_param = pBase.value();
            if (_fctCallMapping.containsKey(original_param)) {
                original_param = _fctCallMapping.get(original_param);
            }
            Xnode pUpdate = null;
            for (Xnode param : _parentFctType.getParams().getAll()) {
                if (original_param.equals(param.value())) {
                    pUpdate = param;
                }
            }
            if (pUpdate == null) {
                // field is not a parameter but maybe out field
                Xdecl d = fDef.getDeclarationTable().get(original_param);
                if (d != null) {
                    pUpdate = d.matchSeq(Xcode.NAME);
                }
            // TODO handle deferred shape
            }
            if (pUpdate != null) {
                if (pUpdate.getAttribute(Xattr.TYPE) == null || XnodeUtil.isBuiltInType(pUpdate.getAttribute(Xattr.TYPE))) {
                    continue;
                }
                XbasicType typeBase = (_localFct) ? (XbasicType) xcodeml.getTypeTable().get(pBase.getAttribute(Xattr.TYPE)) : (XbasicType) _mod.getTypeTable().get(pBase.getAttribute(Xattr.TYPE));
                XbasicType typeToUpdate = (XbasicType) xcodeml.getTypeTable().get(pUpdate.getAttribute(Xattr.TYPE));
                int targetDim = typeBase.getDimensions();
                int baseDim = typeToUpdate.getDimensions();
                // Types have different dimensions
                if (typeBase.getDimensions() > typeToUpdate.getDimensions()) {
                    // TODO check intent rules
                    /*if(!isParam && typeToUpdate.getIntent() != Xintent.OUT){
              continue;
            }*/
                    List<ClawDimension> dimensions = TransformationHelper.findDimensions(_fctType);
                    OverPosition overPos = OverPosition.fromString(pBase.getAttribute(ClawAttr.OVER.toString()));
                    String type = _localFct ? TransformationHelper.duplicateWithDimension(typeBase, typeToUpdate, xcodeml, xcodeml, overPos, dimensions) : TransformationHelper.duplicateWithDimension(typeBase, typeToUpdate, xcodeml, _mod, overPos, dimensions);
                    pUpdate.setAttribute(Xattr.TYPE, type);
                    Xid id = fDef.getSymbolTable().get(original_param);
                    if (id != null) {
                        id.setAttribute(Xattr.TYPE, type);
                    }
                    Xdecl varDecl = fDef.getDeclarationTable().get(original_param);
                    if (varDecl != null) {
                        varDecl.matchSeq(Xcode.NAME).setAttribute(Xattr.TYPE, type);
                    }
                    _promotedVar.add(original_param);
                    addPromotedVar(original_param, overPos);
                    _promotions.put(original_param, new PromotionInfo(pBase.value(), baseDim, targetDim, type));
                }
            }
        }
        if (!_parentFctType.getBooleanAttribute(Xattr.IS_PRIVATE)) {
            // 3. Replicate the change in a potential module file
            XmoduleDefinition modDef = fDef.findParentModule();
            TransformationHelper.updateModuleSignature(xcodeml, fDef, _parentFctType, modDef, _claw, transformer, false);
        } else if (_fctCall.matchSeq(Xcode.NAME).hasAttribute(Xattr.DATAREF)) {
            /* The function/subroutine is private but accessible through the type
         * as a type-bound procedure. In this case, the function is not in the
         * type table of the .xmod file. We need to insert it first and then
         * we can update it. */
            XmoduleDefinition modDef = fDef.findParentModule();
            TransformationHelper.updateModuleSignature(xcodeml, fDef, _parentFctType, modDef, _claw, transformer, true);
        }
    }
    updateResultVar(xcodeml);
    propagatePromotion(xcodeml, (ClawTransformer) transformer);
}
Also used : OverPosition(cx2x.translator.language.common.OverPosition) ClawDimension(cx2x.translator.language.common.ClawDimension) IllegalTransformationException(cx2x.xcodeml.exception.IllegalTransformationException)

Aggregations

IllegalTransformationException (cx2x.xcodeml.exception.IllegalTransformationException)16 ClawDimension (cx2x.translator.language.common.ClawDimension)6 OverPosition (cx2x.translator.language.common.OverPosition)3 ClawReshapeInfo (cx2x.translator.language.common.ClawReshapeInfo)2 Xnode (cx2x.xcodeml.xnode.Xnode)2 Node (org.w3c.dom.Node)2 NestedDoStatement (cx2x.translator.common.NestedDoStatement)1 ClawConstraint (cx2x.translator.language.common.ClawConstraint)1 ClawMapping (cx2x.translator.language.common.ClawMapping)1 ClawMappingVar (cx2x.translator.language.common.ClawMappingVar)1 PromotionInfo (cx2x.translator.transformation.claw.parallelize.PromotionInfo)1 ClawTransformer (cx2x.translator.transformer.ClawTransformer)1 IllegalDirectiveException (cx2x.xcodeml.exception.IllegalDirectiveException)1 TransformationGroup (cx2x.xcodeml.transformation.TransformationGroup)1 XfunctionDefinition (cx2x.xcodeml.xnode.XfunctionDefinition)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Transformer (javax.xml.transform.Transformer)1 DOMSource (javax.xml.transform.dom.DOMSource)1