MLIR  19.0.0git
SwapExtractSliceWithFillPatterns.cpp
Go to the documentation of this file.
1 //===- SwapExtractSliceWithFillPatterns.cpp -------------------------------===//
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 
10 #include "mlir/IR/PatternMatch.h"
11 
12 using namespace mlir;
13 using namespace mlir::linalg;
14 
15 /// Swaps tensor.extract_slice(linalg.fill(%cst, %init)) into linalg.fill(%cst,
16 /// tensor.extract_slice(%init)) when the linalg.fill op have no other users.
17 /// This helps to reduce the fill footprint.
19  : public OpRewritePattern<tensor::ExtractSliceOp> {
21 
22  LogicalResult matchAndRewrite(tensor::ExtractSliceOp extractOp,
23  PatternRewriter &rewriter) const override {
24  auto fillOp = extractOp.getSource().getDefiningOp<FillOp>();
25  if (!fillOp || !fillOp->hasOneUse())
26  return failure();
27 
28  auto newExtractOp = rewriter.create<tensor::ExtractSliceOp>(
29  extractOp.getLoc(), extractOp.getType(), fillOp.getOutputs()[0],
30  extractOp.getMixedOffsets(), extractOp.getMixedSizes(),
31  extractOp.getMixedStrides());
32  rewriter.replaceOpWithNewOp<FillOp>(extractOp, fillOp.getInputs(),
33  ValueRange{newExtractOp.getResult()});
34  return success();
35  }
36 };
37 
39  RewritePatternSet &patterns) {
40  patterns.add<SwapExtractSliceOfFill>(patterns.getContext());
41 }
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:464
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:785
MLIRContext * getContext() const
Definition: PatternMatch.h:822
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:846
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:536
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
void populateSwapExtractSliceWithFillPatterns(RewritePatternSet &patterns)
Adds patterns that waps tensor.extract_slice(linalg.fill(cst, init)) into linalg.fill(cst,...
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
Swaps tensor.extract_slice(linalg.fill(cst, init)) into linalg.fill(cst, tensor.extract_slice(init)) ...
LogicalResult matchAndRewrite(tensor::ExtractSliceOp extractOp, PatternRewriter &rewriter) const override
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:358
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...
Definition: PatternMatch.h:362