Skip to content

Commit 9ec9287

Browse files
committed
Revert "[HLSL] Move length support out of the DirectX Backend (#121611)"
This reverts commit a6b7181. Breaks Clang :: CodeGenHLSL/builtins/length.hlsl, see #121611 (comment)
1 parent 1c99907 commit 9ec9287

File tree

14 files changed

+324
-198
lines changed

14 files changed

+324
-198
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,6 +4865,12 @@ 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+
48684874
def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
48694875
let Spellings = ["__builtin_hlsl_lerp"];
48704876
let Attributes = [NoThrow, Const];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19334,6 +19334,20 @@ 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+
}
1933719351
case Builtin::BI__builtin_hlsl_normalize: {
1933819352
Value *X = EmitScalarExpr(E->getArg(0));
1933919353

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ 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)
8081
GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
8182
GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
8283
GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ 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-
2416
template <bool B, typename T> struct enable_if {};
2517

2618
template <typename T> struct enable_if<true, T> {
@@ -41,21 +33,6 @@ constexpr enable_if_t<sizeof(U) == sizeof(T), U> bit_cast(T F) {
4133
return __builtin_bit_cast(U, F);
4234
}
4335

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-
5936
} // namespace __detail
6037
} // namespace hlsl
6138
#endif //_HLSL_HLSL_DETAILS_H_

clang/lib/Headers/hlsl/hlsl_intrinsics.h

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

1300-
const inline half length(half X) { return __detail::length_impl(X); }
1301-
const inline float length(float X) { return __detail::length_impl(X); }
1302-
1303-
template <int N> const inline half length(vector<half, N> X) {
1304-
return __detail::length_vec_impl(X);
1305-
}
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);
13061312

1307-
template <int N> const inline float length(vector<float, N> X) {
1308-
return __detail::length_vec_impl(X);
1309-
}
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);
13101321

13111322
//===----------------------------------------------------------------------===//
13121323
// log builtins

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,24 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
21122112
return true;
21132113
break;
21142114
}
2115+
case Builtin::BI__builtin_hlsl_length: {
2116+
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
2117+
return true;
2118+
if (SemaRef.checkArgCount(TheCall, 1))
2119+
return true;
2120+
2121+
ExprResult A = TheCall->getArg(0);
2122+
QualType ArgTyA = A.get()->getType();
2123+
QualType RetTy;
2124+
2125+
if (auto *VTy = ArgTyA->getAs<VectorType>())
2126+
RetTy = VTy->getElementType();
2127+
else
2128+
RetTy = TheCall->getArg(0)->getType();
2129+
2130+
TheCall->setType(RetTy);
2131+
break;
2132+
}
21152133
case Builtin::BI__builtin_hlsl_mad: {
21162134
if (SemaRef.checkArgCount(TheCall, 3))
21172135
return true;
Lines changed: 73 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,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-
}
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+
}

0 commit comments

Comments
 (0)