[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
Fri Oct 9 07:52:31 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 1/2] [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 09b6c6bc3449fb..df973f26f1d3a1 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 a77506e3299349..1a2fb58381d4b9 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 761ade6c1e97d4..b826f0495fe268 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 7605e2ac49b071..e318e1dc4ad726 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
>From 758503c733fb2698cb4f6e8ead593dd1b8c9b9cc Mon Sep 17 00:00:00 2001
From: Mihai Zegheru <mihai.z.2005 at gmail.com>
Date: Fri, 9 Oct 2026 17:51:04 +0300
Subject: [PATCH 2/2] [flang][OpenMP] Add regression coverage for subprograms
---
flang/lib/Semantics/check-omp-structure.cpp | 12 ++++++++++++
flang/lib/Semantics/check-omp-structure.h | 2 ++
flang/test/Semantics/modfile54.f90 | 9 +++++++++
3 files changed, 23 insertions(+)
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index df973f26f1d3a1..98e847d7c0bbea 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -252,6 +252,12 @@ void OmpStructureChecker::Leave(const parser::Submodule &x) {
scopeStack_.pop_back();
}
+bool OmpStructureChecker::Enter(const parser::SubroutineSubprogram &x) {
+ const auto &stmt{std::get<parser::Statement<parser::SubroutineStmt>>(x.t)};
+ const auto &name{std::get<parser::Name>(stmt.statement.t)};
+ return name.symbol != nullptr;
+}
+
// Function/subroutine subprogram nodes don't appear in INTERFACEs, but
// the subprogram/end statements do.
void OmpStructureChecker::Enter(const parser::SubroutineStmt &x) {
@@ -283,6 +289,12 @@ void OmpStructureChecker::Enter(const parser::EndSubroutineStmt &x) {
scopeStack_.pop_back();
}
+bool OmpStructureChecker::Enter(const parser::FunctionSubprogram &x) {
+ const auto &stmt{std::get<parser::Statement<parser::FunctionStmt>>(x.t)};
+ const auto &name{std::get<parser::Name>(stmt.statement.t)};
+ return name.symbol != nullptr;
+}
+
void OmpStructureChecker::Enter(const parser::FunctionStmt &x) {
const Symbol *sym{std::get<parser::Name>(x.t).symbol};
scopeStack_.push_back(sym->scope());
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index 1a2fb58381d4b9..22d6936e0fceca 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -99,8 +99,10 @@ class OmpStructureChecker : public OmpStructureCheckerBase {
void Leave(const parser::Module &);
bool Enter(const parser::Submodule &);
void Leave(const parser::Submodule &);
+ bool Enter(const parser::SubroutineSubprogram &);
void Enter(const parser::SubroutineStmt &);
void Enter(const parser::EndSubroutineStmt &);
+ bool Enter(const parser::FunctionSubprogram &);
void Enter(const parser::FunctionStmt &);
void Enter(const parser::EndFunctionStmt &);
void Enter(const parser::MpSubprogramStmt &);
diff --git a/flang/test/Semantics/modfile54.f90 b/flang/test/Semantics/modfile54.f90
index e318e1dc4ad726..ccfd43b8ddc4e5 100644
--- a/flang/test/Semantics/modfile54.f90
+++ b/flang/test/Semantics/modfile54.f90
@@ -14,3 +14,12 @@ module m2
module m3
use m1
end
+
+subroutine s()
+ use m1
+end subroutine s
+
+integer function f()
+ use m1
+ f = 0
+end function f
More information about the flang-commits
mailing list