Skip to content

[flang] Fix missed case of symbol renaming in module file generation #132475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025

Conversation

klausler
Copy link
Contributor

The map of symbols requiring new local aliases for USE association needs to use the symbols' ultimate resolutions to avoid missing cases that can arise in convoluted codes with lots of confusing renamings.

Fixes #132435.

The map of symbols requiring new local aliases for USE association
needs to use the symbols' ultimate resolutions to avoid missing
cases that can arise in convoluted codes with lots of confusing
renamings.

Fixes llvm#132435.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Mar 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

The map of symbols requiring new local aliases for USE association needs to use the symbols' ultimate resolutions to avoid missing cases that can arise in convoluted codes with lots of confusing renamings.

Fixes #132435.


Full diff: https://github.com/llvm/llvm-project/pull/132475.diff

3 Files Affected:

  • (modified) flang/lib/Evaluate/formatting.cpp (+2-1)
  • (modified) flang/lib/Semantics/mod-file.cpp (+1-1)
  • (added) flang/test/Semantics/bug132435.f90 (+85)
diff --git a/flang/lib/Evaluate/formatting.cpp b/flang/lib/Evaluate/formatting.cpp
index c4c3998257830..6778fac9a44fd 100644
--- a/flang/lib/Evaluate/formatting.cpp
+++ b/flang/lib/Evaluate/formatting.cpp
@@ -129,7 +129,8 @@ llvm::raw_ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
 llvm::raw_ostream &EmitVar(llvm::raw_ostream &o, const Symbol &symbol,
     std::optional<parser::CharBlock> name = std::nullopt) {
   const auto &renamings{symbol.owner().context().moduleFileOutputRenamings()};
-  if (auto iter{renamings.find(&symbol)}; iter != renamings.end()) {
+  if (auto iter{renamings.find(&symbol.GetUltimate())};
+      iter != renamings.end()) {
     return o << iter->second.ToString();
   } else if (name) {
     return o << name->ToString();
diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index c3b46261228df..ee356e56e4458 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -348,7 +348,7 @@ void ModFileWriter::PrepareRenamings(const Scope &scope) {
     uses_ << DEREF(sMod->symbol()).name() << ",only:";
     if (rename != s->name()) {
       uses_ << rename << "=>";
-      renamings.emplace(&*s, rename);
+      renamings.emplace(&s->GetUltimate(), rename);
     }
     uses_ << s->name() << '\n';
     useExtraAttrs_ << "private::" << rename << '\n';
diff --git a/flang/test/Semantics/bug132435.f90 b/flang/test/Semantics/bug132435.f90
new file mode 100644
index 0000000000000..be8b661fcf6cc
--- /dev/null
+++ b/flang/test/Semantics/bug132435.f90
@@ -0,0 +1,85 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+module m1
+  type foo
+    integer :: c1 = 123
+  end type
+end
+
+module m2
+  use m1, only: foo
+  type baz
+    type(foo) :: d = foo()
+  end type
+  type bar
+    type(baz) :: e = baz()
+  end type
+end
+
+module m3
+  use m1, only: m1foo => foo
+  type foo
+    type(m1foo), private :: c2 = m1foo()
+  end type
+end
+
+module m4
+  use m2, only: m3foo => foo
+  type foo
+    type(m3foo), private :: c3 = m3foo()
+  end type
+end
+
+module m5
+  use m2, only: m2bar => bar
+  use m4, only: foo
+  type blah
+    type(m2bar) :: f = m2bar()
+  end type
+end
+
+!Expect: m1.mod
+!module m1
+!type::foo
+!integer(4)::c1=123_4
+!end type
+!end
+
+!Expect: m2.mod
+!module m2
+!use m1,only:foo
+!type::baz
+!type(foo)::d=foo(c1=123_4)
+!end type
+!type::bar
+!type(baz)::e=baz(d=foo(c1=123_4))
+!end type
+!end
+
+!Expect: m3.mod
+!module m3
+!use m1,only:m1foo=>foo
+!type::foo
+!type(m1foo),private::c2=m1foo(c1=123_4)
+!end type
+!end
+
+!Expect: m4.mod
+!module m4
+!use m2,only:m3foo=>foo
+!type::foo
+!type(m3foo),private::c3=m3foo(c1=123_4)
+!end type
+!end
+
+!Expect: m5.mod
+!module m5
+!use m2,only:m2$foo=>foo
+!use m2,only:baz
+!use m2,only:m2bar=>bar
+!use m4,only:foo
+!private::m2$foo
+!private::baz
+!type::blah
+!type(m2bar)::f=m2bar(e=baz(d=m2$foo(c1=123_4)))
+!end type
+!end

@klausler klausler merged commit 6df27dd into llvm:main Mar 26, 2025
14 checks passed
@pawosm-arm pawosm-arm added this to the LLVM 20.X Release milestone Mar 27, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Mar 27, 2025
@pawosm-arm
Copy link
Contributor

considering importance of CP2K in the Fortran world, I'd suggest cherrypicking to LLVM20 release branch.

@pawosm-arm
Copy link
Contributor

/cherry-pick 6df27dd

@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2025

/pull-request #133223

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
Development

Successfully merging this pull request may close these issues.

[flang] Another issue in cp2k 2025.1 compilation
4 participants