[flang-commits] [flang] a21a57c - [flang][OpenMP] Fix the privatization of common blocks (#200446)

via flang-commits flang-commits at lists.llvm.org
Thu Jun 11 07:26:20 PDT 2026


Author: Leandro Lupori
Date: 2026-06-11T11:25:52-03:00
New Revision: a21a57c483376359d16de7f7f0dce0d546800d9c

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

LOG: [flang][OpenMP] Fix the privatization of common blocks (#200446)

Common block objects were being replaced on every privatization,
which caused errors for distinct privatizations on nested constructs.

Modifying a common block clone, instead of the original one, fixes the
issue. Cloning is limited to DSA flags because target mapping lowering
expects to find the common block address, through its defining
operation.

Fixes #178790

Added: 
    flang/test/Lower/OpenMP/nested-commonblock.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 7738c49d38f1e..5d72698b0250d 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -3076,6 +3076,15 @@ void OmpAttributeVisitor::PropagateOmpFlagToEquivalenceSet(
 
 void OmpAttributeVisitor::ResolveOmpCommonBlock(
     const parser::Name &name, Symbol::Flag ompFlag) {
+  if (name.symbol) {
+    if (auto *details{name.symbol->detailsIf<CommonBlockDetails>()}) {
+      if (!details->objects().empty()) {
+        // Common block already resolved
+        return;
+      }
+    }
+  }
+
   if (auto *symbol{ResolveOmpCommonBlockName(&name)}) {
     if (!dataCopyingAttributeFlags.test(ompFlag)) {
       CheckMultipleAppearances(name, *symbol, Symbol::Flag::OmpCommonBlock);
@@ -3084,6 +3093,8 @@ void OmpAttributeVisitor::ResolveOmpCommonBlock(
     // same meaning as if every explicit member of the common block
     // appeared in the list
     auto &details{symbol->get<CommonBlockDetails>()};
+    bool cloneCommonBlock{dataSharingAttributeFlags.test(ompFlag)};
+    CommonBlockDetails cloneDetails{symbol->name()};
     for (auto [index, object] : llvm::enumerate(details.objects())) {
       if (auto *resolvedObject{ResolveOmp(*object, ompFlag, currScope())}) {
         if (dataCopyingAttributeFlags.test(ompFlag)) {
@@ -3091,7 +3102,11 @@ void OmpAttributeVisitor::ResolveOmpCommonBlock(
         } else {
           AddToContextObjectWithExplicitDSA(*resolvedObject, ompFlag);
         }
-        details.replace_object(*resolvedObject, index);
+        if (cloneCommonBlock) {
+          cloneDetails.add_object(*resolvedObject);
+        } else {
+          details.replace_object(*resolvedObject, index);
+        }
 
         // Propagate the flag to symbols in the equivalence set
         if (ompFlag == Symbol::Flag::OmpThreadprivate) {
@@ -3099,6 +3114,10 @@ void OmpAttributeVisitor::ResolveOmpCommonBlock(
         }
       }
     }
+    if (cloneCommonBlock) {
+      name.symbol = &currScope().MakeSymbol(
+          symbol->name(), symbol->attrs(), std::move(cloneDetails));
+    }
   } else {
     context_.Say(name.source, // 2.15.3
         "COMMON block must be declared in the same scoping unit in which the OpenMP directive or clause appears"_err_en_US);

diff  --git a/flang/test/Lower/OpenMP/nested-commonblock.f90 b/flang/test/Lower/OpenMP/nested-commonblock.f90
new file mode 100644
index 0000000000000..65c0b6d2ecbd8
--- /dev/null
+++ b/flang/test/Lower/OpenMP/nested-commonblock.f90
@@ -0,0 +1,15 @@
+! RUN: %flang_fc1 -fopenmp -emit-hlfir -o - %s 2>&1 | FileCheck %s
+
+!CHECK-LABEL: func @_QPtest_nested()
+!CHECK:         omp.parallel private(@_QFtest_nestedEa_firstprivate_i32 {{.*}}, @_QFtest_nestedEb_firstprivate_i32 {{.*}})
+!CHECK:           omp.parallel private(@_QFtest_nestedEa_private_i32 {{.*}}, @_QFtest_nestedEb_private_i32 {{.*}})
+
+subroutine test_nested()
+  integer :: a, b
+  common /com/ a, b
+
+  !$omp parallel firstprivate(/com/)
+    !$omp parallel private(/com/)
+    !$omp end parallel
+  !$omp end parallel
+end subroutine


        


More information about the flang-commits mailing list