MLIR  22.0.0git
FusePadOpWithLinalgProducer.cpp
Go to the documentation of this file.
1 //===- FusePadOpWithLinalgProducer.cpp ---- Fuse pad with linalg producer -===//
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 implements patterns that fuses a linalg.generic -> tensor.pad op
10 // chain into a tensor.extract_slice -> linalg.generic -> tensor.insert_slice
11 // op chain.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 
18 
19 using namespace mlir;
20 
21 namespace {
22 
23 /// A sequence of operations
24 ///
25 /// ```mlir
26 /// %0 = linalg. ...
27 /// %1 = tensor.pad %0 ...
28 /// ```
29 ///
30 /// can be replaced with
31 ///
32 /// ```mlir
33 /// %0 = linalg.fill
34 /// %1 = tensor.extract_slice %0 ...
35 /// %2 = linalg. .... outs(..., %1, ....) ....
36 /// %3 = tensor.insert_slice %2 into %1 ...
37 /// ```
38 ///
39 /// if the `linalg.generic` has all parallel iterator types.
40 struct FusePadOp : OpRewritePattern<tensor::PadOp> {
42 
43  LogicalResult matchAndRewrite(tensor::PadOp padOp,
44  PatternRewriter &rewriter) const override {
45  // Only works on padding op that sets the padded value to a constant.
46  Value padValue = padOp.getConstantPaddingValue();
47  if (!padValue)
48  return rewriter.notifyMatchFailure(padOp, "non constant padding");
49 
50  // This pattern could work for any Linalg op. For now restrict it to generic
51  // ops.
52  Value source = padOp.getSource();
53  auto linalgOp = source.getDefiningOp<linalg::GenericOp>();
54  if (!linalgOp) {
55  return rewriter.notifyMatchFailure(
56  padOp, "expected source to be linalg.generic op");
57  }
58  // All iterator types need to be parallel.
59  if (linalgOp.getNumLoops() != linalgOp.getNumParallelLoops()) {
60  return rewriter.notifyMatchFailure(
61  padOp, "only supported for ops with all parallel iterator types");
62  }
63  ReifiedRankedShapedTypeDims resultShape;
64  if (failed(reifyResultShapes(rewriter, padOp, resultShape)) ||
65  resultShape.size() != 1) {
66  return rewriter.notifyMatchFailure(
67  padOp, "failed to get shape of pad op result");
68  }
69 
70  Location loc = padOp.getLoc();
71 
72  // Create the tensor of same size as output of the pad op.
73  RankedTensorType padResultType = padOp.getResultType();
74  auto resultSizes = resultShape[0];
75  auto emptyTensor = tensor::EmptyOp::create(rewriter, loc, resultSizes,
76  padResultType.getElementType());
77 
78  // Fill the tensor with the pad value.
79  // TODO: There is an option to fill only the boundaries. For now just
80  // filling the whole tensor.
81  auto fillTensor = linalg::FillOp::create(rewriter, loc, padValue,
82  emptyTensor.getResult());
83 
84  // Construct a slice of the fill result that is to be replaced with the
85  // result of the generic op. The low pad values are the offsets, the size of
86  // the source is the size of the slice.
87  // TODO: This insert/extract could be potentially made a utility method.
88  unsigned resultNumber = cast<OpResult>(source).getResultNumber();
89  SmallVector<OpFoldResult> offsets = padOp.getMixedLowPad();
91  sizes.reserve(offsets.size());
92  for (const auto &shape :
93  llvm::enumerate(cast<RankedTensorType>(source.getType()).getShape())) {
94  if (ShapedType::isDynamic(shape.value())) {
95  sizes.push_back(
96  tensor::DimOp::create(rewriter, loc, source, shape.index())
97  .getResult());
98  } else {
99  sizes.push_back(rewriter.getIndexAttr(shape.value()));
100  }
101  }
102  SmallVector<OpFoldResult> strides(offsets.size(), rewriter.getIndexAttr(1));
103  auto slice = tensor::ExtractSliceOp::create(
104  rewriter, loc, fillTensor.getResult(0), offsets, sizes, strides);
105 
106  // Clone the generic op.
107  auto clonedOp =
108  cast<linalg::GenericOp>(rewriter.clone(*linalgOp.getOperation()));
109  clonedOp.setDpsInitOperand(resultNumber, slice.getResult());
110 
111  // Insert it back into the result of the fill.
112  rewriter.replaceOpWithNewOp<tensor::InsertSliceOp>(
113  padOp, clonedOp.getResult(resultNumber), fillTensor.getResult(0),
114  offsets, sizes, strides);
115  return success();
116  }
117 };
118 } // namespace
119 
122  patterns.add<FusePadOp>(patterns.getContext());
123 }
IntegerAttr getIndexAttr(int64_t value)
Definition: Builders.cpp:103
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
Definition: Builders.cpp:548
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:783
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
Definition: PatternMatch.h:716
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Definition: PatternMatch.h:519
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Type getType() const
Return the type of this value.
Definition: Value.h:105
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition: Value.cpp:18
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:344
void populateFuseTensorPadWithProducerLinalgOpPatterns(RewritePatternSet &patterns)
Pattern to fuse a tensor.pad operation with the producer of its source, if the producer is a linalg o...
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
Include the generated interface declarations.
LogicalResult reifyResultShapes(OpBuilder &b, Operation *op, ReifiedRankedShapedTypeDims &reifiedReturnShapes)
Reify the shape of the result of an operation (typically in terms of the shape of its operands).
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:314