use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class XnodeUtil method duplicateBound.
/**
* Duplicate a lower or an upper bound between two different XcodeML units.
*
* @param baseBound Base bound to be duplicated.
* @param xcodemlDst Destination XcodeML unit. Duplicate will be created here.
* @param xcodemlSrc Source XcodeML unit. Contains base bound.
* @return The newly duplicated bound element.
* @throws IllegalTransformationException If bound cannot be duplicated.
*/
public static Xnode duplicateBound(Xnode baseBound, XcodeML xcodemlDst, XcodeML xcodemlSrc) throws IllegalTransformationException {
if (baseBound.opcode() != Xcode.LOWERBOUND && baseBound.opcode() != Xcode.UPPERBOUND) {
throw new IllegalTransformationException("Cannot duplicate bound");
}
if (xcodemlSrc == xcodemlDst) {
return baseBound.cloneNode();
}
Xnode boundChild = baseBound.child(0);
if (boundChild == null) {
throw new IllegalTransformationException("Cannot duplicate bound as it " + "has no children element");
}
Xnode bound = new Xnode(baseBound.opcode(), xcodemlDst);
if (boundChild.opcode() == Xcode.FINTCONSTANT || boundChild.opcode() == Xcode.VAR) {
bound.append(xcodemlDst.importConstOrVar(boundChild, xcodemlSrc), false);
} else if (boundChild.opcode() == Xcode.PLUSEXPR) {
Xnode lhs = boundChild.child(0);
Xnode rhs = boundChild.child(1);
Xnode plusExpr = new Xnode(Xcode.PLUSEXPR, xcodemlDst);
bound.append(plusExpr, false);
plusExpr.append(xcodemlDst.importConstOrVar(lhs, xcodemlSrc), false);
plusExpr.append(xcodemlDst.importConstOrVar(rhs, xcodemlSrc), false);
} else {
throw new IllegalTransformationException(String.format("Lower/upper bound type currently not supported (%s)", boundChild.opcode().toString()));
}
return bound;
}
use of cx2x.xcodeml.exception.IllegalTransformationException 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 transformer The transformer 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, Transformer transformer, Transformation transformation) throws Exception {
/*
* DUPLICATE THE FUNCTION
*/
// Duplicate function definition
XfunctionDefinition clonedFctDef = _fctDefToExtract.cloneNode();
String newFctTypeHash = xcodeml.getTypeTable().generateFctTypeHash();
String newFctName = clonedFctDef.getName().value() + ClawConstant.EXTRACTION_SUFFIX + transformer.getNextTransformationCounter();
clonedFctDef.getName().setValue(newFctName);
clonedFctDef.getName().setAttribute(Xattr.TYPE, newFctTypeHash);
// Update the symbol table in the fct definition
Xid fctId = clonedFctDef.getSymbolTable().get(_fctDefToExtract.getName().value());
fctId.setType(newFctTypeHash);
fctId.setName(newFctName);
// Get the fctType in typeTable
XfunctionType fctType = (XfunctionType) xcodeml.getTypeTable().get(_fctDefToExtract.getName().getAttribute(Xattr.TYPE));
XfunctionType 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().value());
// 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);
if (XmOption.isDebugOutput()) {
System.out.println("loop-extract transformation: " + _claw.getPragma().value());
System.out.println(" created subroutine: " + clonedFctDef.getName().value());
}
/*
* REMOVE BODY FROM THE LOOP AND DELETE THE LOOP
*/
// 1. append body into fct body after loop
XnodeUtil.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);
if (XmOption.isDebugOutput()) {
System.out.println(" call wrapped with loop: " + _fctCall.matchDirectDescendant(Xcode.NAME).value() + " --> " + clonedFctDef.getName().value());
}
// Change called fct name
_fctCall.matchDirectDescendant(Xcode.NAME).setValue(newFctName);
_fctCall.matchDirectDescendant(Xcode.NAME).setAttribute(Xattr.TYPE, newFctTypeHash);
// Adapt function call parameters and function declaration
XdeclTable fctDeclarations = clonedFctDef.getDeclarationTable();
XsymbolTable fctSymbols = clonedFctDef.getSymbolTable();
Utility.debug(" Start to apply mapping: " + _claw.getMappings().size());
for (ClawMapping mapping : _claw.getMappings()) {
Utility.debug("Apply mapping (" + mapping.getMappedDimensions() + ") ");
for (ClawMappingVar var : mapping.getMappedVariables()) {
Utility.debug(" Var: " + var);
Xnode argument = XnodeUtil.findArg(var.getArgMapping(), _fctCall);
if (argument == null) {
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.opcode() == Xcode.VAR) {
XbasicType type = (XbasicType) xcodeml.getTypeTable().get(argument.getAttribute(Xattr.TYPE));
// 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 = new Xnode(Xcode.FARRAYREF, xcodeml);
newArg.setAttribute(Xattr.TYPE, type.getRef());
Xnode varRef = new Xnode(Xcode.VARREF, xcodeml);
varRef.setAttribute(Xattr.TYPE, argument.getAttribute(Xattr.TYPE));
varRef.append(argument, true);
newArg.append(varRef, false);
// create arrayIndex
for (ClawMappingVar mappingVar : mapping.getMappingVariables()) {
Xnode arrayIndex = new Xnode(Xcode.ARRAYINDEX, xcodeml);
// Find the mapping var in the local table (fct scope)
Xdecl mappingVarDecl = _fctDef.getDeclarationTable().get(mappingVar.getArgMapping());
// Add to arrayIndex
Xnode newMappingVar = new Xnode(Xcode.VAR, xcodeml);
newMappingVar.setAttribute(Xattr.SCLASS, Xscope.LOCAL.toString());
newMappingVar.setAttribute(Xattr.TYPE, mappingVarDecl.matchSeq(Xcode.NAME).getAttribute(Xattr.TYPE));
newMappingVar.setValue(mappingVarDecl.matchSeq(Xcode.NAME).value());
arrayIndex.append(newMappingVar, false);
newArg.append(arrayIndex, false);
}
argument.insertAfter(newArg);
argument.delete();
}
// Case 2: ArrayRef (n arrayIndex) --> ArrayRef (n+m arrayIndex)
/*else if(argument.opcode() == Xcode.FARRAYREF) {
// TODO
}*/
// Change variable declaration in extracted fct
Xdecl varDecl = fctDeclarations.get(var.getFctMapping());
Xid id = fctSymbols.get(var.getFctMapping());
XbasicType varDeclType = (XbasicType) xcodeml.getTypeTable().get(varDecl.matchSeq(Xcode.NAME).getAttribute(Xattr.TYPE));
// Case 1: variable is demoted to scalar then take the ref type
if (varDeclType.getDimensions() == mapping.getMappedDimensions()) {
Xnode tempName = new Xnode(Xcode.NAME, xcodeml);
tempName.setValue(var.getFctMapping());
tempName.setAttribute(Xattr.TYPE, varDeclType.getRef());
Xdecl newVarDecl = new Xdecl(new Xnode(Xcode.VARDECL, xcodeml).element());
newVarDecl.append(tempName, false);
fctDeclarations.replace(newVarDecl, var.getFctMapping());
id.setType(varDeclType.getRef());
}
/* else {
// Case 2: variable is not totally demoted then create new type
// TODO
}*/
}
// Loop mapped variables
}
// Loop over mapping clauses
// Adapt array reference in function body
List<Xnode> arrayReferences = clonedFctDef.body().matchAll(Xcode.FARRAYREF);
for (Xnode ref : arrayReferences) {
if (!(ref.matchSeq(Xcode.VARREF).child(0).opcode() == Xcode.VAR)) {
continue;
}
String mappedVar = ref.matchSeq(Xcode.VARREF, 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.opcode() == Xcode.ARRAYINDEX) {
List<Xnode> children = e.children();
if (children.size() > 0 && children.get(0).opcode() == 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.VARREF, Xcode.VAR).cloneNode());
ref.delete();
}
}
}
// Generate accelerator pragmas if needed
AcceleratorHelper.generateAdditionalDirectives(_claw, xcodeml, extractedLoop, extractedLoop);
// TODO must be triggered by a clause
//AcceleratorHelper.generateRoutineDirectives(_claw, xcodeml, clonedFctDef);
// Add any additional transformation defined in the directive clauses
TransformationHelper.generateAdditionalTransformation(_claw, xcodeml, transformer, extractedLoop);
_claw.getPragma().delete();
this.transformed();
}
use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class TransformationHelper method duplicateWithDimension.
/**
* Duplicates the type to update and add extra dimensions to match the base
* type.
*
* @param base Base type.
* @param toUpdate Type to update.
* @param xcodemlDst Destination XcodeML unit. Duplicate will be created here.
* @param xcodemlSrc Source XcodeML unit. Contains base dimension.
* @return The new type hash generated.
*/
public static String duplicateWithDimension(XbasicType base, XbasicType toUpdate, XcodeML xcodemlDst, XcodeML xcodemlSrc, OverPosition overPos, List<ClawDimension> dimensions) throws IllegalTransformationException {
XbasicType newType = toUpdate.cloneNode();
String type = xcodemlDst.getTypeTable().generateArrayTypeHash();
newType.setAttribute(Xattr.TYPE, type);
if (base.isAllAssumedShape() && toUpdate.isAllAssumedShape()) {
int additionalDimensions = base.getDimensions() - toUpdate.getDimensions();
for (int i = 0; i < additionalDimensions; ++i) {
Xnode index = xcodemlDst.createEmptyAssumedShaped();
newType.addDimension(index, 0);
}
} else if (base.isAllAssumedShape() && !toUpdate.isAllAssumedShape()) {
switch(overPos) {
case BEFORE:
// TODO control and validate the before/after
for (ClawDimension dim : dimensions) {
newType.addDimension(dim.generateIndexRange(xcodemlDst, false), XbasicType.APPEND);
}
break;
case AFTER:
for (ClawDimension dim : dimensions) {
newType.addDimension(dim.generateIndexRange(xcodemlDst, false), 0);
}
break;
case MIDDLE:
throw new IllegalTransformationException("Not supported yet. " + "Insertion in middle for duplicated array type.", 0);
}
} else {
newType.resetDimension();
for (int i = 0; i < base.getDimensions(); ++i) {
Xnode newDim = new Xnode(Xcode.INDEXRANGE, xcodemlDst);
newType.append(newDim, false);
Xnode baseDim = base.getDimensions(i);
Xnode lowerBound = baseDim.matchSeq(Xcode.LOWERBOUND);
Xnode upperBound = baseDim.matchSeq(Xcode.UPPERBOUND);
if (lowerBound != null) {
Xnode newLowerBound = XnodeUtil.duplicateBound(lowerBound, xcodemlDst, xcodemlSrc);
newDim.append(newLowerBound, false);
}
if (upperBound != null) {
Xnode newUpperBound = XnodeUtil.duplicateBound(upperBound, xcodemlDst, xcodemlSrc);
newDim.append(newUpperBound, false);
}
newType.addDimension(newDim, XbasicType.APPEND);
}
}
xcodemlDst.getTypeTable().add(newType);
return type;
}
use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class TransformationHelper method updateModuleSignature.
/**
* Update the function signature in the module file to reflects local changes.
*
* @param xcodeml Current XcodeML file unit.
* @param fctDef Function definition that has been changed.
* @param fctType Function type that has been changed.
* @param modDef Module definition holding the function definition.
* @param claw Pragma that has triggered the transformation.
* @param transformer Current transformer object.
* @throws IllegalTransformationException If the module file or the function
* cannot be located
*/
public static void updateModuleSignature(XcodeProgram xcodeml, XfunctionDefinition fctDef, XfunctionType fctType, XmoduleDefinition modDef, ClawLanguage claw, cx2x.xcodeml.transformation.Transformer transformer, boolean importFctType) throws IllegalTransformationException {
Xmod mod;
if (transformer.getModCache().isModuleLoaded(modDef.getName())) {
mod = transformer.getModCache().get(modDef.getName());
} else {
mod = fctDef.findContainingModule();
if (mod == null) {
throw new IllegalTransformationException("Unable to locate module file for: " + modDef.getName(), claw.getPragma().lineNo());
}
transformer.getModCache().add(modDef.getName(), mod);
}
XfunctionType fctTypeMod;
if (importFctType) {
Node rawNode = mod.getDocument().importNode(fctType.element(), true);
mod.getTypeTable().element().appendChild(rawNode);
XfunctionType importedFctType = new XfunctionType((Element) rawNode);
Xid importedFctTypeId = mod.createId(importedFctType.getType(), Xname.SCLASS_F_FUNC, fctDef.getName().value());
mod.getIdentifiers().add(importedFctTypeId);
// check if params need to be imported as well
if (importedFctType.getParameterNb() > 0) {
for (Xnode param : importedFctType.getParams().getAll()) {
mod.importType(xcodeml, param.getAttribute(Xattr.TYPE));
}
}
return;
} else {
fctTypeMod = (XfunctionType) mod.getTypeTable().get(fctDef.getName().getAttribute(Xattr.TYPE));
}
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().value() + " in module " + modDef.getName();
int lineNo = claw.getPragma().lineNo();
// If not, try to matchSeq the correct FfunctionType in the module definitions
Xid id = mod.getIdentifiers().get(fctDef.getName().value());
if (id == null) {
throw new IllegalTransformationException(errorMsg, lineNo);
}
fctTypeMod = (XfunctionType) mod.getTypeTable().get(id.getType());
if (fctTypeMod == null) {
throw new IllegalTransformationException(errorMsg, lineNo);
}
}
XbasicType modIntTypeIntentIn = mod.createBasicType(mod.getTypeTable().generateIntegerTypeHash(), Xname.TYPE_F_INT, Xintent.IN);
mod.getTypeTable().add(modIntTypeIntentIn);
List<Xnode> paramsLocal = fctType.getParams().getAll();
List<Xnode> paramsMod = fctTypeMod.getParams().getAll();
if (paramsLocal.size() < paramsMod.size()) {
throw new IllegalTransformationException("Local function has more parameters than module counterpart.", claw.getPragma().lineNo());
}
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(ClawAttr.IS_CLAW.toString())) {
// new parameter
Xnode param = mod.createAndAddParamIfNotExists(pLocal.value(), modIntTypeIntentIn.getType(), fctTypeMod);
if (param != null) {
param.setAttribute(ClawAttr.IS_CLAW.toString(), Xname.TRUE);
}
} else {
Xnode pMod = paramsMod.get(i);
String localType = pLocal.getAttribute(Xattr.TYPE);
String modType = pMod.getAttribute(Xattr.TYPE);
if (!localType.equals(modType)) {
// Param has been update so have to replicate the change to mod file
XbasicType lType = (XbasicType) xcodeml.getTypeTable().get(localType);
XbasicType crtType = (XbasicType) mod.getTypeTable().get(modType);
List<ClawDimension> dimensions = TransformationHelper.findDimensions(fctType);
OverPosition overPos = OverPosition.fromString(pLocal.getAttribute(ClawAttr.OVER.toString()));
if (lType.isArray()) {
String newType = TransformationHelper.duplicateWithDimension(lType, crtType, mod, xcodeml, overPos, dimensions);
pMod.setAttribute(Xattr.TYPE, newType);
}
}
// Propagate the over attribute
copyAttribute(pLocal, pMod, ClawAttr.OVER);
}
}
}
use of cx2x.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class TransformationHelper method promoteField.
/**
* Promote a field with the information stored in the defined dimensions.
*
* @param fieldId Id of the field as defined in the symbol table.
* @param update If true, update current type otherwise, create a type from
* scratch.
* @param assumed If true, generate assumed dimension range, otherwise, use
* the information in the defined dimension.
* @param overIndex Over clause to be used for promotion.
* @param xcodeml Current XcodeML program unit in which the element will be
* created.
* @param overPos Force behavior as if over clause is present.
* @throws IllegalTransformationException If type cannot be found.
*/
public static PromotionInfo promoteField(String fieldId, boolean update, boolean assumed, int overIndex, int overDimensions, XfunctionDefinition fctDef, XfunctionType fctType, List<ClawDimension> dimensions, ClawLanguage claw, XcodeProgram xcodeml, OverPosition overPos) throws IllegalTransformationException {
Xid id = fctDef.getSymbolTable().get(fieldId);
Xdecl decl = fctDef.getDeclarationTable().get(fieldId);
String type = xcodeml.getTypeTable().generateArrayTypeHash();
XbasicType newType;
if (update) {
if (XnodeUtil.isBuiltInType(id.getType())) {
newType = xcodeml.createBasicType(type, id.getType(), Xintent.NONE);
} else {
XbasicType old = (XbasicType) xcodeml.getTypeTable().get(id.getType());
if (old == null) {
throw new IllegalTransformationException("Cannot matchSeq type for " + fieldId, claw.getPragma().lineNo());
} else {
newType = old.cloneNode();
newType.setType(type);
}
}
} else {
newType = xcodeml.createBasicType(type, id.getType(), Xintent.NONE);
}
PromotionInfo proInfo = new PromotionInfo(fieldId, newType.getDimensions(), newType.getDimensions() + dimensions.size(), type);
if (assumed) {
if (newType.isAllAssumedShape() && (fctType.hasParam(fieldId) || newType.isPointer())) {
for (int i = 0; i < overDimensions; ++i) {
Xnode index = xcodeml.createEmptyAssumedShaped();
newType.addDimension(index, 0);
}
} else {
if (claw.hasOverClause() || overPos != null) {
/* If the directive has an over clause, there is three possibility to
* insert the newly defined dimensions.
* 1. Insert the dimensions in the middle on currently existing ones.
* 2. Insert the dimensions before currently existing ones.
* 3. Insert the dimensions after currently existing ones. */
if (overPos == null) {
overPos = getOverPosition(claw.getOverClauseValues().get(overIndex));
}
if (overPos == OverPosition.MIDDLE) {
// Insert new dimension in middle (case 1)
int startIdx = 1;
for (ClawDimension dim : dimensions) {
Xnode index = dim.generateIndexRange(xcodeml, false);
newType.addDimension(index, startIdx++);
}
} else if (overPos == OverPosition.AFTER) {
// Insert new dimensions at the end (case 3)
for (ClawDimension dim : dimensions) {
Xnode index = dim.generateIndexRange(xcodeml, false);
newType.addDimension(index, XbasicType.APPEND);
}
} else {
// Insert new dimension at the beginning (case 2)
for (ClawDimension dim : dimensions) {
Xnode index = dim.generateIndexRange(xcodeml, false);
newType.addDimension(index, 0);
}
}
} else {
for (ClawDimension dim : dimensions) {
Xnode index = dim.generateIndexRange(xcodeml, false);
newType.addDimension(index, 0);
}
}
}
} else {
for (ClawDimension dim : dimensions) {
Xnode index = dim.generateIndexRange(xcodeml, false);
newType.addDimension(index, XbasicType.APPEND);
}
}
id.setType(type);
decl.matchSeq(Xcode.NAME).setAttribute(Xattr.TYPE, type);
xcodeml.getTypeTable().add(newType);
// Update params in function type
for (Xnode param : fctType.getParams().getAll()) {
if (param.value().equals(fieldId)) {
// Update type with new promoted type
param.setAttribute(Xattr.TYPE, type);
// Save the over clause for parallelize forward transformation
if (claw.hasOverClause()) {
param.setAttribute(ClawAttr.OVER.toString(), getOverPosition(claw.getOverClauseValues().get(overIndex)).toString());
}
}
}
if (fctType.hasAttribute(Xattr.RESULT_NAME) && fctType.getAttribute(Xattr.RESULT_NAME).equals(fieldId)) {
if (claw.hasOverClause()) {
fctType.setAttribute(ClawAttr.OVER.toString(), getOverPosition(claw.getOverClauseValues().get(overIndex)).toString());
}
}
return proInfo;
}
Aggregations