[flang-commits] [flang] [flang][hlfir] Return function call result as AsExpr. (PR #67769)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Sep 29 12:23:56 PDT 2023


================
@@ -1232,8 +1232,28 @@ genUserCall(Fortran::lower::PreparedActualArguments &loweredActuals,
 
   if (!fir::getBase(result))
     return std::nullopt; // subroutine call.
-  // TODO: "move" non pointer results into hlfir.expr.
-  return extendedValueToHlfirEntity(loc, builder, result, ".tmp.func_result");
+
+  hlfir::Entity resultEntity =
+      extendedValueToHlfirEntity(loc, builder, result, ".tmp.func_result");
+
+  if (!fir::isPointerType(fir::getBase(result).getType())) {
+    // Just load trivial scalars right away.
+    if (resultEntity.isScalar() &&
+        fir::isa_trivial(resultEntity.getFortranElementType()))
+      resultEntity = loadTrivialScalar(loc, builder, resultEntity);
+
+    if (resultEntity.isVariable()) {
+      // Function result must not be freed, since it is allocated on the stack.
----------------
vzakhari wrote:

Tom, it makes sense to use `hlfir.destroy` for the clean-up of the non-elemental result.  I will do it, when we get rid of the FIR path.  I hope using as_expr now should not result in incorrect MLIR optimizations.

https://github.com/llvm/llvm-project/pull/67769


More information about the flang-commits mailing list