brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · 68f5b5a Raw
98 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9//===----------------------------------------------------------------------===//10/// \file11/// This pass removes redundant fir.pack_array operations, if it can prove12/// that the source array is contiguous. In this case, it relink all uses13/// 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 same15/// 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 24namespace fir {25#define GEN_PASS_DEF_OPTIMIZEARRAYREPACKING26#include "flang/Optimizer/Transforms/Passes.h.inc"27} // namespace fir28 29#define DEBUG_TYPE "optimize-array-repacking"30 31namespace {32class OptimizeArrayRepackingPass33    : public fir::impl::OptimizeArrayRepackingBase<OptimizeArrayRepackingPass> {34public:35  void runOnOperation() override;36};37 38/// Relinks all uses of redundant fir.pack_array to the source.39class PackingOfContiguous : public mlir::OpRewritePattern<fir::PackArrayOp> {40public:41  using OpRewritePattern::OpRewritePattern;42  mlir::LogicalResult matchAndRewrite(fir::PackArrayOp,43                                      mlir::PatternRewriter &) const override;44};45 46/// Erases fir.unpack_array with have the matching temp and original47/// operands.48class NoopUnpacking : public mlir::OpRewritePattern<fir::UnpackArrayOp> {49public:50  using OpRewritePattern::OpRewritePattern;51  mlir::LogicalResult matchAndRewrite(fir::UnpackArrayOp,52                                      mlir::PatternRewriter &) const override;53};54} // namespace55 56mlir::LogicalResult57PackingOfContiguous::matchAndRewrite(fir::PackArrayOp op,58                                     mlir::PatternRewriter &rewriter) const {59  mlir::Value box = op.getArray();60  if (hlfir::isSimplyContiguous(box, !op.getInnermost())) {61    rewriter.replaceOp(op, box);62    return mlir::success();63  }64  return mlir::failure();65}66 67mlir::LogicalResult68NoopUnpacking::matchAndRewrite(fir::UnpackArrayOp op,69                               mlir::PatternRewriter &rewriter) const {70  if (op.getTemp() == op.getOriginal()) {71    rewriter.eraseOp(op);72    return mlir::success();73  }74  return mlir::failure();75}76 77void OptimizeArrayRepackingPass::runOnOperation() {78  mlir::func::FuncOp funcOp = getOperation();79  mlir::MLIRContext *context = &getContext();80  mlir::RewritePatternSet patterns(context);81  mlir::GreedyRewriteConfig config;82  config83      .setRegionSimplificationLevel(mlir::GreedySimplifyRegionLevel::Disabled)84      // Traverse the operations top-down, so that fir.pack_array85      // operations are optimized before their using fir.pack_array86      // operations. This way the rewrite may converge faster.87      .setUseTopDownTraversal();88  patterns.insert<PackingOfContiguous>(context);89  patterns.insert<NoopUnpacking>(context);90  if (mlir::failed(91          mlir::applyPatternsGreedily(funcOp, std::move(patterns), config))) {92    // Failure may happen if the rewriter does not converge soon enough.93    // That is not an error, so just report a diagnostic under debug.94    LLVM_DEBUG(mlir::emitError(funcOp.getLoc(),95                               "failure in array repacking optimization"));96  }97}98