[flang-commits] [flang] 77e32a7 - [flang][cuda] Avoid false semantic error on unified array component (#190389)

via flang-commits flang-commits at lists.llvm.org
Fri Apr 3 14:13:34 PDT 2026


Author: Valentin Clement (バレンタイン クレメン)
Date: 2026-04-03T14:13:28-07:00
New Revision: 77e32a749f3eff940908afe4e2f44ab4199ce35e

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

LOG: [flang][cuda] Avoid false semantic error on unified array component (#190389)

Added: 
    

Modified: 
    flang/lib/Semantics/check-cuda.cpp
    flang/test/Semantics/cuf09.cuf

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-cuda.cpp b/flang/lib/Semantics/check-cuda.cpp
index 6be263c895792..d266e1939d1d9 100644
--- a/flang/lib/Semantics/check-cuda.cpp
+++ b/flang/lib/Semantics/check-cuda.cpp
@@ -115,6 +115,21 @@ struct DeviceExprChecker
   SemanticsContext &context_;
 };
 
+static bool IsHostArray(const Symbol &symbol) {
+  if (const auto *details{
+          symbol.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()}) {
+    if (details->cudaDataAttr() &&
+        (*details->cudaDataAttr() == common::CUDADataAttr::Device ||
+            *details->cudaDataAttr() == common::CUDADataAttr::Constant ||
+            *details->cudaDataAttr() == common::CUDADataAttr::Managed ||
+            *details->cudaDataAttr() == common::CUDADataAttr::Shared ||
+            *details->cudaDataAttr() == common::CUDADataAttr::Unified)) {
+      return false;
+    }
+  }
+  return true;
+}
+
 struct FindHostArray
     : public evaluate::AnyTraverse<FindHostArray, const Symbol *> {
   using Result = const Symbol *;
@@ -123,10 +138,25 @@ struct FindHostArray
   using Base::operator();
   Result operator()(const evaluate::Component &x) const {
     const Symbol &symbol{x.GetLastSymbol()};
+    if (symbol.IsFuncResult()) {
+      return nullptr;
+    }
+    if (!IsHostArray(symbol)) {
+      return nullptr;
+    }
     if (IsAllocatableOrPointer(symbol)) {
       if (Result hostArray{(*this)(symbol)}) {
         return hostArray;
       }
+    } else if (const auto *details{symbol.GetUltimate()
+                       .detailsIf<semantics::ObjectEntityDetails>()}) {
+      if (details->IsArray()) {
+        const Symbol &baseSymbol{x.base().GetFirstSymbol()};
+        if (!IsHostArray(baseSymbol)) {
+          return nullptr;
+        }
+        return &symbol;
+      }
     }
     return (*this)(x.base());
   }

diff  --git a/flang/test/Semantics/cuf09.cuf b/flang/test/Semantics/cuf09.cuf
index df6568df9b480..4bcb47af88e1f 100644
--- a/flang/test/Semantics/cuf09.cuf
+++ b/flang/test/Semantics/cuf09.cuf
@@ -1,8 +1,14 @@
 ! RUN: %python %S/test_errors.py %s %flang_fc1
 module m
- integer :: m(100)
- integer, constant :: c(10)
- integer, parameter :: p(5) = [1,2,3,4,5]
+  integer :: m(100)
+  integer, constant :: c(10)
+  integer, parameter :: p(5) = [1,2,3,4,5]
+  type :: t1
+    integer, allocatable, unified :: m(:)
+  end type
+  type :: t2
+    integer :: m(10)
+  end type
  contains
   attributes(device) subroutine devsub
     !ERROR: Statement may not appear in device code
@@ -127,8 +133,12 @@ module m
 end
 
 program main
+  use m
   integer, device :: a_d(10 ,10)
   integer :: b(10, 10)
+  type(t1) :: t1d
+  type(t2) :: t2d
+  type(t2), unified :: t2unified
   !$cuf kernel do <<< *, * >>> ! ok
   do j = 1, 0
   end do
@@ -212,6 +222,14 @@ program main
         a_d(i,j) = b(i,j)
      enddo
   enddo
+
+  !$cuf kernel do <<<*, *>>>
+  do j = 1, 10
+    t1d%m(j) = j
+    !ERROR: Host array 'm' cannot be present in device context
+    t2d%m(j) = j
+    t2unified%m(j) = j ! ok. t2unified is unified
+  end do
 end
 
 subroutine host1()


        


More information about the flang-commits mailing list