[flang-commits] [flang] [flang][OpenACC] Relax COMMON block usage restriction in OpenACC directives (PR #162659)

via flang-commits flang-commits at lists.llvm.org
Thu Oct 9 14:03:44 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-openacc

Author: Eugene Epshteyn (eugeneepshteyn)

<details>
<summary>Changes</summary>

Unlike OpenMP, OpenACC doesn't require that the COMMON block be defined in the same scope as the directive.

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


2 Files Affected:

- (modified) flang/lib/Semantics/resolve-directives.cpp (+19-13) 
- (added) flang/test/Semantics/OpenACC/acc-common.f90 (+19) 


``````````diff
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 18fc63814d973..44cfb75e06164 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1695,17 +1695,23 @@ void AccAttributeVisitor::Post(const parser::Name &name) {
 
 Symbol *AccAttributeVisitor::ResolveAccCommonBlockName(
     const parser::Name *name) {
-  if (auto *prev{name
-              ? GetContext().scope.parent().FindCommonBlock(name->source)
-              : nullptr}) {
-    name->symbol = prev;
-    return prev;
-  }
-  // Check if the Common Block is declared in the current scope
-  if (auto *commonBlockSymbol{
-          name ? GetContext().scope.FindCommonBlock(name->source) : nullptr}) {
-    name->symbol = commonBlockSymbol;
-    return commonBlockSymbol;
+  if (!name) {
+    return nullptr;
+  }
+  // Check the local and surrounding scopes first
+  if (auto *cb{GetProgramUnitOrBlockConstructContaining(GetContext().scope)
+              .FindCommonBlock(name->source)}) {
+    name->symbol = cb;
+    return cb;
+  }
+  // Look for COMMON block in the modules
+  for (const Scope &childScope : context_.globalScope().children()) {
+    if (childScope.kind() == Scope::Kind::Module) {
+      if (auto *cb{childScope.FindCommonBlock(name->source)}) {
+        name->symbol = cb;
+        return cb;
+      }
+    }
   }
   return nullptr;
 }
@@ -1755,8 +1761,8 @@ void AccAttributeVisitor::ResolveAccObject(
               }
             } else {
               context_.Say(name.source,
-                  "COMMON block must be declared in the same scoping unit "
-                  "in which the OpenACC directive or clause appears"_err_en_US);
+                  "Could not find COMMON block '%s' used in OpenACC directive"_err_en_US,
+                  name.ToString());
             }
           },
       },
diff --git a/flang/test/Semantics/OpenACC/acc-common.f90 b/flang/test/Semantics/OpenACC/acc-common.f90
new file mode 100644
index 0000000000000..c6142bdb915f6
--- /dev/null
+++ b/flang/test/Semantics/OpenACC/acc-common.f90
@@ -0,0 +1,19 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenacc
+module acc_common_decl
+  implicit none
+  integer a
+  common /a_common/ a
+!$acc declare create (/a_common/)
+  data a/42/
+end module acc_common_decl
+
+program acc_decl_test
+  use acc_common_decl
+  implicit none
+
+  a = 1
+!$acc update device (/a_common/)
+  a = 2
+!ERROR: Could not find COMMON block 'a_common_bad' used in OpenACC directive
+!$acc update device (/a_common_bad/)
+end program

``````````

</details>


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


More information about the flang-commits mailing list