[flang-commits] [flang] 653865d - [Flang][OpenMP] Fix implicit symbol resolution for USE-renamed arrays (#189215)

via flang-commits flang-commits at lists.llvm.org
Thu Jun 11 06:17:36 PDT 2026


Author: Aditya Trivedi
Date: 2026-06-11T10:17:31-03:00
New Revision: 653865dd8bb337b24a79a288afa30ee5206f8342

URL: https://github.com/llvm/llvm-project/commit/653865dd8bb337b24a79a288afa30ee5206f8342
DIFF: https://github.com/llvm/llvm-project/commit/653865dd8bb337b24a79a288afa30ee5206f8342.diff

LOG: [Flang][OpenMP] Fix implicit symbol resolution for USE-renamed arrays (#189215)

[Flang][OpenMP] Fix USE-renamed array DSA in OpenMP regions

  Problem: for a USE-renamed symbol (e.g. USE mod, ONLY: s_ary => ary),
  the HostAssoc in the OMP scope was created under the original name
  "ary" instead of the local alias "s_ary".

  Fix: add a DeclareNewAccessEntity overload that takes an explicit
  SourceName, and call it with symbol->name() (the alias) rather than
  the ultimate symbol's name, so the HostAssoc is created under the
  name the user wrote.

  Fixes #185344

  Assisted-by: Claude Sonnet 4.6

Added: 
    flang/test/Semantics/OpenMP/use-rename-array-dsa.f90

Modified: 
    flang/lib/Semantics/resolve-directives.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 2c77a48677463..7738c49d38f1e 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -169,6 +169,8 @@ template <typename T> class DirectiveAttributeVisitor {
   const parser::DoConstruct *GetDoConstructIf(
       const parser::ExecutionPartConstruct &);
   Symbol *DeclareNewAccessEntity(const Symbol &, Symbol::Flag, Scope &);
+  Symbol *DeclareNewAccessEntity(
+      const SourceName &, const Symbol &, Symbol::Flag, Scope &);
   Symbol *DeclareAccessEntity(const parser::Name &, Symbol::Flag, Scope &);
   Symbol *DeclareAccessEntity(Symbol &, Symbol::Flag, Scope &);
 
@@ -1128,9 +1130,9 @@ 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)};
+    const SourceName &name, const Symbol &object, Symbol::Flag flag,
+    Scope &scope) {
+  auto &symbol{MakeAssocSymbol(name, object, scope)};
   symbol.set(flag);
   if (flag == Symbol::Flag::OmpCopyIn) {
     // The symbol in copyin clause must be threadprivate entity.
@@ -1139,6 +1141,12 @@ Symbol *DirectiveAttributeVisitor<T>::DeclareNewAccessEntity(
   return &symbol;
 }
 
+template <typename T>
+Symbol *DirectiveAttributeVisitor<T>::DeclareNewAccessEntity(
+    const Symbol &object, Symbol::Flag flag, Scope &scope) {
+  return DeclareNewAccessEntity(object.name(), object, flag, scope);
+}
+
 template <typename T>
 Symbol *DirectiveAttributeVisitor<T>::DeclareAccessEntity(
     const parser::Name &name, Symbol::Flag flag, Scope &scope) {
@@ -2567,8 +2575,8 @@ void OmpAttributeVisitor::CreateImplicitSymbols(
           lastDeclSymbol ? lastDeclSymbol : &symbol->GetUltimate();
       assert(flags.LeastElement());
       Symbol::Flag flag = *flags.LeastElement();
-      lastDeclSymbol = DeclareNewAccessEntity(
-          *hostSymbol, flag, context_.FindScope(dirContext.directiveSource));
+      lastDeclSymbol = DeclareNewAccessEntity(symbol->name(), *hostSymbol, flag,
+          context_.FindScope(dirContext.directiveSource));
       lastDeclSymbol->flags() |= flags;
       return lastDeclSymbol;
     };
@@ -2576,7 +2584,7 @@ void OmpAttributeVisitor::CreateImplicitSymbols(
       if (lastDeclSymbol) {
         const Symbol *hostSymbol =
             lastDeclSymbol ? lastDeclSymbol : &symbol->GetUltimate();
-        MakeAssocSymbol(symbol->name(), *hostSymbol,
+        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..044e6063c4dfc
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/use-rename-array-dsa.f90
@@ -0,0 +1,34 @@
+! 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 OMP region must create a host-association for s_ary under its alias name.
+! CHECK:  MainProgram scope: TEST
+! CHECK:    OtherConstruct scope:
+! CHECK:      ary (OmpShared): HostAssoc => ary
+! CHECK:      i (OmpPrivate, OmpPreDetermined): HostAssoc => i
+! CHECK:      s_ary (OmpShared): HostAssoc => ary


        


More information about the flang-commits mailing list