[clang] [Clang][Sema] Report diagnostic for invalid mask in shufflevector (PR #221558)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 6 04:33:32 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Amr Hesham (AmrDeveloper)
<details>
<summary>Changes</summary>
Clang used to accept a vector with an integer representation as a valid mask; otherwise, it reported a type mismatch. This led to accepting a vector of booleans as a valid mask and reporting that types are not equal if both arguments are equal but not vectors of integers.
Currently, it checks for the mask type and reports a clear error message if the mask is a vector of bool or not a vector of integers
Fixes: #<!-- -->218132
---
Full diff: https://github.com/llvm/llvm-project/pull/221558.diff
4 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+2)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2)
- (modified) clang/lib/Sema/SemaChecking.cpp (+11-2)
- (added) clang/test/SemaCXX/vector-shuffle.cpp (+16)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a43ed2b924622..0c4756572f52c 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -482,6 +482,8 @@ features cannot lower the translation-unit ABI level;
dimension that is a zero integer constant, as in `struct Empty vla[n]` or
`int vla[n][0]`. (#GH28328)
+- Clang now diagnoses passing wrong vector type as a mask to `__builtin_shufflevector`. (#GH218132)
+
### Improvements to Clang's time-trace
### Improvements to Coverage Mapping
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9bd0a526654c7..b118c9d0f3fbe 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -11446,6 +11446,8 @@ def err_shufflevector_argument_too_large : Error<
"of vector elements">;
def err_shufflevector_minus_one_is_undefined_behavior_constexpr : Error<
"index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position %0 is not permitted in a constexpr context">;
+def err_shufflevector_incompatible_mask : Error<
+ "second argument to __builtin_shufflevector must be a vector of any integer type other than boolean (was %0)">;
def err_convertvector_non_vector : Error<
"first argument to __builtin_convertvector must be a vector">;
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index f0a1a529841b2..a670c8a19b1a5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6588,8 +6588,17 @@ ExprResult Sema::BuiltinShuffleVector(CallExpr *TheCall) {
// with mask. If so, verify that RHS is an integer vector type with the
// same number of elts as lhs.
if (NumArgs == 2) {
- if (!RHSType->hasIntegerRepresentation() ||
- RHSType->castAs<VectorType>()->getNumElements() != NumElements)
+ auto *RHSVecType = RHSType->castAs<VectorType>();
+ if (RHSVecType->getElementType()->isBooleanType() ||
+ !RHSVecType->getElementType()->isIntegerType()) {
+ return ExprError(Diag(TheCall->getBeginLoc(),
+ diag::err_shufflevector_incompatible_mask)
+ << RHSType
+ << SourceRange(TheCall->getArg(0)->getBeginLoc(),
+ TheCall->getArg(1)->getEndLoc()));
+ }
+
+ if (RHSVecType->getNumElements() != NumElements)
return ExprError(Diag(TheCall->getBeginLoc(),
diag::err_vec_builtin_incompatible_vector)
<< TheCall->getDirectCallee()
diff --git a/clang/test/SemaCXX/vector-shuffle.cpp b/clang/test/SemaCXX/vector-shuffle.cpp
new file mode 100644
index 0000000000000..c204db2d02a39
--- /dev/null
+++ b/clang/test/SemaCXX/vector-shuffle.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown %s -Wno-null-conversion -fsyntax-only -verify
+
+typedef bool v8b __attribute__((ext_vector_type(8)));
+typedef float v8f __attribute__((ext_vector_type(8)));
+
+void vector_of_bool_mask() {
+ v8b a;
+ v8b b;
+ auto r = __builtin_shufflevector(a, b); // expected-error {{second argument to __builtin_shufflevector must be a vector of any integer type other than boolean (was 'v8b' (vector of 8 'bool' values))}}
+}
+
+void vector_of_float_mask() {
+ v8f a;
+ v8f b;
+ auto r = __builtin_shufflevector(a, b); // expected-error {{second argument to __builtin_shufflevector must be a vector of any integer type other than boolean (was 'v8f' (vector of 8 'float' values))}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/221558
More information about the cfe-commits
mailing list