Skip to content

Commit 12da7e6

Browse files
committed
Cleanup no longer needed changes
1 parent e1e1ceb commit 12da7e6

File tree

5 files changed

+97
-131
lines changed

5 files changed

+97
-131
lines changed

flang/include/flang/Optimizer/Transforms/Utils.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#ifndef FORTRAN_OPTIMIZER_TRANSFORMS_UTILS_H
1414
#define FORTRAN_OPTIMIZER_TRANSFORMS_UTILS_H
1515

16-
#include "flang/Optimizer/Dialect/FIROps.h"
17-
1816
namespace fir {
1917

2018
using MinlocBodyOpGeneratorTy = llvm::function_ref<mlir::Value(
@@ -35,10 +33,6 @@ void genMinMaxlocReductionLoop(fir::FirOpBuilder &builder, mlir::Value array,
3533
mlir::Type maskElemType, mlir::Value resultArr,
3634
bool maskMayBeLogicalScalar);
3735

38-
std::pair<mlir::Block *, mlir::Block *>
39-
convertDoLoopToCFG(DoLoopOp loop, mlir::PatternRewriter &rewriter, bool setNSW,
40-
bool forceLoopToExecuteOnce);
41-
4236
} // namespace fir
4337

4438
#endif // FORTRAN_OPTIMIZER_TRANSFORMS_UTILS_H

flang/lib/Optimizer/OpenMP/SimdOnly.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "flang/Optimizer/Builder/FIRBuilder.h"
10-
#include "flang/Optimizer/Transforms/Utils.h"
1110
#include "mlir/Dialect/Arith/IR/Arith.h"
1211
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
1312
#include "mlir/Dialect/Func/IR/FuncOps.h"

flang/lib/Optimizer/Transforms/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ add_flang_library(FIRTransforms
3535
GenRuntimeCallsForTest.cpp
3636
SimplifyFIROperations.cpp
3737
OptimizeArrayRepacking.cpp
38-
Utils.cpp
3938

4039
DEPENDS
4140
CUFAttrs

flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "flang/Optimizer/Support/InternalNames.h"
1515
#include "flang/Optimizer/Support/TypeCode.h"
1616
#include "flang/Optimizer/Transforms/Passes.h"
17-
#include "flang/Optimizer/Transforms/Utils.h"
1817
#include "flang/Runtime/derived-api.h"
1918
#include "mlir/Dialect/Affine/IR/AffineOps.h"
2019
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
@@ -51,7 +50,103 @@ class CfgLoopConv : public mlir::OpRewritePattern<fir::DoLoopOp> {
5150
llvm::LogicalResult
5251
matchAndRewrite(DoLoopOp loop,
5352
mlir::PatternRewriter &rewriter) const override {
54-
convertDoLoopToCFG(loop, rewriter, setNSW, forceLoopToExecuteOnce);
53+
auto loc = loop.getLoc();
54+
mlir::arith::IntegerOverflowFlags flags{};
55+
if (setNSW)
56+
flags = bitEnumSet(flags, mlir::arith::IntegerOverflowFlags::nsw);
57+
auto iofAttr = mlir::arith::IntegerOverflowFlagsAttr::get(
58+
rewriter.getContext(), flags);
59+
60+
// Create the start and end blocks that will wrap the DoLoopOp with an
61+
// initalizer and an end point
62+
auto *initBlock = rewriter.getInsertionBlock();
63+
auto initPos = rewriter.getInsertionPoint();
64+
auto *endBlock = rewriter.splitBlock(initBlock, initPos);
65+
66+
// Split the first DoLoopOp block in two parts. The part before will be the
67+
// conditional block since it already has the induction variable and
68+
// loop-carried values as arguments.
69+
auto *conditionalBlock = &loop.getRegion().front();
70+
conditionalBlock->addArgument(rewriter.getIndexType(), loc);
71+
auto *firstBlock =
72+
rewriter.splitBlock(conditionalBlock, conditionalBlock->begin());
73+
auto *lastBlock = &loop.getRegion().back();
74+
75+
// Move the blocks from the DoLoopOp between initBlock and endBlock
76+
rewriter.inlineRegionBefore(loop.getRegion(), endBlock);
77+
78+
// Get loop values from the DoLoopOp
79+
auto low = loop.getLowerBound();
80+
auto high = loop.getUpperBound();
81+
assert(low && high && "must be a Value");
82+
auto step = loop.getStep();
83+
84+
// Initalization block
85+
rewriter.setInsertionPointToEnd(initBlock);
86+
auto diff = mlir::arith::SubIOp::create(rewriter, loc, high, low);
87+
auto distance = mlir::arith::AddIOp::create(rewriter, loc, diff, step);
88+
mlir::Value iters =
89+
mlir::arith::DivSIOp::create(rewriter, loc, distance, step);
90+
91+
if (forceLoopToExecuteOnce) {
92+
auto zero = mlir::arith::ConstantIndexOp::create(rewriter, loc, 0);
93+
auto cond = mlir::arith::CmpIOp::create(
94+
rewriter, loc, arith::CmpIPredicate::sle, iters, zero);
95+
auto one = mlir::arith::ConstantIndexOp::create(rewriter, loc, 1);
96+
iters = mlir::arith::SelectOp::create(rewriter, loc, cond, one, iters);
97+
}
98+
99+
llvm::SmallVector<mlir::Value> loopOperands;
100+
loopOperands.push_back(low);
101+
auto operands = loop.getIterOperands();
102+
loopOperands.append(operands.begin(), operands.end());
103+
loopOperands.push_back(iters);
104+
105+
mlir::cf::BranchOp::create(rewriter, loc, conditionalBlock, loopOperands);
106+
107+
// Last loop block
108+
auto *terminator = lastBlock->getTerminator();
109+
rewriter.setInsertionPointToEnd(lastBlock);
110+
auto iv = conditionalBlock->getArgument(0);
111+
mlir::Value steppedIndex =
112+
mlir::arith::AddIOp::create(rewriter, loc, iv, step, iofAttr);
113+
assert(steppedIndex && "must be a Value");
114+
auto lastArg = conditionalBlock->getNumArguments() - 1;
115+
auto itersLeft = conditionalBlock->getArgument(lastArg);
116+
auto one = mlir::arith::ConstantIndexOp::create(rewriter, loc, 1);
117+
mlir::Value itersMinusOne =
118+
mlir::arith::SubIOp::create(rewriter, loc, itersLeft, one);
119+
120+
llvm::SmallVector<mlir::Value> loopCarried;
121+
loopCarried.push_back(steppedIndex);
122+
auto begin = loop.getFinalValue() ? std::next(terminator->operand_begin())
123+
: terminator->operand_begin();
124+
loopCarried.append(begin, terminator->operand_end());
125+
loopCarried.push_back(itersMinusOne);
126+
auto backEdge = mlir::cf::BranchOp::create(rewriter, loc, conditionalBlock,
127+
loopCarried);
128+
rewriter.eraseOp(terminator);
129+
130+
// Copy loop annotations from the do loop to the loop back edge.
131+
if (auto ann = loop.getLoopAnnotation())
132+
backEdge->setAttr("loop_annotation", *ann);
133+
134+
// Conditional block
135+
rewriter.setInsertionPointToEnd(conditionalBlock);
136+
auto zero = mlir::arith::ConstantIndexOp::create(rewriter, loc, 0);
137+
auto comparison = mlir::arith::CmpIOp::create(
138+
rewriter, loc, arith::CmpIPredicate::sgt, itersLeft, zero);
139+
140+
mlir::cf::CondBranchOp::create(rewriter, loc, comparison, firstBlock,
141+
llvm::ArrayRef<mlir::Value>(), endBlock,
142+
llvm::ArrayRef<mlir::Value>());
143+
144+
// The result of the loop operation is the values of the condition block
145+
// arguments except the induction variable on the last iteration.
146+
auto args = loop.getFinalValue()
147+
? conditionalBlock->getArguments()
148+
: conditionalBlock->getArguments().drop_front();
149+
rewriter.replaceOp(loop, args.drop_back());
55150
return success();
56151
}
57152

flang/lib/Optimizer/Transforms/Utils.cpp

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)