Skip to content

Commit 529c8dc

Browse files
Adding asuint implementation to hlsl
1 parent 55a6cad commit 529c8dc

File tree

5 files changed

+61
-41
lines changed

5 files changed

+61
-41
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class MSInt32_64Template : Template<["msint32_t", "int64_t"],
5353
class FloatDoubleTemplate : Template<["float", "double"],
5454
["f", ""]>;
5555

56+
class HLSLFloatAndIntTemplate : Template<
57+
["unsigned int", "int", "float"],
58+
["", "si", "f"]>;
5659
// FIXME: These assume that char -> i8, short -> i16, int -> i32,
5760
// long long -> i64.
5861
class SyncBuiltinsTemplate :
@@ -4751,10 +4754,10 @@ def HLSLRcp : LangBuiltin<"HLSL_LANG"> {
47514754
let Prototype = "void(...)";
47524755
}
47534756

4754-
def HLSLAsUint : LangBuiltin<"HLSL_LANG"> {
4757+
def HLSLAsUint : LangBuiltin<"HLSL_LANG">, HLSLFloatAndIntTemplate {
47554758
let Spellings = ["__builtin_hlsl_elementwise_asuint"];
47564759
let Attributes = [NoThrow, Const];
4757-
let Prototype = "void(...)";
4760+
let Prototype = "unsigned int (T)";
47584761
}
47594762

47604763
def HLSLRSqrt : LangBuiltin<"HLSL_LANG"> {

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18867,7 +18867,7 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1886718867
{}, false, true));
1886818868
}
1886918869
case Builtin::BI__builtin_hlsl_elementwise_asuint: {
18870-
Value *Op = EmitScalarExpr(E->getArg(0)->IgnoreImpCasts());
18870+
Value *Op = EmitScalarExpr(E->getArg(0));
1887118871

1887218872
llvm::Type *DestTy = llvm::Type::getInt32Ty(this->getLLVMContext());
1887318873

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,24 @@ uint3 asuint(float3);
404404
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
405405
uint4 asuint(float4);
406406

407+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
408+
uint asuint(uint);
409+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
410+
uint2 asuint(uint2);
411+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
412+
uint3 asuint(uint3);
413+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
414+
uint4 asuint(uint4);
415+
416+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
417+
uint asuint(int);
418+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
419+
uint2 asuint(int2);
420+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
421+
uint3 asuint(int3);
422+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
423+
uint4 asuint(int4);
424+
407425
//===----------------------------------------------------------------------===//
408426
// atan builtins
409427
//===----------------------------------------------------------------------===//

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,25 +1470,6 @@ bool CheckVectorElementCallArgs(Sema *S, CallExpr *TheCall) {
14701470
return true;
14711471
}
14721472

1473-
bool CheckArgTypeWithoutImplicits(
1474-
Sema *S, Expr *Arg, QualType ExpectedType,
1475-
llvm::function_ref<bool(clang::QualType PassedType)> Check) {
1476-
1477-
QualType ArgTy = Arg->IgnoreImpCasts()->getType();
1478-
1479-
clang::QualType BaseType =
1480-
ArgTy->isVectorType()
1481-
? ArgTy->getAs<clang::VectorType>()->getElementType()
1482-
: ArgTy;
1483-
1484-
if (Check(BaseType)) {
1485-
S->Diag(Arg->getBeginLoc(), diag::err_typecheck_convert_incompatible)
1486-
<< ArgTy << ExpectedType << 1 << 0 << 0;
1487-
return true;
1488-
}
1489-
return false;
1490-
}
1491-
14921473
bool CheckArgsTypesAreCorrect(
14931474
Sema *S, CallExpr *TheCall, QualType ExpectedType,
14941475
llvm::function_ref<bool(clang::QualType PassedType)> Check) {
@@ -1515,12 +1496,16 @@ bool CheckAllArgsHaveFloatRepresentation(Sema *S, CallExpr *TheCall) {
15151496
checkAllFloatTypes);
15161497
}
15171498

1518-
bool CheckArgIsFloatOrIntWithoutImplicits(Sema *S, Expr *Arg) {
1499+
bool CheckNotFloatAndInt(Sema *S, CallExpr *TheCall) {
15191500
auto checkFloat = [](clang::QualType PassedType) -> bool {
1520-
return !PassedType->isFloat32Type() && !PassedType->isIntegerType();
1501+
clang::QualType BaseType =
1502+
PassedType->isVectorType()
1503+
? PassedType->getAs<clang::VectorType>()->getElementType()
1504+
: PassedType;
1505+
return !(BaseType->isFloat32Type() || BaseType->isIntegerType());
15211506
};
15221507

1523-
return CheckArgTypeWithoutImplicits(S, Arg, S->Context.FloatTy, checkFloat);
1508+
return CheckArgsTypesAreCorrect(S, TheCall, S->Context.FloatTy, checkFloat);
15241509
}
15251510

15261511
bool CheckFloatOrHalfRepresentations(Sema *S, CallExpr *TheCall) {
@@ -1784,8 +1769,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
17841769
if (SemaRef.checkArgCount(TheCall, 1))
17851770
return true;
17861771

1787-
Expr *Arg = TheCall->getArg(0);
1788-
if (CheckArgIsFloatOrIntWithoutImplicits(&SemaRef, Arg))
1772+
if (CheckNotFloatAndInt(&SemaRef, TheCall))
17891773
return true;
17901774

17911775
break;
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
1-
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -o - | FileCheck %s
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s
22

3+
// CHECK: define {{.*}}test_uint{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
4+
// CHECK-NOT: bitcast
5+
// CHECK: ret i32 [[VAL]]
6+
export uint test_uint(uint p0) {
7+
return asuint(p0);
8+
}
9+
10+
// CHECK: define {{.*}}test_int{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
11+
// CHECK-NOT: bitcast
12+
// CHECK: ret i32 [[VAL]]
13+
export uint test_int(int p0) {
14+
return asuint(p0);
15+
}
316

4-
// CHECK-LABEL: test_asuint4_uint
5-
// CHECK: ret i32 %0
6-
export uint test_asuint4_uint(uint p0) {
17+
// CHECK: define {{.*}}test_float{{.*}}(float {{.*}} [[VAL:%.*]]){{.*}}
18+
// CHECK: bitcast float [[VAL]] to i32
19+
export uint test_float(float p0) {
720
return asuint(p0);
821
}
922

10-
// CHECK-LABEL: test_asuint4_int
11-
// CHECK: %splat.splatinsert = insertelement <4 x i32> poison, i32 %0, i64 0
12-
export uint4 test_asuint4_int(int p0) {
23+
// CHECK: define {{.*}}test_vector_uint{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
24+
// CHECK-NOT: bitcast
25+
// CHECK: ret <4 x i32> [[VAL]]
26+
export uint4 test_vector_uint(uint4 p0) {
1327
return asuint(p0);
1428
}
1529

16-
// CHECK-LABEL: test_asuint_float
17-
// CHECK: %1 = bitcast float %0 to i32
18-
export uint test_asuint_float(float p0) {
30+
// CHECK: define {{.*}}test_vector_int{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
31+
// CHECK-NOT: bitcast
32+
// CHECK: ret <4 x i32> [[VAL]]
33+
export uint4 test_vector_int(int4 p0) {
1934
return asuint(p0);
2035
}
2136

22-
// CHECK-LABEL: test_asuint_float
23-
// CHECK: %1 = bitcast <4 x float> %0 to <4 x i32>
24-
export uint4 test_asuint_float4(float4 p0) {
37+
// CHECK: define {{.*}}test_vector_float{{.*}}(<4 x float> {{.*}} [[VAL:%.*]]){{.*}}
38+
// CHECK: bitcast <4 x float> [[VAL]] to <4 x i32>
39+
export uint4 test_vector_float(float4 p0) {
2540
return asuint(p0);
26-
}
41+
}

0 commit comments

Comments
 (0)