Skip to content

Commit

Permalink
[flang][NFCI] Relpace LoopOp Op with DoLoopOp Op in FIR Dialect
Browse files Browse the repository at this point in the history
Part of upstreaming effort,
PR: flang-compiler/f18-llvm-project#296

Reviewed By: schweitz

Differential Revision: https://reviews.llvm.org/D95950
  • Loading branch information
SouraVX authored and memfrob committed Oct 4, 2022
1 parent 1732a77 commit a40de4e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions flang/include/flang/Optimizer/Dialect/FIROps.h
Expand Up @@ -18,7 +18,7 @@ using namespace mlir;
namespace fir {

class FirEndOp;
class LoopOp;
class DoLoopOp;
class RealAttr;

void buildCmpFOp(mlir::OpBuilder &builder, mlir::OperationState &result,
Expand All @@ -29,7 +29,7 @@ void buildCmpCOp(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::Value rhs);
unsigned getCaseArgumentOffset(llvm::ArrayRef<mlir::Attribute> cases,
unsigned dest);
LoopOp getForInductionVarOwner(mlir::Value val);
DoLoopOp getForInductionVarOwner(mlir::Value val);
bool isReferenceLike(mlir::Type type);
mlir::ParseResult isValidCaseAttr(mlir::Attribute attr);
mlir::ParseResult parseCmpfOp(mlir::OpAsmParser &parser,
Expand Down
6 changes: 3 additions & 3 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Expand Up @@ -1852,7 +1852,7 @@ def fir_LenParamIndexOp : fir_OneResultOp<"len_param_index", [NoSideEffect]> {

def fir_ResultOp : fir_Op<"result",
[NoSideEffect, ReturnLike, Terminator,
ParentOneOf<["WhereOp", "LoopOp", "IterWhileOp"]>]> {
ParentOneOf<["WhereOp", "DoLoopOp", "IterWhileOp"]>]> {
let summary = "special terminator for use in fir region operations";

let description = [{
Expand Down Expand Up @@ -1883,7 +1883,7 @@ class region_Op<string mnemonic, list<OpTrait> traits = []> :
let parser = [{ return ::parse$cppClass(parser, result); }];
}

def fir_LoopOp : region_Op<"do_loop",
def fir_DoLoopOp : region_Op<"do_loop",
[DeclareOpInterfaceMethods<LoopLikeOpInterface>]> {
let summary = "generalized loop operation";
let description = [{
Expand Down Expand Up @@ -2020,7 +2020,7 @@ def fir_IterWhileOp : region_Op<"iterate_while",
let summary = "DO loop with early exit condition";
let description = [{
This construct is useful for lowering implied-DO loops. It is very similar
to `fir::LoopOp` with the addition that it requires a single loop-carried
to `fir::DoLoopOp` with the addition that it requires a single loop-carried
bool value that signals an early exit condition to the operation. A `true`
disposition means the next loop iteration should proceed. A `false`
indicates that the `fir.iterate_while` operation should terminate and
Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Optimizer/Transforms/Passes.td
Expand Up @@ -17,7 +17,7 @@
include "mlir/Pass/PassBase.td"

def AffineDialectPromotion : FunctionPass<"promote-to-affine"> {
let summary = "Promotes fir.loop and fir.where to affine.for and affine.if where possible";
let summary = "Promotes fir.do_loop and fir.where to affine.for and affine.if where possible";
let description = [{
TODO
}];
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/DoLoopHelper.cpp
Expand Up @@ -19,7 +19,7 @@ void Fortran::lower::DoLoopHelper::createLoop(
auto ubi = builder.convertToIndexType(loc, ub);
assert(step && "step must be an actual Value");
auto inc = builder.convertToIndexType(loc, step);
auto loop = builder.create<fir::LoopOp>(loc, lbi, ubi, inc);
auto loop = builder.create<fir::DoLoopOp>(loc, lbi, ubi, inc);
auto insertPt = builder.saveInsertionPoint();
builder.setInsertionPointToStart(loop.getBody());
auto index = loop.getInductionVar();
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/IO.cpp
Expand Up @@ -387,7 +387,7 @@ static void genIoLoop(Fortran::lower::AbstractConverter &converter,
if (!checkResult) {
// No I/O call result checks - the loop is a fir.do_loop op.
auto loopOp =
builder.create<fir::LoopOp>(loc, lowerValue, upperValue, stepValue);
builder.create<fir::DoLoopOp>(loc, lowerValue, upperValue, stepValue);
builder.setInsertionPointToStart(loopOp.getBody());
auto lcv = builder.createConvert(loc, converter.genType(loopSym),
loopOp.getInductionVar());
Expand Down
39 changes: 20 additions & 19 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Expand Up @@ -786,30 +786,31 @@ mlir::ParseResult fir::LoadOp::getElementOf(mlir::Type &ele, mlir::Type ref) {
}

//===----------------------------------------------------------------------===//
// LoopOp
// DoLoopOp
//===----------------------------------------------------------------------===//

void fir::LoopOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::Value lb, mlir::Value ub, mlir::Value step,
bool unordered, mlir::ValueRange iterArgs,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
void fir::DoLoopOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Value lb,
mlir::Value ub, mlir::Value step, bool unordered,
mlir::ValueRange iterArgs,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
result.addOperands({lb, ub, step});
result.addOperands(iterArgs);
for (auto v : iterArgs)
result.addTypes(v.getType());
mlir::Region *bodyRegion = result.addRegion();
bodyRegion->push_back(new Block{});
if (iterArgs.empty())
LoopOp::ensureTerminator(*bodyRegion, builder, result.location);
DoLoopOp::ensureTerminator(*bodyRegion, builder, result.location);
bodyRegion->front().addArgument(builder.getIndexType());
bodyRegion->front().addArguments(iterArgs.getTypes());
if (unordered)
result.addAttribute(unorderedAttrName(), builder.getUnitAttr());
result.addAttributes(attributes);
}

static mlir::ParseResult parseLoopOp(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
static mlir::ParseResult parseDoLoopOp(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
auto &builder = parser.getBuilder();
mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step;
// Parse the induction variable followed by '='.
Expand All @@ -827,7 +828,7 @@ static mlir::ParseResult parseLoopOp(mlir::OpAsmParser &parser,
return failure();

if (mlir::succeeded(parser.parseOptionalKeyword("unordered")))
result.addAttribute(fir::LoopOp::unorderedAttrName(),
result.addAttribute(fir::DoLoopOp::unorderedAttrName(),
builder.getUnitAttr());

// Parse the optional initial iteration arguments.
Expand Down Expand Up @@ -864,22 +865,22 @@ static mlir::ParseResult parseLoopOp(mlir::OpAsmParser &parser,
if (parser.parseRegion(*body, regionArgs, argTypes))
return failure();

fir::LoopOp::ensureTerminator(*body, builder, result.location);
fir::DoLoopOp::ensureTerminator(*body, builder, result.location);

return mlir::success();
}

fir::LoopOp fir::getForInductionVarOwner(mlir::Value val) {
fir::DoLoopOp fir::getForInductionVarOwner(mlir::Value val) {
auto ivArg = val.dyn_cast<mlir::BlockArgument>();
if (!ivArg)
return {};
assert(ivArg.getOwner() && "unlinked block argument");
auto *containingInst = ivArg.getOwner()->getParentOp();
return dyn_cast_or_null<fir::LoopOp>(containingInst);
return dyn_cast_or_null<fir::DoLoopOp>(containingInst);
}

// Lifted from loop.loop
static mlir::LogicalResult verify(fir::LoopOp op) {
static mlir::LogicalResult verify(fir::DoLoopOp op) {
if (auto cst = dyn_cast_or_null<ConstantIndexOp>(op.step().getDefiningOp()))
if (cst.getValue() <= 0)
return op.emitOpError("constant step operand must be positive");
Expand Down Expand Up @@ -918,9 +919,9 @@ static mlir::LogicalResult verify(fir::LoopOp op) {
return success();
}

static void print(mlir::OpAsmPrinter &p, fir::LoopOp op) {
static void print(mlir::OpAsmPrinter &p, fir::DoLoopOp op) {
bool printBlockTerminators = false;
p << fir::LoopOp::getOperationName() << ' ' << op.getInductionVar() << " = "
p << fir::DoLoopOp::getOperationName() << ' ' << op.getInductionVar() << " = "
<< op.lowerBound() << " to " << op.upperBound() << " step " << op.step();
if (op.unordered())
p << " unordered";
Expand All @@ -935,19 +936,19 @@ static void print(mlir::OpAsmPrinter &p, fir::LoopOp op) {
printBlockTerminators = true;
}
p.printOptionalAttrDictWithKeyword(op->getAttrs(),
{fir::LoopOp::unorderedAttrName()});
{fir::DoLoopOp::unorderedAttrName()});
p.printRegion(op.region(), /*printEntryBlockArgs=*/false,
printBlockTerminators);
}

mlir::Region &fir::LoopOp::getLoopBody() { return region(); }
mlir::Region &fir::DoLoopOp::getLoopBody() { return region(); }

bool fir::LoopOp::isDefinedOutsideOfLoop(mlir::Value value) {
bool fir::DoLoopOp::isDefinedOutsideOfLoop(mlir::Value value) {
return !region().isAncestor(value.getParentRegion());
}

mlir::LogicalResult
fir::LoopOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) {
fir::DoLoopOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) {
for (auto op : ops)
op->moveBefore(*this);
return success();
Expand Down

0 comments on commit a40de4e

Please sign in to comment.