[flang] Introduce option to lower expression to HLFIR
authorJean Perier <jperier@nvidia.com>
Mon, 17 Oct 2022 07:57:16 +0000 (09:57 +0200)
committerJean Perier <jperier@nvidia.com>
Mon, 17 Oct 2022 08:02:56 +0000 (10:02 +0200)
Preliminary work on HLFIR. Introduce option that will allow testing
lowering via HLFIR until this is ready to replace the current expression
lowering.

See https://reviews.llvm.org/D134285 for more context about the plan.

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

flang/include/flang/Lower/AbstractConverter.h
flang/include/flang/Lower/LoweringOptions.h
flang/lib/Lower/Bridge.cpp
flang/test/Lower/HLFIR/expr-addr.f90 [new file with mode: 0644]
flang/test/Lower/HLFIR/expr-box.f90 [new file with mode: 0644]
flang/test/Lower/HLFIR/expr-value.f90 [new file with mode: 0644]
flang/tools/bbc/bbc.cpp

index 6ab649ccf020a6229b9297ca7357a4f4534b3375..a84e34f4bd8a4a3de783ba902b4bb95dcc7508ae 100644 (file)
@@ -128,7 +128,7 @@ public:
   /// expression value. The clean-up for this temporary is added to \p context.
   virtual fir::ExtendedValue genExprAddr(const SomeExpr &expr,
                                          StatementContext &context,
-                                         mlir::Location *loc = nullptr) = 0;
+                                         mlir::Location *locPtr = nullptr) = 0;
 
   /// Generate the address of the location holding the expression, \p expr.
   fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr *expr,
@@ -143,7 +143,7 @@ public:
   /// Generate the computations of the expression to produce a value.
   virtual fir::ExtendedValue genExprValue(const SomeExpr &expr,
                                           StatementContext &context,
-                                          mlir::Location *loc = nullptr) = 0;
+                                          mlir::Location *locPtr = nullptr) = 0;
 
   /// Generate the computations of the expression, \p expr, to produce a value.
   fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr *expr,
index 58736568362dbdd6506ea3cd01ba45189f59209c..d882ff0fb233a4bd4504bb342dcde33decd9179e 100644 (file)
@@ -24,8 +24,14 @@ class LoweringOptions {
   /// If true, enable polymorphic type lowering feature. Off by default.
   unsigned polymorphicTypeImpl : 1;
 
+  /// If true, lower to High level FIR before lowering to FIR.
+  /// Off by default until fully ready.
+  unsigned lowerToHighLevelFIR : 1;
+
 public:
-  LoweringOptions() : optimizeTranspose(true), polymorphicTypeImpl(false) {}
+  LoweringOptions()
+      : optimizeTranspose(true), polymorphicTypeImpl(false),
+        lowerToHighLevelFIR(false) {}
 
   bool getOptimizeTranspose() const { return optimizeTranspose; }
   LoweringOptions &setOptimizeTranspose(bool v) {
@@ -38,6 +44,12 @@ public:
     polymorphicTypeImpl = v;
     return *this;
   }
+
+  bool getLowerToHighLevelFIR() const { return lowerToHighLevelFIR; }
+  LoweringOptions &setLowerToHighLevelFIR(bool v) {
+    lowerToHighLevelFIR = v;
+    return *this;
+  }
 };
 
 } // namespace Fortran::lower
index b40dfd41b5c291a3a473ac086230a64890df08a1..ac29207e31a43b6b43bde915186e183166b01ba2 100644 (file)
@@ -416,23 +416,32 @@ public:
     return iter->second;
   }
 
-  fir::ExtendedValue genExprAddr(const Fortran::lower::SomeExpr &expr,
-                                 Fortran::lower::StatementContext &context,
-                                 mlir::Location *loc = nullptr) override final {
-    return Fortran::lower::createSomeExtendedAddress(
-        loc ? *loc : toLocation(), *this, expr, localSymbols, context);
+  fir::ExtendedValue
+  genExprAddr(const Fortran::lower::SomeExpr &expr,
+              Fortran::lower::StatementContext &context,
+              mlir::Location *locPtr = nullptr) override final {
+    mlir::Location loc = locPtr ? *locPtr : toLocation();
+    if (bridge.getLoweringOptions().getLowerToHighLevelFIR())
+      TODO(loc, "lower expr to HLFIR address");
+    return Fortran::lower::createSomeExtendedAddress(loc, *this, expr,
+                                                     localSymbols, context);
   }
   fir::ExtendedValue
   genExprValue(const Fortran::lower::SomeExpr &expr,
                Fortran::lower::StatementContext &context,
-               mlir::Location *loc = nullptr) override final {
-    return Fortran::lower::createSomeExtendedExpression(
-        loc ? *loc : toLocation(), *this, expr, localSymbols, context);
+               mlir::Location *locPtr = nullptr) override final {
+    mlir::Location loc = locPtr ? *locPtr : toLocation();
+    if (bridge.getLoweringOptions().getLowerToHighLevelFIR())
+      TODO(loc, "lower expr to HLFIR value");
+    return Fortran::lower::createSomeExtendedExpression(loc, *this, expr,
+                                                        localSymbols, context);
   }
 
   fir::ExtendedValue
   genExprBox(mlir::Location loc, const Fortran::lower::SomeExpr &expr,
              Fortran::lower::StatementContext &stmtCtx) override final {
+    if (bridge.getLoweringOptions().getLowerToHighLevelFIR())
+      TODO(loc, "lower expr to HLFIR box");
     return Fortran::lower::createBoxValue(loc, *this, expr, localSymbols,
                                           stmtCtx);
   }
diff --git a/flang/test/Lower/HLFIR/expr-addr.f90 b/flang/test/Lower/HLFIR/expr-addr.f90
new file mode 100644 (file)
index 0000000..1af59e6
--- /dev/null
@@ -0,0 +1,8 @@
+! Test lowering of of expressions as address
+! RUN: %not_todo_cmd bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s
+
+subroutine foo(x)
+  integer :: x
+  ! CHECK: not yet implemented: lower expr to HLFIR address
+  read (*,*) x
+end subroutine
diff --git a/flang/test/Lower/HLFIR/expr-box.f90 b/flang/test/Lower/HLFIR/expr-box.f90
new file mode 100644 (file)
index 0000000..d011d79
--- /dev/null
@@ -0,0 +1,8 @@
+! Test lowering of of expressions as fir.box
+! RUN: %not_todo_cmd bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s
+
+subroutine foo(x)
+  integer :: x(:)
+  ! CHECK: not yet implemented: lower expr to HLFIR box
+  print *, x 
+end subroutine
diff --git a/flang/test/Lower/HLFIR/expr-value.f90 b/flang/test/Lower/HLFIR/expr-value.f90
new file mode 100644 (file)
index 0000000..0a993bc
--- /dev/null
@@ -0,0 +1,7 @@
+! Test lowering of of expressions as values
+! RUN: %not_todo_cmd bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s
+
+subroutine foo()
+  ! CHECK: not yet implemented: lower expr to HLFIR value
+  print *, 42 
+end subroutine
index bd40c9af5f89427d29ddcd98a30af2aeac474c1e..796e7faa288077e18a155c3e4e89d6f9c3872b6d 100644 (file)
@@ -131,6 +131,10 @@ static llvm::cl::opt<bool> enablePolymorphic(
     llvm::cl::desc("enable polymorphic type lowering (experimental)"),
     llvm::cl::init(false));
 
+static llvm::cl::opt<bool> useHLFIR("hlfir",
+                                    llvm::cl::desc("Lower to high level FIR"),
+                                    llvm::cl::init(false));
+
 #define FLANG_EXCLUDE_CODEGEN
 #include "flang/Tools/CLOptions.inc"
 
@@ -227,6 +231,7 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
   // Use default lowering options for bbc.
   Fortran::lower::LoweringOptions loweringOptions{};
   loweringOptions.setPolymorphicTypeImpl(enablePolymorphic);
+  loweringOptions.setLowerToHighLevelFIR(useHLFIR);
   auto burnside = Fortran::lower::LoweringBridge::create(
       ctx, semanticsContext, defKinds, semanticsContext.intrinsics(),
       semanticsContext.targetCharacteristics(), parsing.allCooked(), "",