[flang-commits] [flang] [Flang][OpenMP] Fix false positive common block error in LINEAR clause (PR #189170)

Aditya Trivedi via flang-commits flang-commits at lists.llvm.org
Sun Apr 5 03:25:11 PDT 2026


https://github.com/adit4443ya updated https://github.com/llvm/llvm-project/pull/189170

>From 477c250b322d1155fc1cd8f00141ba88fe1a78b9 Mon Sep 17 00:00:00 2001
From: Aditya Trivedi <adit4443ya at gmail.com>
Date: Sat, 28 Mar 2026 15:33:41 +0000
Subject: [PATCH 1/2] [Flang][OpenMP] Fix false positive common block error in
 LINEAR clause

The check for common block names in the OpenMP LINEAR clause previously
used FindCommonBlockContaining, which incorrectly flagged any variable
that was a member of a common block as violating the restriction that
a common block name cannot appear in a LINEAR clause.

This updates the check to correctly verify whether the symbol itself
has CommonBlockDetails.
---
 flang/lib/Semantics/check-omp-loop.cpp          | 4 ++--
 flang/test/Semantics/OpenMP/linear-clause01.f90 | 1 -
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index 5bbf7bcd627ec..da1aaafbbe080 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -741,9 +741,9 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
           "The list item `%s` in a LINEAR clause must not be Cray Pointer or a variable with POINTER attribute"_err_en_US,
           symbol->name());
     }
-    if (FindCommonBlockContaining(*symbol)) {
+    if (symbol->has<CommonBlockDetails>()) {
       context_.Say(source,
-          "'%s' is a common block name and must not appear in an LINEAR clause"_err_en_US,
+          "'%s' is a common block name and must not appear in a LINEAR clause"_err_en_US,
           symbol->name());
     }
   }
diff --git a/flang/test/Semantics/OpenMP/linear-clause01.f90 b/flang/test/Semantics/OpenMP/linear-clause01.f90
index 63b09c07875e5..60a8ade1c954c 100644
--- a/flang/test/Semantics/OpenMP/linear-clause01.f90
+++ b/flang/test/Semantics/OpenMP/linear-clause01.f90
@@ -44,6 +44,5 @@ subroutine linear_clause_03(arg)
     integer :: i
     common /cc/ i
     !ERROR: The list item `i` must be a dummy argument
-    !ERROR: 'i' is a common block name and must not appear in an LINEAR clause
     !$omp declare simd linear(i)
 end subroutine linear_clause_03

>From f3b626bb004b00f961c38ad852a928f99a5a5c52 Mon Sep 17 00:00:00 2001
From: Aditya Trivedi <adit4443ya at gmail.com>
Date: Sun, 5 Apr 2026 10:24:43 +0000
Subject: [PATCH 2/2] [Flang][OpenMP] Fix false positive common block errors in
 ALIGNED and LINEAR clauses This addresses PR review feedback by: 1. Adding a
 missing test case in linear-clause01.f90 to ensure valid    common block
 names natively trigger the LINEAR restriction. 2. Replacing
 FindCommonBlockContaining with symbol->has<CommonBlockDetails>()    for the
 ALIGNED clause to mirror the LINEAR fix, preventing false    positives when
 variables inside a common block are used. 3. Adding test coverage in
 simd-aligned.f90 to verify the ALIGNED fix.

---
 flang/lib/Semantics/check-omp-structure.cpp     | 2 +-
 flang/test/Semantics/OpenMP/linear-clause01.f90 | 3 +++
 flang/test/Semantics/OpenMP/simd-aligned.f90    | 9 ++++++++-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 9b9da227bdef2..e3caa89fef1d3 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -586,7 +586,7 @@ void OmpStructureChecker::CheckMultListItems() {
     for (const auto &ompObject : alignedList.v) {
       if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
         if (name->symbol) {
-          if (FindCommonBlockContaining(*(name->symbol))) {
+          if (name->symbol->has<CommonBlockDetails>()) {
             context_.Say(clause->source,
                 "'%s' is a common block name and can not appear in an "
                 "ALIGNED clause"_err_en_US,
diff --git a/flang/test/Semantics/OpenMP/linear-clause01.f90 b/flang/test/Semantics/OpenMP/linear-clause01.f90
index 60a8ade1c954c..2132ed31a7321 100644
--- a/flang/test/Semantics/OpenMP/linear-clause01.f90
+++ b/flang/test/Semantics/OpenMP/linear-clause01.f90
@@ -45,4 +45,7 @@ subroutine linear_clause_03(arg)
     common /cc/ i
     !ERROR: The list item `i` must be a dummy argument
     !$omp declare simd linear(i)
+
+    !ERROR: 'cc' is a common block name and must not appear in a LINEAR clause
+    !$omp declare simd linear(/cc/)
 end subroutine linear_clause_03
diff --git a/flang/test/Semantics/OpenMP/simd-aligned.f90 b/flang/test/Semantics/OpenMP/simd-aligned.f90
index 4c410a7c4631b..b72a2c4ff37b8 100644
--- a/flang/test/Semantics/OpenMP/simd-aligned.f90
+++ b/flang/test/Semantics/OpenMP/simd-aligned.f90
@@ -52,13 +52,20 @@ program omp_simd
 
   print *, a
 
-  !ERROR: 'c' is a common block name and can not appear in an ALIGNED clause
+  !ERROR: 'c' in ALIGNED clause must be of type C_PTR, POINTER or ALLOCATABLE
   !$omp simd aligned(c)
   do i = 1, 10
     c = 5
   end do
   !$omp end simd
 
+  !ERROR: 'cmn' is a common block name and can not appear in an ALIGNED clause
+  !$omp simd aligned(/cmn/)
+  do i = 1, 10
+    c = 5
+  end do
+  !$omp end simd
+
   !ERROR: 'd' in ALIGNED clause must be of type C_PTR, POINTER or ALLOCATABLE
   !WARNING: Alignment is not a power of 2, Aligned clause will be ignored [-Wopen-mp-usage]
   !$omp simd aligned(d:100)



More information about the flang-commits mailing list