[flang-commits] [flang] 1981116 - [flang] Lower function return to HLFIR

Jean Perier via flang-commits flang-commits at lists.llvm.org
Mon Dec 5 00:06:58 PST 2022


Author: Jean Perier
Date: 2022-12-05T09:06:32+01:00
New Revision: 19811165482d87582d31219305f946a77208a1f2

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

LOG: [flang] Lower function return to HLFIR

The only special thing that is needed is to update the bridge symbol
lookup to deal with the HLFIR symbol lookup (symbols are mapped to
fir::FortranVariableInterface operations, not Fortran::Lower::SymbolBox).

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

Added: 
    flang/test/Lower/HLFIR/function-return.f90

Modified: 
    flang/lib/Lower/Bridge.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 9dcff61c94f24..3e7e35a7c6afb 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -435,7 +435,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
 
   fir::ExtendedValue
   getSymbolExtendedValue(const Fortran::semantics::Symbol &sym) override final {
-    Fortran::lower::SymbolBox sb = localSymbols.lookupSymbol(sym);
+    Fortran::lower::SymbolBox sb = lookupSymbol(sym);
     assert(sb && "symbol box not found");
     return sb.toExtendedValue();
   }
@@ -838,6 +838,19 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   /// Find the symbol in the local map or return null.
   Fortran::lower::SymbolBox
   lookupSymbol(const Fortran::semantics::Symbol &sym) {
+    if (bridge.getLoweringOptions().getLowerToHighLevelFIR()) {
+      if (llvm::Optional<fir::FortranVariableOpInterface> var =
+              localSymbols.lookupVariableDefinition(sym)) {
+        auto exv =
+            hlfir::translateToExtendedValue(toLocation(), *builder, *var);
+        return exv.match(
+            [](mlir::Value x) -> Fortran::lower::SymbolBox {
+              return Fortran::lower::SymbolBox::Intrinsic{x};
+            },
+            [](auto x) -> Fortran::lower::SymbolBox { return x; });
+      }
+      return {};
+    }
     if (Fortran::lower::SymbolBox v = localSymbols.lookupSymbol(sym))
       return v;
     return {};
@@ -3202,8 +3215,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         // the reference to the host variable, which must be in the map.
         const Fortran::semantics::Symbol &ultimate = sym.GetUltimate();
         if (funit.parentHostAssoc().isAssociated(ultimate)) {
-          Fortran::lower::SymbolBox hostBox =
-              localSymbols.lookupSymbol(ultimate);
+          Fortran::lower::SymbolBox hostBox = lookupSymbol(ultimate);
           assert(hostBox && "host association is not in map");
           localSymbols.addSymbol(sym, hostBox.toExtendedValue());
           continue;

diff  --git a/flang/test/Lower/HLFIR/function-return.f90 b/flang/test/Lower/HLFIR/function-return.f90
new file mode 100644
index 0000000000000..dbabb0597ab64
--- /dev/null
+++ b/flang/test/Lower/HLFIR/function-return.f90
@@ -0,0 +1,44 @@
+! Test lowering of function return to HLFIR
+! RUN: bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s
+
+integer function simple_return()
+  simple_return = 42
+end function
+! CHECK-LABEL: func.func @_QPsimple_return() -> i32 {
+! CHECK:  %[[VAL_0:.*]] = fir.alloca i32
+! CHECK:  %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFsimple_returnEsimple_return"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+! CHECK:  %[[VAL_2:.*]] = arith.constant 42 : i32
+! CHECK:  hlfir.assign %[[VAL_2]] to %[[VAL_1]]#0 : i32, !fir.ref<i32>
+! CHECK:  %[[VAL_3:.*]] = fir.load %[[VAL_1]]#1 : !fir.ref<i32>
+! CHECK:  return %[[VAL_3]] : i32
+
+character(10) function char_return()
+  char_return = "hello"
+end function
+! CHECK-LABEL: func.func @_QPchar_return(
+! CHECK-SAME:  %[[VAL_0:.*]]: !fir.ref<!fir.char<1,10>>
+! CHECK:  %[[VAL_2:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<!fir.char<1,?>>
+! CHECK:  %[[VAL_3:.*]] = arith.constant 10 : index
+! CHECK:  %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_2]] typeparams %[[VAL_3]] {uniq_name = "_QFchar_returnEchar_return"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+! CHECK:  %[[VAL_8:.*]] = fir.emboxchar %[[VAL_4]]#1, %[[VAL_3]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:  return %[[VAL_8]] : !fir.boxchar<1>
+
+integer function array_return()
+  dimension :: array_return(10)
+  array_return = 42
+end function
+! CHECK-LABEL: func.func @_QParray_return() -> !fir.array<10xi32> {
+! CHECK:  %[[VAL_1:.*]] = fir.alloca !fir.array<10xi32>
+! CHECK:  %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_1]]{{.*}} {uniq_name = "_QFarray_returnEarray_return"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
+! CHECK:  %[[VAL_4:.*]] = fir.load %[[VAL_3]]#1 : !fir.ref<!fir.array<10xi32>>
+! CHECK:  return %[[VAL_4]] : !fir.array<10xi32>
+
+character(5) function char_array_return()
+  dimension :: char_array_return(10)
+  char_array_return = "hello"
+end function
+! CHECK-LABEL: func.func @_QPchar_array_return() -> !fir.array<10x!fir.char<1,5>> {
+! CHECK:  %[[VAL_2:.*]] = fir.alloca !fir.array<10x!fir.char<1,5>>
+! CHECK:  %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_2]]{{.*}} {uniq_name = "_QFchar_array_returnEchar_array_return"} : (!fir.ref<!fir.array<10x!fir.char<1,5>>>, !fir.shape<1>, index) -> (!fir.ref<!fir.array<10x!fir.char<1,5>>>, !fir.ref<!fir.array<10x!fir.char<1,5>>>)
+! CHECK:  %[[VAL_5:.*]] = fir.load %[[VAL_4]]#1 : !fir.ref<!fir.array<10x!fir.char<1,5>>>
+! CHECK:  return %[[VAL_5]] : !fir.array<10x!fir.char<1,5>>


        


More information about the flang-commits mailing list