[clang] [clang][OpenMP] Use APSInt::compareValues when checking ordered depend count (PR #238672)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 21:27:24 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Pranjal Srivastava (ipranjalks)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/238672.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+4) 
- (modified) clang/lib/Sema/SemaOpenMP.cpp (+1-1) 
- (modified) clang/test/OpenMP/ordered_messages.cpp (+26) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580..4e37123d33c51 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 88217bd836edc..290510ee5a95a 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 8575713698647..ddea01c183059 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
+  }
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/238672


More information about the cfe-commits mailing list