| 1 | //===- OptimizeArrayRepacking.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 | |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | /// \file |
| 11 | /// This pass removes redundant fir.pack_array operations, if it can prove |
| 12 | /// that the source array is contiguous. In this case, it relink all uses |
| 13 | /// of fir.pack_array result to the source. If such a rewrite happens, |
| 14 | /// it may turn the using fir.unpack_array operation into one with the same |
| 15 | /// temp and original operands - these are also removed as redundant. |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "flang/Optimizer/Builder/HLFIRTools.h" |
| 18 | #include "flang/Optimizer/Dialect/FIRDialect.h" |
| 19 | #include "flang/Optimizer/Dialect/FIROps.h" |
| 20 | #include "flang/Optimizer/Support/Utils.h" |
| 21 | #include "flang/Optimizer/Transforms/Passes.h" |
| 22 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 23 | |
| 24 | namespace fir { |
| 25 | #define GEN_PASS_DEF_OPTIMIZEARRAYREPACKING |
| 26 | #include "flang/Optimizer/Transforms/Passes.h.inc" |
| 27 | } // namespace fir |
| 28 | |
| 29 | namespace { |
| 30 | class OptimizeArrayRepackingPass |
| 31 | : public fir::impl::OptimizeArrayRepackingBase<OptimizeArrayRepackingPass> { |
| 32 | public: |
| 33 | void runOnOperation() override; |
| 34 | }; |
| 35 | |
| 36 | /// Relinks all uses of redundant fir.pack_array to the source. |
| 37 | class PackingOfContiguous : public mlir::OpRewritePattern<fir::PackArrayOp> { |
| 38 | public: |
| 39 | using OpRewritePattern::OpRewritePattern; |
| 40 | mlir::LogicalResult matchAndRewrite(fir::PackArrayOp, |
| 41 | mlir::PatternRewriter &) const override; |
| 42 | }; |
| 43 | |
| 44 | /// Erases fir.unpack_array with have the matching temp and original |
| 45 | /// operands. |
| 46 | class NoopUnpacking : public mlir::OpRewritePattern<fir::UnpackArrayOp> { |
| 47 | public: |
| 48 | using OpRewritePattern::OpRewritePattern; |
| 49 | mlir::LogicalResult matchAndRewrite(fir::UnpackArrayOp, |
| 50 | mlir::PatternRewriter &) const override; |
| 51 | }; |
| 52 | } // namespace |
| 53 | |
| 54 | mlir::LogicalResult |
| 55 | PackingOfContiguous::matchAndRewrite(fir::PackArrayOp op, |
| 56 | mlir::PatternRewriter &rewriter) const { |
| 57 | mlir::Value box = op.getArray(); |
| 58 | if (hlfir::isSimplyContiguous(box, !op.getInnermost())) { |
| 59 | rewriter.replaceAllUsesWith(op, box); |
| 60 | rewriter.eraseOp(op); |
| 61 | return mlir::success(); |
| 62 | } |
| 63 | return mlir::failure(); |
| 64 | } |
| 65 | |
| 66 | mlir::LogicalResult |
| 67 | NoopUnpacking::matchAndRewrite(fir::UnpackArrayOp op, |
| 68 | mlir::PatternRewriter &rewriter) const { |
| 69 | if (op.getTemp() == op.getOriginal()) { |
| 70 | rewriter.eraseOp(op); |
| 71 | return mlir::success(); |
| 72 | } |
| 73 | return mlir::failure(); |
| 74 | } |
| 75 | |
| 76 | void OptimizeArrayRepackingPass::runOnOperation() { |
| 77 | mlir::func::FuncOp funcOp = getOperation(); |
| 78 | mlir::MLIRContext *context = &getContext(); |
| 79 | mlir::RewritePatternSet patterns(context); |
| 80 | mlir::GreedyRewriteConfig config; |
| 81 | config.setRegionSimplificationLevel( |
| 82 | mlir::GreedySimplifyRegionLevel::Disabled); |
| 83 | patterns.insert<PackingOfContiguous>(context); |
| 84 | patterns.insert<NoopUnpacking>(context); |
| 85 | if (mlir::failed( |
| 86 | mlir::applyPatternsGreedily(funcOp, std::move(patterns), config))) { |
| 87 | mlir::emitError(funcOp.getLoc(), "failure in array repacking optimization" ); |
| 88 | signalPassFailure(); |
| 89 | } |
| 90 | } |
| 91 | |