[clang] [clang][Sema][NFC] Consolidate checking for vector reduction builtins (PR #238663)
Chen Miao via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 20:50:02 PST 2026
https://github.com/ChenMiaoi created https://github.com/llvm/llvm-project/pull/238663
Share argument conversion, element type extraction, and result type assignment across the single-argument reduction builtins. Preserve each builtin's element type restrictions and diagnostics, and reuse getVectorElementType for fixed-size and sizeless vectors.
>From 120c217b07dd0e6b8700a605b121d681e6423c81 Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Wed, 4 Nov 2026 12:48:53 +0800
Subject: [PATCH] [clang][Sema][NFC] Consolidate checking for vector reduction
builtins
Share argument conversion, element type extraction, and result type
assignment across the single-argument reduction builtins. Preserve each
builtin's element type restrictions and diagnostics, and reuse
getVectorElementType for fixed-size and sizeless vectors.
---
clang/lib/Sema/SemaChecking.cpp | 67 ++++++++-------------------------
1 file changed, 16 insertions(+), 51 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4b9c1d187dfeb9..62c34d68c9b8da 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4060,54 +4060,9 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
return ExprError();
break;
case Builtin::BI__builtin_reduce_max:
- case Builtin::BI__builtin_reduce_min: {
- if (PrepareBuiltinReduceMathOneArgCall(TheCall))
- return ExprError();
-
- const Expr *Arg = TheCall->getArg(0);
- const auto *TyA = Arg->getType()->getAs<VectorType>();
-
- QualType ElTy;
- if (TyA)
- ElTy = TyA->getElementType();
- else if (Arg->getType()->isSizelessVectorType())
- ElTy = Arg->getType()->getSizelessVectorEltType(Context);
-
- if (ElTy.isNull()) {
- Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
- << 1 << /* vector ty */ 2 << /* no int */ 0 << /* no fp */ 0
- << Arg->getType();
- return ExprError();
- }
-
- TheCall->setType(ElTy);
- break;
- }
+ case Builtin::BI__builtin_reduce_min:
case Builtin::BI__builtin_reduce_maximum:
- case Builtin::BI__builtin_reduce_minimum: {
- if (PrepareBuiltinReduceMathOneArgCall(TheCall))
- return ExprError();
-
- const Expr *Arg = TheCall->getArg(0);
- const auto *TyA = Arg->getType()->getAs<VectorType>();
-
- QualType ElTy;
- if (TyA)
- ElTy = TyA->getElementType();
- else if (Arg->getType()->isSizelessVectorType())
- ElTy = Arg->getType()->getSizelessVectorEltType(Context);
-
- 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();
- }
-
- TheCall->setType(ElTy);
- break;
- }
-
+ case Builtin::BI__builtin_reduce_minimum:
// These builtins support vectors of integers only.
// TODO: ADD/MUL should support floating-point types.
case Builtin::BI__builtin_reduce_add:
@@ -4119,12 +4074,22 @@ 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 (ElTy.isNull() || (FloatOnly && !ElTy->isFloatingType()) ||
+ (IntOnly && !ElTy->isIntegerType())) {
Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
- << 1 << /* vector of */ 4 << /* int */ 1 << /* no fp */ 0
- << Arg->getType();
+ << 1 << (FloatOnly || IntOnly ? /* vector of */ 4 : /* vector ty */ 2)
+ << /* int */ IntOnly << /* fp */ FloatOnly << Arg->getType();
return ExprError();
}
More information about the cfe-commits
mailing list