Search in sources :

Example 6 with PromotionInfo

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

the class Field method adaptPointer.

/**
 * Adapt potential pointer that are assigned from a promoted variable in a
 * function definition.
 *
 * @param xcodeml     Current XcodeML program unit.
 * @param fctDef      Function definition in which assignment statements are
 *                    checked.
 * @param promotions  Map of promoted variable with their promotion info.
 * @param pointeeInfo PromotionInformation about the promoted variable.
 * @throws IllegalTransformationException If XcodeML modifications failed.
 */
public static void adaptPointer(XcodeProgram xcodeml, FfunctionDefinition fctDef, Map<String, PromotionInfo> promotions, PromotionInfo pointeeInfo) throws IllegalTransformationException {
    FbasicType varType = xcodeml.getTypeTable().getBasicType(pointeeInfo.getTargetType());
    if (varType != null && varType.isTarget()) {
        List<Xnode> pAssignments = fctDef.matchAll(Xcode.F_POINTER_ASSIGN_STATEMENT);
        for (Xnode pAssignment : pAssignments) {
            Xnode pointer = pAssignment.child(0);
            Xnode pointee = pAssignment.child(1);
            // Check if the pointer assignment has the promoted variable
            if (pointee.value().equals(pointeeInfo.getIdentifier())) {
                FbasicType pointerType = xcodeml.getTypeTable().getBasicType(pointer);
                FbasicType pointeeType = xcodeml.getTypeTable().getBasicType(pointeeInfo.getTargetType());
                // Check if their dimensions differ
                if (pointeeType.getDimensions() != pointerType.getDimensions() && !promotions.containsKey(pointer.value())) {
                    PromotionInfo promotionInfo = new PromotionInfo(pointer.value(), pointeeInfo.getDimensions());
                    Field.promote(promotionInfo, fctDef, xcodeml);
                    promotions.put(pointer.value(), promotionInfo);
                }
            }
        }
    }
}
Also used : PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo)

Example 7 with PromotionInfo

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

the class Xmod method updateSignature.

/**
 * Update the function signature in the module file to reflects local changes.
 *
 * @param moduleName    Xmod name to update.
 * @param xcodeml       Current XcodeML file unit.
 * @param fctDef        Function definition that has been changed.
 * @param fctType       Function type that has been changed.
 * @param importFctType If true, import the functionType.
 * @throws IllegalTransformationException If the module file or the function
 *                                        cannot be located
 */
public static void updateSignature(String moduleName, XcodeProgram xcodeml, FfunctionDefinition fctDef, FfunctionType fctType, boolean importFctType) throws IllegalTransformationException {
    final Context context = xcodeml.context();
    FortranModule mod;
    if (context.getModuleCache().isModuleLoaded(moduleName)) {
        mod = context.getModuleCache().get(moduleName);
    } else {
        mod = fctDef.findContainingXmod(context);
        if (mod == null) {
            throw new IllegalTransformationException("Unable to locate module file for: " + moduleName);
        }
        context.getModuleCache().add(moduleName, mod);
    }
    FfunctionType fctTypeMod;
    if (importFctType) {
        // TODO should be part of XcodeML
        Xnode importedNode = mod.importNode(fctType);
        mod.getTypeTable().append(importedNode);
        FfunctionType importedFctType = new FfunctionType(importedNode);
        Xid importedFctTypeId = mod.createId(importedFctType.getType(), XstorageClass.F_FUNC, fctDef.getName());
        mod.getIdentifiers().add(importedFctTypeId);
        // check if params need to be imported as well
        if (!importedFctType.getParameters().isEmpty()) {
            for (Xnode param : importedFctType.getParameters()) {
                mod.importType(xcodeml, param.getType());
            }
        }
        return;
    } else {
        fctTypeMod = mod.findFunctionType(fctDef.getName());
    }
    if (fctTypeMod == null) {
        /*
             * Workaround for a bug in OMNI Compiler. Look at test case claw/abstraction12.
             * In this test case, the XcodeML/F intermediate representation for the function
             * call points to a FfunctionType element with no parameters. Thus, we have to
             * matchSeq the correct FfunctionType for the same function/subroutine with the
             * same name in the module symbol table.
             */
        String errorMsg = "Unable to locate fct " + fctDef.getName() + " in module " + moduleName;
        /*
             * If not, try to matchSeq the correct FfunctionType in the module definitions
             */
        fctTypeMod = mod.findFunctionType(fctDef.getName());
        if (fctTypeMod == null) {
            throw new IllegalTransformationException(errorMsg);
        }
    }
    FbasicType modIntTypeIntentIn = mod.createBasicType(FortranType.INTEGER, Intent.IN);
    mod.getTypeTable().add(modIntTypeIntentIn);
    List<Xnode> paramsLocal = fctType.getParameters();
    List<Xnode> paramsMod = fctTypeMod.getParameters();
    if (paramsLocal.size() < paramsMod.size()) {
        throw new IllegalTransformationException("Local function has more parameters than module counterpart.");
    }
    for (int i = 0; i < paramsLocal.size(); ++i) {
        Xnode pLocal = paramsLocal.get(i);
        // Number of parameters in the module function as been
        if (pLocal.getBooleanAttribute(Xattr.IS_INSERTED)) {
            // new parameter
            Xnode param = mod.createAndAddParamIfNotExists(pLocal.value(), modIntTypeIntentIn.getType(), fctTypeMod);
            if (param != null) {
                param.setBooleanAttribute(Xattr.IS_INSERTED, true);
            }
        } else {
            Xnode pMod = paramsMod.get(i);
            String localType = pLocal.getType();
            String modType = pMod.getType();
            if (!localType.equals(modType)) {
                // Param has been updated so have to replicate the change to mod file
                FbasicType lType = xcodeml.getTypeTable().getBasicType(pLocal);
                FbasicType crtType = mod.getTypeTable().getBasicType(pMod);
                if (pLocal.hasAttribute(Xattr.PROMOTION_INFO)) {
                    PromotionInfo promotionInfo = new PromotionInfo();
                    promotionInfo.readDimensionsFromString(pLocal.getAttribute(Xattr.PROMOTION_INFO));
                    if (lType.isArray()) {
                        FbasicType newType = Type.duplicateWithDimension(lType, crtType, xcodeml, mod, promotionInfo.getDimensions());
                        pMod.setType(newType);
                    }
                }
            }
            // Copy the promotion information
            pLocal.copyAttribute(pMod, Xattr.PROMOTION_INFO);
        }
    }
    // Sync attribute between local fct type and module fct type.
    for (Xattr attr : SYNCABLE_ATTR) {
        fctType.syncBooleanAttribute(fctTypeMod, attr);
    }
}
Also used : Context(claw.tatsu.common.Context) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) FortranModule(claw.tatsu.xcodeml.xnode.fortran.FortranModule) Xid(claw.tatsu.xcodeml.xnode.common.Xid) FfunctionType(claw.tatsu.xcodeml.xnode.fortran.FfunctionType) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo) Xattr(claw.tatsu.xcodeml.xnode.common.Xattr) FbasicType(claw.tatsu.xcodeml.xnode.fortran.FbasicType)

Example 8 with PromotionInfo

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

the class ScaGPU method applySpecificTransformation.

/**
 * Apply specific transformation steps for GPU target.
 *
 * @param xcodeml Current translation unit.
 * @throws IllegalTransformationException If any transformation fails.
 */
private void applySpecificTransformation(Configuration cfg, XcodeProgram xcodeml) throws IllegalTransformationException {
    final Context context = xcodeml.context();
    AcceleratorConfiguration config = cfg.accelerator();
    if (_fctDef.hasEmptyBody()) {
        // Nothing to do in this function
        return;
    }
    /*
         * Create a nested loop with the new defined dimensions and wrap it around the
         * whole subroutine's body. This is for the moment a really naive transformation
         * idea but it is our start point. Use the first over clause to create it.
         */
    NestedDoStatement loops;
    if (forceAssumedShapedArrayPromotion) {
        if (_promotions.isEmpty()) {
            throw new IllegalTransformationException("Cannot assume shape of " + "array in elemental function/subroutine.", _claw.getPragma().lineNo());
        }
        PromotionInfo pi = _promotions.entrySet().iterator().next().getValue();
        loops = new NestedDoStatement(_claw.getDefaultLayoutReversed(cfg), pi, xcodeml);
    } else {
        loops = new NestedDoStatement(_claw.getDefaultLayoutReversed(cfg), xcodeml);
    }
    /*
         * Subroutine/function can have a contains section with inner subroutines or
         * functions. The newly created (nested) do statements should stop before this
         * contains section if it exists.
         */
    Xnode contains = _fctDef.body().matchSeq(Xcode.F_CONTAINS_STATEMENT);
    if (contains != null) {
        Xnode parallelRegionStart;
        if (_specialParallelRegionStart == null) {
            parallelRegionStart = Directive.findParallelRegionStart(context, _fctDef, null);
        } else {
            parallelRegionStart = _specialParallelRegionStart;
        }
        Xnode parallelRegionEnd = Directive.findParallelRegionEnd(context, _fctDef, contains);
        Body.shiftIn(parallelRegionStart, parallelRegionEnd, loops.getInnerStatement().body(), true);
        contains.insertBefore(loops.getOuterStatement());
    } else {
        // No contains section, all the body is copied to the do statements.
        Xnode parallelRegionStart;
        if (_specialParallelRegionStart == null) {
            parallelRegionStart = Directive.findParallelRegionStart(context, _fctDef, null);
        } else {
            parallelRegionStart = _specialParallelRegionStart;
        }
        Xnode parallelRegionEnd = Directive.findParallelRegionEnd(context, _fctDef, null);
        // Define a hook from where we can insert the new do statement
        Xnode hook = parallelRegionEnd != null ? parallelRegionEnd.nextSibling() : null;
        Body.shiftIn(parallelRegionStart, parallelRegionEnd, loops.getInnerStatement().body(), true);
        // Hook is null then we append the do statement to the current fct body
        if (hook == null) {
            _fctDef.body().append(loops.getOuterStatement());
        } else {
            // Insert new do statement before the hook element
            hook.insertBefore(loops.getOuterStatement());
        }
    }
    // TODO nodep passing!
    int collapse = Directive.generateLoopSeq(xcodeml, loops.getInnerStatement().body(), CompilerDirective.CLAW.getPrefix() + " nodep");
    // Prepare variables list for present/pcreate clauses and handle
    // promotion/privatize local strategy
    List<String> presentList = _fctDef.getPresentVariables(xcodeml);
    List<String> privateList = Collections.emptyList();
    List<String> createList = Collections.emptyList();
    if (config.getLocalStrategy() == AcceleratorLocalStrategy.PRIVATE) {
        privateList = applyPrivateStrategy(xcodeml);
    } else if (config.getLocalStrategy() == AcceleratorLocalStrategy.PROMOTE) {
        createList = applyPromoteStrategy(cfg, xcodeml);
    }
    // Generate the data region
    Xblock doStmtBlock = new Xblock(loops.getOuterStatement());
    Directive.generateDataRegionClause(xcodeml, presentList, createList, doStmtBlock);
    // Generate the parallel region
    Directive.generateParallelLoopClause(xcodeml, privateList, loops.getOuterStatement(), loops.getOuterStatement(), null, loops.size() + collapse);
    Directive.generateRoutineDirectives(xcodeml, _fctDef);
}
Also used : Context(claw.tatsu.common.Context) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) AcceleratorConfiguration(claw.tatsu.directive.configuration.AcceleratorConfiguration) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) Xblock(claw.tatsu.xcodeml.abstraction.Xblock) NestedDoStatement(claw.tatsu.xcodeml.abstraction.NestedDoStatement) PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo)

Example 9 with PromotionInfo

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

the class Sca method promoteFields.

/**
 * Promote all fields declared in the data clause with the additional
 * dimensions.
 *
 * @param xcodeml Current XcodeML program unit in which the element will be
 *                created.
 * @throws IllegalTransformationException if elements cannot be created or
 *                                        elements cannot be found.
 */
private void promoteFields(Configuration cfg, XcodeProgram xcodeml) throws IllegalTransformationException {
    if (_claw.hasClause(ClawClause.DATA_OVER)) {
        for (String fieldId : _claw.getDataOverClauseValues()) {
            PromotionInfo promotionInfo = new PromotionInfo(fieldId, _claw.getLayoutForData(cfg, fieldId));
            Field.promote(promotionInfo, _fctDef, xcodeml);
            _promotions.put(fieldId, promotionInfo);
        }
    } else {
        // Promote all arrays in a similar manner
        for (String fieldId : _arrayFieldsInOut) {
            PromotionInfo promotionInfo = new PromotionInfo(fieldId, _claw.getLayoutForData(cfg, fieldId));
            if (forceAssumedShapedArrayPromotion) {
                promotionInfo.forceAssumedShape();
            }
            Field.promote(promotionInfo, _fctDef, xcodeml);
            _promotions.put(fieldId, promotionInfo);
        }
    }
}
Also used : PromotionInfo(claw.tatsu.xcodeml.abstraction.PromotionInfo)

Example 10 with PromotionInfo

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

the class ScaForward method propagatePromotion.

/**
 * Propagate possible promotion in assignments statements in the parent
 * subroutine of the function call.
 *
 * @param xcodeml    Current XcodeML program unit.
 * @param translator Current translator to store information between
 *                   transformation.
 */
private void propagatePromotion(XcodeProgram xcodeml, ClawTranslator translator) throws IllegalTransformationException {
    // Get all the assignment statements in the function definition
    FfunctionDefinition parentFctDef = _fCall.findParentFunction();
    // Retrieve information of previous forward transformation in the same fct
    List<String> previouslyPromoted = Utility.convertToList(translator.hasElement(parentFctDef));
    List<Xnode> assignments = parentFctDef.matchAll(Xcode.F_ASSIGN_STATEMENT);
    // Find promotion info that can be used.
    // TODO define how default promotion is encoded in xmod file. For the
    // TODO moment using the first information found in fctType.
    PromotionInfo defaultInfo = Function.readPromotionInfo(_fctType, InsertionPosition.BEFORE);
    for (Xnode assignment : assignments) {
        Xnode lhs = assignment.child(0);
        Xnode rhs = assignment.child(1);
        List<Xnode> varsInRhs = rhs.matchAll(Xcode.VAR);
        for (Xnode var : varsInRhs) {
            // Check if the assignment statement uses a promoted variable
            if (_promotedVar.contains(var.value()) && var.matchAncestor(Xcode.FUNCTION_CALL) == null && Xnode.isOfCode(lhs, Xcode.F_ARRAY_REF)) {
                Xnode varInLhs = lhs.matchDescendant(Xcode.VAR);
                if (varInLhs == null) {
                    throw new IllegalTransformationException("Unable to propagate " + "promotion. Internal error.", _claw.getPragma().lineNo());
                }
                // Declare the induction variable if they are not present
                for (DimensionDefinition dim : defaultInfo.getDimensions()) {
                    if (parentFctDef.getDeclarationTable().get(dim.getIdentifier()) == null) {
                        xcodeml.createIdAndDecl(dim.getIdentifier(), FortranType.INTEGER, XstorageClass.F_LOCAL, parentFctDef, DeclarationPosition.LAST);
                    }
                }
                // Generate the do statements and move the assignment statement in
                NestedDoStatement doStmt = new NestedDoStatement(defaultInfo.getDimensions(), xcodeml);
                assignment.insertAfter(doStmt.getOuterStatement());
                doStmt.getInnerStatement().body().append(assignment);
                PromotionInfo promotionInfo;
                if (!previouslyPromoted.contains(varInLhs.value())) {
                    // Perform the promotion on the variable
                    promotionInfo = new PromotionInfo(varInLhs.value(), defaultInfo.getDimensions());
                    Field.promote(promotionInfo, parentFctDef, xcodeml);
                    _promotions.put(promotionInfo.getIdentifier(), promotionInfo);
                } else {
                    promotionInfo = _promotions.get(varInLhs.value());
                }
                _promotedVar.add(varInLhs.value());
                // Adapt the reference in the assignment statement
                for (String id : _promotedVar) {
                    _promotions.get(id).resetFlags();
                    Field.adaptArrayRef(_promotions.get(id), assignment, false, xcodeml);
                }
                // If the array is a target, check if we have to promote a pointer
                if (!previouslyPromoted.contains(varInLhs.value())) {
                    Field.adaptPointer(xcodeml, parentFctDef, _promotions, promotionInfo);
                    previouslyPromoted.add(varInLhs.value());
                }
                break;
            /*
                     * if one var in the rhs of the assignment statement was promoted it's enough
                     * and we can switch to the next assignment statement.
                     */
            }
        }
    }
    translator.storeElement(parentFctDef, previouslyPromoted);
}
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) NestedDoStatement(claw.tatsu.xcodeml.abstraction.NestedDoStatement) DimensionDefinition(claw.tatsu.xcodeml.abstraction.DimensionDefinition)

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