Skip to content

[flang][cuda] Allow assumed-size declaration for SHARED variable #130833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025

Conversation

clementval
Copy link
Contributor

Avoid triggering an assertion for shared variable using the assumed-size syntax.

attributes(global) subroutine sharedstar()
  real, shared :: s(*) ! ok. dynamic shared memory.
end subroutine

@clementval clementval requested a review from wangzpgi March 11, 2025 21:02
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels Mar 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-flang-fir-hlfir

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

Changes

Avoid triggering an assertion for shared variable using the assumed-size syntax.

attributes(global) subroutine sharedstar()
  real, shared :: s(*) ! ok. dynamic shared memory.
end subroutine

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

3 Files Affected:

  • (modified) flang/include/flang/Lower/BoxAnalyzer.h (+3-2)
  • (modified) flang/include/flang/Semantics/tools.h (+10)
  • (added) flang/test/Lower/CUDA/cuda-shared01.cuf (+9)
diff --git a/flang/include/flang/Lower/BoxAnalyzer.h b/flang/include/flang/Lower/BoxAnalyzer.h
index 8eca7d66a71bf..cd9037360a56b 100644
--- a/flang/include/flang/Lower/BoxAnalyzer.h
+++ b/flang/include/flang/Lower/BoxAnalyzer.h
@@ -403,8 +403,9 @@ class BoxAnalyzer : public fir::details::matcher<BoxAnalyzer> {
                 continue;
               }
             } else if (subs.ubound().isStar()) {
-              assert(Fortran::semantics::IsNamedConstant(sym) &&
-                     "expect implied shape constant");
+              assert(Fortran::semantics::IsNamedConstant(sym) ||
+                     Fortran::semantics::IsCUDAShared(sym) &&
+                         "expect implied shape constant");
               shapes.push_back(fir::SequenceType::getUnknownExtent());
               continue;
             }
diff --git a/flang/include/flang/Semantics/tools.h b/flang/include/flang/Semantics/tools.h
index 16fd8d158b0e0..31dfad098f3a7 100644
--- a/flang/include/flang/Semantics/tools.h
+++ b/flang/include/flang/Semantics/tools.h
@@ -222,6 +222,16 @@ inline bool HasCUDAAttr(const Symbol &sym) {
   return false;
 }
 
+inline bool IsCUDAShared(const Symbol &sym) {
+  if (const auto *details{sym.GetUltimate().detailsIf<ObjectEntityDetails>()}) {
+    if (details->cudaDataAttr() &&
+        *details->cudaDataAttr() == common::CUDADataAttr::Shared) {
+      return true;
+    }
+  }
+  return false;
+}
+
 inline bool NeedCUDAAlloc(const Symbol &sym) {
   if (IsDummy(sym)) {
     return false;
diff --git a/flang/test/Lower/CUDA/cuda-shared01.cuf b/flang/test/Lower/CUDA/cuda-shared01.cuf
new file mode 100644
index 0000000000000..a4603bed1f0ed
--- /dev/null
+++ b/flang/test/Lower/CUDA/cuda-shared01.cuf
@@ -0,0 +1,9 @@
+
+! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
+
+attributes(global) subroutine sharedstar()
+  real, shared :: s(*) ! ok. dynamic shared memory.
+end subroutine
+
+! CHECK-LABEL: func.func @_QPsharedstar()
+! CHECK: hlfir.declare %{{.*}}(%{{.*}}) {data_attr = #cuf.cuda<shared>, uniq_name = "_QFsharedstarEs"} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xf32>>, !fir.ref<!fir.array<?xf32>>)

@clementval clementval merged commit 369da84 into llvm:main Mar 13, 2025
15 checks passed
@clementval clementval deleted the cuf_share_assumed_size branch March 13, 2025 18:06
frederik-h pushed a commit to frederik-h/llvm-project that referenced this pull request Mar 18, 2025
…m#130833)

Avoid triggering an assertion for shared variable using the assumed-size
syntax.

```
attributes(global) subroutine sharedstar()
  real, shared :: s(*) ! ok. dynamic shared memory.
end subroutine
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants