[PATCH] D132711: [HLSL] add sqrt library function

Joshua Batista via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 25 17:21:57 PDT 2022


bob80905 created this revision.
bob80905 added reviewers: beanz, pow2clk.
Herald added a subscriber: Anastasia.
Herald added a project: All.
bob80905 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132711

Files:
  clang/lib/Headers/hlsl/hlsl_intrinsics.h
  clang/test/CodeGenHLSL/builtins/sqrt.hlsl


Index: clang/test/CodeGenHLSL/builtins/sqrt.hlsl
===================================================================
--- /dev/null
+++ 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.3-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.3-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)
\ No newline at end of file
Index: clang/lib/Headers/hlsl/hlsl_intrinsics.h
===================================================================
--- clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -12,6 +12,7 @@
 __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);
@@ -21,4 +22,12 @@
 __attribute__((clang_builtin_alias(__builtin_fabsf16))) half abs(half In);
 #endif
 
+// 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_


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132711.455767.patch
Type: text/x-patch
Size: 2228 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220826/954ff79f/attachment-0001.bin>


More information about the cfe-commits mailing list