[flang-commits] [flang] [Flang][OpenMP] Fix crash when common block name is used in LINEAR clause (PR #203250)

Aditya Trivedi via flang-commits flang-commits at lists.llvm.org
Fri Jun 12 06:13:57 PDT 2026


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

>From cbc4f62c03054f17e0dc7417fd16a06120889ea6 Mon Sep 17 00:00:00 2001
From: Aditya Trivedi <adit4443ya at gmail.com>
Date: Thu, 11 Jun 2026 17:00:50 +0530
Subject: [PATCH 1/2] [Flang][OpenMP] Fix crash when common block name is  
 used in LINEAR clause

  Using a common block name in a LINEAR clause (e.g. linear(/c/))
  caused
  a symbol-must-have-a-type crash during lowering. The semantic checker
  was not emitting an error because GetSymbolsInObjectList expands /c/
  to its member variables before the check runs, so the
  symbol->has<CommonBlockDetails>() guard was never reached.

  Fix by checking for common block names directly on the OmpObjectList
  before the expansion, where the Name variant of OmpObject still holds
  the common block symbol.

  Fixes #202329
---
 flang/lib/Semantics/check-omp-loop.cpp          |  9 +++++++++
 flang/test/Semantics/OpenMP/linear-clause01.f90 | 14 ++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index 689e3a0da89ca..70d635ca22000 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -724,6 +724,15 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
   auto &objects{std::get<parser::OmpObjectList>(x.v.t)};
   CheckVarIsNotPartOfAnotherVar(GetContext().clauseSource, objects, "LINEAR");
   CheckCrayPointee(objects, "LINEAR", false);
+  for (const auto &ompObject : objects.v) {
+    if (const auto *name{std::get_if<parser::Name>(&ompObject.u)}) {
+      if (name->symbol && name->symbol->has<CommonBlockDetails>()) {
+        context_.Say(name->source,
+            "'%s' is a common block name and must not appear in a LINEAR clause"_err_en_US,
+            name->symbol->name());
+      }
+    }
+  }
   GetSymbolsInObjectList(objects, symbols);
 
   auto CheckIntegerNoRef{[&](const Symbol *symbol, parser::CharBlock source) {
diff --git a/flang/test/Semantics/OpenMP/linear-clause01.f90 b/flang/test/Semantics/OpenMP/linear-clause01.f90
index 7d3090e069a01..bb87b832d6aa9 100644
--- a/flang/test/Semantics/OpenMP/linear-clause01.f90
+++ b/flang/test/Semantics/OpenMP/linear-clause01.f90
@@ -46,6 +46,20 @@ subroutine linear_clause_03(arg)
     !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
     !ERROR: The list item `i` must be a dummy argument
     !$omp declare simd linear(/cc/)
 end subroutine linear_clause_03
+
+! Case 4
+subroutine linear_clause_04(x)
+    real :: x(10)
+    integer :: b
+    common /c/ b
+    !ERROR: 'c' is a common block name and must not appear in a LINEAR clause
+    !$omp simd linear(/c/)
+      do i = 1, 10
+        x(i) = x(i) * 2
+      end do
+    !$omp end simd
+end subroutine linear_clause_04

>From e99fc07f13eadc4b923df9248aa9c048cbeaa4d4 Mon Sep 17 00:00:00 2001
From: Aditya Trivedi <adit4443ya at gmail.com>
Date: Fri, 12 Jun 2026 18:43:23 +0530
Subject: [PATCH 2/2] Apply code review: shift the commonblock detection to
 CheckArgumentObjectKind

---
 flang/lib/Semantics/check-omp-loop.cpp      |  9 ---------
 flang/lib/Semantics/check-omp-structure.cpp | 10 ++++++++++
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index 70d635ca22000..689e3a0da89ca 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -724,15 +724,6 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
   auto &objects{std::get<parser::OmpObjectList>(x.v.t)};
   CheckVarIsNotPartOfAnotherVar(GetContext().clauseSource, objects, "LINEAR");
   CheckCrayPointee(objects, "LINEAR", false);
-  for (const auto &ompObject : objects.v) {
-    if (const auto *name{std::get_if<parser::Name>(&ompObject.u)}) {
-      if (name->symbol && name->symbol->has<CommonBlockDetails>()) {
-        context_.Say(name->source,
-            "'%s' is a common block name and must not appear in a LINEAR clause"_err_en_US,
-            name->symbol->name());
-      }
-    }
-  }
   GetSymbolsInObjectList(objects, symbols);
 
   auto CheckIntegerNoRef{[&](const Symbol *symbol, parser::CharBlock source) {
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 52b174fba7f21..7300e759357d5 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -693,11 +693,15 @@ void OmpStructureChecker::CheckArgumentObjectKind(const parser::OmpClause &x) {
   // clauses. Assumed-size arrays are allowed on "map", "shared", and
   // "use_device_addr". The check for this happens a few lines below.
   bool AssumedSizeArrayAllowed{false};
+  bool CommonBlockAllowed{true};
   bool NamedConstantAllowed{false};
   switch (clauseId) {
   case llvm::omp::Clause::OMPC_firstprivate:
     NamedConstantAllowed = true;
     break;
+  case llvm::omp::Clause::OMPC_linear:
+    CommonBlockAllowed = false;
+    break;
   case llvm::omp::Clause::OMPC_map:
     AssumedSizeArrayAllowed = true;
     break;
@@ -730,6 +734,12 @@ void OmpStructureChecker::CheckArgumentObjectKind(const parser::OmpClause &x) {
           parser::omp::GetUpperName(clauseId, version));
       continue;
     }
+    if (!CommonBlockAllowed && IsCommonBlock(*symbol)) {
+      context_.Say(source,
+          "'%s' is a common block name and must not appear in a %s clause"_err_en_US,
+          symbol->name(), parser::omp::GetUpperName(clauseId, version));
+      continue;
+    }
     // Emit a more user-friendly diagnostic than "'kind' must be a variable
     // list item" for x%kind, or for cplx%re.
     if (IsTypeParamInquiry(*symbol)) {



More information about the flang-commits mailing list