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

via flang-commits flang-commits at lists.llvm.org
Mon Oct 2 00:59:18 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.
----------------
jeanPerier wrote:

> 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?

Yes, it would be cleaner. As you mentioned, the clean-up is currently done on the memory that was available before it is moved, this probably does not cause any issue currently, but could if the life of hlfir.expr is prolongated for whatever reasons. I agree the change will be easier without having to deal with both FIR and HLFIR.

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


More information about the flang-commits mailing list