[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
Fri Feb 20 16:07:15 PST 2026
https://github.com/YutongZhuu created https://github.com/llvm/llvm-project/pull/182627
This PR attempts to fix #173614.
The ``TryGetExprRange`` function now checks if the oprand is an unsigned vector when computing the ranges.
Thanks for reviewing.
>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/2] 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/2] 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);
+}
More information about the cfe-commits
mailing list