Skip to content

[HLSL] Expose half types and intrinsics always #81782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions clang/lib/Headers/hlsl/hlsl_basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ typedef vector<uint64_t, 2> uint64_t2;
typedef vector<uint64_t, 3> uint64_t3;
typedef vector<uint64_t, 4> uint64_t4;

#ifdef __HLSL_ENABLE_16_BIT
typedef vector<half, 2> half2;
typedef vector<half, 3> half3;
typedef vector<half, 4> half4;
#endif

typedef vector<float, 2> float2;
typedef vector<float, 3> float3;
Expand Down
131 changes: 59 additions & 72 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ namespace hlsl {
#define _HLSL_AVAILABILITY(environment, version) \
__attribute__((availability(environment, introduced = version)))

#ifdef __HLSL_ENABLE_16_BIT
#define _HLSL_16BIT_AVAILABILITY(environment, version) \
__attribute__((availability(environment, introduced = version)))
#else
#define _HLSL_16BIT_AVAILABILITY(environment, version)
#endif

//===----------------------------------------------------------------------===//
// abs builtins
//===----------------------------------------------------------------------===//
Expand All @@ -42,20 +49,20 @@ int16_t3 abs(int16_t3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_abs)
int16_t4 abs(int16_t4);
#endif

_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently in dxc the16-bit types are only available when the -enable-16bit-types command line flag is provided. They are not available by default in SM 6.2+.

Is this availability macro making the 16-bit types always available when targeting SM 6.2+?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is complicated. half is a valid type regardless of whether or not 16bit types are enabled, and half is a distinctly different type when 16-bit types are enabled. What this does is put the availability annotation on the 16-bit half type's overloads, but not the 32-bit half type's overloads.

That way if you're writing a shader for SM 6.0, using 32-bit half you get don't get blocked by the SM 6.2 availability annotation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. So this isn't changing how the enabling of 16bit types are working. It just lets the 32-bit half overloads work in earlier shader models.

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_abs)
half abs(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_abs)
half2 abs(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_abs)
half3 abs(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_abs)
half4 abs(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_abs)
int abs(int);
Expand Down Expand Up @@ -102,20 +109,18 @@ double4 abs(double4);
/// the input value, \a Val.
/// \param Val The input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_ceil)
half ceil(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_ceil)
half2 ceil(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_ceil)
half3 ceil(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_ceil)
half4 ceil(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_ceil)
float ceil(float);
Expand Down Expand Up @@ -143,20 +148,18 @@ double4 ceil(double4);
/// \brief Returns the cosine of the input value, \a Val.
/// \param Val The input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_cos)
half cos(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_cos)
half2 cos(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_cos)
half3 cos(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_cos)
half4 cos(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_cos)
float cos(float);
Expand Down Expand Up @@ -185,20 +188,18 @@ double4 cos(double4);
/// value, \a Val.
/// \param Val The input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
half floor(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
half2 floor(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
half3 floor(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
half4 floor(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_floor)
float floor(float);
Expand Down Expand Up @@ -229,20 +230,18 @@ double4 floor(double4);
/// If \a Val is negative, this result is undefined. If \a Val is 0, this
/// function returns negative infinity.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log)
half log(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log)
half2 log(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log)
half3 log(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log)
half4 log(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log)
float log(float);
Expand Down Expand Up @@ -273,20 +272,18 @@ double4 log(double4);
/// If \a Val is negative, this result is undefined. If \a Val is 0, this
/// function returns negative infinity.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log10)
half log10(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log10)
half2 log10(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log10)
half3 log10(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log10)
half4 log10(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log10)
float log10(float);
Expand Down Expand Up @@ -317,20 +314,18 @@ double4 log10(double4);
/// If \a Val is negative, this result is undefined. If \a Val is 0, this
/// function returns negative infinity.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log2)
half log2(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log2)
half2 log2(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log2)
half3 log2(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log2)
half4 log2(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_log2)
float log2(float);
Expand Down Expand Up @@ -359,20 +354,20 @@ double4 log2(double4);
/// \param X The X input value.
/// \param Y The Y input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_max)
half max(half, half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_max)
half2 max(half2, half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_max)
half3 max(half3, half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_max)
half4 max(half4, half4);

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_max)
int16_t max(int16_t, int16_t);
Expand Down Expand Up @@ -463,20 +458,20 @@ double4 max(double4, double4);
/// \param X The X input value.
/// \param Y The Y input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_min)
half min(half, half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_min)
half2 min(half2, half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_min)
half3 min(half3, half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_min)
half4 min(half4, half4);

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_min)
int16_t min(int16_t, int16_t);
Expand Down Expand Up @@ -567,20 +562,18 @@ double4 min(double4, double4);
/// \param Val The input value.
/// \param Pow The specified power.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_pow)
half pow(half, half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_pow)
half2 pow(half2, half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_pow)
half3 pow(half3, half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_pow)
half4 pow(half4, half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_pow)
float pow(float, float);
Expand Down Expand Up @@ -680,20 +673,18 @@ uint64_t4 reversebits(uint64_t4);
/// \brief Returns the sine of the input value, \a Val.
/// \param Val The input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_sin)
half sin(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_sin)
half2 sin(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_sin)
half3 sin(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_sin)
half4 sin(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_sin)
float sin(float);
Expand Down Expand Up @@ -721,11 +712,9 @@ double4 sin(double4);
/// \brief Returns the square root of the input value, \a Val.
/// \param Val The input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_sqrtf16)
half sqrt(half In);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_sqrtf)
float sqrt(float In);
Expand All @@ -741,20 +730,18 @@ double sqrt(double In);
/// \brief Returns the truncated integer value of the input value, \a Val.
/// \param Val The input value.

#ifdef __HLSL_ENABLE_16_BIT
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_trunc)
half trunc(half);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_trunc)
half2 trunc(half2);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_trunc)
half3 trunc(half3);
_HLSL_AVAILABILITY(shadermodel, 6.2)
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
_HLSL_BUILTIN_ALIAS(__builtin_elementwise_trunc)
half4 trunc(half4);
#endif

_HLSL_BUILTIN_ALIAS(__builtin_elementwise_trunc)
float trunc(float);
Expand Down
5 changes: 4 additions & 1 deletion clang/test/CodeGenHLSL/builtins/abs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// RUN: -emit-llvm -disable-llvm-passes -O3 -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: -D__HLSL_ENABLE_16_BIT -o - | FileCheck %s --check-prefix=NO_HALF
// RUN: -o - | FileCheck %s --check-prefix=NO_HALF

using hlsl::abs;

#ifdef __HLSL_ENABLE_16_BIT
// CHECK: define noundef i16 @
// CHECK: call i16 @llvm.abs.i16(
int16_t test_abs_int16_t ( int16_t p0 ) {
Expand All @@ -27,6 +28,8 @@ int16_t3 test_abs_int16_t3 ( int16_t3 p0 ) {
int16_t4 test_abs_int16_t4 ( int16_t4 p0 ) {
return abs ( p0 );
}
#endif // __HLSL_ENABLE_16_BIT

// CHECK: define noundef half @
// CHECK: call half @llvm.fabs.f16(
// NO_HALF: define noundef float @"?test_abs_half@@YA$halff@$halff@@Z"(
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenHLSL/builtins/ceil.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: -emit-llvm -disable-llvm-passes -O3 -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: -D__HLSL_ENABLE_16_BIT -o - | FileCheck %s --check-prefix=NO_HALF
// RUN: -o - | FileCheck %s --check-prefix=NO_HALF

using hlsl::ceil;

Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenHLSL/builtins/cos.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: -emit-llvm -disable-llvm-passes -O3 -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: -D__HLSL_ENABLE_16_BIT -o - | FileCheck %s --check-prefix=NO_HALF
// RUN: -o - | FileCheck %s --check-prefix=NO_HALF

// CHECK: define noundef half @
// CHECK: call half @llvm.cos.f16(
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenHLSL/builtins/floor.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: -emit-llvm -disable-llvm-passes -O3 -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: -D__HLSL_ENABLE_16_BIT -o - | FileCheck %s --check-prefix=NO_HALF
// RUN: -o - | FileCheck %s --check-prefix=NO_HALF

using hlsl::floor;

Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenHLSL/builtins/log.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: -emit-llvm -disable-llvm-passes -O3 -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: -D__HLSL_ENABLE_16_BIT -o - | FileCheck %s --check-prefix=NO_HALF
// RUN: -o - | FileCheck %s --check-prefix=NO_HALF

// CHECK: define noundef half @
// CHECK: call half @llvm.log.f16(
Expand Down
Loading