use of claw.tatsu.xcodeml.xnode.common.Xnode 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);
}
use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.
the class ScaForward method analyze.
@Override
public boolean analyze(XcodeProgram xcodeml, Translator translator) {
Xnode next = _claw.getPragma().nextSibling();
if (next == null) {
xcodeml.addError("Directive is not followed by a valid statement.", _claw.getPragma());
return false;
}
if (Xnode.isOfCode(next, Xcode.EXPR_STATEMENT) || Xnode.isOfCode(next, Xcode.F_ASSIGN_STATEMENT)) {
_isNestedInAssignment = Xnode.isOfCode(next, Xcode.F_ASSIGN_STATEMENT);
Xnode fctCallNode = next.matchSeq(Xcode.FUNCTION_CALL);
if (fctCallNode != null) {
_fCall = new FunctionCall(fctCallNode);
return analyzeForward(xcodeml);
}
} else if (Xnode.isOfCode(next, Xcode.F_DO_STATEMENT)) {
_doStatements = new NestedDoStatement(next);
return analyzeForwardWithDo(xcodeml);
}
xcodeml.addError("Directive is not followed by a valid statement.", _claw.getPragma());
return false;
}
use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.
the class ScaForward method analyzeForward.
/**
* Analyze the directive when it is used just before a function call.
*
* @param xcodeml Current XcodeML file unit.
* @return True if the analysis succeed. False otherwise.
*/
private boolean analyzeForward(XcodeProgram xcodeml) {
final Context context = xcodeml.context();
if (_fCall == null) {
xcodeml.addError("Directive is not followed by a fct call.", _claw.getPragma());
return false;
}
detectParameterMapping(context, _fCall);
_calledFctName = _fCall.getFctName();
FfunctionDefinition fctDef = xcodeml.getGlobalDeclarationsTable().getFunctionDefinition(_calledFctName);
FfunctionDefinition parentFctDef = _claw.getPragma().findParentFunction();
if (parentFctDef == null) {
xcodeml.addError("SCA directive is not nested in a " + "function/subroutine.", _claw.getPragma());
return false;
}
FmoduleDefinition parentModule = parentFctDef.findParentModule();
if (_fCall.isTbpCall()) {
/*
* If type is a FbasicType element for a type-bound procedure, we have to
* matchSeq the correct function in the typeTable. TODO if there is a rename.
* TODO generic call
*/
Xid id = parentModule.getSymbolTable().get(_calledFctName);
if (id == null) {
List<Xnode> uses = parentFctDef.getDeclarationTable().uses();
uses.addAll(parentModule.getDeclarationTable().uses());
if (!findInModule(context, uses)) {
xcodeml.addError("Function definition not found in module ", _claw.getPragma());
return false;
}
} else {
_fctType = xcodeml.getTypeTable().getFunctionType(id);
}
} else {
if (xcodeml.getTypeTable().isFunctionType(_fCall)) {
_fctType = xcodeml.getTypeTable().getFunctionType(_fCall);
} else {
xcodeml.addError("Unsupported type of XcodeML/F element for the function " + _calledFctName, _claw.getPragma());
return false;
}
}
/*
* Workaround for a bug in OMNI Compiler. Look at test case claw/abstraction10.
* 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.
*/
if (_fctType.getParameters().isEmpty()) {
/*
* If not, try to matchSeq the correct FfunctionType in the module definitions
*/
Xid id = (parentModule == null) ? null : parentModule.getSymbolTable().get(_calledFctName);
if (id == null) {
// Function is not located in the current module.
List<Xnode> uses = parentFctDef.getDeclarationTable().uses();
if (parentModule != null) {
uses.addAll(parentModule.getDeclarationTable().uses());
}
if (!findInModule(context, uses)) {
xcodeml.addError(String.format("Function definition %s not found in module.", _calledFctName), _claw.getPragma());
return false;
}
} else {
_fctType = xcodeml.getTypeTable().getFunctionType(id);
if (_fctType == null) {
xcodeml.addError("Called function cannot be found in the same module ", _claw.getPragma());
return false;
}
}
}
// end of workaround
_callingFctName = parentFctDef.getName();
if (_fctType != null && fctDef != null) {
_localFct = true;
} else {
// Has been found already
if (_fctType != null && _calledFctName == null) {
return true;
}
// Get all the use statements in the fct and module definitions
List<Xnode> uses = parentFctDef.getDeclarationTable().uses();
if (parentModule != null) {
uses.addAll(parentModule.getDeclarationTable().uses());
}
// Try to locate the fct in the modules defined in use statements
if (findInModule(context, uses)) {
return true;
}
xcodeml.addError("Function signature not found in the current module.", _claw.getPragma());
return false;
}
return true;
}
use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.
the class ClawPragmaTest method analyzeErrors.
private void analyzeErrors(String pragma, int nbExpectedToken) {
Xnode p = XmlHelper.createXpragma();
p.setValue(pragma);
p.setLine(1);
cfg.init(CompilerDirective.OPENACC, Target.GPU);
context.init(CompilerDirective.OPENACC, Target.GPU, null, 80);
try {
ClawPragma.analyze(p);
} catch (IllegalDirectiveException e) {
if (nbExpectedToken != 0) {
assertEquals(nbExpectedToken, e.getExpectedTokens().size());
}
assertNotNull(e.getMessage());
}
}
use of claw.tatsu.xcodeml.xnode.common.Xnode in project claw-compiler by C2SM-RCM.
the class FieldTest method performAndAssertPromotion.
/**
* Perform the promotion transformation and assert its result.
*
* @param id Identifier of the field to be promoted.
* @param dims Dimension definitions to use.
* @param fctDef Function definition in which the promotion is performed.
* @param xcodeml Current XcodeML translation unit.
* @param base Number of dimension before the promotion.
* @param target Number of dimension after the promotion.
* @param dimensions Expected dimensions after promotion.
*/
private void performAndAssertPromotion(String id, List<DimensionDefinition> dims, FfunctionDefinition fctDef, XcodeProgram xcodeml, int base, int target, int[] dimensions) {
try {
int diff = target - base;
PromotionInfo promotionInfo = new PromotionInfo(id);
promotionInfo.setDimensions(dims);
Xnode decl = fctDef.getDeclarationTable().get(id);
assertNotNull(decl);
FbasicType bt;
if (base != 0) {
bt = xcodeml.getTypeTable().getBasicType(decl);
assertNotNull(bt);
assertTrue(bt.isArray());
assertEquals(base, bt.getDimensions());
} else {
assertEquals(FortranType.REAL, FortranType.fromString(decl.getType()));
}
// Perform the promotion
Field.promote(promotionInfo, fctDef, xcodeml);
decl = fctDef.getDeclarationTable().get(id);
assertNotNull(decl);
bt = xcodeml.getTypeTable().getBasicType(decl);
assertNotNull(bt);
assertTrue(bt.isArray());
assertEquals(target, bt.getDimensions());
assertEquals(target, dimensions.length / 2);
assertEquals(target, promotionInfo.getTargetDimension());
assertEquals(base, promotionInfo.getBaseDimension());
assertEquals(diff, promotionInfo.diffDimension());
assertEquals(bt.getType(), promotionInfo.getTargetType().getType());
if (base > 0) {
assertEquals(PromotionInfo.PromotionType.ARRAY_TO_ARRAY, promotionInfo.getPromotionType());
} else {
assertEquals(PromotionInfo.PromotionType.SCALAR_TO_ARRAY, promotionInfo.getPromotionType());
}
for (int i = 0; i < dimensions.length / 2; ++i) {
assertDimension(bt.getDimensions(i), dimensions[i * 2], dimensions[(i * 2) + 1]);
}
} catch (IllegalTransformationException itex) {
System.err.println(itex.getMessage());
fail();
}
}
Aggregations