[clang] disable unary, vector mask (PR #130400)

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 7 23:45:34 PST 2025


https://github.com/wsehjk created https://github.com/llvm/llvm-project/pull/130400

This pr is to fix #92342.


>From a6ca9cc820e98b093215b1bd0787d4fa3d31742d Mon Sep 17 00:00:00 2001
From: wang shiwen <Wse1714401046 at gmail.com>
Date: Sat, 8 Mar 2025 15:40:22 +0800
Subject: [PATCH] disable unary, vector mask

---
 clang/lib/Sema/SemaChecking.cpp              | 24 +++++---------------
 clang/test/Sema/constant_builtins_vector.cpp |  4 ++++
 2 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index fac9a58fa2689..8305da3962c88 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8113,16 +8113,15 @@ bool Sema::BuiltinVSX(CallExpr *TheCall) {
 
 /// BuiltinShuffleVector - Handle __builtin_shufflevector.
 // This is declared to take (...), so we have to check everything.
+// disable unary, vector mask: (lhs, mask)
+// allow binary, scalar mask: (lhs, rhs, index, ..., index)
 ExprResult Sema::BuiltinShuffleVector(CallExpr *TheCall) {
-  if (TheCall->getNumArgs() < 2)
+  if (TheCall->getNumArgs() <= 2)
     return ExprError(Diag(TheCall->getEndLoc(),
                           diag::err_typecheck_call_too_few_args_at_least)
-                     << 0 /*function call*/ << 2 << TheCall->getNumArgs()
+                     << 0 /*function call*/ << 3 << TheCall->getNumArgs()
                      << /*is non object*/ 0 << TheCall->getSourceRange());
 
-  // Determine which of the following types of shufflevector we're checking:
-  // 1) unary, vector mask: (lhs, mask)
-  // 2) binary, scalar mask: (lhs, rhs, index, ..., index)
   QualType resType = TheCall->getArg(0)->getType();
   unsigned numElements = 0;
 
@@ -8141,19 +8140,8 @@ ExprResult Sema::BuiltinShuffleVector(CallExpr *TheCall) {
     numElements = LHSType->castAs<VectorType>()->getNumElements();
     unsigned numResElements = TheCall->getNumArgs() - 2;
 
-    // Check to see if we have a call with 2 vector arguments, the unary shuffle
-    // with mask.  If so, verify that RHS is an integer vector type with the
-    // same number of elts as lhs.
-    if (TheCall->getNumArgs() == 2) {
-      if (!RHSType->hasIntegerRepresentation() ||
-          RHSType->castAs<VectorType>()->getNumElements() != numElements)
-        return ExprError(Diag(TheCall->getBeginLoc(),
-                              diag::err_vec_builtin_incompatible_vector)
-                         << TheCall->getDirectCallee()
-                         << /*isMorethantwoArgs*/ false
-                         << SourceRange(TheCall->getArg(1)->getBeginLoc(),
-                                        TheCall->getArg(1)->getEndLoc()));
-    } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
+
+    if (!Context.hasSameUnqualifiedType(LHSType, RHSType) ) {
       return ExprError(Diag(TheCall->getBeginLoc(),
                             diag::err_vec_builtin_incompatible_vector)
                        << TheCall->getDirectCallee()
diff --git a/clang/test/Sema/constant_builtins_vector.cpp b/clang/test/Sema/constant_builtins_vector.cpp
index c6b1b37cef28b..211e914ef0c65 100644
--- a/clang/test/Sema/constant_builtins_vector.cpp
+++ b/clang/test/Sema/constant_builtins_vector.cpp
@@ -690,6 +690,10 @@ constexpr vector4char vector4charConst1 = {0, 1, 2, 3};
 constexpr vector4char vector4charConst2 = {4, 5, 6, 7};
 constexpr vector8char vector8intConst = {8, 9, 10, 11, 12, 13, 14, 15};
 
+
+constexpr vector4char vectorShuffle =
+    __builtin_shufflevector(vector4charConst1, vector4charConst2);// expected-error {{too few arguments to function call, expected at least 3, have 2}}
+
 constexpr vector4char vectorShuffle1 =
     __builtin_shufflevector(vector4charConst1, vector4charConst2, 0, 1, 2, 3);
 static_assert(__builtin_bit_cast(unsigned, vectorShuffle1) ==



More information about the cfe-commits mailing list