24#define GEN_PASS_DEF_SCFFORLOOPRANGEFOLDING
25#include "mlir/Dialect/SCF/Transforms/Passes.h.inc"
35 void initialize() { setHasBoundedRewriteRecursion(); }
37 LogicalResult matchAndRewrite(ForOp op,
38 PatternRewriter &rewriter)
const override {
39 Value indVar = op.getInductionVar();
41 auto canBeFolded = [&](Value value) {
42 return op.isDefinedOutsideOfLoop(value) || value == indVar;
52 Operation *user = *indVar.
getUsers().begin();
54 [&](Operation *u) { return u == user; }))
57 if (!isa<arith::AddIOp, arith::MulIOp>(user))
60 if (!llvm::all_of(user->
getOperands(), canBeFolded))
63 if (
auto mulOp = dyn_cast<arith::MulIOp>(user)) {
68 (mulOp.getLhs() == indVar) ? mulOp.getRhs() : mulOp.getLhs();
70 if (!multiplierVal || *multiplierVal <= 0)
76 lbMap.
map(indVar, op.getLowerBound());
78 ubMap.
map(indVar, op.getUpperBound());
80 stepMap.
map(indVar, op.getStep());
82 Operation *lbFold = rewriter.
clone(*user, lbMap);
83 Operation *ubFold = rewriter.
clone(*user, ubMap);
84 Operation *stepFold =
nullptr;
85 if (
auto addOp = dyn_cast<arith::AddIOp>(user)) {
87 if (addOp.getLhs() == indVar && addOp.getRhs() == indVar)
88 stepFold = rewriter.
clone(*user, stepMap);
90 stepFold = rewriter.
clone(*user, stepMap);
104struct ForLoopRangeFolding
106 void runOnOperation()
override;
110void ForLoopRangeFolding::runOnOperation() {
112 patterns.add<ForLoopRangeFoldingPattern>(&
getContext());
113 GreedyRewriteConfig config;
114 config.setStrictness(GreedyRewriteStrictness::ExistingOps)
115 .enableFolding(
false)
116 .enableConstantCSE(
false)
117 .setRegionSimplificationLevel(GreedySimplifyRegionLevel::Disabled);
124 return std::make_unique<ForLoopRangeFolding>();
LogicalResult initialize(unsigned origNumLoops, ArrayRef< ReassociationIndices > foldedIterationDims)
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
operand_range getOperands()
Returns an iterator on the underlying Value's.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
bool use_empty() const
Returns true if this value has no uses.
user_range getUsers() const
Include the generated interface declarations.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
std::unique_ptr< Pass > createForLoopRangeFoldingPass()
Creates a pass which folds arith ops on induction variable into loop range.
LogicalResult applyPatternsGreedily(Region ®ion, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...