MLIR  22.0.0git
BubbleDownMemorySpaceCasts.cpp
Go to the documentation of this file.
1 //===- BubbleDownMemorySpaceCasts.cpp - Bubble down casts transform -------===//
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"
12 #include "mlir/Pass/Pass.h"
14 #include "mlir/Transforms/Passes.h"
15 #include "llvm/Support/Debug.h"
16 
17 using namespace mlir;
18 
19 namespace mlir {
20 #define GEN_PASS_DEF_BUBBLEDOWNMEMORYSPACECASTS
21 #include "mlir/Transforms/Passes.h.inc"
22 } // namespace mlir
23 
24 namespace {
25 //===----------------------------------------------------------------------===//
26 // BubbleDownCastsPattern pattern
27 //===----------------------------------------------------------------------===//
28 /// Pattern to bubble down casts into consumer operations.
29 struct BubbleDownCastsPattern
30  : public OpInterfaceRewritePattern<MemorySpaceCastConsumerOpInterface> {
32 
33  LogicalResult matchAndRewrite(MemorySpaceCastConsumerOpInterface op,
34  PatternRewriter &rewriter) const override {
35  FailureOr<std::optional<SmallVector<Value>>> results =
36  op.bubbleDownCasts(rewriter);
37  if (failed(results))
38  return failure();
39  if (!results->has_value()) {
40  rewriter.modifyOpInPlace(op, []() {});
41  return success();
42  }
43  rewriter.replaceOp(op, **results);
44  return success();
45  }
46 };
47 
48 //===----------------------------------------------------------------------===//
49 // BubbleDownMemorySpaceCasts pass
50 //===----------------------------------------------------------------------===//
51 
52 struct BubbleDownMemorySpaceCasts
53  : public impl::BubbleDownMemorySpaceCastsBase<BubbleDownMemorySpaceCasts> {
54  using impl::BubbleDownMemorySpaceCastsBase<
55  BubbleDownMemorySpaceCasts>::BubbleDownMemorySpaceCastsBase;
56 
57  void runOnOperation() override {
60  if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
61  signalPassFailure();
62  }
63 };
64 } // namespace
65 
68  patterns.add<BubbleDownCastsPattern>(patterns.getContext(), benefit);
69 }
static MLIRContext * getContext(OpFoldResult val)
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
Definition: PatternMatch.h:34
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:793
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
Definition: PatternMatch.h:638
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
const FrozenRewritePatternSet & patterns
void populateBubbleDownMemorySpaceCastPatterns(RewritePatternSet &patterns, PatternBenefit benefit)
Collect a set of patterns to bubble-down memory-space cast operations.
OpInterfaceRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting a...
Definition: PatternMatch.h:333
OpInterfaceRewritePattern(MLIRContext *context, PatternBenefit benefit=1)
Definition: PatternMatch.h:338