[flang-commits] [flang] b4643ba - [flang][cuda] Do not use cuf.alloc for derived-type function results (#227863)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 30 15:46:10 PDT 2026


Author: Valentin Clement (バレンタイン クレメン)
Date: 2026-09-30T15:45:59-07:00
New Revision: b4643ba5638f1b906a39120b1f48164aa879089f

URL: https://github.com/llvm/llvm-project/commit/b4643ba5638f1b906a39120b1f48164aa879089f
DIFF: https://github.com/llvm/llvm-project/commit/b4643ba5638f1b906a39120b1f48164aa879089f.diff

LOG: [flang][cuda] Do not use cuf.alloc for derived-type function results (#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.

Added: 
    

Modified: 
    flang/lib/Lower/ConvertVariable.cpp
    flang/test/Lower/CUDA/cuda-derived.cuf

Removed: 
    


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