| 1 | //===- LoopCanonicalization.cpp - Cross-dialect canonicalization patterns -===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains cross-dialect canonicalization patterns that cannot be |
| 10 | // actual canonicalization patterns due to undesired additional dependencies. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/SCF/Transforms/Passes.h" |
| 15 | |
| 16 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 17 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 18 | #include "mlir/Dialect/SCF/IR/SCF.h" |
| 19 | #include "mlir/Dialect/SCF/Transforms/Patterns.h" |
| 20 | #include "mlir/Dialect/SCF/Utils/AffineCanonicalizationUtils.h" |
| 21 | #include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 22 | #include "mlir/IR/PatternMatch.h" |
| 23 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 24 | |
| 25 | namespace mlir { |
| 26 | #define GEN_PASS_DEF_SCFFORLOOPCANONICALIZATION |
| 27 | #include "mlir/Dialect/SCF/Transforms/Passes.h.inc" |
| 28 | } // namespace mlir |
| 29 | |
| 30 | using namespace mlir; |
| 31 | using namespace mlir::scf; |
| 32 | |
| 33 | namespace { |
| 34 | /// Fold dim ops of iter_args to dim ops of their respective init args. E.g.: |
| 35 | /// |
| 36 | /// ``` |
| 37 | /// %0 = ... : tensor<?x?xf32> |
| 38 | /// scf.for ... iter_args(%arg0 = %0) -> (tensor<?x?xf32>) { |
| 39 | /// %1 = tensor.dim %arg0, %c0 : tensor<?x?xf32> |
| 40 | /// ... |
| 41 | /// } |
| 42 | /// ``` |
| 43 | /// |
| 44 | /// is folded to: |
| 45 | /// |
| 46 | /// ``` |
| 47 | /// %0 = ... : tensor<?x?xf32> |
| 48 | /// scf.for ... iter_args(%arg0 = %0) -> (tensor<?x?xf32>) { |
| 49 | /// %1 = tensor.dim %0, %c0 : tensor<?x?xf32> |
| 50 | /// ... |
| 51 | /// } |
| 52 | /// ``` |
| 53 | /// |
| 54 | /// Note: Dim ops are folded only if it can be proven that the runtime type of |
| 55 | /// the iter arg does not change with loop iterations. |
| 56 | template <typename OpTy> |
| 57 | struct DimOfIterArgFolder : public OpRewritePattern<OpTy> { |
| 58 | using OpRewritePattern<OpTy>::OpRewritePattern; |
| 59 | |
| 60 | LogicalResult matchAndRewrite(OpTy dimOp, |
| 61 | PatternRewriter &rewriter) const override { |
| 62 | auto blockArg = dyn_cast<BlockArgument>(dimOp.getSource()); |
| 63 | if (!blockArg) |
| 64 | return failure(); |
| 65 | auto forOp = dyn_cast<ForOp>(blockArg.getParentBlock()->getParentOp()); |
| 66 | if (!forOp) |
| 67 | return failure(); |
| 68 | if (!isShapePreserving(forOp, blockArg.getArgNumber() - 1)) |
| 69 | return failure(); |
| 70 | |
| 71 | Value initArg = forOp.getTiedLoopInit(blockArg)->get(); |
| 72 | rewriter.modifyOpInPlace( |
| 73 | dimOp, [&]() { dimOp.getSourceMutable().assign(initArg); }); |
| 74 | |
| 75 | return success(); |
| 76 | }; |
| 77 | }; |
| 78 | |
| 79 | /// Fold dim ops of loop results to dim ops of their respective init args. E.g.: |
| 80 | /// |
| 81 | /// ``` |
| 82 | /// %0 = ... : tensor<?x?xf32> |
| 83 | /// %r = scf.for ... iter_args(%arg0 = %0) -> (tensor<?x?xf32>) { |
| 84 | /// ... |
| 85 | /// } |
| 86 | /// %1 = tensor.dim %r, %c0 : tensor<?x?xf32> |
| 87 | /// ``` |
| 88 | /// |
| 89 | /// is folded to: |
| 90 | /// |
| 91 | /// ``` |
| 92 | /// %0 = ... : tensor<?x?xf32> |
| 93 | /// %r = scf.for ... iter_args(%arg0 = %0) -> (tensor<?x?xf32>) { |
| 94 | /// ... |
| 95 | /// } |
| 96 | /// %1 = tensor.dim %0, %c0 : tensor<?x?xf32> |
| 97 | /// ``` |
| 98 | /// |
| 99 | /// Note: Dim ops are folded only if it can be proven that the runtime type of |
| 100 | /// the iter arg does not change with loop iterations. |
| 101 | template <typename OpTy> |
| 102 | struct DimOfLoopResultFolder : public OpRewritePattern<OpTy> { |
| 103 | using OpRewritePattern<OpTy>::OpRewritePattern; |
| 104 | |
| 105 | LogicalResult matchAndRewrite(OpTy dimOp, |
| 106 | PatternRewriter &rewriter) const override { |
| 107 | auto forOp = dimOp.getSource().template getDefiningOp<scf::ForOp>(); |
| 108 | if (!forOp) |
| 109 | return failure(); |
| 110 | auto opResult = cast<OpResult>(dimOp.getSource()); |
| 111 | unsigned resultNumber = opResult.getResultNumber(); |
| 112 | if (!isShapePreserving(forOp, resultNumber)) |
| 113 | return failure(); |
| 114 | rewriter.modifyOpInPlace(dimOp, [&]() { |
| 115 | dimOp.getSourceMutable().assign(forOp.getInitArgs()[resultNumber]); |
| 116 | }); |
| 117 | return success(); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | /// Canonicalize AffineMinOp/AffineMaxOp operations in the context of scf.for |
| 122 | /// and scf.parallel loops with a known range. |
| 123 | template <typename OpTy> |
| 124 | struct AffineOpSCFCanonicalizationPattern : public OpRewritePattern<OpTy> { |
| 125 | using OpRewritePattern<OpTy>::OpRewritePattern; |
| 126 | |
| 127 | LogicalResult matchAndRewrite(OpTy op, |
| 128 | PatternRewriter &rewriter) const override { |
| 129 | return scf::canonicalizeMinMaxOpInLoop(rewriter, op, loopMatcher: scf::matchForLikeLoop); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | struct SCFForLoopCanonicalization |
| 134 | : public impl::SCFForLoopCanonicalizationBase<SCFForLoopCanonicalization> { |
| 135 | void runOnOperation() override { |
| 136 | auto *parentOp = getOperation(); |
| 137 | MLIRContext *ctx = parentOp->getContext(); |
| 138 | RewritePatternSet patterns(ctx); |
| 139 | scf::populateSCFForLoopCanonicalizationPatterns(patterns); |
| 140 | if (failed(Result: applyPatternsGreedily(op: parentOp, patterns: std::move(patterns)))) |
| 141 | signalPassFailure(); |
| 142 | } |
| 143 | }; |
| 144 | } // namespace |
| 145 | |
| 146 | void mlir::scf::populateSCFForLoopCanonicalizationPatterns( |
| 147 | RewritePatternSet &patterns) { |
| 148 | MLIRContext *ctx = patterns.getContext(); |
| 149 | patterns |
| 150 | .add<AffineOpSCFCanonicalizationPattern<affine::AffineMinOp>, |
| 151 | AffineOpSCFCanonicalizationPattern<affine::AffineMaxOp>, |
| 152 | DimOfIterArgFolder<tensor::DimOp>, DimOfIterArgFolder<memref::DimOp>, |
| 153 | DimOfLoopResultFolder<tensor::DimOp>, |
| 154 | DimOfLoopResultFolder<memref::DimOp>>(arg&: ctx); |
| 155 | } |
| 156 | |
| 157 | std::unique_ptr<Pass> mlir::createSCFForLoopCanonicalizationPass() { |
| 158 | return std::make_unique<SCFForLoopCanonicalization>(); |
| 159 | } |
| 160 | |