Skip to content

Commit

Permalink
changing asuint implementation to call __builtin_bit_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosaffran-zz committed Sep 9, 2024
1 parent d0619c7 commit 844a929
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 83 deletions.
9 changes: 0 additions & 9 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ class MSInt32_64Template : Template<["msint32_t", "int64_t"],
class FloatDoubleTemplate : Template<["float", "double"],
["f", ""]>;

class HLSLFloatAndIntTemplate : Template<
["unsigned int", "int", "float"],
["", "si", "f"]>;
// FIXME: These assume that char -> i8, short -> i16, int -> i32,
// long long -> i64.
class SyncBuiltinsTemplate :
Expand Down Expand Up @@ -4754,12 +4751,6 @@ def HLSLRcp : LangBuiltin<"HLSL_LANG"> {
let Prototype = "void(...)";
}

def HLSLAsUint : LangBuiltin<"HLSL_LANG">, HLSLFloatAndIntTemplate {
let Spellings = ["__builtin_hlsl_elementwise_asuint"];
let Attributes = [NoThrow, Const];
let Prototype = "unsigned int (T)";
}

def HLSLRSqrt : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_elementwise_rsqrt"];
let Attributes = [NoThrow, Const];
Expand Down
13 changes: 0 additions & 13 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18815,19 +18815,6 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
llvm::FunctionType::get(IntTy, {}, false), "__hlsl_wave_get_lane_index",
{}, false, true));
}
case Builtin::BI__builtin_hlsl_elementwise_asuint: {
Value *Op = EmitScalarExpr(E->getArg(0));

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

if (Op->getType()->isVectorTy()) {
const VectorType *VecTy = E->getArg(0)->getType()->getAs<VectorType>();
DestTy = llvm::VectorType::get(
DestTy, ElementCount::getFixed(VecTy->getNumElements()));
}

return Builder.CreateBitCast(Op, DestTy);
}
case Builtin::BI__builtin_hlsl_wave_is_first_lane: {
Intrinsic::ID ID = CGM.getHLSLRuntime().getWaveIsFirstLaneIntrinsic();
return EmitRuntimeCall(Intrinsic::getDeclaration(&CGM.getModule(), ID));
Expand Down
38 changes: 12 additions & 26 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,32 +395,18 @@ float4 asin(float4);
/// \brief Interprets the bit pattern of x as an unsigned integer.
/// \param Val The input value.

_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint asuint(float);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint2 asuint(float2);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint3 asuint(float3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint4 asuint(float4);

_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint asuint(uint);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint2 asuint(uint2);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint3 asuint(uint3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint4 asuint(uint4);

_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint asuint(int);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint2 asuint(int2);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint3 asuint(int3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_asuint)
uint4 asuint(int4);
template <typename T, int N>
__attribute__((__always_inline__, __nodebug__)) static inline vector<uint, N>
asuint(vector<T, N> V) {
_Static_assert(sizeof(T) == 4, "Input type to asuint must be 32-bit");
return __builtin_bit_cast(vector<uint, N>, V);
}

template <typename T>
__attribute__((__always_inline__, __nodebug__)) static inline uint asuint(T F) {
_Static_assert(sizeof(T) == 4, "Input type to asuint must be 32-bit");
return __builtin_bit_cast(uint, F);
}

//===----------------------------------------------------------------------===//
// atan builtins
Expand Down
21 changes: 0 additions & 21 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1491,18 +1491,6 @@ bool CheckAllArgsHaveFloatRepresentation(Sema *S, CallExpr *TheCall) {
checkAllFloatTypes);
}

bool CheckNotFloatAndInt(Sema *S, CallExpr *TheCall) {
auto checkFloat = [](clang::QualType PassedType) -> bool {
clang::QualType BaseType =
PassedType->isVectorType()
? PassedType->getAs<clang::VectorType>()->getElementType()
: PassedType;
return !(BaseType->isFloat32Type() || BaseType->isIntegerType());
};

return CheckArgsTypesAreCorrect(S, TheCall, S->Context.FloatTy, checkFloat);
}

bool CheckFloatOrHalfRepresentations(Sema *S, CallExpr *TheCall) {
auto checkFloatorHalf = [](clang::QualType PassedType) -> bool {
clang::QualType BaseType =
Expand Down Expand Up @@ -1657,15 +1645,6 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
return true;
break;
}
case Builtin::BI__builtin_hlsl_elementwise_asuint: {
if (SemaRef.checkArgCount(TheCall, 1))
return true;

if (CheckNotFloatAndInt(&SemaRef, TheCall))
return true;

break;
}
case Builtin::BI__builtin_elementwise_acos:
case Builtin::BI__builtin_elementwise_asin:
case Builtin::BI__builtin_elementwise_atan:
Expand Down
12 changes: 6 additions & 6 deletions clang/test/CodeGenHLSL/builtins/asuint.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@
// CHECK: define {{.*}}test_uint{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
// CHECK-NOT: bitcast
// CHECK: ret i32 [[VAL]]
export uint test_uint(uint p0) {
uint test_uint(uint p0) {
return asuint(p0);
}

// CHECK: define {{.*}}test_int{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
// CHECK-NOT: bitcast
// CHECK: ret i32 [[VAL]]
export uint test_int(int p0) {
uint test_int(int p0) {
return asuint(p0);
}

// CHECK: define {{.*}}test_float{{.*}}(float {{.*}} [[VAL:%.*]]){{.*}}
// CHECK: bitcast float [[VAL]] to i32
export uint test_float(float p0) {
uint test_float(float p0) {
return asuint(p0);
}

// CHECK: define {{.*}}test_vector_uint{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
// CHECK-NOT: bitcast
// CHECK: ret <4 x i32> [[VAL]]
export uint4 test_vector_uint(uint4 p0) {
uint4 test_vector_uint(uint4 p0) {
return asuint(p0);
}

// CHECK: define {{.*}}test_vector_int{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
// CHECK-NOT: bitcast
// CHECK: ret <4 x i32> [[VAL]]
export uint4 test_vector_int(int4 p0) {
uint4 test_vector_int(int4 p0) {
return asuint(p0);
}

// CHECK: define {{.*}}test_vector_float{{.*}}(<4 x float> {{.*}} [[VAL:%.*]]){{.*}}
// CHECK: bitcast <4 x float> [[VAL]] to <4 x i32>
export uint4 test_vector_float(float4 p0) {
uint4 test_vector_float(float4 p0) {
return asuint(p0);
}
16 changes: 8 additions & 8 deletions clang/test/SemaHLSL/BuiltIns/asuint-errors.hlsl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// 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


export uint4 test_asuint_too_many_arg(float p0, float p1) {
return __builtin_hlsl_elementwise_asuint(p0, p1);
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
uint4 test_asuint_too_many_arg(float p0, float p1) {
return asuint(p0, p1);
// expected-error@-1 {{no matching function for call to 'asuint'}}
}


export uint fn(double p1) {
uint test_asuint_double(double p1) {
return asuint(p1);
// expected-error@-1 {{passing 'double' to parameter of incompatible type 'float'}}
// expected-error@-1 {/clang/20/include/hlsl/hlsl_intrinsics.h} {{static assertion failed due to requirement 'sizeof(double) == 4': Input type to asuint must be 32-bit}}
}

export uint fn(half p1) {

uint test_asuint_half(half p1) {
return asuint(p1);
// expected-error@-1 {{passing 'half' to parameter of incompatible type 'float'}}
// expected-error@-1 {/clang/20/include/hlsl/hlsl_intrinsics.h} {{static assertion failed due to requirement 'sizeof(double) == 4': Input type to asuint must be 32-bit}}
}

0 comments on commit 844a929

Please sign in to comment.