Skip to content

[flang] [cuda] implicitly set DEVICE attribute to scalars in device routines #140834

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 5 commits into from
May 21, 2025

Conversation

wangzpgi
Copy link
Contributor

Scalars inside device routines also need to implicitly set the DEVICE attribute, except for function results.

@wangzpgi wangzpgi requested a review from clementval May 21, 2025 02:49
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels May 21, 2025
@wangzpgi wangzpgi changed the title implicitly set DEVICE attribute to scalars in device routines [flang] [cuda] implicitly set DEVICE attribute to scalars in device routines May 21, 2025
@llvmbot
Copy link
Member

llvmbot commented May 21, 2025

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

@llvm/pr-subscribers-flang-semantics

Author: Zhen Wang (wangzpgi)

Changes

Scalars inside device routines also need to implicitly set the DEVICE attribute, except for function results.


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

3 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+1-1)
  • (modified) flang/test/Lower/CUDA/cuda-shared.cuf (+1)
  • (added) flang/test/Semantics/cuf21.cuf (+38)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 92a3277191ae0..3f4a06444c4f3 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -9376,7 +9376,7 @@ static void SetImplicitCUDADevice(bool inDeviceSubprogram, Symbol &symbol) {
   if (inDeviceSubprogram && symbol.has<ObjectEntityDetails>()) {
     auto *object{symbol.detailsIf<ObjectEntityDetails>()};
     if (!object->cudaDataAttr() && !IsValue(symbol) &&
-        (IsDummy(symbol) || object->IsArray())) {
+        !IsFunctionResult(symbol)) {
       // Implicitly set device attribute if none is set in device context.
       object->set_cudaDataAttr(common::CUDADataAttr::Device);
     }
diff --git a/flang/test/Lower/CUDA/cuda-shared.cuf b/flang/test/Lower/CUDA/cuda-shared.cuf
index f41011df06ae7..565857f01bdb8 100644
--- a/flang/test/Lower/CUDA/cuda-shared.cuf
+++ b/flang/test/Lower/CUDA/cuda-shared.cuf
@@ -9,4 +9,5 @@ end subroutine
 
 ! CHECK-LABEL: func.func @_QPsharedmem() attributes {cuf.proc_attr = #cuf.cuda_proc<global>}
 ! CHECK: %{{.*}} = cuf.shared_memory !fir.array<32xf32> {bindc_name = "s", uniq_name = "_QFsharedmemEs"} -> !fir.ref<!fir.array<32xf32>>
+! CHECK: cuf.free %{{.*}}#0 : !fir.ref<i32> {data_attr = #cuf.cuda<device>}
 ! CHECK-NOT: cuf.free
diff --git a/flang/test/Semantics/cuf21.cuf b/flang/test/Semantics/cuf21.cuf
new file mode 100644
index 0000000000000..52343daaf66f1
--- /dev/null
+++ b/flang/test/Semantics/cuf21.cuf
@@ -0,0 +1,38 @@
+! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
+
+module mlocModule
+  interface maxlocUpdate
+     module procedure :: &
+          maxlocUpdateR_32F, &
+          maxlocUpdateR_64F, &
+          maxlocUpdateR_32I, &
+          maxlocUpdateR_64I
+  end interface maxlocUpdate
+contains
+
+  attributes(global) subroutine maxlocPartialMaskR_32F1D()
+    implicit none
+    real(4) :: mval
+
+    call maxlocUpdate(mval)
+
+  end subroutine maxlocPartialMaskR_32F1D
+
+  attributes(device) subroutine maxlocUpdateR_32F(mval)
+    real(4) :: mval
+  end subroutine maxlocUpdateR_32F
+
+  attributes(device) subroutine maxlocUpdateR_64F(mval)
+    real(8) :: mval
+  end subroutine maxlocUpdateR_64F
+
+  attributes(device) subroutine maxlocUpdateR_32I(mval)
+    integer(4) :: mval
+  end subroutine maxlocUpdateR_32I
+
+  attributes(device) subroutine maxlocUpdateR_64I(mval)
+    integer(8) :: mval
+  end subroutine maxlocUpdateR_64I
+end module
+
+! CHECK-LABEL: func.func @_QMmlocmodulePmaxlocpartialmaskr_32f1d()

@wangzpgi wangzpgi requested a review from clementval May 21, 2025 16:12
@wangzpgi wangzpgi requested a review from clementval May 21, 2025 17:28
Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@wangzpgi wangzpgi merged commit 4042a00 into llvm:main May 21, 2025
7 of 10 checks passed
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