Skip to content
28 changes: 28 additions & 0 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ void asuint(double3, out uint3, out uint3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
void asuint(double4, out uint4, out uint4);

//===----------------------------------------------------------------------===//
// asuint16 builtins
//===----------------------------------------------------------------------===//

/// \fn uint16_t asuint16(T X)
/// \brief Interprets the bit pattern of \a X as an 16-bit unsigned integer.
/// \param X The input value.
#ifdef __HLSL_ENABLE_16_BIT

template <typename T, int N>
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to add the Shader Model 6.2 availability macro to these two functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added suggested shader model macro.

__detail::is_same<uint16_t, T>::value ||
__detail::is_same<half, T>::value,
vector<uint16_t, N>> asuint16(vector<T, N> V) {
return __detail::bit_cast<uint16_t, T, N>(V);
}

template <typename T>
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
__detail::is_same<uint16_t, T>::value ||
__detail::is_same<half, T>::value,
uint16_t> asuint16(T F) {
return __detail::bit_cast<uint16_t, T>(F);
}
#endif

//===----------------------------------------------------------------------===//
// distance builtins
//===----------------------------------------------------------------------===//
Expand Down
60 changes: 60 additions & 0 deletions clang/test/CodeGenHLSL/builtins/asuint16.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.2-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s

//CHECK-LABEL: define {{.*}}test_ints
//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
//CHECK-NOT: bitcast
//CHECK: entry:
//CHECK: ret i16 [[VAL]]
uint16_t test_int(int16_t p0)
{
return asuint16(p0);
}

//CHECK-LABEL: define {{.*}}test_uint
//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
//CHECK-NOT: bitcast
//CHECK: entry:
//CHECK-NEXT: ret i16 [[VAL]]
uint16_t test_uint(uint16_t p0)
{
return asuint16(p0);
}

//CHECK-LABEL: define {{.*}}test_half
//CHECK-SAME: {{.*}}(half {{.*}} [[VAL:%.*]]){{.*}}
//CHECK: [[RES:%.*]] = bitcast half [[VAL]] to i16
//CHECK-NEXT: ret i16 [[RES]]
uint16_t test_half(half p0)
{
return asuint16(p0);
}

//CHECK-LABEL: define {{.*}}test_vector_int
//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
//CHECK-NOT: bitcast
//CHECK: entry:
//CHECK-NEXT: ret <4 x i16> [[VAL]]
uint16_t4 test_vector_int(int16_t4 p0)
{
return asuint16(p0);
}

//CHECK-LABEL: define {{.*}}test_vector_uint
//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
//CHECK-NOT: bitcast
//CHECK: entry:
//CHECK-NEXT: ret <4 x i16> [[VAL]]
uint16_t4 test_vector_uint(uint16_t4 p0)
{
return asuint16(p0);
}

//CHECK-LABEL: define {{.*}}fn
//CHECK-SAME: {{.*}}(<4 x half> {{.*}} [[VAL:%.*]]){{.*}}
//CHECK: [[RES:%.*]] = bitcast <4 x half> [[VAL]] to <4 x i16>
//CHECK-NEXT: ret <4 x i16> [[RES]]
uint16_t4 fn(half4 p1)
{
return asuint16(p1);
}

52 changes: 52 additions & 0 deletions clang/test/SemaHLSL/BuiltIns/asuint16-errors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.2-library %s -fnative-half-type -verify

uint16_t test_asuint16_less_argument()
{
return asuint16();
// expected-error@-1 {{no matching function for call to 'asuint16'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but no arguments were provided}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but no arguments were provided}}

}

int16_t4 test_asuint16_too_many_arg(uint16_t p0, uint16_t p1)
{
return asuint16(p0, p1);
// expected-error@-1 {{no matching function for call to 'asuint16'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}

}

int16_t test_asuint16_int(int p1)
{
return asuint16(p1);
// expected-error@-1 {{no matching function for call to 'asuint16'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'int'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type'}}
Comment on lines +25 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not an actionable item unless @farzonl has any ideas

The notes here feel quite unhelpful from a user perspective. I'm guessing the error is that int doesn't fit in int16_t but if I was a user and I saw this error then I'd be extremely confusing by what's actually wrong. That said the actual error at least points at the function and tells you it's the wrong overload so maybe not

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a language overload resolution rule and not one we defined in sema there is not much we can do in this PR. Atleast nothing that woudn't be considered scope creep. Worth talk with Chris about though and see if there is something we can change.

}

int16_t test_asuint16_float(float p1)
{
return asuint16(p1);
// expected-error@-1 {{no matching function for call to 'asuint16'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'float'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float]: no type named 'Type'}}
}

int16_t4 test_asuint16_vector_int(int4 p1)
{
return asuint16(p1);
// expected-error@-1 {{no matching function for call to 'asuint16'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int, N = 4]: no type named 'Type'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int4]: no type named 'Type'}}
}

int16_t4 test_asuint16_vector_float(float4 p1)
{
return asuint16(p1);
// expected-error@-1 {{no matching function for call to 'asuint16'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float, N = 4]: no type named 'Type'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float4]: no type named 'Type'}}
}