[flang-commits] [flang] [flang][OpenMP] Fix segfault on module USE cycle with -fopenmp (PR #228037)

Mihai Zegheru via flang-commits flang-commits at lists.llvm.org
Thu Oct 1 04:12:12 PDT 2026


https://github.com/mihaizegheru updated https://github.com/llvm/llvm-project/pull/228037

>From 36953e23ae36564d87555f8cb5d4ede4b30f2676 Mon Sep 17 00:00:00 2001
From: MihaiZegheru <mihai.z.2005 at gmail.com>
Date: Thu, 1 Oct 2026 13:24:07 +0300
Subject: [PATCH] [flang][OpenMP] Fix segfault on module USE cycle with
 -fopenmp

---
 flang/lib/Semantics/check-omp-structure.cpp | 12 ++++++++++--
 flang/lib/Semantics/check-omp-structure.h   |  4 ++--
 flang/lib/Semantics/semantics.cpp           |  9 +++++++++
 flang/test/Semantics/modfile54.f90          |  1 +
 4 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 09b6c6bc3449f..df973f26f1d3a 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -224,20 +224,28 @@ void OmpStructureChecker::Leave(const parser::BlockData &x) {
   scopeStack_.pop_back();
 }
 
-void OmpStructureChecker::Enter(const parser::Module &x) {
+bool OmpStructureChecker::Enter(const parser::Module &x) {
   auto &stmt{std::get<parser::Statement<parser::ModuleStmt>>(x.t)};
   const Symbol *sym{stmt.statement.v.symbol};
+  if (!sym) {
+    return false;
+  }
   scopeStack_.push_back(sym->scope());
+  return true;
 }
 
 void OmpStructureChecker::Leave(const parser::Module &x) {
   scopeStack_.pop_back();
 }
 
-void OmpStructureChecker::Enter(const parser::Submodule &x) {
+bool OmpStructureChecker::Enter(const parser::Submodule &x) {
   auto &stmt{std::get<parser::Statement<parser::SubmoduleStmt>>(x.t)};
   const Symbol *sym{std::get<parser::Name>(stmt.statement.t).symbol};
+  if (!sym) {
+    return false;
+  }
   scopeStack_.push_back(sym->scope());
+  return true;
 }
 
 void OmpStructureChecker::Leave(const parser::Submodule &x) {
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index a77506e329934..1a2fb58381d4b 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -95,9 +95,9 @@ class OmpStructureChecker : public OmpStructureCheckerBase {
   void Leave(const parser::MainProgram &);
   void Enter(const parser::BlockData &);
   void Leave(const parser::BlockData &);
-  void Enter(const parser::Module &);
+  bool Enter(const parser::Module &);
   void Leave(const parser::Module &);
-  void Enter(const parser::Submodule &);
+  bool Enter(const parser::Submodule &);
   void Leave(const parser::Submodule &);
   void Enter(const parser::SubroutineStmt &);
   void Enter(const parser::EndSubroutineStmt &);
diff --git a/flang/lib/Semantics/semantics.cpp b/flang/lib/Semantics/semantics.cpp
index 761ade6c1e97d..b826f0495fe26 100644
--- a/flang/lib/Semantics/semantics.cpp
+++ b/flang/lib/Semantics/semantics.cpp
@@ -85,6 +85,15 @@ class SemanticsVisitor : public virtual BaseChecker, public virtual C... {
     if constexpr (common::HasMember<const N *, ConstructNode>) {
       context_.PushConstruct(node);
     }
+    if constexpr (std::is_same_v<decltype(Enter(node)), bool>) {
+      if (Enter(node)) {
+        return true;
+      }
+      if constexpr (common::HasMember<const N *, ConstructNode>) {
+        context_.PopConstruct();
+      }
+      return false;
+    }
     Enter(node);
     return true;
   }
diff --git a/flang/test/Semantics/modfile54.f90 b/flang/test/Semantics/modfile54.f90
index 7605e2ac49b07..e318e1dc4ad72 100644
--- a/flang/test/Semantics/modfile54.f90
+++ b/flang/test/Semantics/modfile54.f90
@@ -1,4 +1,5 @@
 ! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -fopenmp
 !ERROR: Some modules in this compilation unit form one or more cycles of dependence
 module m1
   use m2



More information about the flang-commits mailing list