[flang-commits] [flang] a6f2f44 - [flang] Introduce `AbstractResultOnGlobalOpt` pass

Daniil Dudkin via flang-commits flang-commits at lists.llvm.org
Thu Aug 25 12:00:24 PDT 2022


Author: Daniil Dudkin
Date: 2022-08-25T21:58:59+03:00
New Revision: a6f2f44f9ca1540ce81655382015ab4bb78e1c0c

URL: https://github.com/llvm/llvm-project/commit/a6f2f44f9ca1540ce81655382015ab4bb78e1c0c
DIFF: https://github.com/llvm/llvm-project/commit/a6f2f44f9ca1540ce81655382015ab4bb78e1c0c.diff

LOG: [flang] Introduce `AbstractResultOnGlobalOpt` pass

This pass allows to convert operations
which use functions with abstract results to ones that do not.

Depends on D130087

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D130088

Added: 
    

Modified: 
    flang/include/flang/Optimizer/Transforms/Passes.h
    flang/include/flang/Optimizer/Transforms/Passes.td
    flang/include/flang/Tools/CLOptions.inc
    flang/lib/Optimizer/Transforms/AbstractResult.cpp
    flang/lib/Optimizer/Transforms/PassDetail.h
    flang/test/Driver/mlir-pass-pipeline.f90
    flang/test/Fir/abstract-results.fir
    flang/test/Fir/basic-program.fir

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Optimizer/Transforms/Passes.h b/flang/include/flang/Optimizer/Transforms/Passes.h
index 2835f3e310cd1..e44cba39c603f 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.h
+++ b/flang/include/flang/Optimizer/Transforms/Passes.h
@@ -9,6 +9,7 @@
 #ifndef FORTRAN_OPTIMIZER_TRANSFORMS_PASSES_H
 #define FORTRAN_OPTIMIZER_TRANSFORMS_PASSES_H
 
+#include "flang/Optimizer/Dialect/FIROps.h"
 #include "mlir/Pass/Pass.h"
 #include "mlir/Pass/PassRegistry.h"
 #include <memory>
@@ -28,6 +29,7 @@ namespace fir {
 //===----------------------------------------------------------------------===//
 
 std::unique_ptr<mlir::Pass> createAbstractResultOnFuncOptPass();
+std::unique_ptr<mlir::Pass> createAbstractResultOnGlobalOptPass();
 std::unique_ptr<mlir::Pass> createAffineDemotionPass();
 std::unique_ptr<mlir::Pass> createArrayValueCopyPass();
 std::unique_ptr<mlir::Pass> createFirToCfgPass();

diff  --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td
index cb0df9cd630c4..b35aa82b40e04 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.td
+++ b/flang/include/flang/Optimizer/Transforms/Passes.td
@@ -39,6 +39,10 @@ def AbstractResultOnFuncOpt : AbstractResultOptBase<"func", "mlir::func::FuncOp"
   let constructor = "::fir::createAbstractResultOnFuncOptPass()";
 }
 
+def AbstractResultOnGlobalOpt : AbstractResultOptBase<"global", "fir::GlobalOp"> {
+  let constructor = "::fir::createAbstractResultOnGlobalOptPass()";
+}
+
 def AffineDialectPromotion : Pass<"promote-to-affine", "::mlir::func::FuncOp"> {
   let summary = "Promotes `fir.{do_loop,if}` to `affine.{for,if}`.";
   let description = [{

diff  --git a/flang/include/flang/Tools/CLOptions.inc b/flang/include/flang/Tools/CLOptions.inc
index ec79902125f55..2c772030d33ba 100644
--- a/flang/include/flang/Tools/CLOptions.inc
+++ b/flang/include/flang/Tools/CLOptions.inc
@@ -195,6 +195,8 @@ inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm) {
   fir::addBoxedProcedurePass(pm);
   pm.addNestedPass<mlir::func::FuncOp>(
       fir::createAbstractResultOnFuncOptPass());
+  pm.addNestedPass<fir::GlobalOp>(
+      fir::createAbstractResultOnGlobalOptPass());
   fir::addCodeGenRewritePass(pm);
   fir::addTargetRewritePass(pm);
   fir::addExternalNameConversionPass(pm);

diff  --git a/flang/lib/Optimizer/Transforms/AbstractResult.cpp b/flang/lib/Optimizer/Transforms/AbstractResult.cpp
index 0a15ffd1af8b6..0ab65cfe7fd08 100644
--- a/flang/lib/Optimizer/Transforms/AbstractResult.cpp
+++ b/flang/lib/Optimizer/Transforms/AbstractResult.cpp
@@ -277,9 +277,39 @@ class AbstractResultOnFuncOpt
     }
   }
 };
+
+inline static bool containsFunctionTypeWithAbstractResult(mlir::Type type) {
+  return mlir::TypeSwitch<mlir::Type, bool>(type)
+      .Case([](fir::BoxProcType boxProc) {
+        return fir::hasAbstractResult(
+            boxProc.getEleTy().cast<mlir::FunctionType>());
+      })
+      .Case([](fir::PointerType pointer) {
+        return fir::hasAbstractResult(
+            pointer.getEleTy().cast<mlir::FunctionType>());
+      })
+      .Default([](auto &&) { return false; });
+}
+
+class AbstractResultOnGlobalOpt
+    : public AbstractResultOptTemplate<AbstractResultOnGlobalOpt,
+                                       fir::AbstractResultOnGlobalOptBase> {
+public:
+  void runOnSpecificOperation(fir::GlobalOp global, bool,
+                              mlir::RewritePatternSet &,
+                              mlir::ConversionTarget &) {
+    if (containsFunctionTypeWithAbstractResult(global.getType())) {
+      TODO(global->getLoc(), "support for procedure pointers");
+    }
+  }
+};
 } // end anonymous namespace
 } // namespace fir
 
 std::unique_ptr<mlir::Pass> fir::createAbstractResultOnFuncOptPass() {
   return std::make_unique<AbstractResultOnFuncOpt>();
 }
+
+std::unique_ptr<mlir::Pass> fir::createAbstractResultOnGlobalOptPass() {
+  return std::make_unique<AbstractResultOnGlobalOpt>();
+}

diff  --git a/flang/lib/Optimizer/Transforms/PassDetail.h b/flang/lib/Optimizer/Transforms/PassDetail.h
index e64951efbfa4e..c3e0db927aac3 100644
--- a/flang/lib/Optimizer/Transforms/PassDetail.h
+++ b/flang/lib/Optimizer/Transforms/PassDetail.h
@@ -9,6 +9,7 @@
 #define FORTRAN_OPTMIZER_TRANSFORMS_PASSDETAIL_H
 
 #include "flang/Optimizer/Dialect/FIRDialect.h"
+#include "flang/Optimizer/Dialect/FIROps.h"
 #include "mlir/Dialect/Affine/IR/AffineOps.h"
 #include "mlir/Dialect/Func/IR/FuncOps.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"

diff  --git a/flang/test/Driver/mlir-pass-pipeline.f90 b/flang/test/Driver/mlir-pass-pipeline.f90
index 17b9c0c495e7c..9ccbe31953349 100644
--- a/flang/test/Driver/mlir-pass-pipeline.f90
+++ b/flang/test/Driver/mlir-pass-pipeline.f90
@@ -52,8 +52,11 @@
 ! ALL-NEXT:   (S) 0 num-dce'd - Number of operations DCE'd
 ! ALL-NEXT: BoxedProcedurePass
 
-! ALL-NEXT: 'func.func' Pipeline
-! ALL-NEXT:   AbstractResultOnFuncOpt
+! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func']
+! ALL-NEXT:   'fir.global' Pipeline
+! ALL-NEXT:    AbstractResultOnGlobalOpt
+! ALL-NEXT:  'func.func' Pipeline
+! ALL-NEXT:    AbstractResultOnFuncOpt
 
 ! ALL-NEXT: CodeGenRewrite
 ! ALL-NEXT:   (S) 0 num-dce'd - Number of operations eliminated

diff  --git a/flang/test/Fir/abstract-results.fir b/flang/test/Fir/abstract-results.fir
index 30f22f3a342e3..580f7c6d22e13 100644
--- a/flang/test/Fir/abstract-results.fir
+++ b/flang/test/Fir/abstract-results.fir
@@ -3,6 +3,8 @@
 
 // RUN: fir-opt %s --abstract-result-on-func-opt | FileCheck %s --check-prefix=FUNC-REF
 // RUN: fir-opt %s --abstract-result-on-func-opt=abstract-result-as-box | FileCheck %s --check-prefix=FUNC-BOX
+// RUN: fir-opt %s --abstract-result-on-global-opt | FileCheck %s --check-prefix=GLOBAL-REF
+// RUN: fir-opt %s --abstract-result-on-global-opt=abstract-result-as-box | FileCheck %s --check-prefix=GLOBAL-BOX
 
 // ----------------------- Test declaration rewrite ----------------------------
 
@@ -253,3 +255,24 @@ func.func @test_indirect_calls(%arg0: () -> ()) {
   // FUNC-BOX: fir.call %[[conv]](%[[box]], %c100) : (!fir.box<!fir.array<?xf32>>, index) -> ()
   // FUNC-BOX-NOT: fir.save_result
 }
+
+// ----------------------- Test GlobalOp rewrite ------------------------
+
+// This is needed to separate GlobalOp tests from FuncOp tests for FileCheck
+// FUNC-REF-LABEL: fir.global {{.*}} : {{.*}} {
+// FUNC-BOX-LABEL: fir.global {{.*}} : {{.*}} {
+
+// GLOBAL-REF-LABEL: fir.global @global_test_address_of : i32 {
+// GLOBAL-BOX-LABEL: fir.global @global_test_address_of : i32 {
+fir.global @global_test_address_of : i32 {
+  %0 = fir.address_of(@arrayfunc) : (i32) -> !fir.array<?xf32>
+
+  // GLOBAL-REF: %[[addrOf:.*]] = fir.address_of(@arrayfunc) : (!fir.ref<!fir.array<?xf32>>, i32) -> ()
+  // GLOBAL-REF: %[[conv:.*]] = fir.convert %[[addrOf]] : ((!fir.ref<!fir.array<?xf32>>, i32) -> ()) -> ((i32) -> !fir.array<?xf32>)
+
+  // GLOBAL-BOX: %[[addrOf:.*]] = fir.address_of(@arrayfunc) : (!fir.box<!fir.array<?xf32>>, i32) -> ()
+  // GLOBAL-BOX: %[[conv:.*]] = fir.convert %[[addrOf]] : ((!fir.box<!fir.array<?xf32>>, i32) -> ()) -> ((i32) -> !fir.array<?xf32>)
+
+  %c42 = arith.constant 42 : i32
+  fir.has_value %c42 : i32
+}

diff  --git a/flang/test/Fir/basic-program.fir b/flang/test/Fir/basic-program.fir
index 94b264cf584c2..af7912ebcab20 100644
--- a/flang/test/Fir/basic-program.fir
+++ b/flang/test/Fir/basic-program.fir
@@ -52,8 +52,11 @@ func.func @_QQmain() {
 // PASSES-NEXT:   (S) 0 num-dce'd - Number of operations DCE'd
 // PASSES-NEXT: BoxedProcedurePass
 
-// PASSES-NEXT: 'func.func' Pipeline
-// PASSES-NEXT:   AbstractResultOnFuncOpt
+// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func']
+// PASSES-NEXT:   'fir.global' Pipeline
+// PASSES-NEXT:    AbstractResultOnGlobalOpt
+// PASSES-NEXT:  'func.func' Pipeline
+// PASSES-NEXT:    AbstractResultOnFuncOpt
 
 // PASSES-NEXT: CodeGenRewrite
 // PASSES-NEXT:   (S) 0 num-dce'd - Number of operations eliminated


        


More information about the flang-commits mailing list