https://github.com/farzonl created https://github.com/llvm/llvm-project/pull/117240
- For the HLSL intrinsic used `const inline` because that seems to be the pattern `countbits` - For the helper functions the pattern in `hlsl_detail.h` was to do `constexpr` like with `bit_cast` and `enable_if_t` So did the same here. - Distance in DXC is defined as [Length(X-Y)](https://github.com/microsoft/DirectXShaderCompiler/blob/848b7c42bd8da13693273513412c0a554c7918a1/lib/HLSL/HLOperationLower.cpp#L2188C1-L2198C2), So doing the same here. - Maybe this also means we need to move length into the header - This resolves all the DirectX specific parts of https://github.com/llvm/llvm-project/issues/99107 - The codegen will be functionally correct for SPIRV, but will not emit the GLSL Distance opcode. - There are many potential solutions for that. Atm, we intend to address the GLSL specific ops with Inline SPIRV. >From 6aaf0c42844409fca63f30fa716b9b65f2127f85 Mon Sep 17 00:00:00 2001 From: Farzon Lotfi <farzonlo...@microsoft.com> Date: Thu, 21 Nov 2024 14:46:31 -0500 Subject: [PATCH] [HLSL] Implement a header only distance intrinsic --- clang/lib/Headers/hlsl/hlsl_detail.h | 8 ++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 39 ++++++++++ clang/test/CodeGenHLSL/builtins/distance.hlsl | 76 +++++++++++++++++++ .../SemaHLSL/BuiltIns/distance-errors.hlsl | 33 ++++++++ 4 files changed, 156 insertions(+) create mode 100644 clang/test/CodeGenHLSL/builtins/distance.hlsl create mode 100644 clang/test/SemaHLSL/BuiltIns/distance-errors.hlsl diff --git a/clang/lib/Headers/hlsl/hlsl_detail.h b/clang/lib/Headers/hlsl/hlsl_detail.h index 8d5fd941331531..99cb3fa4a6d2e8 100644 --- a/clang/lib/Headers/hlsl/hlsl_detail.h +++ b/clang/lib/Headers/hlsl/hlsl_detail.h @@ -13,6 +13,14 @@ namespace hlsl { namespace __detail { +template <typename T, typename U> struct is_same { + static const bool value = false; +}; + +template <typename T> struct is_same<T, T> { + static const bool value = true; +}; + template <bool B, typename T> struct enable_if {}; template <typename T> struct enable_if<true, T> { diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index a484d04155d6b2..93d180618c6657 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -853,6 +853,45 @@ float3 degrees(float3); _HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_degrees) float4 degrees(float4); +//===----------------------------------------------------------------------===// +// distance builtins +//===----------------------------------------------------------------------===// + +/// \fn K distance(T X, T Y) +/// \brief Returns a distance scalar between two vectors of \a X and \a Y. +/// \param X The X input value. +/// \param Y The Y input value. + +template <typename T> +constexpr __detail::enable_if_t< + __detail::is_same<float, T>::value || __detail::is_same<half, T>::value, T> +distance_impl(T X, T Y) { + return __builtin_elementwise_abs(X - Y); +} + +template <typename T, int N> +constexpr __detail::enable_if_t< + __detail::is_same<float, T>::value || __detail::is_same<half, T>::value, T> +distance_vec_impl(vector<T, N> X, vector<T, N> Y) { + return __builtin_hlsl_length(X - Y); +} + +_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) +const inline half distance(half X, half Y) { return distance_impl(X, Y); } + +template <int N> +_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2) +const inline half distance(vector<half, N> X, vector<half, N> Y) { + return distance_vec_impl(X, Y); +} + +const inline float distance(float X, float Y) { return distance_impl(X, Y); } + +template <int N> +const inline float distance(vector<float, N> X, vector<float, N> Y) { + return distance_vec_impl(X, Y); +} + //===----------------------------------------------------------------------===// // dot product builtins //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGenHLSL/builtins/distance.hlsl b/clang/test/CodeGenHLSL/builtins/distance.hlsl new file mode 100644 index 00000000000000..2ff2947ac49095 --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/distance.hlsl @@ -0,0 +1,76 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5 +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \ +// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \ +// RUN: -emit-llvm -O1 -o - | FileCheck %s + +// CHECK-LABEL: define noundef half @_Z18test_distance_halfDhDh( +// CHECK-SAME: half noundef [[X:%.*]], half noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub half [[X]], [[Y]] +// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call noundef half @llvm.fabs.f16(half [[SUB_I]]) +// CHECK-NEXT: ret half [[ELT_ABS_I]] +// +half test_distance_half(half X, half Y) { return distance(X, Y); } + +// CHECK-LABEL: define noundef half @_Z19test_distance_half2Dv2_DhS_( +// CHECK-SAME: <2 x half> noundef [[X:%.*]], <2 x half> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub <2 x half> [[X]], [[Y]] +// CHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef half @llvm.dx.length.v2f16(<2 x half> [[SUB_I]]) +// CHECK-NEXT: ret half [[HLSL_LENGTH_I]] +// +half test_distance_half2(half2 X, half2 Y) { return distance(X, Y); } + +// CHECK-LABEL: define noundef half @_Z19test_distance_half3Dv3_DhS_( +// CHECK-SAME: <3 x half> noundef [[X:%.*]], <3 x half> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub <3 x half> [[X]], [[Y]] +// CHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef half @llvm.dx.length.v3f16(<3 x half> [[SUB_I]]) +// CHECK-NEXT: ret half [[HLSL_LENGTH_I]] +// +half test_distance_half3(half3 X, half3 Y) { return distance(X, Y); } + +// CHECK-LABEL: define noundef half @_Z19test_distance_half4Dv4_DhS_( +// CHECK-SAME: <4 x half> noundef [[X:%.*]], <4 x half> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub <4 x half> [[X]], [[Y]] +// CHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef half @llvm.dx.length.v4f16(<4 x half> [[SUB_I]]) +// CHECK-NEXT: ret half [[HLSL_LENGTH_I]] +// +half test_distance_half4(half4 X, half4 Y) { return distance(X, Y); } + +// CHECK-LABEL: define noundef float @_Z19test_distance_floatff( +// CHECK-SAME: float noundef [[X:%.*]], float noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub float [[X]], [[Y]] +// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call noundef float @llvm.fabs.f32(float [[SUB_I]]) +// CHECK-NEXT: ret float [[ELT_ABS_I]] +// +float test_distance_float(float X, float Y) { return distance(X, Y); } + +// CHECK-LABEL: define noundef float @_Z20test_distance_float2Dv2_fS_( +// CHECK-SAME: <2 x float> noundef [[X:%.*]], <2 x float> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub <2 x float> [[X]], [[Y]] +// CHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef float @llvm.dx.length.v2f32(<2 x float> [[SUB_I]]) +// CHECK-NEXT: ret float [[HLSL_LENGTH_I]] +// +float test_distance_float2(float2 X, float2 Y) { return distance(X, Y); } + +// CHECK-LABEL: define noundef float @_Z20test_distance_float3Dv3_fS_( +// CHECK-SAME: <3 x float> noundef [[X:%.*]], <3 x float> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub <3 x float> [[X]], [[Y]] +// CHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef float @llvm.dx.length.v3f32(<3 x float> [[SUB_I]]) +// CHECK-NEXT: ret float [[HLSL_LENGTH_I]] +// +float test_distance_float3(float3 X, float3 Y) { return distance(X, Y); } + +// CHECK-LABEL: define noundef float @_Z20test_distance_float4Dv4_fS_( +// CHECK-SAME: <4 x float> noundef [[X:%.*]], <4 x float> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: [[SUB_I:%.*]] = fsub <4 x float> [[X]], [[Y]] +// CHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef float @llvm.dx.length.v4f32(<4 x float> [[SUB_I]]) +// CHECK-NEXT: ret float [[HLSL_LENGTH_I]] +// +float test_distance_float4(float4 X, float4 Y) { return distance(X, Y); } diff --git a/clang/test/SemaHLSL/BuiltIns/distance-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/distance-errors.hlsl new file mode 100644 index 00000000000000..e996bf5d2cb7c5 --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/distance-errors.hlsl @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify + +float test_no_second_arg(float2 p0) { + return distance(p0); + // expected-error@-1 {{no matching function for call to 'distance'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 1 was provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 1 was provided}} +} + +float test_too_many_arg(float2 p0) { + return distance(p0, p0, p0); + // expected-error@-1 {{no matching function for call to 'distance'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires 2 arguments, but 3 were provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 3 were provided}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires 2 arguments, but 3 were provided}} +} + +float test_double_inputs(double p0, double p1) { + return distance(p0, p1); + // expected-error@-1 {{call to 'distance' is ambiguous}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}} +} + +float test_int_inputs(int p0, int p1) { + return distance(p0, p1); + // expected-error@-1 {{call to 'distance' is ambiguous}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits