Skip to content

Commit 49d0ccf

Browse files
committed
Add check for args to match bool type
1 parent f8f728a commit 49d0ccf

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,14 @@ static bool CheckFloatingOrIntRepresentation(Sema *S, CallExpr *TheCall) {
20792079
checkAllSignedTypes);
20802080
}
20812081

2082+
static bool CheckBoolRepresentation(Sema *S, CallExpr *TheCall) {
2083+
auto checkAllBoolTypes = [](clang::QualType PassedType) -> bool {
2084+
return !PassedType->hasIntegerRepresentation();
2085+
};
2086+
return CheckAllArgTypesAreCorrect(S, TheCall, S->Context.BoolTy,
2087+
checkAllBoolTypes);
2088+
}
2089+
20822090
static bool CheckUnsignedIntRepresentation(Sema *S, CallExpr *TheCall) {
20832091
auto checkAllUnsignedTypes = [](clang::QualType PassedType) -> bool {
20842092
return !PassedType->hasUnsignedIntegerRepresentation();
@@ -2250,6 +2258,8 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
22502258
return true;
22512259
if (CheckVectorElementCallArgs(&SemaRef, TheCall))
22522260
return true;
2261+
if (CheckBoolRepresentation(&SemaRef, TheCall))
2262+
return true;
22532263
ExprResult A = TheCall->getArg(0);
22542264
QualType ArgTyA = A.get()->getType();
22552265
// return type is the same as the input type

clang/test/CodeGenHLSL/builtins/and.hlsl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,26 @@ bool4 test_and_bool4(bool4 x, bool4 y) {
4343
return and(x, y);
4444
}
4545

46+
// CHECK-LABEL: define noundef <4 x i1> @_Z13test_and_int4Dv4_iS_(
47+
// CHECK-SAME: <4 x i32> noundef [[X:%.*]], <4 x i32> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
48+
// CHECK-NEXT: [[ENTRY:.*:]]
49+
// CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne <4 x i32> [[X]], zeroinitializer
50+
// CHECK-NEXT: [[TOBOOL1:%.*]] = icmp ne <4 x i32> [[Y]], zeroinitializer
51+
// CHECK-NEXT: [[HLSL_AND:%.*]] = and <4 x i1> [[TOBOOL]], [[TOBOOL1]]
52+
// CHECK-NEXT: ret <4 x i1> [[HLSL_AND]]
53+
//
54+
bool4 test_and_int4(int4 x, int4 y) {
55+
return and(x, y);
56+
}
57+
58+
// CHECK-LABEL: define noundef <4 x i1> @_Z15test_and_float4Dv4_fS_(
59+
// CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[X:%.*]], <4 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
60+
// CHECK-NEXT: [[ENTRY:.*:]]
61+
// CHECK-NEXT: [[TOBOOL:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[X]], zeroinitializer
62+
// CHECK-NEXT: [[TOBOOL1:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une <4 x float> [[Y]], zeroinitializer
63+
// CHECK-NEXT: [[HLSL_AND:%.*]] = and <4 x i1> [[TOBOOL]], [[TOBOOL1]]
64+
// CHECK-NEXT: ret <4 x i1> [[HLSL_AND]]
65+
//
66+
bool4 test_and_float4(float4 x, float4 y) {
67+
return and(x, y);
68+
}

clang/test/SemaHLSL/BuiltIns/and-errors.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ struct S {
2323

2424
bool test_invalid_type_conversion(S s) {
2525
return __builtin_hlsl_and(s, s);
26-
// expected-error@-1{{no viable conversion from returned value of type 'S' to function return type 'bool'}}
26+
// expected-error@-1{{passing 'S' to parameter of incompatible type 'bool'}}
2727
}

0 commit comments

Comments
 (0)