Skip to content

Commit c56202d

Browse files
committed
implement @farzonl suggestions
1 parent f84e2c9 commit c56202d

File tree

6 files changed

+71
-96
lines changed

6 files changed

+71
-96
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,10 +4897,16 @@ def HLSLClamp : LangBuiltin<"HLSL_LANG"> {
48974897
let Prototype = "void(...)";
48984898
}
48994899

4900-
def HLSLCross: LangBuiltin<"HLSL_LANG"> {
4901-
let Spellings = ["__builtin_hlsl_cross"];
4900+
def HLSLCrossFloat: LangBuiltin<"HLSL_LANG"> {
4901+
let Spellings = ["__builtin_hlsl_crossf32"];
49024902
let Attributes = [NoThrow, Const];
4903-
let Prototype = "void(...)";
4903+
let Prototype = "_ExtVector<3, float>(_ExtVector<3, float>, _ExtVector<3, float>)";
4904+
}
4905+
4906+
def HLSLCrossHalf: LangBuiltin<"HLSL_LANG"> {
4907+
let Spellings = ["__builtin_hlsl_crossf16"];
4908+
let Attributes = [NoThrow, Const];
4909+
let Prototype = "_ExtVector<3, __fp16>(_ExtVector<3, __fp16>, _ExtVector<3, __fp16>)";
49044910
}
49054911

49064912
def HLSLDegrees : LangBuiltin<"HLSL_LANG"> {
@@ -4959,7 +4965,7 @@ def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
49594965

49604966
def HLSLMad : LangBuiltin<"HLSL_LANG"> {
49614967
let Spellings = ["__builtin_hlsl_mad"];
4962-
let Attributes = [NoThrow, Const];
4968+
let Attributes = [NoThrow, Const, CustomTypeChecking];
49634969
let Prototype = "void(...)";
49644970
}
49654971

clang/lib/CodeGen/CGHLSLBuiltins.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
351351
/*ReturnType=*/OpX->getType(), Intr,
352352
ArrayRef<Value *>{OpX, OpMin, OpMax}, nullptr, "hlsl.clamp");
353353
}
354-
case Builtin::BI__builtin_hlsl_cross: {
354+
case Builtin::BI__builtin_hlsl_crossf16:
355+
case Builtin::BI__builtin_hlsl_crossf32: {
355356
Value *Op0 = EmitScalarExpr(E->getArg(0));
356357
Value *Op1 = EmitScalarExpr(E->getArg(1));
357358
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&

clang/lib/Headers/hlsl/hlsl_alias_intrinsics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,10 +1882,10 @@ uint64_t4 reversebits(uint64_t4);
18821882
/// x[0] * y[1] - y[0] * x[1]
18831883

18841884
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1885-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_cross)
1885+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_crossf16)
18861886
half3 cross(half3, half3);
18871887

1888-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_cross)
1888+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_crossf32)
18891889
float3 cross(float3, float3);
18901890

18911891
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,19 +2045,6 @@ static bool CheckAllArgTypesAreCorrect(
20452045
return false;
20462046
}
20472047

2048-
static bool CheckFloatOrHalfVecRepresentation(Sema *S, SourceLocation Loc,
2049-
int ArgOrdinal,
2050-
clang::QualType PassedType) {
2051-
if (auto *VecTy = PassedType->getAs<VectorType>())
2052-
if (VecTy->getElementType()->isHalfType() ||
2053-
VecTy->getElementType()->isFloat32Type())
2054-
return false;
2055-
2056-
return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
2057-
<< ArgOrdinal << /* vector of */ 4 << /* no int */ 0
2058-
<< /* half or float */ 2 << PassedType;
2059-
}
2060-
20612048
static bool CheckFloatOrHalfRepresentation(Sema *S, SourceLocation Loc,
20622049
int ArgOrdinal,
20632050
clang::QualType PassedType) {
@@ -2085,11 +2072,14 @@ static bool CheckModifiableLValue(Sema *S, CallExpr *TheCall,
20852072

20862073
static bool CheckNoDoubleVectors(Sema *S, SourceLocation Loc, int ArgOrdinal,
20872074
clang::QualType PassedType) {
2088-
if (const auto *VecTy = PassedType->getAs<VectorType>())
2089-
if (VecTy->getElementType()->isDoubleType())
2090-
return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
2091-
<< ArgOrdinal << /* scalar */ 1 << /* no int */ 0 << /* fp */ 1
2092-
<< PassedType;
2075+
const auto *VecTy = PassedType->getAs<VectorType>();
2076+
if (!VecTy)
2077+
return false;
2078+
2079+
if (VecTy->getElementType()->isDoubleType())
2080+
return S->Diag(Loc, diag::err_builtin_invalid_arg_type)
2081+
<< ArgOrdinal << /* scalar */ 1 << /* no int */ 0 << /* fp */ 1
2082+
<< PassedType;
20932083
return false;
20942084
}
20952085

@@ -2383,11 +2373,15 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
23832373
case Builtin::BI__builtin_hlsl_asdouble: {
23842374
if (SemaRef.checkArgCount(TheCall, 2))
23852375
return true;
2386-
if (CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.UnsignedIntTy,
2387-
0)) // only check for uint
2376+
if (CheckScalarOrVector(
2377+
&SemaRef, TheCall,
2378+
/*only check for uint*/ SemaRef.Context.UnsignedIntTy,
2379+
/* arg index */ 0))
23882380
return true;
2389-
if (CheckScalarOrVector(&SemaRef, TheCall, SemaRef.Context.UnsignedIntTy,
2390-
1)) // only check for uint
2381+
if (CheckScalarOrVector(
2382+
&SemaRef, TheCall,
2383+
/*only check for uint*/ SemaRef.Context.UnsignedIntTy,
2384+
/* arg index */ 1))
23912385
return true;
23922386
if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
23932387
return true;
@@ -2402,45 +2396,8 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
24022396
return true;
24032397
break;
24042398
}
2405-
case Builtin::BI__builtin_hlsl_cross: {
2406-
if (SemaRef.checkArgCount(TheCall, 2))
2407-
return true;
2408-
2409-
// ensure args are a half3 or float3
2410-
if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
2411-
CheckFloatOrHalfVecRepresentation))
2412-
return true;
2413-
if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
2414-
return true;
2415-
// ensure both args have 3 elements
2416-
int NumElementsArg1 =
2417-
TheCall->getArg(0)->getType()->castAs<VectorType>()->getNumElements();
2418-
int NumElementsArg2 =
2419-
TheCall->getArg(1)->getType()->castAs<VectorType>()->getNumElements();
2420-
2421-
if (NumElementsArg1 != 3) {
2422-
int LessOrMore = NumElementsArg1 > 3 ? 1 : 0;
2423-
SemaRef.Diag(TheCall->getBeginLoc(),
2424-
diag::err_vector_incorrect_num_elements)
2425-
<< LessOrMore << 3 << NumElementsArg1 << /*operand*/ 1;
2426-
return true;
2427-
}
2428-
if (NumElementsArg2 != 3) {
2429-
int LessOrMore = NumElementsArg2 > 3 ? 1 : 0;
2430-
2431-
SemaRef.Diag(TheCall->getBeginLoc(),
2432-
diag::err_vector_incorrect_num_elements)
2433-
<< LessOrMore << 3 << NumElementsArg2 << /*operand*/ 1;
2434-
return true;
2435-
}
2436-
2437-
ExprResult A = TheCall->getArg(0);
2438-
QualType ArgTyA = A.get()->getType();
2439-
// return type is the same as the input type
2440-
TheCall->setType(ArgTyA);
2441-
break;
2442-
}
24432399
case Builtin::BI__builtin_hlsl_dot: {
2400+
// arg count is checked by BuiltinVectorToScalarMath
24442401
if (SemaRef.BuiltinVectorToScalarMath(TheCall))
24452402
return true;
24462403
if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall, CheckNoDoubleVectors))

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,60 @@
22

33
void test_too_few_arg()
44
{
5-
return __builtin_hlsl_cross();
5+
return cross();
6+
// expected-error@-1 {{no matching function for call to 'cross'}}
7+
// expected-note@hlsl/hlsl_alias_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 0 were provided}}
8+
// expected-note@hlsl/hlsl_alias_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 0 were provided}}
9+
}
10+
11+
void test_too_few_arg_f32()
12+
{
13+
return __builtin_hlsl_crossf32();
614
// expected-error@-1 {{too few arguments to function call, expected 2, have 0}}
715
}
816

9-
void test_too_many_arg(float3 p0)
17+
void test_too_few_arg_f16()
1018
{
11-
return __builtin_hlsl_cross(p0, p0, p0);
12-
// expected-error@-1 {{too many arguments to function call, expected 2, have 3}}
19+
return __builtin_hlsl_crossf16();
20+
// expected-error@-1 {{too few arguments to function call, expected 2, have 0}}
1321
}
1422

15-
bool builtin_bool_to_float_type_promotion(bool p1)
23+
void test_too_many_arg(float3 p0)
1624
{
17-
return __builtin_hlsl_cross(p1, p1);
18-
// expected-error@-1 {{1st argument must be a vector of 16 or 32 bit floating-point types (was 'bool')}}
25+
return cross(p0, p0, p0);
26+
// expected-error@-1 {{no matching function for call to 'cross'}}
27+
// expected-note@hlsl/hlsl_alias_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}}
28+
// expected-note@hlsl/hlsl_alias_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}}
1929
}
2030

21-
bool builtin_cross_int_to_float_promotion(int p1)
31+
void test_too_many_arg_f32(float3 p0)
2232
{
23-
return __builtin_hlsl_cross(p1, p1);
24-
// expected-error@-1 {{1st argument must be a vector of 16 or 32 bit floating-point types (was 'int')}}
33+
return __builtin_hlsl_crossf32(p0, p0, p0);
34+
// expected-error@-1 {{too many arguments to function call, expected 2, have 3}}
2535
}
2636

27-
bool2 builtin_cross_int2_to_float2_promotion(int2 p1)
37+
void test_too_many_arg_f16(half3 p0)
2838
{
29-
return __builtin_hlsl_cross(p1, p1);
30-
// expected-error@-1 {{1st argument must be a vector of 16 or 32 bit floating-point types (was 'int2' (aka 'vector<int, 2>'))}}
39+
return __builtin_hlsl_crossf16(p0, p0, p0);
40+
// expected-error@-1 {{too many arguments to function call, expected 2, have 3}}
3141
}
3242

33-
float2 builtin_cross_float2(float2 p1, float2 p2)
43+
bool2 builtin_cross_int2_to_float2_promotion(int2 p1)
3444
{
35-
return __builtin_hlsl_cross(p1, p2);
36-
// expected-error@-1 {{too many elements in vector operand (expected 3 elements, have 2)}}
45+
return __builtin_hlsl_crossf32(p1, p1);
46+
// expected-error@-1 {{cannot initialize a parameter of type 'vector<float, 3>' (vector of 3 'float' values) with an lvalue of type 'int2' (aka 'vector<int, 2>')}}
3747
}
3848

39-
float3 builtin_cross_float3_int3(float3 p1, int3 p2)
49+
float2 builtin_cross_float2(float2 p1, float2 p2)
4050
{
41-
return __builtin_hlsl_cross(p1, p2);
42-
// expected-error@-1 {{2nd argument must be a vector of 16 or 32 bit floating-point types (was 'int3' (aka 'vector<int, 3>'))}}
51+
return __builtin_hlsl_crossf32(p1, p2);
52+
// expected-error@-1 {{cannot initialize a parameter of type 'vector<float, 3>' (vector of 3 'float' values) with an lvalue of type 'float2' (aka 'vector<float, 2>')}}
4353
}
4454

45-
half3 builtin_cross_same_type(half3 p0, float3 p1)
55+
void test_ambiguous(int p0)
4656
{
47-
return __builtin_hlsl_cross(p0, p1);
48-
// expected-error@-1 {{all arguments to '__builtin_hlsl_cross' must have the same type}}
57+
return cross(p0,p0);
58+
// expected-error@-1 {{call to 'cross' is ambiguous}}
59+
// expected-note@hlsl/hlsl_alias_intrinsics.h:* {{candidate function}}
60+
// expected-note@hlsl/hlsl_alias_intrinsics.h:* {{candidate function}}
4961
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ float2 test_mad_no_second_arg(float2 p0) {
2222

2323
float2 test_mad_vector_size_mismatch(float3 p0, float2 p1) {
2424
return mad(p0, p0, p1);
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)}}
25+
// expected-error@-1 {{arguments are of different types ('vector<[...], 3>' vs 'vector<[...], 2>')}}
2726
}
2827

2928
float2 test_mad_builtin_vector_size_mismatch(float3 p0, float2 p1) {
@@ -43,17 +42,17 @@ float2 test_mad_element_type_mismatch(half2 p0, float2 p1) {
4342

4443
float2 test_builtin_mad_float2_splat(float p0, float2 p1) {
4544
return __builtin_hlsl_mad(p0, p1, p1);
46-
// expected-error@-1 {{arguments are of different types ('double' vs 'float2' (aka 'vector<float, 2>'))}}
45+
// expected-error@-1 {{arguments are of different types ('float' vs 'float2' (aka 'vector<float, 2>'))}}
4746
}
4847

4948
float3 test_builtin_mad_float3_splat(float p0, float3 p1) {
5049
return __builtin_hlsl_mad(p0, p1, p1);
51-
// expected-error@-1 {{arguments are of different types ('double' vs 'float3' (aka 'vector<float, 3>'))}}
50+
// expected-error@-1 {{arguments are of different types ('float' vs 'float3' (aka 'vector<float, 3>'))}}
5251
}
5352

5453
float4 test_builtin_mad_float4_splat(float p0, float4 p1) {
5554
return __builtin_hlsl_mad(p0, p1, p1);
56-
// expected-error@-1 {{arguments are of different types ('double' vs 'float4' (aka 'vector<float, 4>'))}}
55+
// expected-error@-1 {{arguments are of different types ('float' vs 'float4' (aka 'vector<float, 4>'))}}
5756
}
5857

5958
float2 test_mad_float2_int_splat(float2 p0, int p1) {
@@ -68,7 +67,7 @@ float3 test_mad_float3_int_splat(float3 p0, int p1) {
6867

6968
float2 test_builtin_mad_int_vect_to_float_vec_promotion(int2 p0, float p1) {
7069
return __builtin_hlsl_mad(p0, p1, p1);
71-
// expected-error@-1 {{arguments are of different types ('int2' (aka 'vector<int, 2>') vs 'double')}}
70+
// expected-error@-1 {{arguments are of different types ('int2' (aka 'vector<int, 2>') vs 'float')}}
7271
}
7372

7473
float builtin_bool_to_float_type_promotion(float p0, bool p1) {
@@ -83,7 +82,7 @@ float builtin_bool_to_float_type_promotion2(bool p0, float p1) {
8382

8483
float builtin_mad_int_to_float_promotion(float p0, int p1) {
8584
return __builtin_hlsl_mad(p0, p0, p1);
86-
// expected-error@-1 {{arguments are of different types ('double' vs 'int')}}
85+
// expected-error@-1 {{arguments are of different types ('float' vs 'int')}}
8786
}
8887

8988
int builtin_mad_mixed_enums() {

0 commit comments

Comments
 (0)