Skip to content

Commit 3311cbd

Browse files
committed
update semantic checking for __builtin_hlsl_elementwise_clamp; update test with new error message
1 parent ac260e6 commit 3311cbd

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,20 @@ static bool CheckVectorElementCallArgs(Sema *S, CallExpr *TheCall) {
20412041
return false;
20422042
}
20432043

2044+
static bool CheckAllArgsHaveSameType(Sema *S, CallExpr *TheCall) {
2045+
QualType ArgTy0 = TheCall->getArg(0)->getType();
2046+
2047+
for (unsigned I = 1, N = TheCall->getNumArgs(); I < N; ++I) {
2048+
if (!S->getASTContext().hasSameUnqualifiedType(ArgTy0, TheCall->getArg(I)->getType())) {
2049+
S->Diag(TheCall->getBeginLoc(), diag::err_vec_builtin_incompatible_vector)
2050+
<< TheCall->getDirectCallee() << /*useAllTerminology*/ true
2051+
<< SourceRange(TheCall->getArg(0)->getBeginLoc(), TheCall->getArg(N-1)->getEndLoc());
2052+
return true;
2053+
}
2054+
}
2055+
return false;
2056+
}
2057+
20442058
static bool CheckArgTypeMatches(Sema *S, Expr *Arg, QualType ExpectedType) {
20452059
QualType ArgType = Arg->getType();
20462060
if (!S->getASTContext().hasSameUnqualifiedType(ArgType, ExpectedType)) {
@@ -2393,15 +2407,8 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
23932407
if (SemaRef.checkArgCount(TheCall, 3))
23942408
return true;
23952409
if (CheckAnyScalarOrVector(&SemaRef, TheCall, 0) ||
2396-
!(SemaRef.Context.hasSameUnqualifiedType(TheCall->getArg(0)->getType(),
2397-
TheCall->getArg(1)->getType()) &&
2398-
SemaRef.Context.hasSameUnqualifiedType(TheCall->getArg(0)->getType(),
2399-
TheCall->getArg(2)->getType()))) {
2400-
SemaRef.Diag(TheCall->getBeginLoc(), diag::err_typecheck_call_different_arg_types)
2401-
<< TheCall->getArg(0)->getType() << TheCall->getArg(1)->getType()
2402-
<< TheCall->getBeginLoc() << TheCall->getBeginLoc();
2410+
CheckAllArgsHaveSameType(&SemaRef, TheCall))
24032411
return true;
2404-
}
24052412
if (SemaRef.BuiltinElementwiseTernaryMath(
24062413
TheCall, /*CheckForFloatArgs*/
24072414
TheCall->getArg(0)->getType()->hasFloatingRepresentation()))

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected=note
22

33
float2 test_no_second_arg(float2 p0) {
44
return __builtin_hlsl_elementwise_clamp(p0);
@@ -22,52 +22,55 @@ float2 test_clamp_no_second_arg(float2 p0) {
2222

2323
float2 test_clamp_vector_size_mismatch(float3 p0, float2 p1) {
2424
return clamp(p0, p0, p1);
25-
// expected-error@-1 {{call to 'clamp' is ambiguous}}
25+
// expected-warning@-1 {{implicit conversion truncates vector: 'float3' (aka 'vector<float, 3>') to 'vector<float, 2>' (vector of 2 'float' values)}}
26+
// expected-warning@-2 {{implicit conversion truncates vector: 'float3' (aka 'vector<float, 3>') to 'vector<float, 2>' (vector of 2 'float' values)}}
2627
}
2728

2829
float2 test_clamp_builtin_vector_size_mismatch(float3 p0, float2 p1) {
2930
return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
3031
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
3132
}
3233

34+
// allowed by the overloads in hlsl_compat_overloads.h
35+
// support for this overload might be removed in a future version of hlsl
3336
float test_clamp_scalar_mismatch(float p0, half p1) {
3437
return clamp(p1, p0, p1);
35-
// expected-error@-1 {{call to 'clamp' is ambiguous}}
3638
}
3739

40+
// allowed by the overloads in hlsl_compat_overloads.h
41+
// support for this overload might be removed in a future version of hlsl
3842
float2 test_clamp_element_type_mismatch(half2 p0, float2 p1) {
3943
return clamp(p1, p0, p1);
40-
// expected-error@-1 {{call to 'clamp' is ambiguous}}
4144
}
4245

4346
float2 test_builtin_clamp_float2_splat(float p0, float2 p1) {
4447
return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
45-
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must be vectors}}
48+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
4649
}
4750

4851
float3 test_builtin_clamp_float3_splat(float p0, float3 p1) {
4952
return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
50-
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must be vectors}}
53+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
5154
}
5255

5356
float4 test_builtin_clamp_float4_splat(float p0, float4 p1) {
5457
return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
55-
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must be vectors}}
58+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
5659
}
5760

5861
float2 test_clamp_float2_int_splat(float2 p0, int p1) {
5962
return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
60-
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must be vectors}}
63+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
6164
}
6265

6366
float3 test_clamp_float3_int_splat(float3 p0, int p1) {
6467
return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
65-
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must be vectors}}
68+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
6669
}
6770

6871
float2 test_builtin_clamp_int_vect_to_float_vec_promotion(int2 p0, float p1) {
6972
return __builtin_hlsl_elementwise_clamp(p0, p1, p1);
70-
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must be vectors}}
73+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
7174
}
7275

7376
float test_builtin_clamp_bool_type_promotion(bool p0) {
@@ -77,15 +80,15 @@ float test_builtin_clamp_bool_type_promotion(bool p0) {
7780

7881
float builtin_bool_to_float_type_promotion(float p0, bool p1) {
7982
return __builtin_hlsl_elementwise_clamp(p0, p0, p1);
80-
// expected-error@-1 {{3rd argument must be a floating point type (was 'bool')}}
83+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
8184
}
8285

8386
float builtin_bool_to_float_type_promotion2(bool p0, float p1) {
8487
return __builtin_hlsl_elementwise_clamp(p1, p0, p1);
85-
// expected-error@-1 {{2nd argument must be a floating point type (was 'bool')}}
88+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
8689
}
8790

8891
float builtin_clamp_int_to_float_promotion(float p0, int p1) {
8992
return __builtin_hlsl_elementwise_clamp(p0, p0, p1);
90-
// expected-error@-1 {{3rd argument must be a floating point type (was 'int')}}
93+
// expected-error@-1 {{all arguments to '__builtin_hlsl_elementwise_clamp' must have the same type}}
9194
}

0 commit comments

Comments
 (0)