diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp --- a/flang/lib/Lower/Bridge.cpp +++ b/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 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; diff --git a/flang/test/Lower/HLFIR/function-return.f90 b/flang/test/Lower/HLFIR/function-return.f90 new file mode 100644 --- /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) -> (!fir.ref, !fir.ref) +! CHECK: %[[VAL_2:.*]] = arith.constant 42 : i32 +! CHECK: hlfir.assign %[[VAL_2]] to %[[VAL_1]]#0 : i32, !fir.ref +! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]]#1 : !fir.ref +! 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> +! CHECK: %[[VAL_2:.*]] = fir.convert %[[VAL_0]] : (!fir.ref>) -> !fir.ref> +! 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>, index) -> (!fir.boxchar<1>, !fir.ref>) +! CHECK: %[[VAL_8:.*]] = fir.emboxchar %[[VAL_4]]#1, %[[VAL_3]] : (!fir.ref>, 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.shape<1>) -> (!fir.ref>, !fir.ref>) +! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3]]#1 : !fir.ref> +! 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.shape<1>, index) -> (!fir.ref>>, !fir.ref>>) +! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_4]]#1 : !fir.ref>> +! CHECK: return %[[VAL_5]] : !fir.array<10x!fir.char<1,5>>