[flang-commits] [flang] [flang][cuda] Detect illegal data transfer in semantic (PR #125591)

via flang-commits flang-commits at lists.llvm.org
Mon Feb 3 14:03:01 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

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

<details>
<summary>Changes</summary>

When the LHS is a device variable and the RHS has implicit transfer, this is considered as an illegal transfer according to https://docs.nvidia.com/hpc-sdk/compilers/cuda-fortran-prog-guide/index.html#implicit-data-transfer-in-expressions. 

Detect this is semantic . 

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


2 Files Affected:

- (modified) flang/lib/Semantics/assignment.cpp (+11) 
- (added) flang/test/Semantics/cuf18.cuf (+11) 


``````````diff
diff --git a/flang/lib/Semantics/assignment.cpp b/flang/lib/Semantics/assignment.cpp
index 0b57197fb8db8b..2b562571a679ee 100644
--- a/flang/lib/Semantics/assignment.cpp
+++ b/flang/lib/Semantics/assignment.cpp
@@ -90,6 +90,17 @@ void AssignmentContext::Analyze(const parser::AssignmentStmt &stmt) {
     if (whereDepth_ > 0) {
       CheckShape(lhsLoc, &lhs);
     }
+    if (context_.foldingContext().languageFeatures().IsEnabled(
+            common::LanguageFeature::CUDA)) {
+      const auto &scope{context_.FindScope(lhsLoc)};
+      const Scope &progUnit{GetProgramUnitContaining(scope)};
+      if (!IsCUDADeviceContext(&progUnit)) {
+        if (Fortran::evaluate::HasCUDADeviceAttrs(lhs) &&
+            Fortran::evaluate::HasCUDAImplicitTransfer(rhs)) {
+          context_.Say(lhsLoc, "Unsupported CUDA data transfer"_err_en_US);
+        }
+      }
+    }
   }
 }
 
diff --git a/flang/test/Semantics/cuf18.cuf b/flang/test/Semantics/cuf18.cuf
new file mode 100644
index 00000000000000..ce9a2a31ca0d15
--- /dev/null
+++ b/flang/test/Semantics/cuf18.cuf
@@ -0,0 +1,11 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+subroutine sub1()
+  real, allocatable, device :: a(:)
+
+!ERROR: Unsupported CUDA data transfer
+  a = a + 10 ! Illegal expression according to 3.4.2
+end subroutine
+
+
+

``````````

</details>


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


More information about the flang-commits mailing list