[flang-commits] [PATCH] D139201: [flang] Lower function return to HLFIR

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Dec 2 11:06:31 PST 2022


jeanPerier updated this revision to Diff 479687.
jeanPerier added a comment.

Rebase so that  D139196 <https://reviews.llvm.org/D139196> fix is incorporated.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139201/new/

https://reviews.llvm.org/D139201

Files:
  flang/lib/Lower/Bridge.cpp
  flang/test/Lower/HLFIR/function-return.f90


Index: flang/test/Lower/HLFIR/function-return.f90
===================================================================
--- /dev/null
+++ 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>>
Index: flang/lib/Lower/Bridge.cpp
===================================================================
--- flang/lib/Lower/Bridge.cpp
+++ flang/lib/Lower/Bridge.cpp
@@ -435,7 +435,7 @@
 
   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 @@
   /// 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 @@
         // 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139201.479687.patch
Type: text/x-patch
Size: 4703 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221202/1cc73913/attachment-0001.bin>


More information about the flang-commits mailing list