Skip to content

Commit 718838d

Browse files
Hlsl asint16 intrinsic (#131900)
Implemented the asint16 function and added test cases for codegen, Sema, and SPIR-V backend. fixes #99184 --------- Co-authored-by: Ashley Coleman <[email protected]>
1 parent 44573bc commit 718838d

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ template <typename T> constexpr int asint(T F) {
4646
return __detail::bit_cast<int, T>(F);
4747
}
4848

49+
//===----------------------------------------------------------------------===//
50+
// asint16 builtins
51+
//===----------------------------------------------------------------------===//
52+
53+
/// \fn int16_t asint16(T X)
54+
/// \brief Interprets the bit pattern of \a X as an 16-bit integer.
55+
/// \param X The input value.
56+
57+
#ifdef __HLSL_ENABLE_16_BIT
58+
59+
template <typename T, int N>
60+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
61+
constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
62+
__detail::is_same<uint16_t, T>::value ||
63+
__detail::is_same<half, T>::value,
64+
vector<int16_t, N>> asint16(vector<T, N> V) {
65+
return __detail::bit_cast<int16_t, T, N>(V);
66+
}
67+
68+
template <typename T>
69+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
70+
constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
71+
__detail::is_same<uint16_t, T>::value ||
72+
__detail::is_same<half, T>::value,
73+
int16_t> asint16(T F) {
74+
return __detail::bit_cast<int16_t, T>(F);
75+
}
76+
#endif
77+
4978
//===----------------------------------------------------------------------===//
5079
// asuint builtins
5180
//===----------------------------------------------------------------------===//
@@ -87,6 +116,7 @@ void asuint(double4, out uint4, out uint4);
87116
/// \fn uint16_t asuint16(T X)
88117
/// \brief Interprets the bit pattern of \a X as an 16-bit unsigned integer.
89118
/// \param X The input value.
119+
90120
#ifdef __HLSL_ENABLE_16_BIT
91121

92122
template <typename T, int N>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.2-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s
2+
3+
//CHECK-LABEL: define {{.*}}test_ints
4+
//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
5+
//CHECK-NOT: bitcast
6+
//CHECK: entry:
7+
//CHECK-NEXT: ret i16 [[VAL]]
8+
int16_t test_int(int16_t p0)
9+
{
10+
return asint16(p0);
11+
}
12+
13+
//CHECK-LABEL: define {{.*}}test_uint
14+
//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
15+
//CHECK-NOT:bitcast
16+
//CHECK: entry:
17+
//CHECK-NEXT: ret i16 [[VAL]]
18+
int16_t test_uint(uint16_t p0)
19+
{
20+
return asint16(p0);
21+
}
22+
23+
//CHECK-LABEL: define {{.*}}test_half
24+
//CHECK-SAME: {{.*}}(half {{.*}} [[VAL:%.*]]){{.*}}
25+
//CHECK: [[RES:%.*]] = bitcast half [[VAL]] to i16
26+
//CHECK-NEXT : ret i16 [[RES]]
27+
int16_t test_half(half p0)
28+
{
29+
return asint16(p0);
30+
}
31+
32+
//CHECK-LABEL: define {{.*}}test_vector_int
33+
//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
34+
//CHECK-NOT: bitcast
35+
//CHECK: entry:
36+
//CHECK-NEXT: ret <4 x i16> [[VAL]]
37+
int16_t4 test_vector_int(int16_t4 p0)
38+
{
39+
return asint16(p0);
40+
}
41+
42+
//CHECK-LABEL: define {{.*}}test_vector_uint
43+
//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
44+
//CHECK-NOT: bitcast
45+
//CHECK-NEXT: entry:
46+
//CHECK-NEXT: ret <4 x i16> [[VAL]]
47+
int16_t4 test_vector_uint(uint16_t4 p0)
48+
{
49+
return asint16(p0);
50+
}
51+
52+
//CHECK-LABEL: define {{.*}}fn
53+
//CHECK-SAME: {{.*}}(<4 x half> {{.*}} [[VAL:%.*]]){{.*}}
54+
//CHECK: [[RES:%.*]] = bitcast <4 x half> [[VAL]] to <4 x i16>
55+
//CHECK-NEXT: ret <4 x i16> [[RES]]
56+
int16_t4 fn(half4 p1)
57+
{
58+
return asint16(p1);
59+
}
60+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.2-library %s -fnative-half-type -verify
2+
3+
4+
int16_t4 test_asint16_too_many_arg(uint16_t p0, uint16_t p1)
5+
{
6+
return asint16(p0, p1);
7+
// expected-error@-1 {{no matching function for call to 'asint16'}}
8+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
9+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
10+
}
11+
12+
int16_t test_asint16_int(int p1)
13+
{
14+
return asint16(p1);
15+
// expected-error@-1 {{no matching function for call to 'asint16'}}
16+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'int'}}
17+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type'}}
18+
}
19+
20+
int16_t test_asint16_float(float p1)
21+
{
22+
return asint16(p1);
23+
// expected-error@-1 {{no matching function for call to 'asint16'}}
24+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'float'}}
25+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float]: no type named 'Type'}}
26+
}
27+
28+
int16_t4 test_asint16_vector_int(int4 p1)
29+
{
30+
return asint16(p1);
31+
// expected-error@-1 {{no matching function for call to 'asint16'}}
32+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int, N = 4]: no type named 'Type'}}
33+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int4]: no type named 'Type'}}
34+
}
35+
36+
int16_t4 test_asint16_vector_float(float4 p1)
37+
{
38+
return asint16(p1);
39+
// expected-error@-1 {{no matching function for call to 'asint16'}}
40+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float, N = 4]: no type named 'Type'}}
41+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float4]: no type named 'Type'}}
42+
}
43+

llvm/test/CodeGen/SPIRV/bitcast.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
; CHECK-SPIRV-DAG: %[[#TyInt32:]] = OpTypeInt 32 0
55
; CHECK-SPIRV-DAG: %[[#TyInt16:]] = OpTypeInt 16 0
66
; CHECK-SPIRV-DAG: %[[#TyHalf:]] = OpTypeFloat 16
7+
; CHECK-SPIRV-DAG: %[[#vec4_float_16:]] = OpTypeVector %[[#TyHalf]] 4
78
; CHECK-SPIRV-DAG: %[[#Arg32:]] = OpFunctionParameter %[[#TyInt32]]
89
; CHECK-SPIRV-DAG: %[[#Arg16:]] = OpUConvert %[[#TyInt16]] %[[#Arg32]]
910
; CHECK-SPIRV-DAG: %[[#ValHalf:]] = OpBitcast %[[#TyHalf]] %[[#Arg16:]]
@@ -19,3 +20,12 @@ entry:
1920
%res = bitcast half %val2 to i16
2021
ret i16 %res
2122
}
23+
24+
define <4 x i16> @test_vector_half4(<4 x half> nofpclass(nan inf) %p1) {
25+
entry:
26+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#vec4_float_16]]
27+
; CHECK: %[[#Res1:]] = OpBitcast %[[#vec4_int_16]] %[[#arg0]]
28+
%0 = bitcast <4 x half> %p1 to <4 x i16>
29+
; CHECK: OpReturnValue %[[#Res1]]
30+
ret <4 x i16> %0
31+
}

0 commit comments

Comments
 (0)