Search in sources :

Example 11 with XcodeProgram

use of claw.tatsu.xcodeml.xnode.common.XcodeProgram in project claw-compiler by C2SM-RCM.

the class DimensionTest method dimensionDefinitionTest.

@Test
public void dimensionDefinitionTest() {
    Context context = new TestContext();
    XcodeProgram xcodeml = XmlHelper.getDummyXcodeProgram(context);
    DimensionDefinition dimDef = new DimensionDefinition("nproma", "1", "nend");
    DimensionDefinition dimDef2 = new DimensionDefinition("nproma", "nstart", "nend");
    assertEquals("nproma(nstart:nend)", dimDef2.toString());
    assertNotNull(dimDef.getLowerBound());
    assertNotNull(dimDef.getUpperBound());
    assertNotNull(dimDef.getIterationLowerBound());
    assertNotNull(dimDef.getIterationUpperBound());
    assertNotNull(dimDef.getIterationStep());
    assertFalse(dimDef.getLowerBound().isVar());
    assertEquals(1, dimDef.getLowerBound().getIntValue());
    assertFalse(dimDef.getIterationLowerBound().isVar());
    assertEquals(1, dimDef.getIterationLowerBound().getIntValue());
    assertTrue(dimDef.getUpperBound().isVar());
    assertEquals("nend", dimDef.getUpperBound().getValue());
    assertTrue(dimDef.getIterationUpperBound().isVar());
    assertEquals("nend", dimDef.getIterationUpperBound().getValue());
    assertFalse(dimDef.getIterationStep().isVar());
    assertEquals(1, dimDef.getIterationStep().getIntValue());
    assertEquals("nproma", dimDef.getIdentifier());
    assertEquals(InsertionPosition.BEFORE, dimDef.getInsertionPosition());
    dimDef.setInsertionPosition(InsertionPosition.AFTER);
    assertEquals(InsertionPosition.AFTER, dimDef.getInsertionPosition());
    assertEquals("nproma(1:nend)", dimDef.toString());
    Xnode indexRange = dimDef.generateIndexRange(xcodeml, false, false);
    assertNotNull(indexRange);
    assertEquals(Xcode.INDEX_RANGE, indexRange.opcode());
    assertEquals(2, indexRange.children().size());
    assertEquals(Xcode.LOWER_BOUND, indexRange.firstChild().opcode());
    assertEquals(Xcode.UPPER_BOUND, indexRange.lastChild().opcode());
    Xnode indexRangeStep = dimDef.generateIndexRange(xcodeml, true, false);
    assertNotNull(indexRangeStep);
    assertEquals(Xcode.INDEX_RANGE, indexRangeStep.opcode());
    assertEquals(3, indexRangeStep.children().size());
    assertEquals(Xcode.LOWER_BOUND, indexRangeStep.firstChild().opcode());
    assertEquals(Xcode.UPPER_BOUND, indexRangeStep.child(1).opcode());
    assertEquals(Xcode.STEP, indexRangeStep.lastChild().opcode());
    Xnode arrayIndex = dimDef.generateArrayIndex(xcodeml);
    assertNotNull(arrayIndex);
    assertEquals(Xcode.ARRAY_INDEX, arrayIndex.opcode());
    assertEquals(Xcode.VAR, arrayIndex.firstChild().opcode());
    assertEquals(dimDef.getIdentifier(), arrayIndex.firstChild().value());
    Xnode allocateNode = dimDef.generateAllocateNode(xcodeml);
    assertEquals(Xcode.ARRAY_INDEX, allocateNode.opcode());
    assertEquals(Xcode.VAR, allocateNode.firstChild().opcode());
    assertEquals(dimDef.getUpperBound().getValue(), allocateNode.firstChild().value());
}
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)

Example 12 with XcodeProgram

use of claw.tatsu.xcodeml.xnode.common.XcodeProgram in project claw-compiler by C2SM-RCM.

the class TransformationAndGroupTest method basicTransformationTest.

@Test
public void basicTransformationTest() {
    Transformation t1 = new T1();
    assertTrue(t1.abortOnFailedAnalysis());
    assertEquals(0, t1.getStartLine());
    t1.setStartLine(10);
    assertEquals(10, t1.getStartLine());
    assertNull(t1.getDirective());
    assertFalse(t1.isTransformed());
    Context context = new TestContext();
    XcodeProgram xcodeml = XmlHelper.getDummyXcodeProgram(context);
    Xnode pragma = xcodeml.createNode(Xcode.F_PRAGMA_STATEMENT);
    pragma.setLine(10);
    t1 = new T1(new AnalyzedPragma(pragma));
    assertNotNull(t1.getDirective());
    assertNotNull(t1.getDirective().getPragma());
    assertEquals(10, t1.getStartLine());
}
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) AnalyzedPragma(claw.shenron.translator.AnalyzedPragma) Test(org.junit.Test)

Example 13 with XcodeProgram

use of claw.tatsu.xcodeml.xnode.common.XcodeProgram in project claw-compiler by C2SM-RCM.

the class TransformationAndGroupTest method transformationGroupBaseTest.

@Test
public void transformationGroupBaseTest() {
    TransformationGroup ig = new IndependentTransformationGroup("loop-hoist");
    TransformationGroup dg = new DependentTransformationGroup("loop-fusion");
    groupTest(dg, "loop-fusion");
    groupTest(ig, "loop-hoist");
    Transformation t1 = new T1();
    ig.add(t1);
    assertEquals(1, ig.count());
    assertFalse(t1.isTransformed());
    Context context = new TestContext();
    XcodeProgram xcodeml = XmlHelper.getDummyXcodeProgram(context);
    assertNotNull(xcodeml);
    try {
        ig.applyTransformations(xcodeml, null);
    } catch (Exception e) {
        fail();
    }
    assertTrue(t1.isTransformed());
}
Also used : Context(claw.tatsu.common.Context) TestContext(helper.Utils.TestContext) TestContext(helper.Utils.TestContext) XcodeProgram(claw.tatsu.xcodeml.xnode.common.XcodeProgram) Test(org.junit.Test)

Example 14 with XcodeProgram

use of claw.tatsu.xcodeml.xnode.common.XcodeProgram in project claw-compiler by C2SM-RCM.

the class ExpandNotation method generateDoStmtNotation.

/**
 * Generate the corresponding do statements for the array notations. A do
 * statement is generated per dimension of the arrays. Iteration index range are
 * computed with array dimensions.
 *
 * @param xcodeml    The XcodeML on which the transformations are applied.
 * @param translator The translator used to applied the transformations.
 * @param fctDef     The function definition in which the array notation is
 *                   nested.
 * @param ranges     The list of iteration ranges to be applied to the created
 *                   do statements.
 * @param statements The list of assign statements (array notation) that will be
 *                   included in the nested do statements.
 * @param doStmtGrip Grip for the code insertion. Do statements will be inserted
 *                   after the grip element.
 * @return Block surrounding the do statements generated.
 */
private Xblock generateDoStmtNotation(XcodeProgram xcodeml, ClawTranslator translator, FfunctionDefinition fctDef, List<Xnode> ranges, List<Xnode> statements, Xnode doStmtGrip) throws IllegalTransformationException {
    String[] inductionVars = new String[ranges.size()];
    Xnode[] doStmts = new Xnode[ranges.size()];
    Xnode var = statements.get(0).matchSeq(Xcode.F_ARRAY_REF, Xcode.VAR_REF, Xcode.VAR);
    if (var == null) {
        var = statements.get(0).matchSeq(Xcode.F_ARRAY_REF, Xcode.VAR_REF, Xcode.F_MEMBER_REF);
    }
    // 1. Create do statements with induction variables
    for (int i = 0; i < ranges.size(); ++i) {
        // 1.1 Create induction variables
        if (_clawStart.hasClause(ClawClause.INDUCTION)) {
            // Use user names
            inductionVars[i] = _clawStart.values(ClawClause.INDUCTION).get(i);
        } else {
            // generate new names
            inductionVars[i] = "claw_induction_" + translator.getNextTransformationCounter();
        }
        // 2.2 inject a new entry in the symbol table
        if (!fctDef.getSymbolTable().contains(inductionVars[i])) {
            Xid inductionVarId = xcodeml.createId(FortranType.INTEGER, XstorageClass.F_LOCAL, inductionVars[i]);
            fctDef.getSymbolTable().add(inductionVarId, false);
        }
        // 2.3 inject a new entry in the declaration table
        if (!fctDef.getDeclarationTable().contains(inductionVars[i])) {
            Xnode inductionVarDecl = xcodeml.createVarDecl(FortranType.INTEGER, inductionVars[i]);
            fctDef.getDeclarationTable().add(inductionVarDecl);
        }
        // 2.4 create do statements
        Xnode inductionVar = xcodeml.createVar(FortranType.INTEGER, inductionVars[i], Xscope.LOCAL);
        Xnode range;
        if (ranges.get(i).getBooleanAttribute(Xattr.IS_ASSUMED_SHAPE)) {
            // Allocatable array
            // dimension argument of size starts at one
            range = xcodeml.createRangeForAssumedShapeArray(var, 1, i + 1);
        } else {
            range = ranges.get(i).cloneNode();
        }
        doStmts[i] = xcodeml.createDoStmt(inductionVar, range);
        statements.get(0).copyEnhancedInfo(doStmts[i]);
        if (i == 0) {
            // most outer loop goes after the pragma
            doStmtGrip.insertAfter(doStmts[i]);
        } else {
            // others loop go in the previous one
            doStmts[i - 1].body().append(doStmts[i]);
        }
    }
    for (Xnode stmt : statements) {
        // 3. Adapt array reference with induction variables
        List<Xnode> allArrayRef = stmt.matchAll(Xcode.F_ARRAY_REF);
        for (Xnode arrayRef : allArrayRef) {
            for (int i = 0; i < arrayRef.children().size() - 1; ++i) {
                Xnode el = arrayRef.child(i + 1);
                if (Xnode.isOfCode(el, Xcode.INDEX_RANGE) && i < doStmts.length) {
                    String induction = doStmts[i].matchSeq(Xcode.VAR).value();
                    Xnode inductionVar = xcodeml.createVar(FortranType.INTEGER, induction, Xscope.LOCAL);
                    Xnode arrayIdx = xcodeml.createNode(Xcode.ARRAY_INDEX);
                    arrayIdx.append(inductionVar);
                    el.insertAfter(arrayIdx);
                    el.delete();
                }
            }
        }
        stmt.matchAll(Xcode.FUNCTION_CALL).stream().map(FunctionCall::new).filter(x -> x.isIntrinsicCall(Xintrinsic.SUM)).forEach(FunctionCall::adaptIntrinsicSumCall);
        stmt.matchAll(Xcode.FUNCTION_CALL).stream().map(FunctionCall::new).filter(x -> x.isIntrinsicCall(Xintrinsic.SPREAD)).forEach(FunctionCall::adaptIntrinsicSpreadCall);
        // 4. Move assignment statement inside the most inner loop
        doStmts[ranges.size() - 1].body().append(stmt, true);
        stmt.delete();
    }
    // Add any additional transformation defined in the directive clauses
    translator.generateAdditionalTransformation(_clawStart, xcodeml, doStmts[0]);
    return new Xblock(doStmts[0]);
}
Also used : Serialization(claw.wani.serialization.Serialization) FunctionCall(claw.tatsu.xcodeml.abstraction.FunctionCall) ClawTranslator(claw.wani.x2t.translator.ClawTranslator) Transformation(claw.shenron.transformation.Transformation) XcodeProgram(claw.tatsu.xcodeml.xnode.common.XcodeProgram) Directive(claw.tatsu.directive.common.Directive) Xblock(claw.tatsu.xcodeml.abstraction.Xblock) ClawPragma(claw.wani.language.ClawPragma) Range(claw.tatsu.primitive.Range) ClawBlockTransformation(claw.wani.transformation.ClawBlockTransformation) XnodeUtil(claw.tatsu.xcodeml.xnode.XnodeUtil) ArrayList(java.util.ArrayList) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) HashSet(java.util.HashSet) ClawClause(claw.wani.language.ClawClause) Target(claw.tatsu.common.Target) Set(java.util.Set) Context(claw.tatsu.common.Context) Translator(claw.shenron.translator.Translator) FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition) Collectors(java.util.stream.Collectors) SerializationStep(claw.wani.serialization.SerializationStep) FortranType(claw.tatsu.xcodeml.xnode.fortran.FortranType) List(java.util.List) Xid(claw.tatsu.xcodeml.xnode.common.Xid) Xcode(claw.tatsu.xcodeml.xnode.common.Xcode) Xintrinsic(claw.tatsu.xcodeml.xnode.fortran.Xintrinsic) DataMovement(claw.tatsu.directive.common.DataMovement) Xattr(claw.tatsu.xcodeml.xnode.common.Xattr) XstorageClass(claw.tatsu.xcodeml.xnode.common.XstorageClass) Configuration(claw.wani.x2t.configuration.Configuration) Xscope(claw.tatsu.xcodeml.xnode.common.Xscope) IllegalTransformationException(claw.tatsu.xcodeml.exception.IllegalTransformationException) Collections(java.util.Collections) Xnode(claw.tatsu.xcodeml.xnode.common.Xnode) Xid(claw.tatsu.xcodeml.xnode.common.Xid) Xblock(claw.tatsu.xcodeml.abstraction.Xblock) FunctionCall(claw.tatsu.xcodeml.abstraction.FunctionCall)

Example 15 with XcodeProgram

use of claw.tatsu.xcodeml.xnode.common.XcodeProgram in project claw-compiler by C2SM-RCM.

the class FieldTest method promoteTest.

@Test
public void promoteTest() {
    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);
    Context context = new TestContext();
    XcodeProgram xcodeml = XcodeProgram.createFromFile(TestConstant.TEST_PROMOTION, context);
    assertNotNull(xcodeml);
    List<FfunctionDefinition> fctDefs = xcodeml.getAllFctDef();
    assertEquals(1, fctDefs.size());
    FfunctionDefinition fctDef = fctDefs.get(0);
    assertEquals("sub1", fctDef.getName());
    // Scalar to array promotion with 1 additional dimension
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    performAndAssertPromotion("s1", dimensions1, fctDef, xcodeml, 0, 1, new int[] { 1, 30 });
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    performAndAssertPromotion("s2", dimensions1, fctDef, xcodeml, 0, 1, new int[] { 1, 30 });
    dim1.setInsertionPosition(InsertionPosition.AFTER);
    performAndAssertPromotion("s3", dimensions1, fctDef, xcodeml, 0, 1, new int[] { 1, 30 });
    // Scalar to array promotion with 2 additional dimension
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    dim2.setInsertionPosition(InsertionPosition.BEFORE);
    performAndAssertPromotion("s4", dimensions2, fctDef, xcodeml, 0, 2, new int[] { 1, 30, 1, 40 });
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    dim2.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    performAndAssertPromotion("s5", dimensions2, fctDef, xcodeml, 0, 2, new int[] { 1, 30, 1, 40 });
    dim1.setInsertionPosition(InsertionPosition.AFTER);
    dim2.setInsertionPosition(InsertionPosition.AFTER);
    performAndAssertPromotion("s6", dimensions2, fctDef, xcodeml, 0, 2, new int[] { 1, 30, 1, 40 });
    // Promotion with 1 additional dimension
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    performAndAssertPromotion("a", dimensions1, fctDef, xcodeml, 2, 3, new int[] { 1, 30, 1, 10, 1, 20 });
    dim1.setInsertionPosition(InsertionPosition.AFTER);
    performAndAssertPromotion("b", dimensions1, fctDef, xcodeml, 2, 3, new int[] { 1, 10, 1, 20, 1, 30 });
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    performAndAssertPromotion("c", dimensions1, fctDef, xcodeml, 2, 3, new int[] { 1, 10, 1, 30, 1, 20 });
    // Promotion with 2 additional dimensions at the same position
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    dim2.setInsertionPosition(InsertionPosition.BEFORE);
    performAndAssertPromotion("d", dimensions2, fctDef, xcodeml, 2, 4, new int[] { 1, 30, 1, 40, 1, 10, 1, 20 });
    dim1.setInsertionPosition(InsertionPosition.AFTER);
    dim2.setInsertionPosition(InsertionPosition.AFTER);
    performAndAssertPromotion("e", dimensions2, fctDef, xcodeml, 2, 4, new int[] { 1, 10, 1, 20, 1, 30, 1, 40 });
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    dim2.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    performAndAssertPromotion("f", dimensions2, fctDef, xcodeml, 2, 4, new int[] { 1, 10, 1, 30, 1, 40, 1, 20 });
    // Promotion with 2 additional dimensions at different position
    dim1.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    dim2.setInsertionPosition(InsertionPosition.AFTER);
    performAndAssertPromotion("g", dimensions2, fctDef, xcodeml, 2, 4, new int[] { 1, 10, 1, 30, 1, 20, 1, 40 });
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    dim2.setInsertionPosition(InsertionPosition.IN_MIDDLE);
    performAndAssertPromotion("h", dimensions2, fctDef, xcodeml, 2, 4, new int[] { 1, 30, 1, 10, 1, 40, 1, 20 });
    dim1.setInsertionPosition(InsertionPosition.BEFORE);
    dim2.setInsertionPosition(InsertionPosition.AFTER);
    performAndAssertPromotion("i", dimensions2, fctDef, xcodeml, 2, 4, new int[] { 1, 30, 1, 10, 1, 20, 1, 40 });
}
Also used : Context(claw.tatsu.common.Context) TestContext(helper.Utils.TestContext) FfunctionDefinition(claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition) TestContext(helper.Utils.TestContext) DimensionDefinition(claw.tatsu.xcodeml.abstraction.DimensionDefinition) XcodeProgram(claw.tatsu.xcodeml.xnode.common.XcodeProgram) Test(org.junit.Test)

Aggregations

XcodeProgram (claw.tatsu.xcodeml.xnode.common.XcodeProgram)25 Context (claw.tatsu.common.Context)24 TestContext (helper.Utils.TestContext)22 Test (org.junit.Test)22 Xnode (claw.tatsu.xcodeml.xnode.common.Xnode)20 FfunctionDefinition (claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition)7 IllegalTransformationException (claw.tatsu.xcodeml.exception.IllegalTransformationException)4 ArrayList (java.util.ArrayList)3 Transformation (claw.shenron.transformation.Transformation)2 Translator (claw.shenron.translator.Translator)2 Target (claw.tatsu.common.Target)2 DataMovement (claw.tatsu.directive.common.DataMovement)2 Directive (claw.tatsu.directive.common.Directive)2 Range (claw.tatsu.primitive.Range)2 FunctionCall (claw.tatsu.xcodeml.abstraction.FunctionCall)2 Xblock (claw.tatsu.xcodeml.abstraction.Xblock)2 XnodeUtil (claw.tatsu.xcodeml.xnode.XnodeUtil)2 Xattr (claw.tatsu.xcodeml.xnode.common.Xattr)2 Xcode (claw.tatsu.xcodeml.xnode.common.Xcode)2 Xid (claw.tatsu.xcodeml.xnode.common.Xid)2