[flang-commits] [flang] [flang] Fix Cray pointers in module file output (PR #130315)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Mar 7 09:58:47 PST 2025


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/130315

>From c00712e3dd0c52b828989e6866f0db5e8b1eb894 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Fri, 7 Mar 2025 09:51:06 -0800
Subject: [PATCH] [flang] Fix Cray pointers in module file output

The relationship between a Cray pointee and its pointer is not in
the symbol table entry, but instead in a per-scope map.  Use this
information to ensure that if a pointee/pointer is needed in the
module file, so is its pointer/pointee.

Fixes https://github.com/llvm/llvm-project/issues/130270.
---
 flang/lib/Semantics/mod-file.cpp   | 19 ++++++++++++++-----
 flang/test/Semantics/modfile74.f90 | 19 +++++++++++++++++++
 2 files changed, 33 insertions(+), 5 deletions(-)
 create mode 100644 flang/test/Semantics/modfile74.f90

diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index ac176978a45a5..c3b46261228df 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -978,11 +978,9 @@ void ModFileWriter::PutObjectEntity(
         << ") " << symbol.name() << '\n';
   }
   if (symbol.test(Fortran::semantics::Symbol::Flag::CrayPointer)) {
-    if (!symbol.owner().crayPointers().empty()) {
-      for (const auto &[pointee, pointer] : symbol.owner().crayPointers()) {
-        if (pointer == symbol) {
-          os << "pointer(" << symbol.name() << "," << pointee << ")\n";
-        }
+    for (const auto &[pointee, pointer] : symbol.owner().crayPointers()) {
+      if (pointer == symbol) {
+        os << "pointer(" << symbol.name() << "," << pointee << ")\n";
       }
     }
   }
@@ -1725,6 +1723,17 @@ void SubprogramSymbolCollector::DoSymbol(
   if (!scope.IsDerivedType()) {
     need_.push_back(symbol);
   }
+  if (symbol.test(Fortran::semantics::Symbol::Flag::CrayPointer)) {
+    for (const auto &[pointee, pointer] : symbol.owner().crayPointers()) {
+      if (&*pointer == &symbol) {
+        auto iter{symbol.owner().find(pointee)};
+        CHECK(iter != symbol.owner().end());
+        DoSymbol(*iter->second);
+      }
+    }
+  } else if (symbol.test(Fortran::semantics::Symbol::Flag::CrayPointee)) {
+    DoSymbol(GetCrayPointer(symbol));
+  }
 }
 
 void SubprogramSymbolCollector::DoType(const DeclTypeSpec *type) {
diff --git a/flang/test/Semantics/modfile74.f90 b/flang/test/Semantics/modfile74.f90
new file mode 100644
index 0000000000000..f24ce8024c100
--- /dev/null
+++ b/flang/test/Semantics/modfile74.f90
@@ -0,0 +1,19 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+module m
+ contains
+  function f() result(ptr)
+    character :: str
+    pointer(ptr, str)
+    ptr = 0
+  end
+end
+
+!Expect: m.mod
+!module m
+!contains
+!function f() result(ptr)
+!integer(8)::ptr
+!pointer(ptr,str)
+!character(1_8,1)::str
+!end
+!end



More information about the flang-commits mailing list