Author: metkarpoonam
Date: 2025-04-09T12:25:37-06:00
New Revision: 3ef2bb6d0bcfcdcfde72d8aa3354e72c2366e22b

URL: 
https://github.com/llvm/llvm-project/commit/3ef2bb6d0bcfcdcfde72d8aa3354e72c2366e22b
DIFF: 
https://github.com/llvm/llvm-project/commit/3ef2bb6d0bcfcdcfde72d8aa3354e72c2366e22b.diff

LOG: [HLSL] Implement the dst HLSL Function  (#133828)

fixes: https://github.com/llvm/llvm-project/issues/99108
Implement dst algorithm in the hlsl_intrinsics.h and added test cases
for HLSL codegen and sema


- [x] implement dst algorithm in the hlsl_intrinsics.h

- [x] Add HLSL codegen tests to clang/test/CodeGenHLSL/builtins/dst.hlsl

- [x] Add sema tests to clang/test/SemaHLSL/BuiltIns/dst-errors.hlsl

Added: 
    clang/test/CodeGenHLSL/builtins/dst.hlsl

Modified: 
    clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
    clang/lib/Headers/hlsl/hlsl_intrinsics.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
index a8f025b1b5f5f..b5d88ee1f65cb 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
@@ -35,6 +35,11 @@ length_vec_impl(vector<T, N> X) {
 #endif
 }
 
+template <typename T>
+constexpr vector<T, 4> dst_impl(vector<T, 4> Src0, vector<T, 4> Src1) {
+  return {1, Src0[1] * Src1[1], Src0[2], Src1[3]};
+}
+
 template <typename T> constexpr T distance_impl(T X, T Y) {
   return length_impl(X - Y);
 }

diff  --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 1a61fdba4fc19..8197b5ddf078d 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -190,6 +190,30 @@ const inline float dot2add(half2 A, half2 B, float C) {
   return __detail::dot2add_impl(A, B, C);
 }
 
+//===----------------------------------------------------------------------===//
+// dst builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn vector<T, 4> dst(vector<T, 4>, vector<T, 4>)
+/// \brief Calculates a distance vector.
+/// \param Src0 [in] Contains the squared distance
+/// \param Src1 [in] Contains the reciprocal distance
+///
+/// Return the computed distance vector
+
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+const inline half4 dst(half4 Src0, half4 Src1) {
+  return __detail::dst_impl(Src0, Src1);
+}
+
+const inline float4 dst(float4 Src0, float4 Src1) {
+  return __detail::dst_impl(Src0, Src1);
+}
+
+const inline double4 dst(double4 Src0, double4 Src1) {
+  return __detail::dst_impl(Src0, Src1);
+}
+
 
//===----------------------------------------------------------------------===//
 // fmod builtins
 
//===----------------------------------------------------------------------===//

diff  --git a/clang/test/CodeGenHLSL/builtins/dst.hlsl 
b/clang/test/CodeGenHLSL/builtins/dst.hlsl
new file mode 100644
index 0000000000000..3d4e90969f10a
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/dst.hlsl
@@ -0,0 +1,51 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple 
dxil-pc-shadermodel6.2-library %s -fnative-half-type -emit-llvm 
-disable-llvm-passes -o - | FileCheck %s
+
+
+// CHECK-LABEL: define {{.*}} <4 x float> 
@{{[A-Za-z1-9_]+}}dst_impl{{[A-Za-z1-9_]*}}(
+// CHECK-SAME: <4 x float> {{[A-Za-z )(]*}} [[P:%.*]], <4 x float> {{[A-Za-z 
)(]*}} [[Q:%.*]]) #[[ATTR0:[0-9]+]] { 
+// CHECK: [[VECEXT:%.*]] = extractelement <4 x float> [[PADDR:%.*]], i32 1
+// CHECK: [[VECEXT1:%.*]] = extractelement <4 x float> [[QADDR:%.*]], i32 1
+// CHECK: [[MULRES:%.*]] = fmul {{[A-Za-z ]*}} float [[VECEXT]], [[VECEXT1]]
+// CHECK: [[VECINIT:%.*]] = insertelement <4 x float> <float 1.000000e+00, 
float poison, float poison, float poison>, float [[MULRES]], i32 1
+// CHECK: [[VECINIT2:%.*]] = extractelement <4 x float> [[PADDR2:%.*]], i32 2
+// CHECK: [[VECINIT3:%.*]] = insertelement <4 x float> [[VECINIT]], float 
[[VECINIT2]], i32 2
+// CHECK: [[VECINIT4:%.*]] = extractelement <4 x float> [[QADDR3:%.*]], i32 3
+// CHECK: [[VECINIT5:%.*]] = insertelement <4 x float> [[VECINIT3]], float 
[[VECINIT4]], i32 3
+// CHECK-NEXT: ret <4 x float> [[VECINIT5]]
+float4 dstWithFloat(float4 p1, float4 p2)
+{
+    return dst(p1, p2);
+}
+
+// CHECK-LABEL: define {{.*}} <4 x half> 
@{{[A-Za-z1-9_]+}}dst_impl{{[A-Za-z1-9_]*}}(
+// CHECK-SAME: <4 x half> {{[A-Za-z )(]*}} [[P:%.*]], <4 x half> {{[A-Za-z 
)(]*}} [[Q:%.*]]) #[[ATTR0]] {
+// CHECK: [[VECEXT:%.*]] = extractelement <4 x half> [[PADDR:%.*]], i32 1
+// CHECK: [[VECEXT1:%.*]] = extractelement <4 x half> [[QADDR:%.*]], i32 1
+// CHECK: [[MULRES:%.*]] = fmul {{[A-Za-z ]*}} half [[VECEXT]], [[VECEXT1]]
+// CHECK: [[VECINIT:%.*]] = insertelement <4 x half> <half 0xH3C00, half 
poison, half poison, half poison>, half [[MULRES]], i32 1
+// CHECK: [[VECINIT2:%.*]] = extractelement <4 x half> [[PADDR2:%.*]], i32 2
+// CHECK: [[VECINIT3:%.*]] = insertelement <4 x half> [[VECINIT]], half 
[[VECINIT2]], i32 2
+// CHECK: [[VECINIT4:%.*]] = extractelement <4 x half> [[QADDR3:%.*]], i32 3
+// CHECK: [[VECINIT5:%.*]] = insertelement <4 x half> [[VECINIT3]], half 
[[VECINIT4]], i32 3
+// CHECK-NEXT: ret <4 x half> [[VECINIT5]]
+half4 dstwithHalf(half4 p1, half4 p2)
+{
+    return dst(p1, p2);
+}
+
+// CHECK-LABEL: define {{.*}} <4 x double> 
@{{[A-Za-z1-9_]+}}dst_impl{{[A-Za-z1-9_]*}}(
+// CHECK-SAME: <4 x double> {{[A-Za-z )(]*}} [[P:%.*]], <4 x double> {{[A-Za-z 
)(]*}} [[Q:%.*]]) #[[ATTR0:[0-9]+]] {
+// CHECK: [[VECEXT:%.*]] = extractelement <4 x double> [[PADDR:%.*]], i32 1
+// CHECK: [[VECEXT1:%.*]] = extractelement <4 x double> [[QADDR:%.*]], i32 1
+// CHECK: [[MULRES:%.*]] = fmul {{[A-Za-z ]*}} double [[VECEXT]], [[VECEXT1]]
+// CHECK: [[VECINIT:%.*]] = insertelement <4 x double> <double 1.000000e+00, 
double poison, double poison, double poison>, double [[MULRES]], i32 1
+// CHECK: [[VECINIT2:%.*]] = extractelement <4 x double> [[PADDR2:%.*]], i32 2
+// CHECK: [[VECINIT3:%.*]] = insertelement <4 x double> [[VECINIT]], double 
[[VECINIT2]], i32 2
+// CHECK: [[VECINIT4:%.*]] = extractelement <4 x double> [[QADDR3:%.*]], i32 3
+// CHECK: [[VECINIT5:%.*]] = insertelement <4 x double> [[VECINIT3]], double 
[[VECINIT4]], i32 3
+// CHECK-NEXT: ret <4 x double> [[VECINIT5]]
+double4 dstWithDouble(double4 p1, double4 p2)
+{
+    return dst(p1, p2);
+}
+


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to