[clang] b95c574 - [HLSL] add sqrt library function

Chris Bieneman via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 20 13:42:36 PDT 2022


Author: Joshua Batista
Date: 2022-09-20T15:42:08-05:00
New Revision: b95c57444a8ac3f81bc33a3d52d0f88b7a592a2f

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

LOG: [HLSL] add sqrt library function

This change exposes the sqrt library function for HLSL scalar types,
excluding long and long long doubles. Sqrt is supported for all scalar, vector,
and matrix types. This patch only adds a subset of scalar type support.

Long and long long double support is missing in this patch because that type
doesn't exist in HLSL.

The full documentation of the HLSL asin function is available here:
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-sqrt

Reviewed By: beanz

Differential Revision: https://reviews.llvm.org/D132711

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

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 02db79a2bc37d..3eb16b22d5ab9 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -13,6 +13,13 @@ __attribute__((availability(shadermodel, introduced = 6.0)))
 __attribute__((clang_builtin_alias(__builtin_hlsl_wave_active_count_bits))) uint
 WaveActiveCountBits(bool bBit);
 
+
+// abs builtins
+__attribute__((clang_builtin_alias(__builtin_abs))) int abs(int In);
+__attribute__((clang_builtin_alias(__builtin_labs))) int64_t abs(int64_t In);
+__attribute__((clang_builtin_alias(__builtin_fabsf))) float abs(float In);
+__attribute__((clang_builtin_alias(__builtin_fabs))) double abs(double In);
+
 #ifdef __HLSL_ENABLE_16_BIT
 __attribute__((clang_builtin_alias(__builtin_elementwise_abs)))
 int16_t abs(int16_t);
@@ -31,6 +38,7 @@ __attribute__((clang_builtin_alias(__builtin_elementwise_abs)))
 half4 abs(half4);
 #endif
 
+
 __attribute__((clang_builtin_alias(__builtin_elementwise_abs))) int abs(int);
 __attribute__((clang_builtin_alias(__builtin_elementwise_abs))) int2 abs(int2);
 __attribute__((clang_builtin_alias(__builtin_elementwise_abs))) int3 abs(int3);
@@ -60,4 +68,13 @@ double3 abs(double3);
 __attribute__((clang_builtin_alias(__builtin_elementwise_abs)))
 double4 abs(double4);
 
+// sqrt builtins
+__attribute__((clang_builtin_alias(__builtin_sqrt))) double sqrt(double In);
+__attribute__((clang_builtin_alias(__builtin_sqrtf))) float sqrt(float In);
+
+#ifdef __HLSL_ENABLE_16_BIT
+__attribute__((clang_builtin_alias(__builtin_sqrtf16))) half sqrt(half In);
+#endif
+
+
 #endif //_HLSL_HLSL_INTRINSICS_H_

diff  --git a/clang/test/CodeGenHLSL/builtins/sqrt.hlsl b/clang/test/CodeGenHLSL/builtins/sqrt.hlsl
new file mode 100644
index 0000000000000..c083ce1175e59
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/sqrt.hlsl
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.2-library %s -fnative-half-type \
+// RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.2-library %s -emit-llvm -disable-llvm-passes \
+// RUN:   -o - | FileCheck %s --check-prefix=NO_HALF
+
+double sqrt_d(double x)
+{
+  return sqrt(x);
+}
+
+// CHECK: define noundef double @"?sqrt_d@@YANN at Z"(
+// CHECK: call double @llvm.sqrt.f64(double %0)
+
+float sqrt_f(float x)
+{
+  return sqrt(x);
+}
+
+// CHECK: define noundef float @"?sqrt_f@@YAMM at Z"(
+// CHECK: call float @llvm.sqrt.f32(float %0)
+
+half sqrt_h(half x)
+{
+  return sqrt(x);
+}
+
+// CHECK: define noundef half @"?sqrt_h@@YA$f16@$f16@@Z"(
+// CHECK: call half @llvm.sqrt.f16(half %0)
+// NO_HALF: define noundef float @"?sqrt_h@@YA$halff@$halff@@Z"(
+// NO_HALF: call float @llvm.sqrt.f32(float %0)


        


More information about the cfe-commits mailing list