diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp index 044c2e775a6a7..a45fed2a36798 100644 --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -19,6 +19,7 @@ #include "flang/Optimizer/Dialect/FIRType.h" #include "flang/Optimizer/Support/FIRContext.h" #include "mlir/Transforms/DialectConversion.h" +#include "mlir/Transforms/RegionUtils.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/Debug.h" @@ -284,7 +285,6 @@ class CodeGenRewrite : public fir::impl::CodeGenRewriteBase { public: void runOn(mlir::Operation *op, mlir::Region ®ion) { auto &context = getContext(); - mlir::OpBuilder rewriter(&context); mlir::ConversionTarget target(context); target.addLegalDialect(); @@ -305,9 +305,11 @@ class CodeGenRewrite : public fir::impl::CodeGenRewriteBase { mlir::emitError(mlir::UnknownLoc::get(&context), "error in running the pre-codegen conversions"); signalPassFailure(); + return; } - // Erase any residual. - simplifyRegion(region); + // Erase any residual (fir.shape, fir.slice...). + mlir::IRRewriter rewriter(&context); + (void)mlir::runRegionDCE(rewriter, op->getRegions()); } void runOnOperation() override final { @@ -318,48 +320,6 @@ class CodeGenRewrite : public fir::impl::CodeGenRewriteBase { for (auto global : mod.getOps()) runOn(global, global.getRegion()); } - - // Clean up the region. - void simplifyRegion(mlir::Region ®ion) { - for (auto &block : region.getBlocks()) - for (auto &op : block.getOperations()) { - for (auto ® : op.getRegions()) - simplifyRegion(reg); - maybeEraseOp(&op); - } - doDCE(); - } - - /// Run a simple DCE cleanup to remove any dead code after the rewrites. - void doDCE() { - std::vector workList; - workList.swap(opsToErase); - while (!workList.empty()) { - for (auto *op : workList) { - std::vector opOperands(op->operand_begin(), - op->operand_end()); - LLVM_DEBUG(llvm::dbgs() << "DCE on " << *op << '\n'); - ++numDCE; - op->erase(); - for (auto opnd : opOperands) - maybeEraseOp(opnd.getDefiningOp()); - } - workList.clear(); - workList.swap(opsToErase); - } - } - - void maybeEraseOp(mlir::Operation *op) { - if (!op) - return; - if (op->hasTrait()) - return; - if (mlir::isOpTriviallyDead(op)) - opsToErase.push_back(op); - } - -private: - std::vector opsToErase; }; } // namespace diff --git a/flang/test/Fir/declare-codegen.fir b/flang/test/Fir/declare-codegen.fir index 1b0bea88e9cff..9d68d3b2f9d4d 100644 --- a/flang/test/Fir/declare-codegen.fir +++ b/flang/test/Fir/declare-codegen.fir @@ -18,3 +18,13 @@ func.func private @bar(%arg0: !fir.ref>) // CHECK-LABEL: func.func @test( // CHECK-SAME: %[[arg0:.*]]: !fir.ref>) { // CHECK-NEXT: fir.call @bar(%[[arg0]]) : (!fir.ref>) -> () + +func.func @useless_shape_with_duplicate_extent_operand(%arg0: !fir.ref>) { + %c3 = arith.constant 3 : index + %1 = fir.shape %c3, %c3 : (index, index) -> !fir.shape<2> + %2 = fir.declare %arg0(%1) {uniq_name = "u"} : (!fir.ref>, !fir.shape<2>) -> !fir.ref> + return +} + +// CHECK-LABEL: func.func @useless_shape_with_duplicate_extent_operand( +// CHECK-NEXT: return