[flang-commits] [flang] [flang] Fix host association for ASYNCHRONOUS/VOLATILE in submodules (PR #211669)

via flang-commits flang-commits at lists.llvm.org
Thu Jul 23 22:04:16 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: mleair

<details>
<summary>Changes</summary>

In `HandleAttributeStmt`, when an `ASYNCHRONOUS` or `VOLATILE` statement
names a variable not already in the current scope, flang creates a
host-association symbol — but only for `Subprogram` and `BlockConstruct`
scopes. Submodule scopes have kind `Module`, so the check excluded them.
This caused a fresh `EntityDetails` symbol (zero-initialized) to be
created instead of a `HostAssocDetails` symbol pointing at the ancestor
module variable, producing value 0 instead of the initialized value.

Fix: extend the condition to also fire when `currScope().IsSubmodule()`.
`Scope::FindSymbol` already traverses into the parent module for
submodules, so the host variable is found correctly.

Note: the `volatile`/`asynchronous` `fortran_attrs` are not propagated
to `hlfir.declare` for host-associated variables in general (subprograms,
BLOCK constructs, and submodules); that is a separate lowering issue
tracked in #<!-- -->208588.

Fixes #<!-- -->208362

Assisted-By: AI

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


2 Files Affected:

- (modified) flang/lib/Semantics/resolve-names.cpp (+2-1) 
- (added) flang/test/Lower/submodule-async-volatile.f90 (+39) 


``````````diff
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 996c05d0e193b..83d92f253e626 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -6429,7 +6429,8 @@ Symbol &DeclarationVisitor::HandleAttributeStmt(
     // these can be set on a symbol that is host-assoc or use-assoc
     if (!symbol &&
         (currScope().kind() == Scope::Kind::Subprogram ||
-            currScope().kind() == Scope::Kind::BlockConstruct)) {
+            currScope().kind() == Scope::Kind::BlockConstruct ||
+            currScope().IsSubmodule())) {
       if (auto *hostSymbol{FindSymbol(name)}) {
         symbol = &MakeHostAssocSymbol(name, *hostSymbol);
       }
diff --git a/flang/test/Lower/submodule-async-volatile.f90 b/flang/test/Lower/submodule-async-volatile.f90
new file mode 100644
index 0000000000000..0fb601b0fc49e
--- /dev/null
+++ b/flang/test/Lower/submodule-async-volatile.f90
@@ -0,0 +1,39 @@
+! RUN: %flang_fc1 -emit-hlfir %s -o - | FileCheck %s
+
+! Test that ASYNCHRONOUS/VOLATILE statements in a submodule correctly
+! host-associate variables from the ancestor module rather than creating
+! new local symbols. GitHub issue #208362.
+
+module m1
+  integer :: n = 1, k = 2
+  interface
+    module subroutine sub()
+    end subroutine
+  end interface
+end module m1
+
+submodule(m1) submod
+  volatile :: n
+  asynchronous :: k
+contains
+  ! CHECK-LABEL: func @_QMm1Psub
+  ! n and k must resolve to the module globals; no local alloca for either.
+  ! CHECK-NOT: fir.alloca i32 {{.*}}bindc_name = "n"
+  ! CHECK-NOT: fir.alloca i32 {{.*}}bindc_name = "k"
+  ! CHECK-DAG: %[[N:.*]] = fir.address_of(@_QMm1En) : !fir.ref<i32>
+  ! CHECK-DAG: %[[K:.*]] = fir.address_of(@_QMm1Ek) : !fir.ref<i32>
+  ! CHECK-DAG: hlfir.declare %[[N]] {uniq_name = "_QMm1En"}
+  ! CHECK-DAG: hlfir.declare %[[K]] {uniq_name = "_QMm1Ek"}
+  ! FIXME: The volatile/asynchronous fortran_attrs are not propagated to the
+  ! hlfir.declare or fir.ref type for host-associated variables (this affects
+  ! regular subprograms too, not just submodules).
+  ! See https://github.com/llvm/llvm-project/issues/208588.
+  module subroutine sub()
+    implicit none
+    if (n /= 1) print *, 'Error n=', n
+    if (k /= 2) print *, 'Error k=', k
+  end subroutine
+end submodule submod
+
+! CHECK: fir.global @_QMm1Ek : i32
+! CHECK: fir.global @_QMm1En : i32

``````````

</details>


https://github.com/llvm/llvm-project/pull/211669


More information about the flang-commits mailing list