[clang] [clang][Sema][NFC] Consolidate checking for vector reduction builtins (PR #238663)

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 05:50:14 PST 2026


================
@@ -4119,11 +4074,34 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
       return ExprError();
 
     const Expr *Arg = TheCall->getArg(0);
-
     QualType ElTy = getVectorElementType(Context, Arg->getType());
-    if (ElTy.isNull() || !ElTy->isIntegerType()) {
+
+    bool FloatOnly = BuiltinID == Builtin::BI__builtin_reduce_maximum ||
+                     BuiltinID == Builtin::BI__builtin_reduce_minimum;
+
+    bool IntOnly = BuiltinID == Builtin::BI__builtin_reduce_add ||
+                   BuiltinID == Builtin::BI__builtin_reduce_mul ||
+                   BuiltinID == Builtin::BI__builtin_reduce_xor ||
+                   BuiltinID == Builtin::BI__builtin_reduce_or ||
+                   BuiltinID == Builtin::BI__builtin_reduce_and;
+
+    if (FloatOnly) {
+      if (ElTy.isNull() || !ElTy->isFloatingType()) {
+        Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+            << 1 << /* vector of */ 4 << /* no int */ 0 << /* fp */ 1
+            << Arg->getType();
+        return ExprError();
+      }
+    } else if (IntOnly) {
+      if (ElTy.isNull() || !ElTy->isIntegerType()) {
+        Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+            << 1 << /* vector of */ 4 << /* int */ 1 << /* no fp */ 0
+            << Arg->getType();
+        return ExprError();
+      }
+    } else if (ElTy.isNull()) {
       Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
-          << 1 << /* vector of */ 4 << /* int */ 1 << /* no fp */ 0
+          << 1 << /* vector ty */ 2 << /* no int */ 0 << /* no fp */ 0
           << Arg->getType();
       return ExprError();
----------------
ChenMiaoi wrote:

Like this?

``` cc
if (ElTy.isNull()) { Diag Float/Int }

if (FloatOnly && !ElTy->isFloatingType()) { Diag Float }

if (IntOnly && !ElTy->isIntegerType()) { Diag Int } 
```

But if we move it earlier, wouldn’t the diagnostic under the `isNull` check change to something like the following? Because after moving it earlier, the diagnostic would also need to account for both floating-point and integer cases.

``` cc
<< (FloatOnly || IntOnly ? /* vector of */ 4 : /* vector ty */ 2)
<< /* int */ IntOnly << /* fp */ FloatOnly << Arg->getType();
```

https://github.com/llvm/llvm-project/pull/238663


More information about the cfe-commits mailing list