[clang] [clang][OpenMP] Use APSInt::compareValues when checking ordered depend count (PR #238672)
Pranjal Srivastava via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 21:26:48 PST 2026
https://github.com/ipranjalks created https://github.com/llvm/llvm-project/pull/238672
In `ProcessOpenMPDoacrossClauseCommon`, `DepCounter` is initialized as a 32-bit unsigned APSInt, whereas `TotalDepCount` is evaluated from the enclosing `ordered(n)` clause expression and retains that expression's bit-width and signedness (e.g., 16-bit int on AVR/MSP430, short, or 64-bit long long). Comparing them directly with operator `>=` asserts on a bit-width or signedness mismatch. Use `llvm::APSInt::compareValues` instead.
Fixes #235971
Assisted-by: Gemini
>From c10a8371a154b16fb3984d844449c124aa938cdf Mon Sep 17 00:00:00 2001
From: Pranjal Srivastava <pranjalks at google.com>
Date: Wed, 4 Nov 2026 05:23:47 +0000
Subject: [PATCH] Use APSInt::compareValues when checking ordered depend count
---
clang/docs/ReleaseNotes.md | 4 ++++
clang/lib/Sema/SemaOpenMP.cpp | 2 +-
clang/test/OpenMP/ordered_messages.cpp | 26 ++++++++++++++++++++++++++
3 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580b..4e37123d33c518 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1578,6 +1578,10 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
captured by copy when a nested construct has `default(firstprivate)` or
`default(private)`, which could silently lose writes such as the result of
a reduction on the nested construct.
+- Fixed a crash when analyzing an OpenMP `ordered` directive with a `depend` or
+ `doacross` clause inside a loop whose `ordered` clause argument has a
+ bit-width other than 32 bits (such as `int` on 16-bit targets or `long long`).
+ (#GH235971)
### SYCL Support
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 88217bd836edc5..290510ee5a95a9 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -23751,7 +23751,7 @@ ProcessOpenMPDoacrossClauseCommon(Sema &SemaRef, bool IsSource,
Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
if (!IsSource) {
if (Stack->getParentOrderedRegionParam().first &&
- DepCounter >= TotalDepCount) {
+ llvm::APSInt::compareValues(DepCounter, TotalDepCount) >= 0) {
SemaRef.Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
continue;
}
diff --git a/clang/test/OpenMP/ordered_messages.cpp b/clang/test/OpenMP/ordered_messages.cpp
index 8575713698647c..ddea01c1830590 100644
--- a/clang/test/OpenMP/ordered_messages.cpp
+++ b/clang/test/OpenMP/ordered_messages.cpp
@@ -416,3 +416,29 @@ int k;
return foo<int>(); // expected-note {{in instantiation of function template specialization 'foo<int>' requested here}}
}
+
+void gh235971() {
+ int k;
+#pragma omp for ordered(1LL)
+ for (int i = 0; i < 10; ++i) {
+#if _OPENMP >= 202111
+#pragma omp ordered doacross(sink : i - 1)
+#pragma omp ordered doacross(sink : i, k) // omp52-error {{unexpected expression: number of expressions is larger than the number of associated loops}}
+#else
+#pragma omp ordered depend(sink : i - 1)
+#pragma omp ordered depend(sink : i, k) // expected-error {{unexpected expression: number of expressions is larger than the number of associated loops}}
+#endif
+ }
+
+ const short One = 1;
+#pragma omp for ordered(One)
+ for (int i = 0; i < 10; ++i) {
+#if _OPENMP >= 202111
+#pragma omp ordered doacross(sink : i - 1)
+#pragma omp ordered doacross(sink : i, k) // omp52-error {{unexpected expression: number of expressions is larger than the number of associated loops}}
+#else
+#pragma omp ordered depend(sink : i - 1)
+#pragma omp ordered depend(sink : i, k) // expected-error {{unexpected expression: number of expressions is larger than the number of associated loops}}
+#endif
+ }
+}
More information about the cfe-commits
mailing list