[flang-commits] [flang] [flang] Suppress USEs of non-USE'able names in module files (PR #124980)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Jan 29 12:21:29 PST 2025


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/124980

When harvesting and formatting symbols USE'd from other modules, don't emit USE statements to module files for names unless they come from the topmost scope of the module.  There was a check to prevent names from derived type scopes from escaping in this way, but it must be made more general to prevent other cases like dummy arguments in interfaces.

Fixes https://github.com/llvm/llvm-project/issues/124716.

>From de375fa97b965a488532237bda517a427726e9cd Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 29 Jan 2025 12:15:43 -0800
Subject: [PATCH] [flang] Suppress USEs of non-USE'able names in module files

When harvesting and formatting symbols USE'd from other modules,
don't emit USE statements to module files for names unless they
come from the topmost scope of the module.  There was a check
to prevent names from derived type scopes from escaping in this
way, but it must be made more general to prevent other cases like
dummy arguments in interfaces.

Fixes https://github.com/llvm/llvm-project/issues/124716.
---
 flang/lib/Semantics/mod-file.cpp   |  6 +++--
 flang/test/Semantics/bug124716.f90 | 36 ++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)
 create mode 100644 flang/test/Semantics/bug124716.f90

diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index b45f1c060da2fd..bef934beaacfa4 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -306,8 +306,10 @@ void ModFileWriter::PrepareRenamings(const Scope &scope) {
   // to their names in this scope, creating those new names when needed.
   auto &renamings{context_.moduleFileOutputRenamings()};
   for (SymbolRef s : symbolsNeeded) {
-    if (s->owner().kind() == Scope::Kind::DerivedType) {
-      continue; // component or binding: ok
+    if (s->owner().kind() != Scope::Kind::Module) {
+      // Not a USE'able name from a module's top scope;
+      // component, binding, dummy argument, &c.
+      continue;
     }
     const Scope *sMod{FindModuleContaining(s->owner())};
     if (!sMod || sMod == &scope) {
diff --git a/flang/test/Semantics/bug124716.f90 b/flang/test/Semantics/bug124716.f90
new file mode 100644
index 00000000000000..c1487a235721c0
--- /dev/null
+++ b/flang/test/Semantics/bug124716.f90
@@ -0,0 +1,36 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+MODULE m1
+  INTERFACE
+    MODULE SUBROUTINE sub1(N, ARR)
+      INTEGER, INTENT(IN) :: N
+      INTEGER, DIMENSION(N) :: ARR
+    END SUBROUTINE
+  END INTERFACE
+END MODULE
+SUBMODULE (m1) m1sub
+ CONTAINS
+  MODULE SUBROUTINE sub1(N, ARR)
+    INTEGER, INTENT(IN) :: N
+    INTEGER, DIMENSION(N) :: ARR
+    PRINT *, "sub1", N, ARR
+  END SUBROUTINE
+END SUBMODULE
+
+!Expect: m1.mod
+!module m1
+!interface
+!module subroutine sub1(n,arr)
+!integer(4),intent(in)::n
+!integer(4)::arr(1_8:int(n,kind=8))
+!end
+!end interface
+!end
+
+!Expect: m1-m1sub.mod
+!submodule(m1) m1sub
+!contains
+!module subroutine sub1(n,arr)
+!integer(4),intent(in)::n
+!integer(4)::arr(1_8:int(n,kind=8))
+!end
+!end



More information about the flang-commits mailing list