MLIR 22.0.0git
ConcatOpPatterns.cpp
Go to the documentation of this file.
1//===- ConcatOpPatterns.cpp - Patterns related to tensor.concat lowering --===//
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
12
13using namespace mlir;
14using namespace mlir::tensor;
15
16namespace {
17
18/// Decompose `tensor.concat` into `tensor.empty` and a chain of slice inserts.
19///
20/// %concat = tensor.concat dim(1) %0, %1 :
21/// (tensor<2x3xf32>, tensor<2x4xf32>) -> tensor<2x7xf32>
22///
23/// Becomes
24///
25/// %empty = tensor.empty() : tensor<2x7xf32>
26/// %insert0 = tensor.insert_slice %0 into %empty[0, 0][2, 3][1, 1]
27/// %concat = tensor.insert_slice %1 into %insert0[0, 3][2, 4][1, 1]
28struct DecomposeTensorConcatOp : public OpRewritePattern<ConcatOp> {
29 using OpRewritePattern<ConcatOp>::OpRewritePattern;
30
31 LogicalResult matchAndRewrite(ConcatOp concatOp,
32 PatternRewriter &rewriter) const override {
33 FailureOr<SmallVector<Value>> decomposed =
34 concatOp.decomposeOperation(rewriter);
35 if (failed(decomposed)) {
36 return rewriter.notifyMatchFailure(
37 concatOp, "failed to get the decomposed insert slices");
38 }
39 rewriter.replaceOp(concatOp, decomposed.value()[0]);
40 return success();
41 }
42};
43
44} // namespace
45
48 patterns.add<DecomposeTensorConcatOp>(patterns.getContext());
49}
return success()
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
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,...
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:561
void populateDecomposeTensorConcatPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that decompose tensor.concat into tensor.empty of a tensor of the co...
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...