[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
This commit is contained in:
Chris Bieneman 2022-08-25 18:25:20 -05:00
parent e723c69b94
commit 18385cffc5
4 changed files with 68 additions and 2 deletions

View File

@ -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">>,

View File

@ -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,

View File

@ -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_

View File

@ -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@Z"(
// CHECK: call float @llvm.fabs.f32(float %0)
double abs_double(double X) {
return abs(X);
}
// CHECK: define noundef double @"?abs_double@@YANN@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@Z"(i32
// CHECK: define noundef i32 @"?abs_int@@YAHH@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@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]]