Skip to content

[flang][cuda] Implicitly add DEVICE attribute in device/global functions #119743

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 2 commits into from
Dec 12, 2024

Conversation

clementval
Copy link
Contributor

Variables in global and device function/subroutine that have no CUDA Fortran data attribute are implicitly DEVICE.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Dec 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2024

@llvm/pr-subscribers-flang-semantics

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

Changes

Variables in global and device function/subroutine that have no CUDA Fortran data attribute are implicitly DEVICE.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+19)
  • (modified) flang/test/Semantics/modfile55.cuf (+1)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index b576f59e8c7e52..a38c86e683b6b3 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -8953,6 +8953,18 @@ void ResolveNamesVisitor::FinishSpecificationPart(
   misparsedStmtFuncFound_ = false;
   funcResultStack().CompleteFunctionResultType();
   CheckImports();
+  bool inDeviceSubprogram = false;
+  if (auto *subp{currScope().symbol()
+              ? currScope().symbol()->detailsIf<SubprogramDetails>()
+              : nullptr}) {
+    if (auto attrs{subp->cudaSubprogramAttrs()}) {
+      if (*attrs != common::CUDASubprogramAttrs::Device ||
+          *attrs != common::CUDASubprogramAttrs::Global ||
+          *attrs != common::CUDASubprogramAttrs::Grid_Global) {
+        inDeviceSubprogram = true;
+      }
+    }
+  }
   for (auto &pair : currScope()) {
     auto &symbol{*pair.second};
     if (inInterfaceBlock()) {
@@ -8961,6 +8973,13 @@ void ResolveNamesVisitor::FinishSpecificationPart(
     if (NeedsExplicitType(symbol)) {
       ApplyImplicitRules(symbol);
     }
+    if (inDeviceSubprogram && IsDummy(symbol) && symbol.has<ObjectEntityDetails>()) {
+      auto *dummy{symbol.detailsIf<ObjectEntityDetails>()};
+      if (!dummy->cudaDataAttr()) {
+        // Implicitly set device attribute if none is set in device context.
+        dummy->set_cudaDataAttr(common::CUDADataAttr::Device);
+      }
+    }
     if (IsDummy(symbol) && isImplicitNoneType() &&
         symbol.test(Symbol::Flag::Implicit) && !context().HasError(symbol)) {
       Say(symbol.name(),
diff --git a/flang/test/Semantics/modfile55.cuf b/flang/test/Semantics/modfile55.cuf
index cf01bdd5f58f6f..6c0d152a382a88 100644
--- a/flang/test/Semantics/modfile55.cuf
+++ b/flang/test/Semantics/modfile55.cuf
@@ -29,6 +29,7 @@ end
 !contains
 !attributes(global) subroutine globsub(x,y,z)
 !real(4),value::x
+!attributes(device) x
 !real(4)::y
 !attributes(device) y
 !real(4)::z

Copy link

github-actions bot commented Dec 12, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@clementval clementval merged commit 7141837 into llvm:main Dec 12, 2024
8 checks passed
@clementval clementval deleted the cuf_implicit branch December 12, 2024 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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