[flang-commits] [flang] [flang][cuda] Avoid triggering host array error in host device proc (PR #134909)

via flang-commits flang-commits at lists.llvm.org
Tue Apr 8 12:04:58 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Valentin Clement (バレンタイン クレメン) (clementval)

<details>
<summary>Changes</summary>

we cannot enforce the detection of host arrays in device code when the procedure is host, device. Relax the check for those. 

---
Full diff: https://github.com/llvm/llvm-project/pull/134909.diff


2 Files Affected:

- (modified) flang/lib/Semantics/check-cuda.cpp (+6) 
- (modified) flang/test/Semantics/cuf09.cuf (+7) 


``````````diff
diff --git a/flang/lib/Semantics/check-cuda.cpp b/flang/lib/Semantics/check-cuda.cpp
index dea170f7e099b..fd1ec2b2c69f8 100644
--- a/flang/lib/Semantics/check-cuda.cpp
+++ b/flang/lib/Semantics/check-cuda.cpp
@@ -261,6 +261,9 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
           subp->cudaSubprogramAttrs().value_or(
               common::CUDASubprogramAttrs::Host) !=
               common::CUDASubprogramAttrs::Host) {
+        isHostDevice = subp->cudaSubprogramAttrs() &&
+            subp->cudaSubprogramAttrs() ==
+                common::CUDASubprogramAttrs::HostDevice;
         Check(body);
       }
     }
@@ -357,6 +360,8 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
   }
   template <typename A>
   void ErrorIfHostSymbol(const A &expr, parser::CharBlock source) {
+    if (isHostDevice)
+      return;
     if (const Symbol * hostArray{FindHostArray{}(expr)}) {
       context_.Say(source,
           "Host array '%s' cannot be present in device context"_err_en_US,
@@ -502,6 +507,7 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
   }
 
   SemanticsContext &context_;
+  bool isHostDevice{false};
 };
 
 void CUDAChecker::Enter(const parser::SubroutineSubprogram &x) {
diff --git a/flang/test/Semantics/cuf09.cuf b/flang/test/Semantics/cuf09.cuf
index b59d02192f618..193b22213da61 100644
--- a/flang/test/Semantics/cuf09.cuf
+++ b/flang/test/Semantics/cuf09.cuf
@@ -221,3 +221,10 @@ subroutine ieee_test
     ll(i) = ieee_is_finite(y(i)) ! allow ieee_arithmetic functions on the device.
   end do
 end subroutine
+
+attributes(host,device) subroutine do2(a,b,c,i)
+  integer a(*), b(*), c(*)
+  integer, value :: i  
+  c(i) = a(i) - b(i) ! ok. Should not error with Host array 
+                     ! cannot be present in device context
+end

``````````

</details>


https://github.com/llvm/llvm-project/pull/134909


More information about the flang-commits mailing list