Skip to content

Commit 8cc9a48

Browse files
authored
[clang] Improve diagnostics for vector builtins (#125673)
This commit improves the diagnostics for vector (elementwise) builtins in a couple of ways. It primarily provides more precise type-checking diagnostics for builtins with specific type requirements. Previously many builtins were receiving a catch-all diagnostic suggesting types which aren't valid. It also makes consistent the type-checking behaviour between various binary and ternary builtins. The binary builtins would check for mismatched argument types before specific type requirements, whereas ternary builtins would perform the checks in the reverse order. The binary builtins now behave as the ternary ones do.
1 parent e6c3d94 commit 8cc9a48

24 files changed

+396
-386
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12523,16 +12523,31 @@ def err_builtin_is_within_lifetime_invalid_arg : Error<
1252312523
"%select{non-|function }0pointer argument to '__builtin_is_within_lifetime' "
1252412524
"is not allowed">;
1252512525

12526-
def err_builtin_invalid_arg_type: Error <
12527-
"%ordinal0 argument must be "
12528-
"%select{a vector, integer or floating point type|a matrix|"
12529-
"a pointer to a valid matrix element type|"
12530-
"a signed integer or floating point type|a vector type|"
12531-
"a floating point type|"
12532-
"a vector of integers|"
12533-
"an unsigned integer|"
12534-
"an 'int'|"
12535-
"a vector of floating points}1 (was %2)">;
12526+
// A multi-component builtin type diagnostic. The first component broadly
12527+
// selects a scalar or container type (scalar, vector or matrix). The second
12528+
// component selects integer types and the third component selects
12529+
// floating-point types. Any component can be left empty.
12530+
def err_builtin_invalid_arg_type: Error<
12531+
"%ordinal0 argument must be a "
12532+
// First component: scalar or container types
12533+
"%select{|scalar|vector|matrix|vector of|scalar or vector of}1"
12534+
// A comma after generic vector/matrix types if there are non-empty second
12535+
// and third components, to initiate a list.
12536+
"%plural{[2,3]:%plural{0:|:%plural{0:|:,}2}3|:}1"
12537+
// A space after a non-empty first component
12538+
"%plural{0:|: }1"
12539+
// Second component: integer-like types
12540+
"%select{|integer|signed integer|unsigned integer|'int'|"
12541+
"pointer to a valid matrix element}2"
12542+
// A space after a non-empty second component
12543+
"%plural{0:|: }2"
12544+
// An 'or' if non-empty second and third components are combined
12545+
"%plural{0:|:%plural{0:|:or }2}3"
12546+
// Third component: floating-point types
12547+
"%select{|floating-point}3"
12548+
// A space after a non-empty third component
12549+
"%plural{0:|: }3"
12550+
"%plural{[0,3]:type|:types}1 (was %4)">;
1253612551

1253712552
def err_builtin_matrix_disabled: Error<
1253812553
"matrix types extension is disabled. Pass -fenable-matrix to enable it">;

clang/include/clang/Sema/Sema.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,9 +2353,18 @@ class Sema final : public SemaBase {
23532353
bool CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
23542354
const FunctionProtoType *Proto);
23552355

2356+
enum class EltwiseBuiltinArgTyRestriction {
2357+
None,
2358+
FloatTy,
2359+
IntegerTy,
2360+
SignedIntOrFloatTy,
2361+
};
2362+
23562363
/// \param FPOnly restricts the arguments to floating-point types.
2357-
std::optional<QualType> BuiltinVectorMath(CallExpr *TheCall,
2358-
bool FPOnly = false);
2364+
std::optional<QualType>
2365+
BuiltinVectorMath(CallExpr *TheCall,
2366+
EltwiseBuiltinArgTyRestriction ArgTyRestr =
2367+
EltwiseBuiltinArgTyRestriction::None);
23592368
bool BuiltinVectorToScalarMath(CallExpr *TheCall);
23602369

23612370
void checkLifetimeCaptureBy(FunctionDecl *FDecl, bool IsMemberFunction,
@@ -2459,9 +2468,13 @@ class Sema final : public SemaBase {
24592468
bool *ICContext = nullptr,
24602469
bool IsListInit = false);
24612470

2462-
bool BuiltinElementwiseTernaryMath(CallExpr *TheCall,
2463-
bool CheckForFloatArgs = true);
2464-
bool PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall);
2471+
bool
2472+
BuiltinElementwiseTernaryMath(CallExpr *TheCall,
2473+
EltwiseBuiltinArgTyRestriction ArgTyRestr =
2474+
EltwiseBuiltinArgTyRestriction::FloatTy);
2475+
bool PrepareBuiltinElementwiseMathOneArgCall(
2476+
CallExpr *TheCall, EltwiseBuiltinArgTyRestriction ArgTyRestr =
2477+
EltwiseBuiltinArgTyRestriction::None);
24652478

24662479
private:
24672480
void CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
@@ -2570,7 +2583,9 @@ class Sema final : public SemaBase {
25702583
AtomicExpr::AtomicOp Op);
25712584

25722585
/// \param FPOnly restricts the arguments to floating-point types.
2573-
bool BuiltinElementwiseMath(CallExpr *TheCall, bool FPOnly = false);
2586+
bool BuiltinElementwiseMath(CallExpr *TheCall,
2587+
EltwiseBuiltinArgTyRestriction ArgTyRestr =
2588+
EltwiseBuiltinArgTyRestriction::None);
25742589
bool PrepareBuiltinReduceMathOneArgCall(CallExpr *TheCall);
25752590

25762591
bool BuiltinNonDeterministicValue(CallExpr *TheCall);

0 commit comments

Comments
 (0)