[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 08:56: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:

I think it should be possible to use `hlfir.destroy`, though, currently we destroy the function result as an in-memory entity (e.g. see https://github.com/llvm/llvm-project/pull/67768).  It may even be a potential correctness issue currently, e.g. if the `as_expr` is moved along with its use past the destroy call.  @jeanPerier, do you think we need to make the buffer live-range explicit here with `hlfir.as_exp`->`hlfir.destroy` def-use?

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


More information about the flang-commits mailing list