[clang] [Clang][OpenMP] Fix crash on a `sizes` argument of type `bool` or `_BitInt` (PR #239214)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 04:47:12 PST 2026
https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/239214
Fixes #229039
`tile` and `stripe` clamp a non-constant `sizes` argument to at least one with `(TS <= 0) ? 1 : TS`. The `0` and `1` literals took their width from `getTypeSize`, so for a `bool` size they were 8 bits wide for a 1-bit type and `IntegerLiteral` asserted. `_BitInt` and `enum : bool` sizes hit the same assert.
The literals now take their width from `getIntWidth`, which is what `IntegerLiteral` checks and what the other loop transformations already use.
AI assisted!
>From 5dd4a73a29413194145c3770f7e2d153580e2dff Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Thu, 5 Nov 2026 18:14:44 +0530
Subject: [PATCH] [Clang][OpenMP] Fix crash on a `sizes` argument of type
`bool` or `_BitInt`
Fixes #229039
`tile` and `stripe` clamp a non-constant `sizes` argument to at least one with `(TS <= 0) ? 1 : TS`. The `0` and `1` literals took their width from `getTypeSize`, so for a `bool` size they were 8 bits wide for a 1-bit type and `IntegerLiteral` asserted. `_BitInt` and `enum : bool` sizes hit the same assert.
The literals now take their width from `getIntWidth`, which is what `IntegerLiteral` checks and what the other loop transformations already use.
AI assisted!
---
clang/docs/ReleaseNotes.md | 2 +
clang/lib/Sema/SemaOpenMP.cpp | 4 +-
clang/test/OpenMP/stripe_messages.cpp | 38 +++++++++++++
clang/test/OpenMP/tile_messages.cpp | 80 +++++++++++++++++++++++++++
4 files changed, 122 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..50dd30c31d5669 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1518,6 +1518,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
`order(concurrent)` clause. Previously, such constructs (e.g. `parallel`
inside `tile` inside `simd`, or `critical` inside `tile` inside a loop with
`order(concurrent)`) were incorrectly accepted.
+- Fixed a crash when a `tile` or `stripe` `sizes` argument has type `bool` or
+ `_BitInt`. (#GH229039)
- Added support for capturing structured bindings in OpenMP regions
(a C++20 extension; warned as an extension in C++17). Individual bindings
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 9acf3b3724527c..4d12b2d2ab6289 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -16094,7 +16094,7 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
// (TS <= 0) ? 1 : TS
// \endcode
QualType DimTy = DimTileSizeExpr->getType();
- uint64_t DimWidth = Context.getTypeSize(DimTy);
+ unsigned DimWidth = Context.getIntWidth(DimTy);
IntegerLiteral *Zero = IntegerLiteral::Create(
Context, llvm::APInt::getZero(DimWidth), DimTy, {});
IntegerLiteral *One =
@@ -16448,7 +16448,7 @@ StmtResult SemaOpenMP::ActOnOpenMPStripeDirective(ArrayRef<OMPClause *> Clauses,
// (TS <= 0) ? 1 : TS
// \endcode
QualType DimTy = DimStripeSizeExpr->getType();
- uint64_t DimWidth = Context.getTypeSize(DimTy);
+ unsigned DimWidth = Context.getIntWidth(DimTy);
IntegerLiteral *Zero = IntegerLiteral::Create(
Context, llvm::APInt::getZero(DimWidth), DimTy, {});
IntegerLiteral *One =
diff --git a/clang/test/OpenMP/stripe_messages.cpp b/clang/test/OpenMP/stripe_messages.cpp
index 2e4675c35cc6c9..6b10def3494c69 100644
--- a/clang/test/OpenMP/stripe_messages.cpp
+++ b/clang/test/OpenMP/stripe_messages.cpp
@@ -182,3 +182,41 @@ void stripe_of_reverse_intervening() {
;
}
}
+
+// Clang would previously crash on sizes of type bool or _BitInt.
+namespace GH229039 {
+enum E : bool { Off, On };
+
+void nonconstant_bool(bool b) {
+#pragma omp stripe sizes(b)
+ for (int i = 0; i < 2; i++)
+ ;
+}
+
+void constant_bool() {
+#pragma omp stripe sizes(true)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+void bitint(_BitInt(5) n) {
+#pragma omp stripe sizes(n)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+void enum_bool(E e) {
+#pragma omp stripe sizes(e)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+template <bool B>
+void nttp_bool() {
+#pragma omp stripe sizes(B)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+void instantiate() { nttp_bool<true>(); }
+} // namespace GH229039
diff --git a/clang/test/OpenMP/tile_messages.cpp b/clang/test/OpenMP/tile_messages.cpp
index 72ceef71566d69..3efaec2e858e94 100644
--- a/clang/test/OpenMP/tile_messages.cpp
+++ b/clang/test/OpenMP/tile_messages.cpp
@@ -223,3 +223,83 @@ void tile_of_reverse_intervening() {
;
}
}
+
+// Clang would previously crash on sizes of type bool or _BitInt.
+namespace GH229039 {
+enum E : bool { Off, On };
+
+void nonconstant_bool(bool b) {
+#pragma omp tile sizes(b)
+ for (int i = 0; i < 2; i++)
+ ;
+}
+
+void constant_bool() {
+#pragma omp tile sizes(true)
+ for (int i = 0; i < 7; ++i)
+ ;
+
+ // Not diagnosed, like sizes(0u).
+#pragma omp tile sizes(false)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+void bitint(_BitInt(5) n) {
+#pragma omp tile sizes(n)
+ for (int i = 0; i < 7; ++i)
+ ;
+
+#pragma omp tile sizes((_BitInt(5))3)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+void enum_bool(E e) {
+#pragma omp tile sizes(e)
+ for (int i = 0; i < 7; ++i)
+ ;
+
+#pragma omp tile sizes(On)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+template <bool B>
+void nttp_bool() {
+#pragma omp tile sizes(B)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+template <typename T>
+void type_dependent(T t) {
+#pragma omp tile sizes(t)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+void instantiate() {
+ nttp_bool<true>();
+ type_dependent(true);
+ type_dependent<_BitInt(5)>(3);
+}
+
+void consumed(bool b) {
+#pragma omp for
+#pragma omp tile sizes(b)
+ for (int i = 0; i < 7; ++i)
+ ;
+
+#pragma omp unroll partial(2)
+#pragma omp tile sizes(b)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+
+void nonconstant_int(int n) {
+#pragma omp tile sizes(n)
+ for (int i = 0; i < 7; ++i)
+ ;
+}
+} // namespace GH229039
More information about the cfe-commits
mailing list