[clang] [Clang][OpenMP] Reject 'continue' in a loop with a scan directive (PR #249114)
Anjia Wang via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 15:45:57 PST 2026
https://github.com/ouankou created https://github.com/llvm/llvm-project/pull/249114
The `scan` directive splits the loop body into two structured block sequences. A `continue` of the loop branches out of the sequence it is in, and in the first sequence it also skips the `scan` directive and the whole second sequence. Clang accepted such loops and miscompiled most of them, while GCC rejects them. For example:
```c
#pragma omp parallel for reduction(inscan, + : x)
for (int i = 0; i < n; ++i) {
if (i % 2)
continue;
x += a[i];
#pragma omp scan inclusive(x)
b[i] = x;
}
```
before: b and x differ from the serial loop, even on one thread
after: error: 'continue' statement cannot be used in OpenMP loop
with 'scan' directive
Record each `continue` of an OpenMP loop on its DSA frame, and diagnose it at the end of the loop directive if the loop has a `scan` directive. The `scan` can come after the `continue`, so unlike `break`, which is rejected in any OpenMP loop, it can't be diagnosed at the statement. A `continue` of a loop nested in the body is still allowed.
Assisted-by: Claude Code / Claude Opus 5.5
>From b5fcc950d7eda3ab40b40789bf2c4d31a1d8df46 Mon Sep 17 00:00:00 2001
From: Anjia Wang <anjia.wang at intel.com>
Date: Thu, 3 Dec 2026 23:54:44 +0100
Subject: [PATCH] [Clang][OpenMP] Reject 'continue' in a loop with a scan
directive
The `scan` directive splits the loop body into two structured block
sequences. A `continue` of the loop branches out of the sequence it is
in, and in the first sequence it also skips the `scan` directive and
the whole second sequence. Clang accepted such loops and miscompiled
most of them, while GCC rejects them. For example:
```c
#pragma omp parallel for reduction(inscan, + : x)
for (int i = 0; i < n; ++i) {
if (i % 2)
continue;
x += a[i];
#pragma omp scan inclusive(x)
b[i] = x;
}
```
before: b and x differ from the serial loop, even on one thread
after: error: 'continue' statement cannot be used in OpenMP loop
with 'scan' directive
Record each `continue` of an OpenMP loop on its DSA frame, and diagnose
it at the end of the loop directive if the loop has a `scan` directive.
The `scan` can come after the `continue`, so unlike `break`, which is
rejected in any OpenMP loop, it can't be diagnosed at the statement. A
`continue` of a loop nested in the body is still allowed.
Assisted-by: Claude Code / Claude Opus 5.5
---
.../clang/Basic/DiagnosticSemaKinds.td | 2 +
clang/include/clang/Sema/SemaOpenMP.h | 4 ++
clang/lib/Sema/SemaOpenMP.cpp | 31 +++++++++++++++
clang/lib/Sema/SemaStmt.cpp | 3 ++
clang/test/OpenMP/scan_messages.cpp | 38 +++++++++++++++++++
5 files changed, 78 insertions(+)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df55b87a4394fb..065dcf6161630b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -12847,6 +12847,8 @@ def err_omp_depobj_single_clause_expected : Error<
"exactly one of 'depend', 'destroy', or 'update' clauses is expected">;
def err_omp_scan_single_clause_expected : Error<
"exactly one of 'inclusive' or 'exclusive' clauses is expected">;
+def err_omp_scan_loop_continue : Error<
+ "'continue' statement cannot be used in OpenMP loop with 'scan' directive">;
def err_omp_inclusive_exclusive_not_reduction : Error<
"the list item must appear in 'reduction' clause with the 'inscan' modifier "
"of the parent directive">;
diff --git a/clang/include/clang/Sema/SemaOpenMP.h b/clang/include/clang/Sema/SemaOpenMP.h
index a8a291d02722ce..27846bb138ec24 100644
--- a/clang/include/clang/Sema/SemaOpenMP.h
+++ b/clang/include/clang/Sema/SemaOpenMP.h
@@ -211,6 +211,10 @@ class SemaOpenMP : public SemaBase {
/// \param Init First part of the for loop.
void ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init);
+ /// Called on a 'continue' statement of the loop associated with the current
+ /// OpenMP loop directive.
+ void ActOnOpenMPLoopContinue(SourceLocation ContinueLoc);
+
/// Called for metadirectives with user conditions that require runtime
/// selection. \p Variants holds one complete directive (or the plain
/// associated statement) per entry of \p TraitInfos.
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index cf037dc5a5bd45..c373e2b487ef23 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -211,6 +211,7 @@ class DSAStackTy {
bool LoopStart = false;
bool BodyComplete = false;
SourceLocation PrevScanLocation;
+ SmallVector<SourceLocation, 1> LoopContinueLocs;
SourceLocation PrevOrderedLocation;
SourceLocation InnerTeamsRegionLoc;
/// Reference to the taskgroup task_reduction reference expression.
@@ -999,6 +1000,21 @@ class DSAStackTy {
const SharingMapTy *Top = getSecondOnStackOrNull();
return Top ? Top->PrevScanLocation : SourceLocation();
}
+ /// Return the location of the scan directive in the current region.
+ SourceLocation getScanDirectiveLoc() const {
+ const SharingMapTy *Top = getTopOfStackOrNull();
+ return Top ? Top->PrevScanLocation : SourceLocation();
+ }
+ /// Mark that the loop of the current region has a 'continue' statement.
+ void addLoopContinue(SourceLocation Loc) {
+ if (SharingMapTy *Top = getTopOfStackOrNull())
+ Top->LoopContinueLocs.push_back(Loc);
+ }
+ /// Return the 'continue' statements of the loop of the current region.
+ ArrayRef<SourceLocation> getLoopContinueLocs() const {
+ const SharingMapTy *Top = getTopOfStackOrNull();
+ return Top ? ArrayRef(Top->LoopContinueLocs) : ArrayRef<SourceLocation>();
+ }
/// Mark that parent region already has ordered directive.
void setParentHasOrderedDirective(SourceLocation Loc) {
if (SharingMapTy *Parent = getSecondOnStackOrNull())
@@ -3179,6 +3195,16 @@ void SemaOpenMP::EndOpenMPDSABlock(Stmt *CurDirective) {
checkReductionClauses(SemaRef, DSAStack, D->clauses());
}
+ // A 'continue' of a loop with a scan directive branches out of the
+ // structured block sequence it is in.
+ if (SourceLocation ScanLoc = DSAStack->getScanDirectiveLoc();
+ ScanLoc.isValid()) {
+ for (SourceLocation Loc : DSAStack->getLoopContinueLocs()) {
+ Diag(Loc, diag::err_omp_scan_loop_continue);
+ Diag(ScanLoc, diag::note_omp_directive_here) << "scan";
+ }
+ }
+
DSAStack->pop();
SemaRef.DiscardCleanupsInEvaluationContext();
SemaRef.PopExpressionEvaluationContext();
@@ -10325,6 +10351,11 @@ void SemaOpenMP::ActOnOpenMPLoopInitialization(SourceLocation ForLoc,
DSAStack->setAssociatedLoops(AssociatedLoops - 1);
}
+void SemaOpenMP::ActOnOpenMPLoopContinue(SourceLocation ContinueLoc) {
+ assert(getLangOpts().OpenMP && "OpenMP is not active.");
+ DSAStack->addLoopContinue(ContinueLoc);
+}
+
namespace {
// Utility for OpenMP doacross clause kind
class OMPDoacrossKind {
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 667b1426200e4b..9e147764f65057 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3513,6 +3513,9 @@ StmtResult Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope,
return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop));
}
+ if (S->isOpenMPLoopScope())
+ OpenMP().ActOnOpenMPLoopContinue(ContinueLoc);
+
// A 'continue' that would normally have execution continue on a block outside
// of a compute construct counts as 'branching out of' the compute construct,
// so diagnose here.
diff --git a/clang/test/OpenMP/scan_messages.cpp b/clang/test/OpenMP/scan_messages.cpp
index 45516768090a84..2f6ca2936dfc0b 100644
--- a/clang/test/OpenMP/scan_messages.cpp
+++ b/clang/test/OpenMP/scan_messages.cpp
@@ -98,6 +98,33 @@ label1 : {
#pragma omp scan exclusive(argc, ub[:]) // expected-error {{section length is unspecified and cannot be inferred because subscripted value is an array of unknown bound}}
;
}
+#pragma omp for reduction(inscan, +: argc)
+ for (int i = 0; i < 10; ++i) {
+ if (i)
+ continue; // expected-error {{'continue' statement cannot be used in OpenMP loop with 'scan' directive}}
+ argc += i;
+#pragma omp scan inclusive(argc) // expected-note {{'scan' directive found here}}
+ ;
+ }
+#pragma omp simd reduction(inscan, +: argc)
+ for (int i = 0; i < 10; ++i) {
+ ub[i] = argc;
+#pragma omp scan exclusive(argc) // expected-note {{'scan' directive found here}}
+ if (i)
+ continue; // expected-error {{'continue' statement cannot be used in OpenMP loop with 'scan' directive}}
+ argc += i;
+ }
+#pragma omp parallel for reduction(inscan, +: argc)
+ for (int i = 0; i < 10; ++i) {
+ for (int j = 0; j < 10; ++j)
+ continue;
+#pragma omp simd
+ for (int j = 0; j < 10; ++j)
+ continue;
+ argc += i;
+#pragma omp scan inclusive(argc)
+ ;
+ }
return T();
}
@@ -203,6 +230,17 @@ label1 : {
#pragma omp scan exclusive(argc, ub[:]) // expected-error {{section length is unspecified and cannot be inferred because subscripted value is an array of unknown bound}}
;
}
+#pragma omp parallel for reduction(inscan, +: argc)
+ for (int i = 0; i < 10; ++i) {
+ switch (i) {
+ case 1:
+ continue; // expected-error {{'continue' statement cannot be used in OpenMP loop with 'scan' directive}}
+ }
+ argc += i;
+#pragma omp scan inclusive(argc) // expected-note 2 {{'scan' directive found here}}
+ if (i)
+ continue; // expected-error {{'continue' statement cannot be used in OpenMP loop with 'scan' directive}}
+ }
return tfoobar<int>(ub); // expected-note {{in instantiation of function template specialization 'tfoobar<int>' requested here}}
}
More information about the cfe-commits
mailing list