Search in sources :

Example 1 with Context

use of claw.tatsu.common.Context in project claw-compiler by C2SM-RCM.

the class Sca method transform.

@Override
public void transform(XcodeProgram xcodeml, Translator translator, Transformation other) throws Exception {
    ClawTranslator trans = (ClawTranslator) translator;
    final Configuration cfg = trans.cfg();
    final Context context = trans.context();
    // Handle PURE function / subroutine
    if (cfg.isForcePure() && _fctType.isPure()) {
        throw new IllegalTransformationException("PURE specifier cannot be removed", _fctDef.lineNo());
    } else {
        removeAttributesWithWaring(xcodeml, _fctType, Xattr.IS_PURE);
    }
    // Insert the declarations of variables to iterate over the new dimensions.
    insertVariableToIterateOverDimension(cfg, xcodeml);
    // Promote all array fields with new dimensions.
    promoteFields(cfg, xcodeml);
    boolean adaptedNakedArrayRef = context.getTarget() == Target.GPU;
    // Adapt array references.
    if (_claw.hasClause(ClawClause.DATA_OVER)) {
        for (String id : _claw.getDataOverClauseValues()) {
            Field.adaptArrayRef(_promotions.get(id), _fctDef.body(), adaptedNakedArrayRef, xcodeml);
        }
    } else {
        for (String id : _arrayFieldsInOut) {
            Field.adaptArrayRef(_promotions.get(id), _fctDef.body(), adaptedNakedArrayRef, xcodeml);
        }
    }
    removePragma();
}
Also used : Context(claw.tatsu.common.Context) Configuration(claw.wani.x2t.configuration.Configuration) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) ClawTranslator(claw.wani.x2t.translator.ClawTranslator)

Example 2 with Context

use of claw.tatsu.common.Context 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 Context

use of claw.tatsu.common.Context in project claw-compiler by C2SM-RCM.

the class ScaRoutine method transform.

@Override
public void transform(XcodeProgram xcodeml, Translator translator, Transformation other) {
    Context context = xcodeml.context();
    if (context.isTarget(Target.GPU)) {
        DirectiveGenerator dirGen = context.getGenerator();
        if (_fctType.isElemental()) {
            _fctType.removeAttribute(Xattr.IS_PURE);
            _fctType.removeAttribute(Xattr.IS_ELEMENTAL);
        }
        if (Directive.hasDirectives(context, _fctDef)) {
            xcodeml.addWarning(String.format("%s %s", SCA_DEBUG_PREFIX, "Function/subroutine has some directives! " + "Cannot insert new directives without breaking existing ones!"), _claw.getPragma());
        } else {
            Directive.addPragmasBefore(xcodeml, dirGen.getRoutineDirective(true), _fctDef.body().child(0));
        }
    }
    removePragma();
}
Also used : Context(claw.tatsu.common.Context) DirectiveGenerator(claw.tatsu.directive.generator.DirectiveGenerator)

Example 4 with Context

use of claw.tatsu.common.Context in project claw-compiler by C2SM-RCM.

the class PragmaTest method splitByContTest2.

@Test
public void splitByContTest2() {
    Context context = new TestContext();
    context.init(CompilerDirective.OPENACC, Target.GPU, null, 80);
    XcodeProgram xcodeml = XmlHelper.getDummyXcodeProgram(context);
    List<FfunctionDefinition> fctDefs = xcodeml.getAllFctDef();
    assertFalse(fctDefs.isEmpty());
    FfunctionDefinition fd = fctDefs.get(0);
    assertNotNull(fd.body());
    List<Xnode> previous = fd.matchAll(Xcode.F_PRAGMA_STATEMENT);
    Xnode p = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    fd.body().append(p);
    p.setValue("acc data copyin(a) acc      present(b)");
    try {
        Pragma.splitByCont(p, CompilerDirective.OPENACC.getPrefix(), xcodeml);
        List<Xnode> splittedPragma = fd.matchAll(Xcode.F_PRAGMA_STATEMENT);
        assertEquals(previous.size() + 2, splittedPragma.size());
    } catch (IllegalTransformationException e) {
        fail();
    }
}
Also used : Context(claw.tatsu.common.Context) TestContext(helper.Utils.TestContext) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) TestContext(helper.Utils.TestContext) XcodeProgram(claw.tatsu.xcodeml.xnode.common.XcodeProgram) Test(org.junit.Test)

Example 5 with Context

use of claw.tatsu.common.Context in project claw-compiler by C2SM-RCM.

the class PragmaTest method getPragmaPrefixTest.

@Test
public void getPragmaPrefixTest() {
    Context context = new TestContext();
    XcodeProgram xcodeml = XmlHelper.getDummyXcodeProgram(context);
    Xnode p1 = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    p1.setValue(CompilerDirective.OPENACC.getPrefix());
    assertEquals(CompilerDirective.OPENACC.getPrefix(), Pragma.getPrefix(p1));
    Xnode p2 = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    p2.setValue(CompilerDirective.OPENMP.getPrefix());
    assertEquals(CompilerDirective.OPENMP.getPrefix(), Pragma.getPrefix(p2));
    Xnode p3 = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    p3.setValue("");
    assertEquals("", Pragma.getPrefix(p3));
    Xnode p4 = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    assertEquals("", Pragma.getPrefix(p4));
    Xnode p5 = xcodeml.createNode(Xcode.F_DO_STATEMENT);
    p5.setValue("acc");
    assertEquals("", Pragma.getPrefix(p5));
    assertEquals("", Pragma.getPrefix(null));
    Xnode p6 = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    p6.setValue(CompilerDirective.OPENACC.getPrefix() + " loop private(a)");
    assertEquals(CompilerDirective.OPENACC.getPrefix(), Pragma.getPrefix(p6));
    p6.setValue(CompilerDirective.OPENMP.getPrefix() + " target");
    assertEquals(CompilerDirective.OPENMP.getPrefix(), Pragma.getPrefix(p6));
}
Also used : Context(claw.tatsu.common.Context) TestContext(helper.Utils.TestContext) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) TestContext(helper.Utils.TestContext) XcodeProgram(claw.tatsu.xcodeml.xnode.common.XcodeProgram) Test(org.junit.Test)

Aggregations

Context (claw.tatsu.common.Context)54 TestContext (helper.Utils.TestContext)44 Test (org.junit.Test)43 Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)27 XcodeProgram (claw.tatsu.xcodeml.xnode.common.XcodeProgram)22 FfunctionDefinition (claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)12 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)9 Xid (claw.tatsu.xcodeml.xnode.common.Xid)4 FfunctionType (claw.tatsu.xcodeml.xnode.fortran.FfunctionType)4 PromotionInfo (claw.tatsu.xcodeml.abstraction.PromotionInfo)3 Xblock (claw.tatsu.xcodeml.abstraction.Xblock)3 FbasicType (claw.tatsu.xcodeml.xnode.fortran.FbasicType)3 FmoduleDefinition (claw.tatsu.xcodeml.xnode.fortran.FmoduleDefinition)3 Configuration (claw.wani.x2t.configuration.Configuration)3 ClawTranslator (claw.wani.x2t.translator.ClawTranslator)3 ArrayList (java.util.ArrayList)3 DirectiveGenerator (claw.tatsu.directive.generator.DirectiveGenerator)2 DimensionDefinition (claw.tatsu.xcodeml.abstraction.DimensionDefinition)2 XcodeML (claw.tatsu.xcodeml.xnode.common.XcodeML)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2