[flang-commits] [flang] [flang][cuda] Record implicit pinned attribution in module files (PR #225315)

Kareem Ergawy via flang-commits flang-commits at lists.llvm.org
Tue Sep 29 11:55:24 PDT 2026


https://github.com/ergawy updated https://github.com/llvm/llvm-project/pull/225315

>From 6c44d4fb429644733c2726c3f9246dc757467128 Mon Sep 17 00:00:00 2001
From: ergawy <kareem.ergawy at gmail.com>
Date: Mon, 21 Sep 2026 22:16:07 -0700
Subject: [PATCH] [flang][cuda] Record implicit pinned attribution in module
 files

Under -gpu=mem:pinned an unattributed ALLOCATABLE is attributed as pinned by
the compiler, the same way -gpu=mem:managed attributes one as managed, but
only the latter recorded that it had done so. The module file therefore spelled
an implicitly applied PINNED the same as one the user wrote, and a reader
treated it as a user requirement: adding -gpu=mem:pinned to a module's build
rejected its OpenACC-only consumers over an attribute the user never wrote.

Record the attribution for pinned as well, so it is written as
PINNED(IMPLICIT) and consumers can tell the two apart.
---
 flang/lib/Semantics/resolve-names.cpp         |  1 +
 .../CUDA/cuda-pinned-implicit-modfile.cuf     | 42 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 flang/test/Semantics/CUDA/cuda-pinned-implicit-modfile.cuf

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 3d06f7d1fa495..67690d6f47d06 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3882,6 +3882,7 @@ void ScopeHandler::ApplyImplicitCUDADataAttr(Symbol &symbol) {
     object->set_cudaDataAttrIsImplicit();
   } else {
     object->set_cudaDataAttr(common::CUDADataAttr::Pinned);
+    object->set_cudaDataAttrIsImplicit();
   }
 }
 
diff --git a/flang/test/Semantics/CUDA/cuda-pinned-implicit-modfile.cuf b/flang/test/Semantics/CUDA/cuda-pinned-implicit-modfile.cuf
new file mode 100644
index 0000000000000..e9a4170c20c95
--- /dev/null
+++ b/flang/test/Semantics/CUDA/cuda-pinned-implicit-modfile.cuf
@@ -0,0 +1,42 @@
+! Under -gpu=pinned the compiler attributes unattributed ALLOCATABLEs as
+! pinned on the user's behalf, exactly as -gpu=managed attributes them as
+! managed. A module file records that it did so, with the (IMPLICIT)
+! qualifier, so that a reader can tell such an attribute from one the user
+! wrote.
+
+! RUN: rm -rf %t && split-file %s %t
+! RUN: cd %t && bbc -emit-hlfir -fcuda -gpu=pinned def.cuf -o /dev/null
+! RUN: cat %t/m.mod | FileCheck %s --check-prefix=MODFILE
+
+! An attribute the compiler applied does not make the module a definer of CUDA
+! symbols, so a consumer without CUDA Fortran enabled is not rejected.
+! RUN: cd %t && bbc -emit-hlfir -fopenacc use_acc.cuf -o /dev/null
+
+! One the user wrote still is, and still rejects such a consumer.
+! RUN: cd %t && bbc -emit-hlfir -fcuda def_explicit.cuf -o /dev/null
+! RUN: cd %t && not bbc -emit-hlfir -fopenacc use_acc_explicit.cuf -o /dev/null 2>&1 \
+! RUN:   | FileCheck %s --check-prefix=EXPLICIT
+
+!--- def.cuf
+module m
+  real, allocatable :: implicit_var(:)
+end module
+
+! MODFILE: real(4),allocatable,pinned(implicit)::implicit_var(:)
+
+!--- use_acc.cuf
+subroutine acc_only()
+  use m
+end subroutine
+
+!--- def_explicit.cuf
+module m_explicit
+  real, allocatable, pinned :: explicit_var(:)
+end module
+
+!--- use_acc_explicit.cuf
+subroutine acc_only_explicit()
+  use m_explicit
+end subroutine
+
+! EXPLICIT: error: Cannot use module file for module 'm_explicit': CUDA is not enabled, but '{{.*}}m_explicit.mod' defines CUDA symbols



More information about the flang-commits mailing list