26#define GEN_PASS_DEF_SCFFORLOOPCANONICALIZATION
27#include "mlir/Dialect/SCF/Transforms/Passes.h.inc"
56template <
typename OpTy>
60 LogicalResult matchAndRewrite(OpTy dimOp,
62 auto blockArg = dyn_cast<BlockArgument>(dimOp.getSource());
65 auto forOp = dyn_cast<ForOp>(blockArg.getParentBlock()->getParentOp());
71 Value initArg = forOp.getTiedLoopInit(blockArg)->get();
73 dimOp, [&]() { dimOp.getSourceMutable().assign(initArg); });
101template <
typename OpTy>
105 LogicalResult matchAndRewrite(OpTy dimOp,
107 auto forOp = dimOp.getSource().template getDefiningOp<scf::ForOp>();
110 auto opResult = cast<OpResult>(dimOp.getSource());
111 unsigned resultNumber = opResult.getResultNumber();
115 dimOp.getSourceMutable().assign(forOp.getInitArgs()[resultNumber]);
123template <
typename OpTy>
125 using OpRewritePattern<OpTy>::OpRewritePattern;
127 LogicalResult matchAndRewrite(OpTy op,
128 PatternRewriter &rewriter)
const override {
133struct SCFForLoopCanonicalization
135 void runOnOperation()
override {
136 auto *parentOp = getOperation();
137 MLIRContext *ctx = parentOp->getContext();
138 RewritePatternSet patterns(ctx);
150 .
add<AffineOpSCFCanonicalizationPattern<affine::AffineMinOp>,
151 AffineOpSCFCanonicalizationPattern<affine::AffineMaxOp>,
152 DimOfIterArgFolder<tensor::DimOp>, DimOfIterArgFolder<memref::DimOp>,
153 DimOfLoopResultFolder<tensor::DimOp>,
154 DimOfLoopResultFolder<memref::DimOp>>(ctx);
158 return std::make_unique<SCFForLoopCanonicalization>();
MLIRContext is the top-level object for a collection of MLIR operations.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
LogicalResult matchForLikeLoop(Value iv, OpFoldResult &lb, OpFoldResult &ub, OpFoldResult &step)
Match "for loop"-like operations from the SCF dialect.
LogicalResult canonicalizeMinMaxOpInLoop(RewriterBase &rewriter, Operation *op, LoopMatcherFn loopMatcher)
Try to canonicalize the given affine.min/max operation in the context of for loops with a known range...
bool isShapePreserving(ForOp forOp, int64_t arg)
A simple, conservative analysis to determine if the loop is shape preserving.
void populateSCFForLoopCanonicalizationPatterns(RewritePatternSet &patterns)
Populate patterns for canonicalizing operations inside SCF loop bodies.
Include the generated interface declarations.
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...
std::unique_ptr< Pass > createSCFForLoopCanonicalizationPass()
Creates a pass that canonicalizes affine.min and affine.max operations inside of scf....
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...