Skip to content

Commit a6b7181

Browse files
authored
[HLSL] Move length support out of the DirectX Backend (#121611)
## Changes - Delete DirectX length intrinsic - Delete HLSL length lang builtin - Implement length algorithm entirely in the header. ## History - In the past if an HLSL intrinsic lowered to either a spirv op code or a DXIL opcode we represented it with intrinsics ## Why we are moving away? - To make HLSL apis more portable the team decided that it makes sense for some intrinsics to be defined only in the header. - Since there tends to be more SPIRV opcodes than DXIL opcodes the plan is to support SPIRV opcodes either with target specific builtins or via pattern matching.
1 parent 0e1c5bf commit a6b7181

File tree

14 files changed

+198
-324
lines changed

14 files changed

+198
-324
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,12 +4865,6 @@ def HLSLIsinf : LangBuiltin<"HLSL_LANG"> {
48654865
let Prototype = "void(...)";
48664866
}
48674867

4868-
def HLSLLength : LangBuiltin<"HLSL_LANG"> {
4869-
let Spellings = ["__builtin_hlsl_length"];
4870-
let Attributes = [NoThrow, Const];
4871-
let Prototype = "void(...)";
4872-
}
4873-
48744868
def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
48754869
let Spellings = ["__builtin_hlsl_lerp"];
48764870
let Attributes = [NoThrow, Const];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19334,20 +19334,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1933419334
/*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
1933519335
ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
1933619336
}
19337-
case Builtin::BI__builtin_hlsl_length: {
19338-
Value *X = EmitScalarExpr(E->getArg(0));
19339-
19340-
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
19341-
"length operand must have a float representation");
19342-
// if the operand is a scalar, we can use the fabs llvm intrinsic directly
19343-
if (!E->getArg(0)->getType()->isVectorType())
19344-
return EmitFAbs(*this, X);
19345-
19346-
return Builder.CreateIntrinsic(
19347-
/*ReturnType=*/X->getType()->getScalarType(),
19348-
CGM.getHLSLRuntime().getLengthIntrinsic(), ArrayRef<Value *>{X},
19349-
nullptr, "hlsl.length");
19350-
}
1935119337
case Builtin::BI__builtin_hlsl_normalize: {
1935219338
Value *X = EmitScalarExpr(E->getArg(0));
1935319339

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class CGHLSLRuntime {
7777
GENERATE_HLSL_INTRINSIC_FUNCTION(Cross, cross)
7878
GENERATE_HLSL_INTRINSIC_FUNCTION(Degrees, degrees)
7979
GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
80-
GENERATE_HLSL_INTRINSIC_FUNCTION(Length, length)
8180
GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
8281
GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
8382
GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ namespace hlsl {
1313

1414
namespace __detail {
1515

16+
template <typename T, typename U> struct is_same {
17+
static const bool value = false;
18+
};
19+
20+
template <typename T> struct is_same<T, T> {
21+
static const bool value = true;
22+
};
23+
1624
template <bool B, typename T> struct enable_if {};
1725

1826
template <typename T> struct enable_if<true, T> {
@@ -33,6 +41,21 @@ constexpr enable_if_t<sizeof(U) == sizeof(T), U> bit_cast(T F) {
3341
return __builtin_bit_cast(U, F);
3442
}
3543

44+
template <typename T>
45+
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
46+
length_impl(T X) {
47+
return __builtin_elementwise_abs(X);
48+
}
49+
50+
template <typename T, int N>
51+
enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
52+
length_vec_impl(vector<T, N> X) {
53+
vector<T, N> XSquared = X * X;
54+
T XSquaredSum = XSquared[0];
55+
[unroll] for (int i = 1; i < N; ++i) XSquaredSum += XSquared[i];
56+
return __builtin_elementwise_sqrt(XSquaredSum);
57+
}
58+
3659
} // namespace __detail
3760
} // namespace hlsl
3861
#endif //_HLSL_HLSL_DETAILS_H_

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,27 +1297,16 @@ float4 lerp(float4, float4, float4);
12971297
///
12981298
/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + ...).
12991299

1300-
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1301-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1302-
half length(half);
1303-
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1304-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1305-
half length(half2);
1306-
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1307-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1308-
half length(half3);
1309-
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1310-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1311-
half length(half4);
1300+
const inline half length(half X) { return __detail::length_impl(X); }
1301+
const inline float length(float X) { return __detail::length_impl(X); }
13121302

1313-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1314-
float length(float);
1315-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1316-
float length(float2);
1317-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1318-
float length(float3);
1319-
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
1320-
float length(float4);
1303+
template <int N> const inline half length(vector<half, N> X) {
1304+
return __detail::length_vec_impl(X);
1305+
}
1306+
1307+
template <int N> const inline float length(vector<float, N> X) {
1308+
return __detail::length_vec_impl(X);
1309+
}
13211310

13221311
//===----------------------------------------------------------------------===//
13231312
// log builtins

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,24 +2100,6 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
21002100
return true;
21012101
break;
21022102
}
2103-
case Builtin::BI__builtin_hlsl_length: {
2104-
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
2105-
return true;
2106-
if (SemaRef.checkArgCount(TheCall, 1))
2107-
return true;
2108-
2109-
ExprResult A = TheCall->getArg(0);
2110-
QualType ArgTyA = A.get()->getType();
2111-
QualType RetTy;
2112-
2113-
if (auto *VTy = ArgTyA->getAs<VectorType>())
2114-
RetTy = VTy->getElementType();
2115-
else
2116-
RetTy = TheCall->getArg(0)->getType();
2117-
2118-
TheCall->setType(RetTy);
2119-
break;
2120-
}
21212103
case Builtin::BI__builtin_hlsl_mad: {
21222104
if (SemaRef.checkArgCount(TheCall, 3))
21232105
return true;
Lines changed: 130 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,130 @@
1-
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2-
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3-
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4-
// RUN: --check-prefixes=CHECK,NATIVE_HALF
5-
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6-
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7-
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
8-
9-
// NATIVE_HALF: define noundef nofpclass(nan inf) half @
10-
// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn half @llvm.fabs.f16(half
11-
// NO_HALF: call reassoc nnan ninf nsz arcp afn float @llvm.fabs.f32(float
12-
// NATIVE_HALF: ret half
13-
// NO_HALF: ret float
14-
half test_length_half(half p0)
15-
{
16-
return length(p0);
17-
}
18-
// NATIVE_HALF: define noundef nofpclass(nan inf) half @
19-
// NATIVE_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn half @llvm.dx.length.v2f16
20-
// NO_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn float @llvm.dx.length.v2f32(
21-
// NATIVE_HALF: ret half %hlsl.length
22-
// NO_HALF: ret float %hlsl.length
23-
half test_length_half2(half2 p0)
24-
{
25-
return length(p0);
26-
}
27-
// NATIVE_HALF: define noundef nofpclass(nan inf) half @
28-
// NATIVE_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn half @llvm.dx.length.v3f16
29-
// NO_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn float @llvm.dx.length.v3f32(
30-
// NATIVE_HALF: ret half %hlsl.length
31-
// NO_HALF: ret float %hlsl.length
32-
half test_length_half3(half3 p0)
33-
{
34-
return length(p0);
35-
}
36-
// NATIVE_HALF: define noundef nofpclass(nan inf) half @
37-
// NATIVE_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn half @llvm.dx.length.v4f16
38-
// NO_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn float @llvm.dx.length.v4f32(
39-
// NATIVE_HALF: ret half %hlsl.length
40-
// NO_HALF: ret float %hlsl.length
41-
half test_length_half4(half4 p0)
42-
{
43-
return length(p0);
44-
}
45-
46-
// CHECK: define noundef nofpclass(nan inf) float @
47-
// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.fabs.f32(float
48-
// CHECK: ret float
49-
float test_length_float(float p0)
50-
{
51-
return length(p0);
52-
}
53-
// CHECK: define noundef nofpclass(nan inf) float @
54-
// CHECK: %hlsl.length = call reassoc nnan ninf nsz arcp afn float @llvm.dx.length.v2f32(
55-
// CHECK: ret float %hlsl.length
56-
float test_length_float2(float2 p0)
57-
{
58-
return length(p0);
59-
}
60-
// CHECK: define noundef nofpclass(nan inf) float @
61-
// CHECK: %hlsl.length = call reassoc nnan ninf nsz arcp afn float @llvm.dx.length.v3f32(
62-
// CHECK: ret float %hlsl.length
63-
float test_length_float3(float3 p0)
64-
{
65-
return length(p0);
66-
}
67-
// CHECK: define noundef nofpclass(nan inf) float @
68-
// CHECK: %hlsl.length = call reassoc nnan ninf nsz arcp afn float @llvm.dx.length.v4f32(
69-
// CHECK: ret float %hlsl.length
70-
float test_length_float4(float4 p0)
71-
{
72-
return length(p0);
73-
}
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -finclude-default-header -triple \
3+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
4+
// RUN: -emit-llvm -O1 -o - | FileCheck %s
5+
6+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z16test_length_halfDh(
7+
// CHECK-SAME: half noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
8+
// CHECK-NEXT: [[ENTRY:.*:]]
9+
// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.fabs.f16(half [[P0]])
10+
// CHECK-NEXT: ret half [[ELT_ABS_I]]
11+
//
12+
half test_length_half(half p0)
13+
{
14+
return length(p0);
15+
}
16+
17+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z17test_length_half2Dv2_Dh(
18+
// CHECK-SAME: <2 x half> noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
19+
// CHECK-NEXT: [[ENTRY:.*:]]
20+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <2 x half> [[P0]], [[P0]]
21+
// CHECK-NEXT: [[VECEXT_I:%.*]] = extractelement <2 x half> [[MUL_I]], i64 0
22+
// CHECK-NEXT: [[VECEXT1_I:%.*]] = extractelement <2 x half> [[MUL_I]], i64 1
23+
// CHECK-NEXT: [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn half [[VECEXT_I]], [[VECEXT1_I]]
24+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.sqrt.f16(half [[ADD_I]])
25+
// CHECK-NEXT: ret half [[TMP0]]
26+
//
27+
half test_length_half2(half2 p0)
28+
{
29+
return length(p0);
30+
}
31+
32+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z17test_length_half3Dv3_Dh(
33+
// CHECK-SAME: <3 x half> noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
34+
// CHECK-NEXT: [[ENTRY:.*:]]
35+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <3 x half> [[P0]], [[P0]]
36+
// CHECK-NEXT: [[VECEXT_I:%.*]] = extractelement <3 x half> [[MUL_I]], i64 0
37+
// CHECK-NEXT: [[VECEXT1_I:%.*]] = extractelement <3 x half> [[MUL_I]], i64 1
38+
// CHECK-NEXT: [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn half [[VECEXT1_I]], [[VECEXT_I]]
39+
// CHECK-NEXT: [[VECEXT1_I_1:%.*]] = extractelement <3 x half> [[MUL_I]], i64 2
40+
// CHECK-NEXT: [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn half [[VECEXT1_I_1]], [[ADD_I]]
41+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.sqrt.f16(half [[ADD_I_1]])
42+
// CHECK-NEXT: ret half [[TMP0]]
43+
//
44+
half test_length_half3(half3 p0)
45+
{
46+
return length(p0);
47+
}
48+
49+
// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z17test_length_half4Dv4_Dh(
50+
// CHECK-SAME: <4 x half> noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
51+
// CHECK-NEXT: [[ENTRY:.*:]]
52+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <4 x half> [[P0]], [[P0]]
53+
// CHECK-NEXT: [[VECEXT_I:%.*]] = extractelement <4 x half> [[MUL_I]], i64 0
54+
// CHECK-NEXT: [[VECEXT1_I:%.*]] = extractelement <4 x half> [[MUL_I]], i64 1
55+
// CHECK-NEXT: [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn half [[VECEXT1_I]], [[VECEXT_I]]
56+
// CHECK-NEXT: [[VECEXT1_I_1:%.*]] = extractelement <4 x half> [[MUL_I]], i64 2
57+
// CHECK-NEXT: [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn half [[VECEXT1_I_1]], [[ADD_I]]
58+
// CHECK-NEXT: [[VECEXT1_I_2:%.*]] = extractelement <4 x half> [[MUL_I]], i64 3
59+
// CHECK-NEXT: [[ADD_I_2:%.*]] = fadd reassoc nnan ninf nsz arcp afn half [[VECEXT1_I_2]], [[ADD_I_1]]
60+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef half @llvm.sqrt.f16(half [[ADD_I_2]])
61+
// CHECK-NEXT: ret half [[TMP0]]
62+
//
63+
half test_length_half4(half4 p0)
64+
{
65+
return length(p0);
66+
}
67+
68+
69+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z17test_length_floatf(
70+
// CHECK-SAME: float noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
71+
// CHECK-NEXT: [[ENTRY:.*:]]
72+
// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.fabs.f32(float [[P0]])
73+
// CHECK-NEXT: ret float [[ELT_ABS_I]]
74+
//
75+
float test_length_float(float p0)
76+
{
77+
return length(p0);
78+
}
79+
80+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z18test_length_float2Dv2_f(
81+
// CHECK-SAME: <2 x float> noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
82+
// CHECK-NEXT: [[ENTRY:.*:]]
83+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <2 x float> [[P0]], [[P0]]
84+
// CHECK-NEXT: [[VECEXT_I:%.*]] = extractelement <2 x float> [[MUL_I]], i64 0
85+
// CHECK-NEXT: [[VECEXT1_I:%.*]] = extractelement <2 x float> [[MUL_I]], i64 1
86+
// CHECK-NEXT: [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn float [[VECEXT_I]], [[VECEXT1_I]]
87+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.sqrt.f32(float [[ADD_I]])
88+
// CHECK-NEXT: ret float [[TMP0]]
89+
//
90+
float test_length_float2(float2 p0)
91+
{
92+
return length(p0);
93+
}
94+
95+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z18test_length_float3Dv3_f(
96+
// CHECK-SAME: <3 x float> noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
97+
// CHECK-NEXT: [[ENTRY:.*:]]
98+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <3 x float> [[P0]], [[P0]]
99+
// CHECK-NEXT: [[VECEXT_I:%.*]] = extractelement <3 x float> [[MUL_I]], i64 0
100+
// CHECK-NEXT: [[VECEXT1_I:%.*]] = extractelement <3 x float> [[MUL_I]], i64 1
101+
// CHECK-NEXT: [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn float [[VECEXT1_I]], [[VECEXT_I]]
102+
// CHECK-NEXT: [[VECEXT1_I_1:%.*]] = extractelement <3 x float> [[MUL_I]], i64 2
103+
// CHECK-NEXT: [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn float [[VECEXT1_I_1]], [[ADD_I]]
104+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.sqrt.f32(float [[ADD_I_1]])
105+
// CHECK-NEXT: ret float [[TMP0]]
106+
//
107+
float test_length_float3(float3 p0)
108+
{
109+
return length(p0);
110+
}
111+
112+
113+
// CHECK-LABEL: define noundef nofpclass(nan inf) float @_Z18test_length_float4Dv4_f(
114+
// CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
115+
// CHECK-NEXT: [[ENTRY:.*:]]
116+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <4 x float> [[P0]], [[P0]]
117+
// CHECK-NEXT: [[VECEXT_I:%.*]] = extractelement <4 x float> [[MUL_I]], i64 0
118+
// CHECK-NEXT: [[VECEXT1_I:%.*]] = extractelement <4 x float> [[MUL_I]], i64 1
119+
// CHECK-NEXT: [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn float [[VECEXT1_I]], [[VECEXT_I]]
120+
// CHECK-NEXT: [[VECEXT1_I_1:%.*]] = extractelement <4 x float> [[MUL_I]], i64 2
121+
// CHECK-NEXT: [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn float [[VECEXT1_I_1]], [[ADD_I]]
122+
// CHECK-NEXT: [[VECEXT1_I_2:%.*]] = extractelement <4 x float> [[MUL_I]], i64 3
123+
// CHECK-NEXT: [[ADD_I_2:%.*]] = fadd reassoc nnan ninf nsz arcp afn float [[VECEXT1_I_2]], [[ADD_I_1]]
124+
// CHECK-NEXT: [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn noundef float @llvm.sqrt.f32(float [[ADD_I_2]])
125+
// CHECK-NEXT: ret float [[TMP0]]
126+
//
127+
float test_length_float4(float4 p0)
128+
{
129+
return length(p0);
130+
}

0 commit comments

Comments
 (0)