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

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Fri Apr 3 12:10:01 PDT 2026


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/190389

None

>From cea17cc46aa56eebf258eb36c58e3f6f9941c9c0 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Fri, 3 Apr 2026 12:08:33 -0700
Subject: [PATCH] [flang][cuda] Avoid false semantic error on unified array
 component

---
 flang/lib/Semantics/check-cuda.cpp | 30 ++++++++++++++++++++++++++++++
 flang/test/Semantics/cuf09.cuf     | 24 +++++++++++++++++++++---
 2 files changed, 51 insertions(+), 3 deletions(-)

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