[flang-commits] [flang] [flang][cuda] Emit CUDA attributes in type declarations in mod files (PR #185462)
via flang-commits
flang-commits at lists.llvm.org
Mon Mar 9 10:06:42 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
Author: Peter Klausler (klausler)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/185462.diff
2 Files Affected:
- (modified) flang/lib/Semantics/mod-file.cpp (+5-4)
- (modified) flang/test/Semantics/modfile55.cuf (+6-12)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/185462
More information about the flang-commits
mailing list