Skip to content

Commit 9a2315c

Browse files
committed
[HLSL] Implement the HLSL distance intrinsic
- Hook of SPIRV builtin - Implement Distance as length(X - Y)
1 parent b900379 commit 9a2315c

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ length_vec_impl(vector<T, N> X) {
5353
return __builtin_elementwise_sqrt(__builtin_hlsl_dot(X, X));
5454
}
5555

56+
template <typename T>
57+
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
58+
distance_impl(T X, T Y) {
59+
return length_impl(X - Y);
60+
}
61+
62+
template <typename T, int N>
63+
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
64+
distance_vec_impl(vector<T, N> X, vector<T, N> Y) {
65+
#if (__has_builtin(__builtin_spirv_distance))
66+
return __builtin_spirv_distance(X, Y);
67+
#else
68+
return length_vec_impl(X - Y);
69+
#endif
70+
}
5671
} // namespace __detail
5772
} // namespace hlsl
5873
#endif //_HLSL_HLSL_DETAILS_H_

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,35 @@ float3 degrees(float3);
871871
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_degrees)
872872
float4 degrees(float4);
873873

874+
//===----------------------------------------------------------------------===//
875+
// distance builtins
876+
//===----------------------------------------------------------------------===//
877+
878+
/// \fn K distance(T X, T Y)
879+
/// \brief Returns a distance scalar between two vectors of \a X and \a Y.
880+
/// \param X The X input value.
881+
/// \param Y The Y input value.
882+
883+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
884+
const inline half distance(half X, half Y) {
885+
return __detail::distance_impl(X, Y);
886+
}
887+
888+
const inline float distance(float X, float Y) {
889+
return __detail::distance_impl(X, Y);
890+
}
891+
892+
template <int N>
893+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
894+
const inline half distance(vector<half, N> X, vector<half, N> Y) {
895+
return __detail::distance_vec_impl(X, Y);
896+
}
897+
898+
template <int N>
899+
const inline float distance(vector<float, N> X, vector<float, N> Y) {
900+
return __detail::distance_vec_impl(X, Y);
901+
}
902+
874903
//===----------------------------------------------------------------------===//
875904
// dot product builtins
876905
//===----------------------------------------------------------------------===//
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
3+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
4+
// RUN: -emit-llvm -O1 -o - | FileCheck %s
5+
// RUN: %clang_cc1 -finclude-default-header -triple \
6+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
7+
// RUN: -emit-llvm -O1 -o - | FileCheck %s --check-prefix=SPVCHECK
8+
9+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z18test_distance_halfDhDh(
10+
// CHECK-SAME: half noundef nofpclass(nan inf) [[X:%.*]], half noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
11+
// CHECK-NEXT: [[ENTRY:.*:]]
12+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn half [[X]], [[Y]]
13+
// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.fabs.f16(half [[SUB_I]])
14+
// CHECK-NEXT: ret half [[ELT_ABS_I]]
15+
//
16+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) half @_Z18test_distance_halfDhDh(
17+
// SPVCHECK-SAME: half noundef nofpclass(nan inf) [[X:%.*]], half noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
18+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
19+
// SPVCHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn half [[X]], [[Y]]
20+
// SPVCHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.fabs.f16(half [[SUB_I]])
21+
// SPVCHECK-NEXT: ret half [[ELT_ABS_I]]
22+
//
23+
half test_distance_half(half X, half Y) { return distance(X, Y); }
24+
25+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z19test_distance_half2Dv2_DhS_(
26+
// CHECK-SAME: <2 x half> noundef nofpclass(nan inf) [[X:%.*]], <2 x half> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
27+
// CHECK-NEXT: [[ENTRY:.*:]]
28+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn <2 x half> [[X]], [[Y]]
29+
// CHECK-NEXT: [[HLSL_DOT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.dx.fdot.v2f16(<2 x half> [[SUB_I]], <2 x half> [[SUB_I]])
30+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.sqrt.f16(half [[HLSL_DOT_I]])
31+
// CHECK-NEXT: ret half [[TMP0]]
32+
//
33+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) half @_Z19test_distance_half2Dv2_DhS_(
34+
// SPVCHECK-SAME: <2 x half> noundef nofpclass(nan inf) [[X:%.*]], <2 x half> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
35+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
36+
// SPVCHECK-NEXT: [[SPV_DISTANCE_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.spv.distance.v2f16(<2 x half> [[X]], <2 x half> [[Y]])
37+
// SPVCHECK-NEXT: ret half [[SPV_DISTANCE_I]]
38+
//
39+
half test_distance_half2(half2 X, half2 Y) { return distance(X, Y); }
40+
41+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z19test_distance_half3Dv3_DhS_(
42+
// CHECK-SAME: <3 x half> noundef nofpclass(nan inf) [[X:%.*]], <3 x half> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
43+
// CHECK-NEXT: [[ENTRY:.*:]]
44+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn <3 x half> [[X]], [[Y]]
45+
// CHECK-NEXT: [[HLSL_DOT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.dx.fdot.v3f16(<3 x half> [[SUB_I]], <3 x half> [[SUB_I]])
46+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.sqrt.f16(half [[HLSL_DOT_I]])
47+
// CHECK-NEXT: ret half [[TMP0]]
48+
//
49+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) half @_Z19test_distance_half3Dv3_DhS_(
50+
// SPVCHECK-SAME: <3 x half> noundef nofpclass(nan inf) [[X:%.*]], <3 x half> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
51+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
52+
// SPVCHECK-NEXT: [[SPV_DISTANCE_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.spv.distance.v3f16(<3 x half> [[X]], <3 x half> [[Y]])
53+
// SPVCHECK-NEXT: ret half [[SPV_DISTANCE_I]]
54+
//
55+
half test_distance_half3(half3 X, half3 Y) { return distance(X, Y); }
56+
57+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z19test_distance_half4Dv4_DhS_(
58+
// CHECK-SAME: <4 x half> noundef nofpclass(nan inf) [[X:%.*]], <4 x half> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
59+
// CHECK-NEXT: [[ENTRY:.*:]]
60+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn <4 x half> [[X]], [[Y]]
61+
// CHECK-NEXT: [[HLSL_DOT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.dx.fdot.v4f16(<4 x half> [[SUB_I]], <4 x half> [[SUB_I]])
62+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.sqrt.f16(half [[HLSL_DOT_I]])
63+
// CHECK-NEXT: ret half [[TMP0]]
64+
//
65+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) half @_Z19test_distance_half4Dv4_DhS_(
66+
// SPVCHECK-SAME: <4 x half> noundef nofpclass(nan inf) [[X:%.*]], <4 x half> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
67+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
68+
// SPVCHECK-NEXT: [[SPV_DISTANCE_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.spv.distance.v4f16(<4 x half> [[X]], <4 x half> [[Y]])
69+
// SPVCHECK-NEXT: ret half [[SPV_DISTANCE_I]]
70+
//
71+
half test_distance_half4(half4 X, half4 Y) { return distance(X, Y); }
72+
73+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z19test_distance_floatff(
74+
// CHECK-SAME: float noundef nofpclass(nan inf) [[X:%.*]], float noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
75+
// CHECK-NEXT: [[ENTRY:.*:]]
76+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn float [[X]], [[Y]]
77+
// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.fabs.f32(float [[SUB_I]])
78+
// CHECK-NEXT: ret float [[ELT_ABS_I]]
79+
//
80+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) float @_Z19test_distance_floatff(
81+
// SPVCHECK-SAME: float noundef nofpclass(nan inf) [[X:%.*]], float noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
82+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
83+
// SPVCHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn float [[X]], [[Y]]
84+
// SPVCHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.fabs.f32(float [[SUB_I]])
85+
// SPVCHECK-NEXT: ret float [[ELT_ABS_I]]
86+
//
87+
float test_distance_float(float X, float Y) { return distance(X, Y); }
88+
89+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z20test_distance_float2Dv2_fS_(
90+
// CHECK-SAME: <2 x float> noundef nofpclass(nan inf) [[X:%.*]], <2 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
91+
// CHECK-NEXT: [[ENTRY:.*:]]
92+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn <2 x float> [[X]], [[Y]]
93+
// CHECK-NEXT: [[HLSL_DOT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.dx.fdot.v2f32(<2 x float> [[SUB_I]], <2 x float> [[SUB_I]])
94+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.sqrt.f32(float [[HLSL_DOT_I]])
95+
// CHECK-NEXT: ret float [[TMP0]]
96+
//
97+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) float @_Z20test_distance_float2Dv2_fS_(
98+
// SPVCHECK-SAME: <2 x float> noundef nofpclass(nan inf) [[X:%.*]], <2 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
99+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
100+
// SPVCHECK-NEXT: [[SPV_DISTANCE_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.spv.distance.v2f32(<2 x float> [[X]], <2 x float> [[Y]])
101+
// SPVCHECK-NEXT: ret float [[SPV_DISTANCE_I]]
102+
//
103+
float test_distance_float2(float2 X, float2 Y) { return distance(X, Y); }
104+
105+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z20test_distance_float3Dv3_fS_(
106+
// CHECK-SAME: <3 x float> noundef nofpclass(nan inf) [[X:%.*]], <3 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
107+
// CHECK-NEXT: [[ENTRY:.*:]]
108+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn <3 x float> [[X]], [[Y]]
109+
// CHECK-NEXT: [[HLSL_DOT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.dx.fdot.v3f32(<3 x float> [[SUB_I]], <3 x float> [[SUB_I]])
110+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.sqrt.f32(float [[HLSL_DOT_I]])
111+
// CHECK-NEXT: ret float [[TMP0]]
112+
//
113+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) float @_Z20test_distance_float3Dv3_fS_(
114+
// SPVCHECK-SAME: <3 x float> noundef nofpclass(nan inf) [[X:%.*]], <3 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
115+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
116+
// SPVCHECK-NEXT: [[SPV_DISTANCE_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.spv.distance.v3f32(<3 x float> [[X]], <3 x float> [[Y]])
117+
// SPVCHECK-NEXT: ret float [[SPV_DISTANCE_I]]
118+
//
119+
float test_distance_float3(float3 X, float3 Y) { return distance(X, Y); }
120+
121+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z20test_distance_float4Dv4_fS_(
122+
// CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[X:%.*]], <4 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
123+
// CHECK-NEXT: [[ENTRY:.*:]]
124+
// CHECK-NEXT: [[SUB_I:%.*]] = fsub reassoc nnan ninf nsz arcp afn <4 x float> [[X]], [[Y]]
125+
// CHECK-NEXT: [[HLSL_DOT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.dx.fdot.v4f32(<4 x float> [[SUB_I]], <4 x float> [[SUB_I]])
126+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.sqrt.f32(float [[HLSL_DOT_I]])
127+
// CHECK-NEXT: ret float [[TMP0]]
128+
//
129+
// SPVCHECK-LABEL: define spir_func noundef nofpclass(nan inf) float @_Z20test_distance_float4Dv4_fS_(
130+
// SPVCHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[X:%.*]], <4 x float> noundef nofpclass(nan inf) [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
131+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
132+
// SPVCHECK-NEXT: [[SPV_DISTANCE_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.spv.distance.v4f32(<4 x float> [[X]], <4 x float> [[Y]])
133+
// SPVCHECK-NEXT: ret float [[SPV_DISTANCE_I]]
134+
//
135+
float test_distance_float4(float4 X, float4 Y) { return distance(X, Y); }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify
2+
3+
float test_no_second_arg(float2 p0) {
4+
return distance(p0);
5+
// expected-error@-1 {{no matching function for call to 'distance'}}
6+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 1 was provided}}
7+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 1 was provided}}
8+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 1 was provided}}
9+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 1 was provided}}
10+
}
11+
12+
float test_too_many_arg(float2 p0) {
13+
return distance(p0, p0, p0);
14+
// expected-error@-1 {{no matching function for call to 'distance'}}
15+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}}
16+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}}
17+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 3 were provided}}
18+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 3 were provided}}
19+
}
20+
21+
float test_double_inputs(double p0, double p1) {
22+
return distance(p0, p1);
23+
// expected-error@-1 {{call to 'distance' is ambiguous}}
24+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
25+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
26+
}
27+
28+
float test_int_inputs(int p0, int p1) {
29+
return distance(p0, p1);
30+
// expected-error@-1 {{call to 'distance' is ambiguous}}
31+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
32+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
33+
}

0 commit comments

Comments
 (0)