[flang-commits] [flang] [flang][cuda] Do not use cuf.alloc for derived-type function results (PR #227863)
via flang-commits
flang-commits at lists.llvm.org
Wed Sep 30 13:48:06 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-fir-hlfir
Author: Valentin Clement (バレンタイン クレメン) (clementval)
<details>
<summary>Changes</summary>
Local variables of a derived type with device allocatable components
are allocated in managed memory with cuf.alloc/cuf.free. This also
applied to function results, but the storage of a derived-type function
result is replaced by the caller-provided buffer in the AbstractResult
pass. Allocating it with cuf.alloc is therefore incorrect, and the
matching cuf.free would release memory that is returned to the caller.
Skip function results in needCUDAAlloc so that they are lowered to a
regular fir.alloca. Explicit device/managed/shared/pinned attributes
on the symbol are still honored.
---
Full diff: https://github.com/llvm/llvm-project/pull/227863.diff
2 Files Affected:
- (modified) flang/lib/Lower/ConvertVariable.cpp (+4)
- (modified) flang/test/Lower/CUDA/cuda-derived.cuf (+19)
``````````diff
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 49ad3c3a1fa8eb..553032bb3909e7 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -754,6 +754,10 @@ bool needCUDAAlloc(const Fortran::semantics::Symbol &sym) {
*details->cudaDataAttr() == Fortran::common::CUDADataAttr::Shared ||
*details->cudaDataAttr() == Fortran::common::CUDADataAttr::Pinned))
return true;
+ // Derived-type function result storage is replaced by the caller-provided
+ // buffer in the AbstractResult pass, so it must not be CUDA allocated.
+ if (Fortran::semantics::IsFunctionResult(sym))
+ return false;
const Fortran::semantics::DeclTypeSpec *type{details->type()};
const Fortran::semantics::DerivedTypeSpec *derived{type ? type->AsDerived()
: nullptr};
diff --git a/flang/test/Lower/CUDA/cuda-derived.cuf b/flang/test/Lower/CUDA/cuda-derived.cuf
index 6fb5b997c095d9..e25bdcfd6bee89 100644
--- a/flang/test/Lower/CUDA/cuda-derived.cuf
+++ b/flang/test/Lower/CUDA/cuda-derived.cuf
@@ -21,6 +21,25 @@ contains
! CHECK-NEXT: cuf.free %[[DECL]]#0 : !fir.ref<!fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {data_attr = #cuf.cuda<managed>}
! CHECK-NEXT: }
+ function mk() result(r)
+ type(ty_device) :: r
+ end function
+
+! CHECK-LABEL: func.func @_QMm1Pmk()
+! CHECK: %[[ALLOC:.*]] = fir.alloca !fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}> <{bindc_name = "r", uniq_name = "_QMm1FmkEr"}>
+! CHECK-NOT: cuf.alloc
+! CHECK-NOT: cuf.free
+! CHECK: return
+
+ function mkarr() result(r)
+ type(ty_device) :: r(2)
+ end function
+
+! CHECK-LABEL: func.func @_QMm1Pmkarr()
+! CHECK-NOT: cuf.alloc
+! CHECK-NOT: cuf.free
+! CHECK: return
+
end module
program main
``````````
</details>
https://github.com/llvm/llvm-project/pull/227863
More information about the flang-commits
mailing list