[flang-commits] [flang] [flang][cuda] Emit CUDA attributes in type declarations in mod files (PR #185462)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Mon Mar 9 10:06:03 PDT 2026
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/185462
The compiler implements CUDA object entity attributes in module files by emitting "attributes()" statements after the type declaration statement for the object. This works fine for variables, but not at all for derived type components -- the "attributes()" statement is not allowed in a derived type definition, and the module file isn't readable later when USE'd. The fix is to emit the attribute as part of the type declaration statement or component declaration statement instead.
>From ef95f23cafc30ff480c7b011f3ad2c9d97f5e553 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 9 Mar 2026 10:01:01 -0700
Subject: [PATCH] [flang][cuda] Emit CUDA attributes in type declarations in
mod files
The compiler implements CUDA object entity attributes in module
files by emitting "attributes()" statements after the type declaration
statement for the object. This works fine for variables, but not
at all for derived type components -- the "attributes()" statement is
not allowed in a derived type definition, and the module file isn't
readable later when USE'd. The fix is to emit the attribute as part
of the type declaration statement or component declaration statement
instead.
---
flang/lib/Semantics/mod-file.cpp | 9 +++++----
flang/test/Semantics/modfile55.cuf | 18 ++++++------------
2 files changed, 11 insertions(+), 16 deletions(-)
diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index 857bcbfafe972..3bfe1e144f961 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -1042,10 +1042,6 @@ void ModFileWriter::PutObjectEntity(
});
os << ") " << symbol.name() << '\n';
}
- if (auto attr{details.cudaDataAttr()}) {
- PutLower(os << "attributes(", common::EnumToString(*attr))
- << ") " << symbol.name() << '\n';
- }
if (symbol.test(Fortran::semantics::Symbol::Flag::CrayPointer)) {
for (const auto &[pointee, pointer] : symbol.owner().crayPointers()) {
if (pointer == symbol) {
@@ -1168,6 +1164,11 @@ void ModFileWriter::PutEntity(llvm::raw_ostream &os, const Symbol &symbol,
std::function<void()> writeType, Attrs attrs) {
writeType();
PutAttrs(os, attrs, symbol.GetBindName(), symbol.GetIsExplicitBindName());
+ if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
+ if (auto attr{details->cudaDataAttr()}) {
+ PutLower(os << ',', common::EnumToString(*attr));
+ }
+ }
if (symbol.owner().kind() == Scope::Kind::DerivedType &&
context_.IsTempName(symbol.name().ToString())) {
os << "::%FILL";
diff --git a/flang/test/Semantics/modfile55.cuf b/flang/test/Semantics/modfile55.cuf
index 2338b745d8355..8328bec163bef 100644
--- a/flang/test/Semantics/modfile55.cuf
+++ b/flang/test/Semantics/modfile55.cuf
@@ -22,21 +22,15 @@ end
!Expect: m.mod
!module m
-!real(4)::dd
-!attributes(device) dd
-!real(4),allocatable::md
-!attributes(managed) md
-!real(4),allocatable::mp
-!attributes(pinned) mp
-!real(4)::cd
-!attributes(constant) cd
+!real(4),device::dd
+!real(4),allocatable,managed::md
+!real(4),allocatable,pinned::mp
+!real(4),constant::cd
!contains
!attributes(global) subroutine globsub(x,y,z)
!real(4),value::x
-!real(4)::y
-!attributes(device) y
-!real(4)::z
-!attributes(managed) z
+!real(4),device::y
+!real(4),managed::z
!end
!attributes(host,device) function foo(x)
!real(4)::x
More information about the flang-commits
mailing list