[clang] 18385cf - [HLSL] Add abs library function
Chris Bieneman via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 25 17:02:46 PDT 2022
Author: Chris Bieneman
Date: 2022-08-25T18:56:51-05:00
New Revision: 18385cffc50a6e549dcf3306cbe8a99e10a11fff
URL: https://github.com/llvm/llvm-project/commit/18385cffc50a6e549dcf3306cbe8a99e10a11fff
DIFF: https://github.com/llvm/llvm-project/commit/18385cffc50a6e549dcf3306cbe8a99e10a11fff.diff
LOG: [HLSL] Add abs library function
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.
Fixes #57100 (https://llvm.org/pr57100)
The full documentation of the HLSL abs function is available here:
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-abs
Reviewed By: bogner
Differential Revision: https://reviews.llvm.org/D131718
Added:
clang/test/CodeGenHLSL/builtins/abs.hlsl
Modified:
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Headers/hlsl/hlsl_intrinsics.h
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 76aed6a7c928f..2b284b405e61c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -495,6 +495,7 @@ defvar render_script = LangOpts<"RenderScript">;
defvar hip = LangOpts<"HIP">;
defvar gnu_mode = LangOpts<"GNUMode">;
defvar asm_preprocessor = LangOpts<"AsmPreprocessor">;
+defvar hlsl = LangOpts<"HLSL">;
defvar std = !strconcat("LangStandard::getLangStandardForKind(", lang_std.KeyPath, ")");
@@ -6134,7 +6135,7 @@ def fnative_half_type: Flag<["-"], "fnative-half-type">,
def fnative_half_arguments_and_returns : Flag<["-"], "fnative-half-arguments-and-returns">,
HelpText<"Use the native __fp16 type for arguments and returns (and skip ABI-specific lowering)">,
MarshallingInfoFlag<LangOpts<"NativeHalfArgsAndReturns">>,
- ImpliedByAnyOf<[open_cl.KeyPath, render_script.KeyPath]>;
+ ImpliedByAnyOf<[open_cl.KeyPath, render_script.KeyPath, hlsl.KeyPath]>;
def fallow_half_arguments_and_returns : Flag<["-"], "fallow-half-arguments-and-returns">,
HelpText<"Allow function arguments and returns of type half">,
MarshallingInfoFlag<LangOpts<"HalfArgsAndReturns">>,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index eaf9a1f23fd50..d9bc80add1cf7 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3520,7 +3520,6 @@ static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
// Add the default headers if dxc_no_stdinc is not set.
if (!Args.hasArg(options::OPT_dxc_no_stdinc))
CmdArgs.push_back("-finclude-default-header");
- CmdArgs.push_back("-fallow-half-arguments-and-returns");
}
static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index b5cdb8b449709..f43a6ad4779a9 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -12,4 +12,13 @@
__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);
+
+#ifdef __HLSL_ENABLE_16_BIT
+__attribute__((clang_builtin_alias(__builtin_fabsf16))) half abs(half In);
+#endif
+
#endif //_HLSL_HLSL_INTRINSICS_H_
diff --git a/clang/test/CodeGenHLSL/builtins/abs.hlsl b/clang/test/CodeGenHLSL/builtins/abs.hlsl
new file mode 100644
index 0000000000000..6578475f40170
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/abs.hlsl
@@ -0,0 +1,57 @@
+// 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
+
+
+float abs_float(float X) {
+ return abs(X);
+}
+
+// CHECK: define noundef float @"?abs_float@@YAMM at Z"(
+// CHECK: call float @llvm.fabs.f32(float %0)
+
+double abs_double(double X) {
+ return abs(X);
+}
+
+// CHECK: define noundef double @"?abs_double@@YANN at Z"(
+// CHECK: call double @llvm.fabs.f64(double %0)
+
+half abs_half(half X) {
+ return abs(X);
+}
+
+// CHECK: define noundef half @"?abs_half@@YA$f16@$f16@@Z"(
+// CHECK: call half @llvm.fabs.f16(half %0)
+// NO_HALF: define noundef float @"?abs_half@@YA$halff@$halff@@Z"(
+// NO_HALF: call float @llvm.fabs.f32(float %0)
+
+int abs_int(int X) {
+ return abs(X);
+}
+
+// NO_HALF: define noundef i32 @"?abs_int@@YAHH at Z"(i32
+// 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]]
+
+int64_t abs_int64(int64_t X) {
+ return abs(X);
+}
+
+// CHECK: define noundef i64 @"?abs_int64@@YAJJ at Z"(i64
+// CHECK: [[A_ADDR:%.*]] = alloca i64, align 8
+// CHECK-NEXT: store i64 [[A:%.*]], ptr [[A_ADDR]], align 8
+// CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[A_ADDR]], align 8
+// CHECK-NEXT: [[NEG:%.*]] = sub nsw i64 0, [[TMP0]]
+// CHECK-NEXT: [[ABSCOND:%.*]] = icmp slt i64 [[TMP0]], 0
+// CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i64 [[NEG]], i64 [[TMP0]]
+// CHECK-NEXT: ret i64 [[ABS]]
More information about the cfe-commits
mailing list