[flang-commits] [flang] [flang][cuda] Do not use cuf.alloc for derived-type function results (PR #227863)
Valentin Clement バレンタイン クレメン via flang-commits
flang-commits at lists.llvm.org
Wed Sep 30 13:47:19 PDT 2026
https://github.com/clementval created https://github.com/llvm/llvm-project/pull/227863
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.
>From d4614b3e1402a68a399aab6a346e7bf870a1608a Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Wed, 30 Sep 2026 13:45:47 -0700
Subject: [PATCH] [flang][cuda] Do not use cuf.alloc for derived-type function
results
---
flang/lib/Lower/ConvertVariable.cpp | 4 ++++
flang/test/Lower/CUDA/cuda-derived.cuf | 19 +++++++++++++++++++
2 files changed, 23 insertions(+)
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 49ad3c3a1fa8e..553032bb3909e 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 6fb5b997c095d..e25bdcfd6bee8 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
More information about the flang-commits
mailing list