[flang-commits] [flang] [Flang][OpenMP] Fix implicit symbol resolution for USE-renamed arrays (PR #189215)
Aditya Trivedi via flang-commits
flang-commits at lists.llvm.org
Thu Jun 4 06:55:35 PDT 2026
https://github.com/adit4443ya updated https://github.com/llvm/llvm-project/pull/189215
>From eb1f3ee9b1eeb7c7e0da3766864a0e2f7f148abb Mon Sep 17 00:00:00 2001
From: Aditya Trivedi <adit4443ya at gmail.com>
Date: Thu, 4 Jun 2026 19:14:24 +0530
Subject: [PATCH] [Flang][OpenMP] Fix USE-renamed array DSA in OpenMP regions
When a module array is imported with a local alias via USE renaming
(e.g. USE mod, ONLY: s_ary => ary), CreateImplicitSymbols was calling
GetUltimate() which bypassed the alias and returned the original module
symbol. This caused the OpenMP scope to create a host-association under
the wrong name (ary instead of s_ary), producing a rank-mismatch
error when two modules both exported a symbol named ary with different
ranks.
Fix by passing the USE-alias symbol directly instead of its ultimate,
so the host-association is created under the local alias name. Also
remove the assert in DeclareNewAccessEntity that was overly strict
since the scope is always passed explicitly to MakeAssocSymbol.
Fixes #185344
---
flang/lib/Semantics/resolve-directives.cpp | 7 ++--
.../Semantics/OpenMP/use-rename-array-dsa.f90 | 32 +++++++++++++++++++
2 files changed, 35 insertions(+), 4 deletions(-)
create mode 100644 flang/test/Semantics/OpenMP/use-rename-array-dsa.f90
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 821724fedc7d4..5538a8a3c28c2 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1128,7 +1128,6 @@ const parser::DoConstruct *DirectiveAttributeVisitor<T>::GetDoConstructIf(
template <typename T>
Symbol *DirectiveAttributeVisitor<T>::DeclareNewAccessEntity(
const Symbol &object, Symbol::Flag flag, Scope &scope) {
- assert(object.owner() != currScope());
auto &symbol{MakeAssocSymbol(object.name(), object, scope)};
symbol.set(flag);
if (flag == Symbol::Flag::OmpCopyIn) {
@@ -2561,7 +2560,7 @@ void OmpAttributeVisitor::CreateImplicitSymbols(
// privatized in p2 and its privatization in p1 to be skipped.
auto makeSymbol = [&](Symbol::Flags flags) {
const Symbol *hostSymbol =
- lastDeclSymbol ? lastDeclSymbol : &symbol->GetUltimate();
+ lastDeclSymbol ? lastDeclSymbol : symbol;
assert(flags.LeastElement());
Symbol::Flag flag = *flags.LeastElement();
lastDeclSymbol = DeclareNewAccessEntity(
@@ -2572,8 +2571,8 @@ void OmpAttributeVisitor::CreateImplicitSymbols(
auto useLastDeclSymbol = [&]() {
if (lastDeclSymbol) {
const Symbol *hostSymbol =
- lastDeclSymbol ? lastDeclSymbol : &symbol->GetUltimate();
- MakeAssocSymbol(symbol->name(), *hostSymbol,
+ lastDeclSymbol ? lastDeclSymbol : symbol;
+ MakeAssocSymbol(hostSymbol->name(), *hostSymbol,
context_.FindScope(dirContext.directiveSource));
}
};
diff --git a/flang/test/Semantics/OpenMP/use-rename-array-dsa.f90 b/flang/test/Semantics/OpenMP/use-rename-array-dsa.f90
new file mode 100644
index 0000000000000..865cdff2eeb24
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/use-rename-array-dsa.f90
@@ -0,0 +1,32 @@
+! RUN: %flang_fc1 -fdebug-dump-symbols -fopenmp %s | FileCheck %s
+
+! Verify that a USE-renamed array (s_ary => ary) is correctly associated in an
+! OpenMP region under the alias name, not the original module name.
+! Regression test for https://github.com/llvm/llvm-project/issues/185344
+
+module use_rename_mod1
+ implicit none
+ real(8), allocatable :: ary(:,:,:)
+end module
+
+module use_rename_mod2
+ implicit none
+ real(8), allocatable :: ary(:,:,:,:,:)
+end module
+
+program test
+ use use_rename_mod1, only: s_ary => ary
+ use use_rename_mod2, only: ary
+ implicit none
+ integer(4) :: i
+
+ !$omp parallel do
+ do i = 1, 10
+ s_ary(i,1,1) = ary(i,1,1,1,1)
+ end do
+ !$omp end parallel do
+end program
+! The loop variable is private and predetermined.
+! CHECK: i (OmpPrivate, OmpPreDetermined): HostAssoc
+! The OMP region must create a host-association for s_ary under its alias name.
+! CHECK: s_ary (OmpShared): HostAssoc
More information about the flang-commits
mailing list