Search in sources :

Example 1 with PromotionInfo

use of claw.tatsu.xcodeml.abstraction.PromotionInfo in project claw-compiler by C2SM-RCM.

the class ScaCPUvectorizeGroup method promote.

/**
 * Promote the given variable and adapt references.
 *
 * @param xcodeml Current translation unit.
 * @param var     Variable name to be promoted.
 * @throws IllegalTransformationException If promotion cannot be done.
 */
private void promote(Configuration cfg, XcodeProgram xcodeml, String var) throws IllegalTransformationException {
    PromotionInfo promotionInfo;
    // Do the promotion if needed
    if (!_promotions.containsKey(var)) {
        Message.debug(xcodeml.context(), String.format("%s promote variable %s", SCA_DEBUG_PREFIX, var));
        promotionInfo = new PromotionInfo(var, _claw.getLayoutForData(cfg, var));
        Field.promote(promotionInfo, _fctDef, xcodeml);
        _promotions.put(var, promotionInfo);
    } else {
        promotionInfo = _promotions.get(var);
    }
    // Adapt references
    Xid id = _fctDef.getSymbolTable().get(var);
    FbasicType bType = xcodeml.getTypeTable().getBasicType(id);
    if (!bType.isArray()) {
        Field.adaptScalarRefToArrayRef(_promotions.get(var), _fctDef, _claw.getDefaultLayout(cfg), xcodeml);
    } else {
        Field.adaptArrayRef(_promotions.get(var), _fctDef.body(), false, xcodeml);
        Field.adaptAllocate(_promotions.get(var), _fctDef.body(), xcodeml);
    }
    promotionInfo.setRefAdapted();
}
Also used : Xid(claw.tatsu.xcodeml.xnode.common.Xid) PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo) FbasicType(claw.tatsu.xcodeml.xnode.fortran.FbasicType)

Example 2 with PromotionInfo

use of claw.tatsu.xcodeml.abstraction.PromotionInfo in project claw-compiler by C2SM-RCM.

the class ScaForward 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 translator Current translator.
 * @throws Exception If something goes wrong.
 */
private void transformStd(XcodeProgram xcodeml, Translator translator) throws Exception {
    ClawTranslator trans = (ClawTranslator) translator;
    final Context context = trans.context();
    final Configuration cfg = trans.cfg();
    FfunctionDefinition fDef = _claw.getPragma().findParentFunction();
    if (fDef == null) {
        throw new IllegalTransformationException("SCA directive is not " + "nested in a function/subroutine.", _claw.getPragma().lineNo());
    }
    FfunctionType parentFctType = xcodeml.getTypeTable().getFunctionType(fDef);
    if (_fctType.isElemental() || _fctType.getBooleanAttribute(Xattr.WAS_ELEMENTAL)) {
        _flatten = true;
    }
    List<Xnode> params = _fctType.getParameters();
    /*
         * 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.isEmpty() && FortranType.STRUCT.isOfType(params.get(0).getType()) && _fCall.isTbpCall()) {
        argOffset = 1;
    }
    // 1. Adapt function call with potential new arguments
    for (int i = 0; i < params.size(); i++) {
        Xnode p = params.get(i);
        String varId = p.value();
        String type;
        FbasicType paramType = xcodeml.getTypeTable().getBasicType(p);
        if (!p.getBooleanAttribute(Xattr.IS_INSERTED)) {
            continue;
        }
        if (!fDef.getSymbolTable().contains(varId)) {
            if (_flatten && !paramType.getBooleanAttribute(Xattr.IS_OPTIONAL)) {
                throw new IllegalTransformationException("Variable " + varId + " must be locally defined where the last call to one_column " + "is made.", _claw.getPragma().lineNo());
            }
            // Size variable have to be declared
            FbasicType bt = xcodeml.createBasicType(FortranType.INTEGER, Intent.IN);
            xcodeml.getTypeTable().add(bt);
            xcodeml.createIdAndDecl(varId, bt.getType(), XstorageClass.F_PARAM, fDef, DeclarationPosition.FIRST);
            type = bt.getType();
            Xnode param = xcodeml.createAndAddParam(varId, type, parentFctType);
            param.setBooleanAttribute(Xattr.IS_INSERTED, true);
        } else {
            // Var exists already. Add to the parameters if not here.
            type = fDef.getSymbolTable().get(varId).getType();
            /*
                 * If flatten mode, we do not add extra parameters to the function definition.
                 */
            if (!_flatten && !fDef.getSymbolTable().contains(varId)) {
                Xnode param = xcodeml.createAndAddParamIfNotExists(varId, type, parentFctType);
                if (param != null) {
                    param.setBooleanAttribute(Xattr.IS_INSERTED, true);
                }
            }
        }
        // Add variable in the function call before the optional parameters
        Xnode arg = xcodeml.createNamedValue(varId);
        arg.append(xcodeml.createVar(type, varId, Xscope.LOCAL));
        Xnode hook = _fCall.arguments().get((i - 1) - argOffset);
        if (hook != null) {
            hook.insertAfter(arg);
        } else {
            _fCall.addArguments(arg);
        }
    }
    // In flatten mode, arguments are demoted if needed.
    if (_flatten) {
        for (Xnode arg : _fCall.arguments()) {
            if (arg.is(Xcode.F_ARRAY_REF) && arg.matchDirectDescendant(Arrays.asList(Xcode.INDEX_RANGE, Xcode.ARRAY_INDEX)) != null) {
                List<Xnode> arrayIndexes = arg.matchAll(Xcode.ARRAY_INDEX);
                for (Xnode n : arrayIndexes) {
                    if (_doStatements != null && XnodeUtil.isInductionIndex(n, _doStatements.getInductionVariables())) {
                        n.insertAfter(xcodeml.createEmptyAssumedShaped());
                        XnodeUtil.safeDelete(n);
                    }
                }
            }
        }
    } else {
        // 2. Adapt function/subroutine in which the function call is nested
        for (Xnode pBase : _fctType.getParameters()) {
            String originalParam = pBase.value();
            if (_fctCallMapping.containsKey(originalParam)) {
                originalParam = _fctCallMapping.get(originalParam);
            }
            Xnode pUpdate = null;
            for (Xnode param : parentFctType.getParameters()) {
                if (originalParam.equals(param.value())) {
                    pUpdate = param;
                }
            }
            if (pUpdate == null) {
                // field is not a parameter but maybe out field
                Xnode d = fDef.getDeclarationTable().get(originalParam);
                if (d != null) {
                    pUpdate = d.matchSeq(Xcode.NAME);
                }
            }
            if (pUpdate != null) {
                if (pUpdate.getType() == null || FortranType.isBuiltInType(pUpdate.getType())) {
                    continue;
                }
                FbasicType typeBase = (_localFct) ? xcodeml.getTypeTable().getBasicType(pBase) : _mod.getTypeTable().getBasicType(pBase);
                FbasicType typeToUpdate = xcodeml.getTypeTable().getBasicType(pUpdate);
                int targetDim = typeBase.getDimensions();
                int baseDim = typeToUpdate.getDimensions();
                // Types have different dimensions
                if (typeBase.getDimensions() > typeToUpdate.getDimensions()) {
                    PromotionInfo promotionInfo = new PromotionInfo(originalParam);
                    promotionInfo.readDimensionsFromString(pBase.getAttribute(Xattr.PROMOTION_INFO));
                    FbasicType type = _localFct ? Type.duplicateWithDimension(typeBase, typeToUpdate, xcodeml, xcodeml, promotionInfo.getDimensions()) : Type.duplicateWithDimension(typeBase, typeToUpdate, _mod, xcodeml, promotionInfo.getDimensions());
                    pUpdate.setType(type);
                    Xid id = fDef.getSymbolTable().get(originalParam);
                    if (id != null) {
                        id.setType(type);
                    }
                    Xnode varDecl = fDef.getDeclarationTable().get(originalParam);
                    if (varDecl != null) {
                        varDecl.matchSeq(Xcode.NAME).setType(type);
                    }
                    promotionInfo.setBaseDimension(baseDim);
                    promotionInfo.setTargetDimension(targetDim);
                    promotionInfo.setTargetType(type);
                    _promotions.put(originalParam, promotionInfo);
                    _promotedVar.add(originalParam);
                    pBase.copyAttribute(pUpdate, Xattr.PROMOTION_INFO);
                }
            }
        }
        if (!parentFctType.getBooleanAttribute(Xattr.IS_PRIVATE)) {
            // 3. Replicate the change in a potential module file
            FmoduleDefinition modDef = fDef.findParentModule();
            if (modDef != null) {
                Xmod.updateSignature(modDef.getName(), xcodeml, fDef, parentFctType, false);
            }
        } else if (_fCall.matchSeq(Xcode.NAME).hasAttribute(Xattr.DATA_REF)) {
            /*
                 * 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.
                 */
            FmoduleDefinition modDef = fDef.findParentModule();
            if (modDef != null) {
                Xmod.updateSignature(modDef.getName(), xcodeml, fDef, parentFctType, true);
            }
        }
    }
    updateResultVar(xcodeml);
    propagatePromotion(xcodeml, (ClawTranslator) translator);
    Xnode fctCallAncestor = _fCall.matchAncestor(Xcode.EXPR_STATEMENT);
    if (fctCallAncestor == null) {
        fctCallAncestor = _fCall.matchAncestor(Xcode.F_ASSIGN_STATEMENT);
    }
    if (_claw.hasClause(ClawClause.CREATE) && context.isTarget(Target.GPU)) {
        List<String> creates = _fCall.gatherArguments(xcodeml, _fctType, _mod, Intent.INOUT, true, false);
        if (_fctType.isFunction()) {
            String returnValue = XnodeUtil.gatherReturnValue(xcodeml, _fCall);
            if (returnValue != null) {
                creates.add(returnValue);
            }
        }
        Xblock fctCallBlock = new Xblock(fctCallAncestor);
        Directive.generateDataRegionClause(xcodeml, Collections.emptyList(), creates, fctCallBlock);
    }
    // Serialization input
    if (_claw.hasClause(ClawClause.SAVEPOINT)) {
        List<String> inFields = _fCall.gatherArguments(xcodeml, _fctType, _mod != null ? _mod : xcodeml, Intent.IN, true, false);
        Serialization.insertImports(cfg, xcodeml, _fCall.findParentFunction());
        if (context.isTarget(Target.CPU)) {
            Serialization.generateWriteSavepoint(cfg, xcodeml, fctCallAncestor, _claw.getMetadataMap(), inFields, _claw.value(ClawClause.SAVEPOINT), SerializationStep.SER_IN);
        } else {
            Serialization.generateReadSavepoint(cfg, xcodeml, fctCallAncestor, _claw.getMetadataMap(), inFields, _claw.value(ClawClause.SAVEPOINT), SerializationStep.SER_IN);
        }
    }
    Xnode postHook = fctCallAncestor;
    if (_claw.hasClause(ClawClause.UPDATE) && context.isTarget(Target.GPU) && cfg.getBooleanParameter(Configuration.SCA_FORWARD_UPDATE_ENABLED)) {
        // Generate update from HOST TO DEVICE
        if ((_claw.getUpdateClauseValue() == DataMovement.TWO_WAY || _claw.getUpdateClauseValue() == DataMovement.HOST_TO_DEVICE) && cfg.updateAtInput()) {
            List<String> in = _fCall.gatherArguments(xcodeml, _fctType, _mod != null ? _mod : xcodeml, Intent.IN, true, false);
            Directive.generateUpdate(xcodeml, fctCallAncestor, in, DataMovement.HOST_TO_DEVICE);
        }
        // Generate update from DEVICE to HOST
        if ((_claw.getUpdateClauseValue() == DataMovement.TWO_WAY || _claw.getUpdateClauseValue() == DataMovement.DEVICE_TO_HOST) && cfg.updateAtOutput()) {
            List<String> out = _fCall.gatherArguments(xcodeml, _fctType, _mod != null ? _mod : xcodeml, Intent.OUT, true, false);
            if (_fctType.isFunction()) {
                String returnValue = XnodeUtil.gatherReturnValue(xcodeml, _fCall);
                if (returnValue != null) {
                    out.add(returnValue);
                }
            }
            postHook = Directive.generateUpdate(xcodeml, fctCallAncestor, out, DataMovement.DEVICE_TO_HOST);
        }
    }
    if (_claw.hasClause(ClawClause.PARALLEL) && context.isTarget(Target.GPU)) {
        Directive.generateParallelRegion(xcodeml, fctCallAncestor, fctCallAncestor);
    }
    // Serialization output
    if (_claw.hasClause(ClawClause.SAVEPOINT)) {
        List<String> outFields = _fCall.gatherArguments(xcodeml, _fctType, _mod != null ? _mod : xcodeml, Intent.OUT, true, false);
        Serialization.insertImports(cfg, xcodeml, _fCall.findParentFunction());
        Serialization.generateWriteSavepoint(cfg, xcodeml, postHook, _claw.getMetadataMap(), outFields, _claw.value(ClawClause.SAVEPOINT), SerializationStep.SER_OUT);
    }
}
Also used : Context(claw.tatsu.common.Context) Configuration(claw.wani.x2t.configuration.Configuration) FfunctionType(claw.tatsu.xcodeml.xnode.fortran.FfunctionType) PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo) FbasicType(claw.tatsu.xcodeml.xnode.fortran.FbasicType) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) Xid(claw.tatsu.xcodeml.xnode.common.Xid) FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) Xblock(claw.tatsu.xcodeml.abstraction.Xblock) FmoduleDefinition(claw.tatsu.xcodeml.xnode.fortran.FmoduleDefinition) ClawTranslator(claw.wani.x2t.translator.ClawTranslator)

Example 3 with PromotionInfo

use of claw.tatsu.xcodeml.abstraction.PromotionInfo in project claw-compiler by C2SM-RCM.

the class ScaForward method updateResultVar.

/**
 * Apply promotion to the result return variable of a forward call.
 *
 * @param xcodeml Current XcodeML program unit.
 * @throws IllegalTransformationException If XcodeML transformation cannot be
 *                                        done.
 */
private void updateResultVar(XcodeProgram xcodeml) throws IllegalTransformationException {
    if (_isNestedInAssignment) {
        Xnode assignment = _claw.getPragma().nextSibling();
        if (assignment == null || !_fctType.hasAttribute(Xattr.PROMOTION_INFO)) {
            return;
        }
        Xnode lhs = assignment.firstChild();
        // TODO handle the case when the array ref is a var directly
        Xnode varInLhs = lhs.matchDescendant(Xcode.VAR);
        FfunctionDefinition parentFctDef = _fCall.findParentFunction();
        PromotionInfo promotionInfo;
        if (!_promotions.containsKey(varInLhs.value())) {
            // Perform the promotion on the variable
            promotionInfo = new PromotionInfo(varInLhs.value());
            promotionInfo.readDimensionsFromString(_fctType.getAttribute(Xattr.PROMOTION_INFO));
            Field.promote(promotionInfo, parentFctDef, xcodeml);
            _promotions.put(varInLhs.value(), promotionInfo);
            _promotedVar.add(varInLhs.value());
        } else {
            promotionInfo = _promotions.get(varInLhs.value());
        }
        // Adapt array index to reflect the new return type
        if (Xnode.isOfCode(lhs, Xcode.F_ARRAY_REF)) {
            for (int i = 0; i < promotionInfo.diffDimension(); ++i) {
                Xnode indexRange = xcodeml.createEmptyAssumedShaped();
                lhs.append(indexRange);
            }
        } else {
            throw new IllegalTransformationException("Unsupported return " + "variable for promotion.", _claw.getPragma().lineNo());
        }
        // If the array is a target, check if we have to promote a pointer
        Field.adaptPointer(xcodeml, parentFctDef, _promotions, promotionInfo);
    }
}
Also used : Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo)

Example 4 with PromotionInfo

use of claw.tatsu.xcodeml.abstraction.PromotionInfo in project claw-compiler by C2SM-RCM.

the class FieldTest method formattedDimensionsTest.

@Test
public void formattedDimensionsTest() {
    DimensionDefinition dim1 = new DimensionDefinition("dim1", "1", "30");
    DimensionDefinition dim2 = new DimensionDefinition("dim2", "1", "40");
    List<DimensionDefinition> dimensions1 = Collections.singletonList(dim1);
    List<DimensionDefinition> dimensions2 = Arrays.asList(dim1, dim2);
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    PromotionInfo p1 = new PromotionInfo("a", dimensions1);
    assertEquals("dim1(1:30),:", p1.getFormattedDimensions());
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    assertEquals(":,dim1(1:30),:", p1.getFormattedDimensions());
    dim1.setInsertionPosition(InsertionPosition.AFTER);
    assertEquals(":,dim1(1:30)", p1.getFormattedDimensions());
    PromotionInfo p2 = new PromotionInfo("a", dimensions2);
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    dim2.setInsertionPosition(InsertionPosition.BEFORE);
    assertEquals("dim1(1:30),dim2(1:40),:", p2.getFormattedDimensions());
    dim1.setInsertionPosition(InsertionPosition.AFTER);
    dim2.setInsertionPosition(InsertionPosition.AFTER);
    assertEquals(":,dim1(1:30),dim2(1:40)", p2.getFormattedDimensions());
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    dim2.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    assertEquals(":,dim1(1:30),dim2(1:40),:", p2.getFormattedDimensions());
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    dim2.setInsertionPosition(InsertionPosition.AFTER);
    assertEquals("dim1(1:30),:,dim2(1:40)", p2.getFormattedDimensions());
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    dim2.setInsertionPosition(InsertionPosition.AFTER);
    assertEquals(":,dim1(1:30),:,dim2(1:40)", p2.getFormattedDimensions());
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    dim2.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    assertEquals("dim1(1:30),:,dim2(1:40),:", p2.getFormattedDimensions());
    PromotionInfo p3 = new PromotionInfo("c");
    assertEquals("", p3.getFormattedDimensions());
}
Also used : PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo) DimensionDefinition(claw.tatsu.xcodeml.abstraction.DimensionDefinition) Test(org.junit.Test)

Example 5 with PromotionInfo

use of claw.tatsu.xcodeml.abstraction.PromotionInfo in project claw-compiler by C2SM-RCM.

the class ScaGPU method applyPromoteStrategy.

/**
 * Apply the promotion local array strategy. Gather all information about local
 * variable requiring a promotion and apply it.
 *
 * @param xcodeml Current translation unit.
 * @return List of promoted variable requiring an allocation.
 * @throws IllegalTransformationException If promotion of variable fails.
 */
private List<String> applyPromoteStrategy(Configuration cfg, XcodeProgram xcodeml) throws IllegalTransformationException {
    List<String> createList = _fctDef.getLocalVariables(xcodeml, true);
    for (String arrayIdentifier : createList) {
        _arrayFieldsInOut.add(arrayIdentifier);
        PromotionInfo promotionInfo = new PromotionInfo(arrayIdentifier, _claw.getLayoutForData(cfg, arrayIdentifier));
        Field.promote(promotionInfo, _fctDef, xcodeml);
        _promotions.put(arrayIdentifier, promotionInfo);
        Field.adaptArrayRef(promotionInfo, _fctDef.body(), false, xcodeml);
        Field.adaptAllocate(promotionInfo, _fctDef.body(), xcodeml);
    }
    return createList;
}
Also used : PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo)

Aggregations

PromotionInfo (claw.tatsu.xcodeml.abstraction.PromotionInfo)12 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)6 Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)6 FbasicType (claw.tatsu.xcodeml.xnode.fortran.FbasicType)4 Context (claw.tatsu.common.Context)3 DimensionDefinition (claw.tatsu.xcodeml.abstraction.DimensionDefinition)3 Xid (claw.tatsu.xcodeml.xnode.common.Xid)3 FfunctionDefinition (claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)3 NestedDoStatement (claw.tatsu.xcodeml.abstraction.NestedDoStatement)2 Xblock (claw.tatsu.xcodeml.abstraction.Xblock)2 FfunctionType (claw.tatsu.xcodeml.xnode.fortran.FfunctionType)2 Test (org.junit.Test)2 AcceleratorConfiguration (claw.tatsu.directive.configuration.AcceleratorConfiguration)1 Xattr (claw.tatsu.xcodeml.xnode.common.Xattr)1 FmoduleDefinition (claw.tatsu.xcodeml.xnode.fortran.FmoduleDefinition)1 FortranModule (claw.tatsu.xcodeml.xnode.fortran.FortranModule)1 Configuration (claw.wani.x2t.configuration.Configuration)1 ClawTranslator (claw.wani.x2t.translator.ClawTranslator)1