[PATCH] D131718: [HLSL] Add abs library function

Chris Bieneman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 11 13:46:08 PDT 2022


beanz created this revision.
beanz added reviewers: tex3d, python3kgae, bogner, pow2clk, bob80905.
Herald added a subscriber: Anastasia.
Herald added a project: All.
beanz requested review of this revision.
Herald added a project: clang.

This change exposes the abs library function for HLSL scalar types. Abs
is supported for all scalar, vector and matrix types. This patch only
adds a subset of scalar type support.

Half support is missing in this patch due to issue #57100.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131718

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


Index: clang/test/CodeGenHLSL/builtins/abs.hlsl
===================================================================
--- /dev/null
+++ clang/test/CodeGenHLSL/builtins/abs.hlsl
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+
+
+float abs_float(float X) {
+  return abs(X);
+}
+
+// CHECK: define noundef float @"?abs_float@@YAMM at Z"(float
+// CHECK: call float @llvm.fabs.f32(float %0)
+
+int abs_int(int X) {
+  return abs(X);
+}
+
+// CHECK: define noundef i32 @"?abs_int@@YAHH at Z"(i32
+// CHECK:         [[A_ADDR:%.*]] = alloca i32, align 4
+// CHECK-NEXT:    store i32 [[A:%.*]], ptr [[A_ADDR]], align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[A_ADDR]], align 4
+// CHECK-NEXT:    [[NEG:%.*]] = sub nsw i32 0, [[TMP0]]
+// CHECK-NEXT:    [[ABSCOND:%.*]] = icmp slt i32 [[TMP0]], 0
+// CHECK-NEXT:    [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[TMP0]]
+// CHECK-NEXT:    ret i32 [[ABS]]
Index: clang/lib/Headers/hlsl/hlsl_intrinsics.h
===================================================================
--- clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -12,4 +12,9 @@
 __attribute__((clang_builtin_alias(__builtin_hlsl_wave_active_count_bits))) uint
 WaveActiveCountBits(bool bBit);
 
+__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);
+
 #endif //_HLSL_HLSL_INTRINSICS_H_


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131718.451970.patch
Type: text/x-patch
Size: 1720 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220811/bf4090d8/attachment.bin>


More information about the cfe-commits mailing list