[clang] f3d6035 - [Clang][OpenMP] Fix crash on `safelen` and `simdlen` of different types (#228700)

via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 3 09:09:45 PDT 2026


Author: Akash Manna
Date: 2026-10-03T16:09:34Z
New Revision: f3d6035cc9484ffb55c55e9c9702cf40106cfd6d

URL: https://github.com/llvm/llvm-project/commit/f3d6035cc9484ffb55c55e9c9702cf40106cfd6d
DIFF: https://github.com/llvm/llvm-project/commit/f3d6035cc9484ffb55c55e9c9702cf40106cfd6d.diff

LOG: [Clang][OpenMP] Fix crash on `safelen` and `simdlen` of different types (#228700)

Fixes #108367

When a `simd` directive has both `safelen` and `simdlen`, Sema checks
that `simdlen` is not greater than `safelen`. It compared the two values
with `APSInt`'s `operator>`, which needs both operands to have the same
bit width and signedness. But each clause keeps its argument in the type
it was written with, so in `safelen(11111111111) simdlen(1)` one value
is 64 bits wide and the other 32, and the comparison asserted. Mixed
signedness, as in `safelen(8u) simdlen(4)`, hit a second assertion on
the same line. A build without assertions rejected the valid code
instead, as if `simdlen` were the larger value.

The check now uses `APSInt::compareValues`, which extends the operands
to a common width and handles mixed signedness, so the values themselves
are compared. The error is still emitted whenever `simdlen` is larger
than `safelen`.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Sema/SemaOpenMP.cpp
    clang/test/OpenMP/simd_misc_messages.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 1c041d4bf54e1..d6c40a2920cf5 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1094,6 +1094,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
 - Mapping of expressions with base-pointers through a user-defined mapper (e.g.
   `map(s.p[0:n])`) now conforms to OpenMP's conditional pointer-attachment,
   matching the behavior of such maps outside a mapper.
+- Fixed a crash when the `safelen` and `simdlen` clauses of an OpenMP directive
+  have arguments of 
diff erent integer types. (#GH108367)
 - The `holds` clause on the `assume` directive now lowers side-effect-free
   conditions to `llvm.assume`, enabling downstream optimizations. Previously
   the clause was parsed but its condition was discarded without effect.

diff  --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 6ef86b8391f40..315f869e0b72e 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -11371,7 +11371,7 @@ static bool checkSimdlenSafelenSpecified(Sema &S,
     // If both simdlen and safelen clauses are specified, the value of the
     // simdlen parameter must be less than or equal to the value of the safelen
     // parameter.
-    if (SimdlenRes > SafelenRes) {
+    if (llvm::APSInt::compareValues(SimdlenRes, SafelenRes) > 0) {
       S.Diag(SimdlenLength->getExprLoc(),
              diag::err_omp_wrong_simdlen_safelen_values)
           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();

diff  --git a/clang/test/OpenMP/simd_misc_messages.c b/clang/test/OpenMP/simd_misc_messages.c
index 818bd9056edbc..c3e2bcbe2ccde 100644
--- a/clang/test/OpenMP/simd_misc_messages.c
+++ b/clang/test/OpenMP/simd_misc_messages.c
@@ -274,6 +274,38 @@ void test_safelen_simdlen(void) {
 #pragma omp simd safelen(5) simdlen(6)
   for (i = 0; i < 16; ++i)
     ;
+#pragma omp simd safelen(8) simdlen(4)
+  for (i = 0; i < 16; ++i)
+    ;
+// expected-error at +1 {{the value of 'simdlen' parameter must be less than or equal to the value of the 'safelen' parameter}}
+#pragma omp simd safelen(4) simdlen(8)
+  for (i = 0; i < 16; ++i)
+    ;
+}
+
+// GH108367
+void test_safelen_simdlen_types(void) {
+#pragma omp simd safelen(11111111111) simdlen(1)
+  for (int i = 0; i < 10; i++)
+    ;
+// expected-error at +1 {{the value of 'simdlen' parameter must be less than or equal to the value of the 'safelen' parameter}}
+#pragma omp simd simdlen(11111111111) safelen(1)
+  for (int i = 0; i < 10; i++)
+    ;
+// expected-error at +1 {{the value of 'simdlen' parameter must be less than or equal to the value of the 'safelen' parameter}}
+#pragma omp simd safelen(11111111111) simdlen(22222222222)
+  for (int i = 0; i < 10; i++)
+    ;
+#pragma omp simd safelen(22222222222) simdlen(11111111111)
+  for (int i = 0; i < 10; i++)
+    ;
+#pragma omp simd safelen(8u) simdlen(4)
+  for (int i = 0; i < 10; i++)
+    ;
+// expected-error at +1 {{the value of 'simdlen' parameter must be less than or equal to the value of the 'safelen' parameter}}
+#pragma omp simd safelen(4u) simdlen(8)
+  for (int i = 0; i < 10; i++)
+    ;
 }
 
 void test_collapse(void) {


        


More information about the cfe-commits mailing list