[clang] [Clang] Fix assertion "unsigned range includes negative?" in AnalyzeComparison during Sema of vector comparison with mismatched signed/unsigned types and __builtin_convertvector (PR #182627)
Yutong Zhu via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 2 11:50:14 PST 2026
https://github.com/YutongZhuu updated https://github.com/llvm/llvm-project/pull/182627
>From 9a862f048bcb456aed54118d0b6a80753c1a3f92 Mon Sep 17 00:00:00 2001
From: Yutong Zhu <y25zhu at uwaterloo.ca>
Date: Fri, 20 Feb 2026 18:31:40 -0500
Subject: [PATCH 1/6] Change ``TryGetExprRange`` such that unsigned vectors get
non negative ranges
---
clang/lib/Sema/SemaChecking.cpp | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index ba4b25961d70d..052828107a867 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11500,6 +11500,15 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
Approximate);
}
+ QualType T = E->getType();
+ if (const auto *VT = T->getAs<VectorType>()) {
+ QualType ElemTy = VT->getElementType();
+ if (ElemTy->isUnsignedIntegerType()) {
+ return TryGetExprRange(C, UO->getSubExpr(), MaxWidth,
+ InConstantContext, Approximate);
+ }
+ }
+
std::optional<IntRange> SubRange = TryGetExprRange(
C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
@@ -11518,6 +11527,15 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
Approximate);
}
+ QualType T = E->getType();
+
+ if (const auto *VT = T->getAs<VectorType>()) {
+ QualType ElemTy = VT->getElementType();
+ if (ElemTy->isUnsignedIntegerType()) {
+ return TryGetExprRange(C, UO->getSubExpr(), MaxWidth,
+ InConstantContext, Approximate);
+ }
+ }
std::optional<IntRange> SubRange = TryGetExprRange(
C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
>From 667aa51d47895a223972b672971400b2a8d550a0 Mon Sep 17 00:00:00 2001
From: Yutong Zhu <y25zhu at uwaterloo.ca>
Date: Fri, 20 Feb 2026 19:02:27 -0500
Subject: [PATCH 2/6] Add tests and edit release notes
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/test/Sema/compare.cpp | 14 ++++++++++++++
2 files changed, 16 insertions(+)
create mode 100644 clang/test/Sema/compare.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 77e2078a5ffbf..18c3667894172 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -498,6 +498,8 @@ Improvements to Clang's diagnostics
- Clang now generates a fix-it for C++20 designated initializers when the
initializers do not match the declaration order in the structure.
+- Clang now doesn't throw assertion errors when comparing unsigned vectors(#173614)
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/test/Sema/compare.cpp b/clang/test/Sema/compare.cpp
new file mode 100644
index 0000000000000..fb5d82847e2a0
--- /dev/null
+++ b/clang/test/Sema/compare.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only
+// Expect no assertion failures in this file (#173614).
+typedef unsigned long __attribute__((__vector_size__(8))) W;
+
+int i;
+W g;
+
+void negation(void) {
+ W w = i == (-g);
+}
+
+void bitwiseNot(void) {
+ W w = i == (~g);
+}
>From c1ef4e51f751885035449ac399ad33ed838697be Mon Sep 17 00:00:00 2001
From: Yutong Zhu <115899167+YutongZhuu at users.noreply.github.com>
Date: Mon, 23 Feb 2026 15:57:47 -0500
Subject: [PATCH 3/6] Update clang/docs/ReleaseNotes.rst
Co-authored-by: Mariya Podchishchaeva <mariya.podchishchaeva at intel.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 18c3667894172..74a75c1cd2fdf 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -498,7 +498,7 @@ Improvements to Clang's diagnostics
- Clang now generates a fix-it for C++20 designated initializers when the
initializers do not match the declaration order in the structure.
-- Clang now doesn't throw assertion errors when comparing unsigned vectors(#173614)
+- Clang now doesn't throw assertion errors when comparing unsigned vectors (#GH173614).
Improvements to Clang's time-trace
----------------------------------
>From 02f299633be2c056b28c53a59a33ecac5d22d4e6 Mon Sep 17 00:00:00 2001
From: Yutong Zhu <y25zhu at uwaterloo.ca>
Date: Mon, 23 Feb 2026 17:11:55 -0500
Subject: [PATCH 4/6] Merge compare.cpp into compare.c
---
clang/test/Sema/compare.c | 15 +++++++++++++++
clang/test/Sema/compare.cpp | 14 --------------
2 files changed, 15 insertions(+), 14 deletions(-)
delete mode 100644 clang/test/Sema/compare.cpp
diff --git a/clang/test/Sema/compare.c b/clang/test/Sema/compare.c
index fdae3bc19841e..1dc9d0d878929 100644
--- a/clang/test/Sema/compare.c
+++ b/clang/test/Sema/compare.c
@@ -481,3 +481,18 @@ int test26(short n) {
return ~n == 32768; // expected-warning {{result of comparison of 16-bit signed value == 32768 is always false}}
}
#endif
+
+typedef unsigned long __attribute__((__vector_size__(8))) W;
+void test27(void) {
+ int i;
+ W g;
+ // We expect no assertion failures here.
+ W w = i == (-g); // expected-warning {{}}
+}
+
+void test28(void) {
+ int i;
+ W g;
+ // We expect no assertion failures here.
+ W w = i == (~g); // expected-warning {{}}
+}
diff --git a/clang/test/Sema/compare.cpp b/clang/test/Sema/compare.cpp
deleted file mode 100644
index fb5d82847e2a0..0000000000000
--- a/clang/test/Sema/compare.cpp
+++ /dev/null
@@ -1,14 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only
-// Expect no assertion failures in this file (#173614).
-typedef unsigned long __attribute__((__vector_size__(8))) W;
-
-int i;
-W g;
-
-void negation(void) {
- W w = i == (-g);
-}
-
-void bitwiseNot(void) {
- W w = i == (~g);
-}
>From dcbfd859c42e8e174a42414277655048347549de Mon Sep 17 00:00:00 2001
From: Yutong Zhu <y25zhu at uwaterloo.ca>
Date: Mon, 2 Mar 2026 13:50:06 -0500
Subject: [PATCH 5/6] Deduplicate the type checking logic and tests
---
clang/lib/Sema/SemaChecking.cpp | 28 ++++++++--------------------
clang/test/Sema/compare.c | 10 ++--------
2 files changed, 10 insertions(+), 28 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 052828107a867..d8c8884547f21 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11223,6 +11223,12 @@ static QualType GetExprType(const Expr *E) {
return Ty;
}
+static bool isUnsignedScalarType(QualType T) {
+ if (const auto *VT = T->getAs<VectorType>())
+ T = VT->getElementType();
+ return T->isUnsignedIntegerType();
+}
+
/// Attempts to estimate an approximate range for the given integer expression.
/// Returns a range if successful, otherwise it returns \c std::nullopt if a
/// reliable estimation cannot be determined.
@@ -11495,20 +11501,11 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
return IntRange::forValueOfType(C, GetExprType(E));
case UO_Minus: {
- if (E->getType()->isUnsignedIntegerType()) {
+ if (isUnsignedScalarType(E->getType())) {
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
Approximate);
}
- QualType T = E->getType();
- if (const auto *VT = T->getAs<VectorType>()) {
- QualType ElemTy = VT->getElementType();
- if (ElemTy->isUnsignedIntegerType()) {
- return TryGetExprRange(C, UO->getSubExpr(), MaxWidth,
- InConstantContext, Approximate);
- }
- }
-
std::optional<IntRange> SubRange = TryGetExprRange(
C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
@@ -11522,20 +11519,11 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
}
case UO_Not: {
- if (E->getType()->isUnsignedIntegerType()) {
+ if (isUnsignedScalarType(E->getType())) {
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
Approximate);
}
- QualType T = E->getType();
-
- if (const auto *VT = T->getAs<VectorType>()) {
- QualType ElemTy = VT->getElementType();
- if (ElemTy->isUnsignedIntegerType()) {
- return TryGetExprRange(C, UO->getSubExpr(), MaxWidth,
- InConstantContext, Approximate);
- }
- }
std::optional<IntRange> SubRange = TryGetExprRange(
C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
diff --git a/clang/test/Sema/compare.c b/clang/test/Sema/compare.c
index 1dc9d0d878929..e9e4f17f825ef 100644
--- a/clang/test/Sema/compare.c
+++ b/clang/test/Sema/compare.c
@@ -487,12 +487,6 @@ void test27(void) {
int i;
W g;
// We expect no assertion failures here.
- W w = i == (-g); // expected-warning {{}}
-}
-
-void test28(void) {
- int i;
- W g;
- // We expect no assertion failures here.
- W w = i == (~g); // expected-warning {{}}
+ W w1 = i == (-g); // expected-warning {{}}
+ W w2 = i == (~g); // expected-warning {{}}
}
>From b98756801f7e5b3f65bfac09772cebf07077afa6 Mon Sep 17 00:00:00 2001
From: Yutong Zhu <y25zhu at uwaterloo.ca>
Date: Mon, 2 Mar 2026 14:49:56 -0500
Subject: [PATCH 6/6] Change function name to be clearer
---
clang/lib/Sema/SemaChecking.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index d8c8884547f21..58a0c30fd6825 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11223,7 +11223,7 @@ static QualType GetExprType(const Expr *E) {
return Ty;
}
-static bool isUnsignedScalarType(QualType T) {
+static bool isUnsignedIntegerOrVectorElementType(QualType T) {
if (const auto *VT = T->getAs<VectorType>())
T = VT->getElementType();
return T->isUnsignedIntegerType();
@@ -11501,7 +11501,7 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
return IntRange::forValueOfType(C, GetExprType(E));
case UO_Minus: {
- if (isUnsignedScalarType(E->getType())) {
+ if (isUnsignedIntegerOrVectorElementType(E->getType())) {
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
Approximate);
}
@@ -11519,7 +11519,7 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
}
case UO_Not: {
- if (isUnsignedScalarType(E->getType())) {
+ if (isUnsignedIntegerOrVectorElementType(E->getType())) {
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
Approximate);
}
More information about the cfe-commits
mailing list