|
5 | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | 6 |
|
7 | 7 | #include "iree/compiler/Codegen/Common/Transforms.h" |
| 8 | +#include "iree/compiler/Dialect/LinalgExt/IR/LinalgExtOps.h" |
8 | 9 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
9 | 10 | #include "mlir/Dialect/Utils/StaticValueUtils.h" |
10 | 11 |
|
11 | 12 | #define DEBUG_TYPE "iree-codegen-common-transforms" |
12 | 13 |
|
13 | 14 | namespace mlir::iree_compiler { |
14 | 15 |
|
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | +// Combining Layout Transformation Ops |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +/// Fold a tensor::ExpandShapeOp or tensor::CollapseShapeOp into a consumer |
| 21 | +/// `mapScatterOp`, by linearizing and then delinearizing the source indices |
| 22 | +/// of the `mapScatterOp`s index transformation. |
| 23 | +template <typename ReshapeOpTy> |
| 24 | +static IREE::LinalgExt::MapScatterOp |
| 25 | +foldReshapeIntoMapScatter(RewriterBase &rewriter, ReshapeOpTy reshapeOp, |
| 26 | + IREE::LinalgExt::MapScatterOp mapScatterOp) { |
| 27 | + assert(mapScatterOp.getInput() == reshapeOp->getResult(0) && |
| 28 | + "expected reshapeOp to be the producer of mapScatterOp"); |
| 29 | + Location loc = reshapeOp->getLoc(); |
| 30 | + OpBuilder::InsertionGuard g(rewriter); |
| 31 | + rewriter.setInsertionPointAfter(reshapeOp); |
| 32 | + SmallVector<OpFoldResult> srcDims = |
| 33 | + tensor::getMixedSizes(rewriter, loc, reshapeOp.getSrc()); |
| 34 | + // There can be leftover tensor.dim ops consuming the result of the reshape, |
| 35 | + // but they are expected to be folded into some affine.apply ops on the source |
| 36 | + // sizes by later cleanup patterns. |
| 37 | + SmallVector<OpFoldResult> resultDims = |
| 38 | + tensor::getMixedSizes(rewriter, loc, reshapeOp.getResult()); |
| 39 | + |
| 40 | + auto indexTransformBuilder = |
| 41 | + [&](ArrayRef<BlockArgument> srcIndices) -> SmallVector<Value> { |
| 42 | + auto linearizeIndexOp = rewriter.create<affine::AffineLinearizeIndexOp>( |
| 43 | + mapScatterOp->getLoc(), srcIndices, srcDims, /*disjoint=*/true); |
| 44 | + auto delinearizeIndexOp = rewriter.create<affine::AffineDelinearizeIndexOp>( |
| 45 | + mapScatterOp->getLoc(), linearizeIndexOp.getResult(), resultDims, |
| 46 | + /*hasOuterBound=*/true); |
| 47 | + return delinearizeIndexOp->getResults(); |
| 48 | + }; |
| 49 | + rewriter.modifyOpInPlace(mapScatterOp, [&]() { |
| 50 | + mapScatterOp.insertTransformationAtStart(rewriter, indexTransformBuilder, |
| 51 | + srcDims.size()); |
| 52 | + mapScatterOp.getInputMutable().assign(reshapeOp->getOperand(0)); |
| 53 | + }); |
| 54 | + return mapScatterOp; |
| 55 | +} |
| 56 | + |
| 57 | +IREE::LinalgExt::MapScatterOp |
| 58 | +foldExpandShapeIntoMapScatter(RewriterBase &rewriter, |
| 59 | + tensor::ExpandShapeOp expandShapeOp, |
| 60 | + IREE::LinalgExt::MapScatterOp mapScatterOp) { |
| 61 | + return foldReshapeIntoMapScatter(rewriter, expandShapeOp, mapScatterOp); |
| 62 | +} |
| 63 | + |
| 64 | +IREE::LinalgExt::MapScatterOp |
| 65 | +foldCollapseShapeIntoMapScatter(RewriterBase &rewriter, |
| 66 | + tensor::CollapseShapeOp collapseShapeOp, |
| 67 | + IREE::LinalgExt::MapScatterOp mapScatterOp) { |
| 68 | + return foldReshapeIntoMapScatter(rewriter, collapseShapeOp, mapScatterOp); |
| 69 | +} |
| 70 | + |
| 71 | +namespace { |
| 72 | + |
| 73 | +struct FoldExpandShapeIntoMapScatterPattern |
| 74 | + : public OpRewritePattern<IREE::LinalgExt::MapScatterOp> { |
| 75 | + using OpRewritePattern<IREE::LinalgExt::MapScatterOp>::OpRewritePattern; |
| 76 | + |
| 77 | + LogicalResult matchAndRewrite(IREE::LinalgExt::MapScatterOp mapScatterOp, |
| 78 | + PatternRewriter &rewriter) const override { |
| 79 | + auto expandOp = |
| 80 | + mapScatterOp.getInput().getDefiningOp<tensor::ExpandShapeOp>(); |
| 81 | + if (!expandOp) { |
| 82 | + return failure(); |
| 83 | + } |
| 84 | + (void)foldExpandShapeIntoMapScatter(rewriter, expandOp, mapScatterOp); |
| 85 | + return success(); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +struct FoldCollapseShapeIntoMapScatterPattern |
| 90 | + : public OpRewritePattern<IREE::LinalgExt::MapScatterOp> { |
| 91 | + using OpRewritePattern<IREE::LinalgExt::MapScatterOp>::OpRewritePattern; |
| 92 | + |
| 93 | + LogicalResult matchAndRewrite(IREE::LinalgExt::MapScatterOp mapScatterOp, |
| 94 | + PatternRewriter &rewriter) const override { |
| 95 | + auto collapseOp = |
| 96 | + mapScatterOp.getInput().getDefiningOp<tensor::CollapseShapeOp>(); |
| 97 | + if (!collapseOp) { |
| 98 | + return failure(); |
| 99 | + } |
| 100 | + (void)foldCollapseShapeIntoMapScatter(rewriter, collapseOp, mapScatterOp); |
| 101 | + return success(); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace |
| 106 | + |
| 107 | +void populateCombineRelayoutOpPatterns(RewritePatternSet &patterns) { |
| 108 | + patterns.add<FoldCollapseShapeIntoMapScatterPattern, |
| 109 | + FoldExpandShapeIntoMapScatterPattern>(patterns.getContext()); |
| 110 | +} |
| 111 | + |
15 | 112 | /// Converts `tensor.extract_slice(tensor.expand_shape)` to |
16 | 113 | /// `tensor.expand_shape(tensor.extract_slice)`. |
17 | 114 | /// For this transformation to be possible, the slice must be fully contiguous |
|
0 commit comments