Skip to content

Commit 2de0c49

Browse files
committed
[HLSL] Move length intrinsic to the header
1 parent a106ad0 commit 2de0c49

File tree

14 files changed

+406
-302
lines changed

14 files changed

+406
-302
lines changed

clang/include/clang/Basic/Builtins.td

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

4855-
def HLSLLength : LangBuiltin<"HLSL_LANG"> {
4856-
let Spellings = ["__builtin_hlsl_length"];
4857-
let Attributes = [NoThrow, Const];
4858-
let Prototype = "void(...)";
4859-
}
4860-
48614855
def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
48624856
let Spellings = ["__builtin_hlsl_lerp"];
48634857
let Attributes = [NoThrow, Const];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19290,31 +19290,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1929019290
/*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
1929119291
ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
1929219292
}
19293-
case Builtin::BI__builtin_hlsl_length: {
19294-
Value *X = EmitScalarExpr(E->getArg(0));
19295-
19296-
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
19297-
"length operand must have a float representation");
19298-
// if the operand is a scalar, we can use the fabs llvm intrinsic directly
19299-
if (!E->getArg(0)->getType()->isVectorType())
19300-
return EmitFAbs(*this, X);
19301-
19302-
return Builder.CreateIntrinsic(
19303-
/*ReturnType=*/X->getType()->getScalarType(),
19304-
CGM.getHLSLRuntime().getLengthIntrinsic(), ArrayRef<Value *>{X},
19305-
nullptr, "hlsl.length");
19306-
}
19307-
case Builtin::BI__builtin_hlsl_normalize: {
19308-
Value *X = EmitScalarExpr(E->getArg(0));
19309-
19310-
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
19311-
"normalize operand must have a float representation");
19312-
19313-
return Builder.CreateIntrinsic(
19314-
/*ReturnType=*/X->getType(),
19315-
CGM.getHLSLRuntime().getNormalizeIntrinsic(), ArrayRef<Value *>{X},
19316-
nullptr, "hlsl.normalize");
19317-
}
1931819293
case Builtin::BI__builtin_hlsl_elementwise_degrees: {
1931919294
Value *X = EmitScalarExpr(E->getArg(0));
1932019295

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: 22 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,20 @@ 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+
constexpr 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 = __builtin_reduce_add(XSquared);
55+
return __builtin_elementwise_sqrt(XSquaredSum);
56+
}
57+
3658
} // namespace __detail
3759
} // namespace hlsl
3860
#endif //_HLSL_HLSL_DETAILS_H_

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,27 +1297,18 @@ 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>
1304+
const inline half length(vector<half, N> X) {
1305+
return __detail::length_vec_impl(X);
1306+
}
1307+
1308+
template <int N>
1309+
const inline float length(vector<float, N> X) {
1310+
return __detail::length_vec_impl(X);
1311+
}
13211312

13221313
//===----------------------------------------------------------------------===//
13231314
// 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: 159 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,159 @@
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 half @
10-
// NATIVE_HALF: call half @llvm.fabs.f16(half
11-
// NO_HALF: call 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 half @
19-
// NATIVE_HALF: %hlsl.length = call half @llvm.dx.length.v2f16
20-
// NO_HALF: %hlsl.length = call 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 half @
28-
// NATIVE_HALF: %hlsl.length = call half @llvm.dx.length.v3f16
29-
// NO_HALF: %hlsl.length = call 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 half @
37-
// NATIVE_HALF: %hlsl.length = call half @llvm.dx.length.v4f16
38-
// NO_HALF: %hlsl.length = call 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 float @
47-
// CHECK: call 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 float @
54-
// CHECK: %hlsl.length = call 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 float @
61-
// CHECK: %hlsl.length = call 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 float @
68-
// CHECK: %hlsl.length = call 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+
// RUN: %clang_cc1 -finclude-default-header -triple \
6+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
7+
// RUN: -emit-llvm -O1 -o - | FileCheck %s --check-prefix=SPVCHECK
8+
9+
10+
// CHECK-LABEL: define noundef half @_Z16test_length_halfDh(
11+
// CHECK-SAME: half noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
12+
// CHECK-NEXT: [[ENTRY:.*:]]
13+
// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call noundef half @llvm.fabs.f16(half [[P0]])
14+
// CHECK-NEXT: ret half [[ELT_ABS_I]]
15+
//
16+
// SPVCHECK-LABEL: define spir_func noundef half @_Z16test_length_halfDh(
17+
// SPVCHECK-SAME: half noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
18+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
19+
// SPVCHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call noundef half @llvm.fabs.f16(half [[P0]])
20+
// SPVCHECK-NEXT: ret half [[ELT_ABS_I]]
21+
//
22+
half test_length_half(half p0)
23+
{
24+
return length(p0);
25+
}
26+
27+
// CHECK-LABEL: define noundef half @_Z17test_length_half2Dv2_Dh(
28+
// CHECK-SAME: <2 x half> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
29+
// CHECK-NEXT: [[ENTRY:.*:]]
30+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul <2 x half> [[P0]], [[P0]]
31+
// CHECK-NEXT: [[RDX_FADD_I:%.*]] = tail call half @llvm.vector.reduce.fadd.v2f16(half 0xH0000, <2 x half> [[MUL_I]])
32+
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef half @llvm.sqrt.f16(half [[RDX_FADD_I]])
33+
// CHECK-NEXT: ret half [[TMP0]]
34+
//
35+
// SPVCHECK-LABEL: define spir_func noundef half @_Z17test_length_half2Dv2_Dh(
36+
// SPVCHECK-SAME: <2 x half> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
37+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
38+
// SPVCHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef half @llvm.spv.length.v2f16(<2 x half> [[P0]])
39+
// SPVCHECK-NEXT: ret half [[HLSL_LENGTH_I]]
40+
//
41+
half test_length_half2(half2 p0)
42+
{
43+
return length(p0);
44+
}
45+
46+
// CHECK-LABEL: define noundef half @_Z17test_length_half3Dv3_Dh(
47+
// CHECK-SAME: <3 x half> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
48+
// CHECK-NEXT: [[ENTRY:.*:]]
49+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul <3 x half> [[P0]], [[P0]]
50+
// CHECK-NEXT: [[RDX_FADD_I:%.*]] = tail call half @llvm.vector.reduce.fadd.v3f16(half 0xH0000, <3 x half> [[MUL_I]])
51+
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef half @llvm.sqrt.f16(half [[RDX_FADD_I]])
52+
// CHECK-NEXT: ret half [[TMP0]]
53+
//
54+
// SPVCHECK-LABEL: define spir_func noundef half @_Z17test_length_half3Dv3_Dh(
55+
// SPVCHECK-SAME: <3 x half> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
56+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
57+
// SPVCHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef half @llvm.spv.length.v3f16(<3 x half> [[P0]])
58+
// SPVCHECK-NEXT: ret half [[HLSL_LENGTH_I]]
59+
//
60+
half test_length_half3(half3 p0)
61+
{
62+
return length(p0);
63+
}
64+
65+
// CHECK-LABEL: define noundef half @_Z17test_length_half4Dv4_Dh(
66+
// CHECK-SAME: <4 x half> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
67+
// CHECK-NEXT: [[ENTRY:.*:]]
68+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul <4 x half> [[P0]], [[P0]]
69+
// CHECK-NEXT: [[RDX_FADD_I:%.*]] = tail call half @llvm.vector.reduce.fadd.v4f16(half 0xH0000, <4 x half> [[MUL_I]])
70+
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef half @llvm.sqrt.f16(half [[RDX_FADD_I]])
71+
// CHECK-NEXT: ret half [[TMP0]]
72+
//
73+
// SPVCHECK-LABEL: define spir_func noundef half @_Z17test_length_half4Dv4_Dh(
74+
// SPVCHECK-SAME: <4 x half> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
75+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
76+
// SPVCHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef half @llvm.spv.length.v4f16(<4 x half> [[P0]])
77+
// SPVCHECK-NEXT: ret half [[HLSL_LENGTH_I]]
78+
//
79+
half test_length_half4(half4 p0)
80+
{
81+
return length(p0);
82+
}
83+
84+
85+
// CHECK-LABEL: define noundef float @_Z17test_length_floatf(
86+
// CHECK-SAME: float noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
87+
// CHECK-NEXT: [[ENTRY:.*:]]
88+
// CHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call noundef float @llvm.fabs.f32(float [[P0]])
89+
// CHECK-NEXT: ret float [[ELT_ABS_I]]
90+
//
91+
// SPVCHECK-LABEL: define spir_func noundef float @_Z17test_length_floatf(
92+
// SPVCHECK-SAME: float noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
93+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
94+
// SPVCHECK-NEXT: [[ELT_ABS_I:%.*]] = tail call noundef float @llvm.fabs.f32(float [[P0]])
95+
// SPVCHECK-NEXT: ret float [[ELT_ABS_I]]
96+
//
97+
float test_length_float(float p0)
98+
{
99+
return length(p0);
100+
}
101+
102+
// CHECK-LABEL: define noundef float @_Z18test_length_float2Dv2_f(
103+
// CHECK-SAME: <2 x float> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
104+
// CHECK-NEXT: [[ENTRY:.*:]]
105+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul <2 x float> [[P0]], [[P0]]
106+
// CHECK-NEXT: [[RDX_FADD_I:%.*]] = tail call float @llvm.vector.reduce.fadd.v2f32(float 0.000000e+00, <2 x float> [[MUL_I]])
107+
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef float @llvm.sqrt.f32(float [[RDX_FADD_I]])
108+
// CHECK-NEXT: ret float [[TMP0]]
109+
//
110+
// SPVCHECK-LABEL: define spir_func noundef float @_Z18test_length_float2Dv2_f(
111+
// SPVCHECK-SAME: <2 x float> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
112+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
113+
// SPVCHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef float @llvm.spv.length.v2f32(<2 x float> [[P0]])
114+
// SPVCHECK-NEXT: ret float [[HLSL_LENGTH_I]]
115+
//
116+
float test_length_float2(float2 p0)
117+
{
118+
return length(p0);
119+
}
120+
121+
// CHECK-LABEL: define noundef float @_Z18test_length_float3Dv3_f(
122+
// CHECK-SAME: <3 x float> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
123+
// CHECK-NEXT: [[ENTRY:.*:]]
124+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul <3 x float> [[P0]], [[P0]]
125+
// CHECK-NEXT: [[RDX_FADD_I:%.*]] = tail call float @llvm.vector.reduce.fadd.v3f32(float 0.000000e+00, <3 x float> [[MUL_I]])
126+
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef float @llvm.sqrt.f32(float [[RDX_FADD_I]])
127+
// CHECK-NEXT: ret float [[TMP0]]
128+
//
129+
// SPVCHECK-LABEL: define spir_func noundef float @_Z18test_length_float3Dv3_f(
130+
// SPVCHECK-SAME: <3 x float> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
131+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
132+
// SPVCHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef float @llvm.spv.length.v3f32(<3 x float> [[P0]])
133+
// SPVCHECK-NEXT: ret float [[HLSL_LENGTH_I]]
134+
//
135+
float test_length_float3(float3 p0)
136+
{
137+
return length(p0);
138+
}
139+
140+
// CHECK-LABEL: define noundef float @_Z18test_length_float4Dv4_f(
141+
// CHECK-SAME: <4 x float> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
142+
// CHECK-NEXT: [[ENTRY:.*:]]
143+
// CHECK-NEXT: [[MUL_I:%.*]] = fmul <4 x float> [[P0]], [[P0]]
144+
// CHECK-NEXT: [[RDX_FADD_I:%.*]] = tail call float @llvm.vector.reduce.fadd.v4f32(float 0.000000e+00, <4 x float> [[MUL_I]])
145+
// CHECK-NEXT: [[TMP0:%.*]] = tail call noundef float @llvm.sqrt.f32(float [[RDX_FADD_I]])
146+
// CHECK-NEXT: [[ADD:%.*]] = fadd float [[TMP0]], [[TMP0]]
147+
// CHECK-NEXT: ret float [[ADD]]
148+
//
149+
// SPVCHECK-LABEL: define spir_func noundef float @_Z18test_length_float4Dv4_f(
150+
// SPVCHECK-SAME: <4 x float> noundef [[P0:%.*]]) local_unnamed_addr #[[ATTR0]] {
151+
// SPVCHECK-NEXT: [[ENTRY:.*:]]
152+
// SPVCHECK-NEXT: [[HLSL_LENGTH_I:%.*]] = tail call noundef float @llvm.spv.length.v4f32(<4 x float> [[P0]])
153+
// SPVCHECK-NEXT: [[ADD:%.*]] = fadd float [[HLSL_LENGTH_I]], [[HLSL_LENGTH_I]]
154+
// SPVCHECK-NEXT: ret float [[ADD]]
155+
//
156+
float test_length_float4(float4 p0)
157+
{
158+
return length(p0) + length(p0);
159+
}

0 commit comments

Comments
 (0)