[flang-commits] [flang] [flang][OpenMP] Clear branch labels in all program units (PR #194152)

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Sat Apr 25 08:20:11 PDT 2026


https://github.com/kparzysz created https://github.com/llvm/llvm-project/pull/194152

The semantic check for branching in or out of an OpenMP construct did not reset its label information in some cases, leading to false positive error messages in valid Fortran code.

>From 9f318728db2d22470e74fe700503969559759367 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Sat, 25 Apr 2026 10:18:02 -0500
Subject: [PATCH] [flang][OpenMP] Clear branch labels in all program units

The semantic check for branching in or out of an OpenMP construct
did not reset its label information in some cases, leading to false
positive error messages in valid Fortran code.
---
 flang/lib/Semantics/check-omp-structure.cpp      |  4 ++++
 flang/lib/Semantics/check-omp-structure.h        |  1 +
 .../Semantics/OpenMP/branching-program-unit.f90  | 16 ++++++++++++++++
 3 files changed, 21 insertions(+)
 create mode 100644 flang/test/Semantics/OpenMP/branching-program-unit.f90

diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 30db2f2ad4ec5..13d5f9294e5a5 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -69,6 +69,10 @@ OmpStructureChecker::OmpStructureChecker(SemanticsContext &context)
   scopeStack_.push_back(&context.globalScope());
 }
 
+void OmpStructureChecker::Enter(const parser::ProgramUnit &) {
+  ClearLabels();
+}
+
 bool OmpStructureChecker::Enter(const parser::MainProgram &x) {
   using StatementProgramStmt = parser::Statement<parser::ProgramStmt>;
   if (auto &stmt{std::get<std::optional<StatementProgramStmt>>(x.t)}) {
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index ce0061594b9fd..35ffe7d1582ca 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -71,6 +71,7 @@ class OmpStructureChecker : public OmpStructureCheckerBase {
 
   using llvmOmpClause = const llvm::omp::Clause;
 
+  void Enter(const parser::ProgramUnit &);
   bool Enter(const parser::MainProgram &);
   void Leave(const parser::MainProgram &);
   bool Enter(const parser::BlockData &);
diff --git a/flang/test/Semantics/OpenMP/branching-program-unit.f90 b/flang/test/Semantics/OpenMP/branching-program-unit.f90
new file mode 100644
index 0000000000000..6f6ec3e6cb5fe
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/branching-program-unit.f90
@@ -0,0 +1,16 @@
+!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
+
+! This should pass semantic checks.
+
+!CHECK: func.func
+
+subroutine f
+  10 continue
+end
+
+subroutine g
+  !$omp parallel
+  goto 10
+  10 continue
+  !$omp end parallel
+end



More information about the flang-commits mailing list