use of claw.wani.x2t.translator.ClawTranslator 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]);
}
use of claw.wani.x2t.translator.ClawTranslator in project claw-compiler by C2SM-RCM.
the class LoopExtraction method transform.
/**
* Apply the transformation. A loop extraction is applied in the following
* steps: 1) Duplicate the function targeted by the transformation 2) Extract
* the loop body in the duplicated function and remove the loop. 3) Adapt
* function call and demote array references in the duplicated function body. 4)
* Optional: Add a LoopFusion transformation to the transformations' queue.
*
* @param xcodeml The XcodeML on which the transformations are applied.
* @param translator The translator used to applied the transformations.
* @param transformation Only for dependent transformation. The other
* transformation part of the transformation.
* @throws IllegalTransformationException if the transformation cannot be
* applied.
*/
@Override
public void transform(XcodeProgram xcodeml, Translator translator, Transformation transformation) throws Exception {
ClawTranslator ct = (ClawTranslator) translator;
final Context context = ct.context();
/*
* DUPLICATE THE FUNCTION
*/
// Duplicate function definition
FfunctionDefinition clonedFctDef = _fctDefToExtract.cloneNode();
String newFctTypeHash = xcodeml.getTypeTable().generateHash(FortranType.FUNCTION);
String newFctName = clonedFctDef.getName() + ClawConstant.EXTRACTION_SUFFIX + translator.getNextTransformationCounter();
clonedFctDef.name().setValue(newFctName);
clonedFctDef.name().setType(newFctTypeHash);
// Update the symbol table in the fct definition
Xid fctId = clonedFctDef.getSymbolTable().get(_fctDefToExtract.getName());
fctId.setType(newFctTypeHash);
fctId.setName(newFctName);
// Get the fctType in typeTable
FfunctionType fctType = xcodeml.getTypeTable().getFunctionType(_fctDefToExtract);
FfunctionType newFctType = fctType.cloneNode();
newFctType.setType(newFctTypeHash);
xcodeml.getTypeTable().add(newFctType);
// Get the id from the global symbols table
Xid globalFctId = xcodeml.getGlobalSymbolsTable().get(_fctDefToExtract.getName());
// If the fct is define in the global symbol table, duplicate it
if (globalFctId != null) {
Xid newFctId = globalFctId.cloneNode();
newFctId.setType(newFctTypeHash);
newFctId.setName(newFctName);
xcodeml.getGlobalSymbolsTable().add(newFctId);
}
// Insert the duplicated function declaration
_fctDefToExtract.insertAfter(clonedFctDef);
// Find the loop that will be extracted
Xnode loopInClonedFct = locateDoStatement(clonedFctDef);
Message.debug(context, "loop-extract transformation: " + _claw.getPragma().value());
Message.debug(context, " created subroutine: " + clonedFctDef.getName());
/*
* REMOVE BODY FROM THE LOOP AND DELETE THE LOOP
*/
// 1. append body into fct body after loop
Loop.extractBody(loopInClonedFct);
// 2. delete loop
loopInClonedFct.delete();
/*
* ADAPT FUNCTION CALL AND DEMOTE ARRAY REFERENCES IN THE BODY OF THE FUNCTION
*/
// Wrap function call with loop
Xnode extractedLoop = wrapCallWithLoop(xcodeml, _extractedLoop);
Message.debug(context, " call wrapped with loop: " + _fctCall.matchDirectDescendant(Xcode.NAME).value() + " --> " + clonedFctDef.getName());
// Change called fct name
_fctCall.matchDirectDescendant(Xcode.NAME).setValue(newFctName);
_fctCall.matchDirectDescendant(Xcode.NAME).setType(newFctTypeHash);
// Adapt function call parameters and function declaration
XdeclTable fctDeclarations = clonedFctDef.getDeclarationTable();
XsymbolTable fctSymbols = clonedFctDef.getSymbolTable();
Message.debug(context, " Start to apply mapping: " + _claw.getMappings().size());
for (ClawMapping mapping : _claw.getMappings()) {
Message.debug(context, "Apply mapping (" + mapping.getMappedDimensions() + ") ");
for (ClawMappingVar var : mapping.getMappedVariables()) {
Message.debug(context, " Var: " + var);
Optional<Xnode> argument = _fctCall.findArg(var.getArgMapping());
if (!argument.isPresent()) {
continue;
}
/*
* Case 1: Var --> ArrayRef Var --> ArrayRef transformation 1. Check that the
* variable used as array index exists in the current scope (XdeclTable). If so,
* get its type value. Create a Var element for the arrayIndex. Create the
* arrayIndex element with Var as child.
*
* 2. Get the reference type of the base variable. 2.1 Create the varRef element
* with the type of base variable 2.2 insert clone of base variable in varRef 3.
* Create arrayRef element with varRef + arrayIndex
*/
if (argument.get().is(Xcode.VAR)) {
FbasicType type = xcodeml.getTypeTable().getBasicType(argument.get());
// Demotion cannot be applied as type dimension is smaller
if (type.getDimensions() < mapping.getMappedDimensions()) {
throw new IllegalTransformationException("mapping dimensions too big. Mapping " + mapping.toString() + " is wrong ...", _claw.getPragma().lineNo());
}
Xnode newArg = xcodeml.createNode(Xcode.F_ARRAY_REF);
newArg.setType(type.getRef());
Xnode varRef = xcodeml.createNode(Xcode.VAR_REF);
varRef.setType(argument.get().getType());
varRef.append(argument.get(), true);
newArg.append(varRef);
// create arrayIndex
for (ClawMappingVar mappingVar : mapping.getMappingVariables()) {
Xnode arrayIndex = xcodeml.createNode(Xcode.ARRAY_INDEX);
// Find the mapping var in the local table (fct scope)
Xnode mappingVarDecl = _fctDef.getDeclarationTable().get(mappingVar.getArgMapping());
// Add to arrayIndex
Xnode newMappingVar = xcodeml.createVar(mappingVarDecl.getType(), mappingVarDecl.matchSeq(Xcode.NAME).value(), Xscope.LOCAL);
arrayIndex.append(newMappingVar);
newArg.append(arrayIndex);
}
argument.get().insertAfter(newArg);
argument.get().delete();
}
// Case 2: ArrayRef (n arrayIndex) --> ArrayRef (n+m arrayIndex)
// Change variable declaration in extracted fct
Xnode varDecl = fctDeclarations.get(var.getFctMapping());
Xid id = fctSymbols.get(var.getFctMapping());
FbasicType varDeclType = xcodeml.getTypeTable().getBasicType(varDecl);
// Case 1: variable is demoted to scalar then take the ref type
if (varDeclType.getDimensions() == mapping.getMappedDimensions()) {
Xnode newVarDecl = xcodeml.createNode(Xcode.VAR_DECL);
newVarDecl.append(xcodeml.createName(var.getFctMapping(), varDeclType.getRef()));
fctDeclarations.replace(newVarDecl, var.getFctMapping());
id.setType(varDeclType.getRef());
}
}
// Loop mapped variables
}
// Loop over mapping clauses
// Adapt array reference in function body
List<Xnode> arrayReferences = clonedFctDef.body().matchAll(Xcode.F_ARRAY_REF);
for (Xnode ref : arrayReferences) {
if (!Xnode.isOfCode(ref.matchSeq(Xcode.VAR_REF).child(0), Xcode.VAR)) {
continue;
}
String mappedVar = ref.matchSeq(Xcode.VAR_REF, Xcode.VAR).value();
if (_fctMappingMap.containsKey(mappedVar)) {
ClawMapping mapping = _fctMappingMap.get(mappedVar);
boolean changeRef = true;
int mappingIndex = 0;
for (Xnode e : ref.children()) {
if (e.is(Xcode.ARRAY_INDEX)) {
List<Xnode> children = e.children();
if (!children.isEmpty() && Xnode.isOfCode(children.get(0), Xcode.VAR)) {
String varName = e.matchSeq(Xcode.VAR).value();
if (varName.equals(mapping.getMappingVariables().get(mappingIndex).getFctMapping())) {
++mappingIndex;
} else {
changeRef = false;
}
}
}
}
if (changeRef) {
// TODO Var ref should be extracted only if the reference can be
// totally demoted
ref.insertBefore(ref.matchSeq(Xcode.VAR_REF, Xcode.VAR).cloneNode());
ref.delete();
}
}
}
// Generate directive pragmas if needed
Xnode grip = null;
if (_claw.hasClause(ClawClause.ACC)) {
/*
* TODO see TODO in ExpandNotation OpenACC and OpenMP loop construct are pretty
* different ... have to look how to do that properly. See issue #22
*/
grip = Directive.generateAcceleratorClause(xcodeml, extractedLoop, _claw.value(ClawClause.ACC));
}
if (_claw.hasClause(ClawClause.PARALLEL)) {
Directive.generateParallelRegion(xcodeml, (grip == null) ? extractedLoop : grip, extractedLoop);
}
// TODO must be triggered by a clause
// Directive.generateRoutineDirectives(_claw, xcodeml, clonedFctDef);
// Add any additional transformation defined in the directive clauses
ct.generateAdditionalTransformation(_claw, xcodeml, extractedLoop);
removePragma();
transformed();
}
use of claw.wani.x2t.translator.ClawTranslator in project claw-compiler by C2SM-RCM.
the class LoopHoist method transform.
/**
* @see Transformation#transform(XcodeProgram, Translator, Transformation)
*/
@Override
public void transform(XcodeProgram xcodeml, Translator translator, Transformation transformation) throws Exception {
ClawTranslator ct = (ClawTranslator) translator;
HoistedNestedDoStatement hoisted = Loop.hoist(_hoistedGroups, _clawStart.getPragma(), _clawEnd.getPragma(), xcodeml);
// Generate dynamic transformation (interchange)
ct.generateAdditionalTransformation(_clawStart, xcodeml, hoisted.getOuterStatement());
// Apply cleanup clause
if (_clawStart.hasClause(ClawClause.CLEANUP)) {
Pragma.remove(_clawStart.getCleanupClauseValue(), _clawStart.getPragma(), _clawEnd.getPragma());
}
// Apply reshape clause
if (_clawStart.hasClause(ClawClause.RESHAPE)) {
FfunctionDefinition fctDef = _clawStart.getPragma().findParentFunction();
if (fctDef == null) {
throw new IllegalTransformationException("Cannot apply reshape clause." + "Parent function definition not found.", _clawStart.getPragma().lineNo());
}
for (ReshapeInfo reshapeInfo : _clawStart.getReshapeClauseValues()) {
Field.reshape(fctDef, reshapeInfo, xcodeml);
}
}
removePragma();
}
use of claw.wani.x2t.translator.ClawTranslator in project claw-compiler by C2SM-RCM.
the class ModelData method analyze.
@Override
public boolean analyze(XcodeProgram xcodeml, Translator translator) {
/*
* Discover variable part of the model configuration and the subroutine holding
* them
*/
ClawTranslator trans = (ClawTranslator) translator;
if (!trans.cfg().getModelConfig().isLoaded()) {
xcodeml.addError("SCA model-data directive requires model configuration!", _clawStart.getPragma());
}
// Locate the subroutine/function in which the directive is defined
FfunctionDefinition sub = getDirective().getPragma().findParentFunction();
Map<String, String> modelVariables;
if (trans.hasElement(sub) != null) {
modelVariables = Utility.convertToMap(trans.hasElement(sub));
} else {
modelVariables = new HashMap<>();
}
for (String data : XnodeUtil.getAllVariables(getDirective().getPragma(), getEndDirective().getPragma())) {
modelVariables.put(data, _clawStart.value(ClawClause.LAYOUT));
}
trans.storeElement(sub, modelVariables);
return true;
}
use of claw.wani.x2t.translator.ClawTranslator in project claw-compiler by C2SM-RCM.
the class ScaCPUvectorizeGroup method transform.
@Override
public void transform(XcodeProgram xcodeml, Translator translator, Transformation other) throws Exception {
// SCA in ELEMENTAL function has no effect for CPU target
if (_fctType.isElemental()) {
xcodeml.addWarning("SCA in ELEMENTAL function has no impact for CPU target", _claw.getPragma());
removePragma();
return;
}
// Apply the common transformation
super.transform(xcodeml, translator, other);
ClawTranslator trans = (ClawTranslator) translator;
// Apply specific steps for CPU smart fusion
applySpecificTransformation(trans.cfg(), xcodeml);
// Finalize the common steps
super.finalizeTransformation(xcodeml);
}
Aggregations