| 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 | |
| 9 | #include "mlir/Transforms/BubbleDownMemorySpaceCasts.h" |
| 10 | #include "mlir/IR/PatternMatch.h" |
| 11 | #include "mlir/Interfaces/MemOpInterfaces.h" |
| 12 | #include "mlir/Pass/Pass.h" |
| 13 | #include "mlir/Transforms/GreedyPatternRewriteDriver.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> { |
| 31 | using Base::Base; |
| 32 | |
| 33 | LogicalResult matchAndRewrite(MemorySpaceCastConsumerOpInterface op, |
| 34 | PatternRewriter &rewriter) const override { |
| 35 | FailureOr<std::optional<SmallVector<Value>>> results = |
| 36 | op.bubbleDownCasts(builder&: rewriter); |
| 37 | if (failed(Result: results)) |
| 38 | return failure(); |
| 39 | if (!results->has_value()) { |
| 40 | rewriter.modifyOpInPlace(root: op, callable: []() {}); |
| 41 | return success(); |
| 42 | } |
| 43 | rewriter.replaceOp(op, newValues: **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 { |
| 58 | RewritePatternSet patterns(&getContext()); |
| 59 | populateBubbleDownMemorySpaceCastPatterns(patterns, benefit: PatternBenefit(1)); |
| 60 | if (failed(Result: applyPatternsGreedily(op: getOperation(), patterns: std::move(patterns)))) |
| 61 | signalPassFailure(); |
| 62 | } |
| 63 | }; |
| 64 | } // namespace |
| 65 | |
| 66 | void mlir::populateBubbleDownMemorySpaceCastPatterns( |
| 67 | RewritePatternSet &patterns, PatternBenefit benefit) { |
| 68 | patterns.add<BubbleDownCastsPattern>(arg: patterns.getContext(), args&: benefit); |
| 69 | } |
| 70 |