use of claw.tatsu.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class XcodeML method write.
/**
* Write the XcodeML to output stream
*
* @param outputStream Output stream
* @param indent Number of spaces used for the indentation
* @throws IllegalTransformationException if XML file cannot be written.
*/
public void write(OutputStream outputStream, int indent) throws IllegalTransformationException {
try {
cleanEmptyTextNodes(this.getDocument());
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
DOMSource source = new DOMSource(this.getDocument());
StreamResult streamResult = new StreamResult(outputStream);
transformer.transform(source, streamResult);
} catch (Exception e) {
throw new IllegalTransformationException("Failed to transform XCodeML into text", e);
}
}
use of claw.tatsu.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class Pragma method splitByLength.
/**
* Split the line by its length and add continuation symbols.
*
* @param pragma Pragma statement to be splitted.
* @param xcodeml The XcodeML on which the transformations are applied.
* @param prefix Prefix of the directive.
* @throws IllegalTransformationException If the given element is not a
* FpragmaStatement.
*/
public static void splitByLength(Xnode pragma, XcodeProgram xcodeml, String prefix) throws IllegalTransformationException {
if (!Xnode.isOfCode(pragma, Xcode.F_PRAGMA_STATEMENT)) {
throw new IllegalTransformationException(TatsuConstant.ERROR_INCOMPATIBLE);
}
String allPragma = pragma.value().toLowerCase();
if (allPragma.length() > xcodeml.context().getMaxColumns()) {
allPragma = Pragma.dropEndingComment(allPragma);
Xnode newlyInserted = pragma;
List<String> splittedPragmas = Pragma.split(allPragma, xcodeml.context().getMaxColumns(), prefix);
for (int i = 0; i < splittedPragmas.size(); ++i) {
// Create pragma with continuation symbol unless for the last item.
newlyInserted = createAndInsertPragma(xcodeml, newlyInserted, pragma.filename(), pragma.lineNo(), splittedPragmas.get(i), i != splittedPragmas.size() - 1);
}
// Delete original not splitted pragma.
pragma.delete();
}
}
use of claw.tatsu.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class Type 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 xcodemlSrc Source XcodeML unit. Contains base dimension.
* @param xcodemlDst Destination XcodeML unit. Duplicate will be created here.
* @param dimensions List of dimensions definitions to be used.
* @return The new type hash generated.
* @throws IllegalTransformationException If action is not supported.
*/
public static FbasicType duplicateWithDimension(FbasicType base, FbasicType toUpdate, XcodeML xcodemlSrc, XcodeML xcodemlDst, List<DimensionDefinition> dimensions) throws IllegalTransformationException {
FbasicType newType = toUpdate.cloneNode();
String type = xcodemlDst.getTypeTable().generateHash(FortranType.ARRAY);
newType.setType(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()) {
for (DimensionDefinition dim : dimensions) {
switch(dim.getInsertionPosition()) {
case BEFORE:
// TODO control and validate the before/after
newType.addDimension(dim.generateIndexRange(xcodemlDst, false, false));
break;
case AFTER:
newType.addDimension(dim.generateIndexRange(xcodemlDst, false, false), 0);
break;
case IN_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 = xcodemlDst.createNode(Xcode.INDEX_RANGE);
newType.append(newDim);
Xnode baseDim = base.getDimensions(i);
Xnode lowerBound = baseDim.matchSeq(Xcode.LOWER_BOUND);
Xnode upperBound = baseDim.matchSeq(Xcode.UPPER_BOUND);
if (lowerBound != null) {
Xnode newLowerBound = Type.duplicateBound(lowerBound, xcodemlSrc, xcodemlDst);
newDim.append(newLowerBound);
}
if (upperBound != null) {
Xnode newUpperBound = Type.duplicateBound(upperBound, xcodemlSrc, xcodemlDst);
newDim.append(newUpperBound);
}
newType.addDimension(newDim);
}
}
xcodemlDst.getTypeTable().add(newType);
return newType;
}
use of claw.tatsu.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class Type method duplicateBound.
/**
* Duplicate a lower or an upper bound between two different XcodeML units.
*
* @param baseBound Base bound to be duplicated.
* @param xcodemlSrc Source XcodeML unit. Contains base bound.
* @param xcodemlDst Destination XcodeML unit. Duplicate will be created here.
* @return The newly duplicated bound element.
* @throws IllegalTransformationException If bound cannot be duplicated.
*/
private static Xnode duplicateBound(Xnode baseBound, XcodeML xcodemlSrc, XcodeML xcodemlDst) throws IllegalTransformationException {
if (!Xnode.isOfCode(baseBound, Xcode.LOWER_BOUND) && !Xnode.isOfCode(baseBound, Xcode.UPPER_BOUND)) {
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 = xcodemlDst.createNode(baseBound.opcode());
bound.append(xcodemlDst.importElement(boundChild, xcodemlSrc));
return bound;
}
use of claw.tatsu.xcodeml.exception.IllegalTransformationException in project claw-compiler by C2SM-RCM.
the class ScaGPU method applySpecificTransformation.
/**
* Apply specific transformation steps for GPU target.
*
* @param xcodeml Current translation unit.
* @throws IllegalTransformationException If any transformation fails.
*/
private void applySpecificTransformation(Configuration cfg, XcodeProgram xcodeml) throws IllegalTransformationException {
final Context context = xcodeml.context();
AcceleratorConfiguration config = cfg.accelerator();
if (_fctDef.hasEmptyBody()) {
// Nothing to do in this function
return;
}
/*
* Create a nested loop with the new defined dimensions and wrap it around the
* whole subroutine's body. This is for the moment a really naive transformation
* idea but it is our start point. Use the first over clause to create it.
*/
NestedDoStatement loops;
if (forceAssumedShapedArrayPromotion) {
if (_promotions.isEmpty()) {
throw new IllegalTransformationException("Cannot assume shape of " + "array in elemental function/subroutine.", _claw.getPragma().lineNo());
}
PromotionInfo pi = _promotions.entrySet().iterator().next().getValue();
loops = new NestedDoStatement(_claw.getDefaultLayoutReversed(cfg), pi, xcodeml);
} else {
loops = new NestedDoStatement(_claw.getDefaultLayoutReversed(cfg), xcodeml);
}
/*
* Subroutine/function can have a contains section with inner subroutines or
* functions. The newly created (nested) do statements should stop before this
* contains section if it exists.
*/
Xnode contains = _fctDef.body().matchSeq(Xcode.F_CONTAINS_STATEMENT);
if (contains != null) {
Xnode parallelRegionStart;
if (_specialParallelRegionStart == null) {
parallelRegionStart = Directive.findParallelRegionStart(context, _fctDef, null);
} else {
parallelRegionStart = _specialParallelRegionStart;
}
Xnode parallelRegionEnd = Directive.findParallelRegionEnd(context, _fctDef, contains);
Body.shiftIn(parallelRegionStart, parallelRegionEnd, loops.getInnerStatement().body(), true);
contains.insertBefore(loops.getOuterStatement());
} else {
// No contains section, all the body is copied to the do statements.
Xnode parallelRegionStart;
if (_specialParallelRegionStart == null) {
parallelRegionStart = Directive.findParallelRegionStart(context, _fctDef, null);
} else {
parallelRegionStart = _specialParallelRegionStart;
}
Xnode parallelRegionEnd = Directive.findParallelRegionEnd(context, _fctDef, null);
// Define a hook from where we can insert the new do statement
Xnode hook = parallelRegionEnd != null ? parallelRegionEnd.nextSibling() : null;
Body.shiftIn(parallelRegionStart, parallelRegionEnd, loops.getInnerStatement().body(), true);
// Hook is null then we append the do statement to the current fct body
if (hook == null) {
_fctDef.body().append(loops.getOuterStatement());
} else {
// Insert new do statement before the hook element
hook.insertBefore(loops.getOuterStatement());
}
}
// TODO nodep passing!
int collapse = Directive.generateLoopSeq(xcodeml, loops.getInnerStatement().body(), CompilerDirective.CLAW.getPrefix() + " nodep");
// Prepare variables list for present/pcreate clauses and handle
// promotion/privatize local strategy
List<String> presentList = _fctDef.getPresentVariables(xcodeml);
List<String> privateList = Collections.emptyList();
List<String> createList = Collections.emptyList();
if (config.getLocalStrategy() == AcceleratorLocalStrategy.PRIVATE) {
privateList = applyPrivateStrategy(xcodeml);
} else if (config.getLocalStrategy() == AcceleratorLocalStrategy.PROMOTE) {
createList = applyPromoteStrategy(cfg, xcodeml);
}
// Generate the data region
Xblock doStmtBlock = new Xblock(loops.getOuterStatement());
Directive.generateDataRegionClause(xcodeml, presentList, createList, doStmtBlock);
// Generate the parallel region
Directive.generateParallelLoopClause(xcodeml, privateList, loops.getOuterStatement(), loops.getOuterStatement(), null, loops.size() + collapse);
Directive.generateRoutineDirectives(xcodeml, _fctDef);
}
Aggregations