Author: Nico Weber
Date: 2025-01-09T14:19:03-05:00
New Revision: 9ec92873ecc1c268a1d05e36b7b52e378860ea5b

URL: 
https://github.com/llvm/llvm-project/commit/9ec92873ecc1c268a1d05e36b7b52e378860ea5b
DIFF: 
https://github.com/llvm/llvm-project/commit/9ec92873ecc1c268a1d05e36b7b52e378860ea5b.diff

LOG: Revert "[HLSL] Move length support out of the DirectX Backend  (#121611)"

This reverts commit a6b7181733c83523a39d4f4e788c6b7a227d477d.
Breaks Clang :: CodeGenHLSL/builtins/length.hlsl, see
https://github.com/llvm/llvm-project/pull/121611#issuecomment-2581004278

Added: 
    llvm/test/CodeGen/DirectX/length.ll
    llvm/test/CodeGen/DirectX/length_error.ll
    llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error.ll
    llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error_scalar.ll

Modified: 
    clang/include/clang/Basic/Builtins.td
    clang/lib/CodeGen/CGBuiltin.cpp
    clang/lib/CodeGen/CGHLSLRuntime.h
    clang/lib/Headers/hlsl/hlsl_detail.h
    clang/lib/Headers/hlsl/hlsl_intrinsics.h
    clang/lib/Sema/SemaHLSL.cpp
    clang/test/CodeGenHLSL/builtins/length.hlsl
    clang/test/SemaHLSL/BuiltIns/length-errors.hlsl
    llvm/include/llvm/IR/IntrinsicsDirectX.td
    llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index f4216bd01a0743..468c16050e2bf0 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4865,6 +4865,12 @@ def HLSLIsinf : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLLength : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_length"];
+  let Attributes = [NoThrow, Const];
+  let Prototype = "void(...)";
+}
+
 def HLSLLerp : LangBuiltin<"HLSL_LANG"> {
   let Spellings = ["__builtin_hlsl_lerp"];
   let Attributes = [NoThrow, Const];

diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2b091976699961..ca03fb665d423d 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -19334,6 +19334,20 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned 
BuiltinID,
         /*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
         ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
   }
+  case Builtin::BI__builtin_hlsl_length: {
+    Value *X = EmitScalarExpr(E->getArg(0));
+
+    assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
+           "length operand must have a float representation");
+    // if the operand is a scalar, we can use the fabs llvm intrinsic directly
+    if (!E->getArg(0)->getType()->isVectorType())
+      return EmitFAbs(*this, X);
+
+    return Builder.CreateIntrinsic(
+        /*ReturnType=*/X->getType()->getScalarType(),
+        CGM.getHLSLRuntime().getLengthIntrinsic(), ArrayRef<Value *>{X},
+        nullptr, "hlsl.length");
+  }
   case Builtin::BI__builtin_hlsl_normalize: {
     Value *X = EmitScalarExpr(E->getArg(0));
 

diff  --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index 00e110e8e6fa27..46e472f0aae213 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -77,6 +77,7 @@ class CGHLSLRuntime {
   GENERATE_HLSL_INTRINSIC_FUNCTION(Cross, cross)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Degrees, degrees)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
+  GENERATE_HLSL_INTRINSIC_FUNCTION(Length, length)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)

diff  --git a/clang/lib/Headers/hlsl/hlsl_detail.h 
b/clang/lib/Headers/hlsl/hlsl_detail.h
index 392075d276b188..8d5fd941331531 100644
--- a/clang/lib/Headers/hlsl/hlsl_detail.h
+++ b/clang/lib/Headers/hlsl/hlsl_detail.h
@@ -13,14 +13,6 @@ namespace hlsl {
 
 namespace __detail {
 
-template <typename T, typename U> struct is_same {
-  static const bool value = false;
-};
-
-template <typename T> struct is_same<T, T> {
-  static const bool value = true;
-};
-
 template <bool B, typename T> struct enable_if {};
 
 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) {
   return __builtin_bit_cast(U, F);
 }
 
-template <typename T>
-constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
-length_impl(T X) {
-  return __builtin_elementwise_abs(X);
-}
-
-template <typename T, int N>
-enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
-length_vec_impl(vector<T, N> X) {
-  vector<T, N> XSquared = X * X;
-  T XSquaredSum = XSquared[0];
-  [unroll] for (int i = 1; i < N; ++i) XSquaredSum += XSquared[i];
-  return __builtin_elementwise_sqrt(XSquaredSum);
-}
-
 } // namespace __detail
 } // namespace hlsl
 #endif //_HLSL_HLSL_DETAILS_H_

diff  --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index cf287e598f76ba..b745997f1d5a2b 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -1297,16 +1297,27 @@ float4 lerp(float4, float4, float4);
 ///
 /// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + ...).
 
-const inline half length(half X) { return __detail::length_impl(X); }
-const inline float length(float X) { return __detail::length_impl(X); }
-
-template <int N> const inline half length(vector<half, N> X) {
-  return __detail::length_vec_impl(X);
-}
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+half length(half);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+half length(half2);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+half length(half3);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+half length(half4);
 
-template <int N> const inline float length(vector<float, N> X) {
-  return __detail::length_vec_impl(X);
-}
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+float length(float);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+float length(float2);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+float length(float3);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
+float length(float4);
 
 
//===----------------------------------------------------------------------===//
 // log builtins

diff  --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 65ddee05a21512..83c38fad2df686 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2112,6 +2112,24 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned 
BuiltinID, CallExpr *TheCall) {
       return true;
     break;
   }
+  case Builtin::BI__builtin_hlsl_length: {
+    if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
+      return true;
+    if (SemaRef.checkArgCount(TheCall, 1))
+      return true;
+
+    ExprResult A = TheCall->getArg(0);
+    QualType ArgTyA = A.get()->getType();
+    QualType RetTy;
+
+    if (auto *VTy = ArgTyA->getAs<VectorType>())
+      RetTy = VTy->getElementType();
+    else
+      RetTy = TheCall->getArg(0)->getType();
+
+    TheCall->setType(RetTy);
+    break;
+  }
   case Builtin::BI__builtin_hlsl_mad: {
     if (SemaRef.checkArgCount(TheCall, 3))
       return true;

diff  --git a/clang/test/CodeGenHLSL/builtins/length.hlsl 
b/clang/test/CodeGenHLSL/builtins/length.hlsl
index ecf2edcf0b05f1..a24f01d275440a 100644
--- a/clang/test/CodeGenHLSL/builtins/length.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/length.hlsl
@@ -1,130 +1,73 @@
-// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 5
-// RUN: %clang_cc1 -finclude-default-header -triple \
-// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
-// RUN: -emit-llvm -O1 -o - | FileCheck %s
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) half @_Z16test_length_halfDh(
-// CHECK-SAME: half noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr 
#[[ATTR0:[0-9]+]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef half @llvm.fabs.f16(half [[P0]])
-// CHECK-NEXT:    ret half [[ELT_ABS_I]]
-//
-half test_length_half(half p0)
-{
-  return length(p0);
-}
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) half 
@_Z17test_length_half2Dv2_Dh(
-// CHECK-SAME: <2 x half> noundef nofpclass(nan inf) [[P0:%.*]]) 
local_unnamed_addr #[[ATTR0]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <2 x 
half> [[P0]], [[P0]]
-// CHECK-NEXT:    [[VECEXT_I:%.*]] = extractelement <2 x half> [[MUL_I]], i64 0
-// CHECK-NEXT:    [[VECEXT1_I:%.*]] = extractelement <2 x half> [[MUL_I]], i64 
1
-// CHECK-NEXT:    [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn half 
[[VECEXT_I]], [[VECEXT1_I]]
-// CHECK-NEXT:    [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef half @llvm.sqrt.f16(half [[ADD_I]])
-// CHECK-NEXT:    ret half [[TMP0]]
-//
-half test_length_half2(half2 p0)
-{
-  return length(p0);
-}
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) half 
@_Z17test_length_half3Dv3_Dh(
-// CHECK-SAME: <3 x half> noundef nofpclass(nan inf) [[P0:%.*]]) 
local_unnamed_addr #[[ATTR0]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <3 x 
half> [[P0]], [[P0]]
-// CHECK-NEXT:    [[VECEXT_I:%.*]] = extractelement <3 x half> [[MUL_I]], i64 0
-// CHECK-NEXT:    [[VECEXT1_I:%.*]] = extractelement <3 x half> [[MUL_I]], i64 
1
-// CHECK-NEXT:    [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn half 
[[VECEXT1_I]], [[VECEXT_I]]
-// CHECK-NEXT:    [[VECEXT1_I_1:%.*]] = extractelement <3 x half> [[MUL_I]], 
i64 2
-// CHECK-NEXT:    [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn half 
[[VECEXT1_I_1]], [[ADD_I]]
-// CHECK-NEXT:    [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef half @llvm.sqrt.f16(half [[ADD_I_1]])
-// CHECK-NEXT:    ret half [[TMP0]]
-//
-half test_length_half3(half3 p0)
-{
-  return length(p0);
-}
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) half 
@_Z17test_length_half4Dv4_Dh(
-// CHECK-SAME: <4 x half> noundef nofpclass(nan inf) [[P0:%.*]]) 
local_unnamed_addr #[[ATTR0]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <4 x 
half> [[P0]], [[P0]]
-// CHECK-NEXT:    [[VECEXT_I:%.*]] = extractelement <4 x half> [[MUL_I]], i64 0
-// CHECK-NEXT:    [[VECEXT1_I:%.*]] = extractelement <4 x half> [[MUL_I]], i64 
1
-// CHECK-NEXT:    [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn half 
[[VECEXT1_I]], [[VECEXT_I]]
-// CHECK-NEXT:    [[VECEXT1_I_1:%.*]] = extractelement <4 x half> [[MUL_I]], 
i64 2
-// CHECK-NEXT:    [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn half 
[[VECEXT1_I_1]], [[ADD_I]]
-// CHECK-NEXT:    [[VECEXT1_I_2:%.*]] = extractelement <4 x half> [[MUL_I]], 
i64 3
-// CHECK-NEXT:    [[ADD_I_2:%.*]] = fadd reassoc nnan ninf nsz arcp afn half 
[[VECEXT1_I_2]], [[ADD_I_1]]
-// CHECK-NEXT:    [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef half @llvm.sqrt.f16(half [[ADD_I_2]])
-// CHECK-NEXT:    ret half [[TMP0]]
-//
-half test_length_half4(half4 p0)
-{
-  return length(p0);
-}
-
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) float 
@_Z17test_length_floatf(
-// CHECK-SAME: float noundef nofpclass(nan inf) [[P0:%.*]]) local_unnamed_addr 
#[[ATTR0]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[ELT_ABS_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef float @llvm.fabs.f32(float [[P0]])
-// CHECK-NEXT:    ret float [[ELT_ABS_I]]
-//
-float test_length_float(float p0)
-{
-  return length(p0);
-}
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) float 
@_Z18test_length_float2Dv2_f(
-// CHECK-SAME: <2 x float> noundef nofpclass(nan inf) [[P0:%.*]]) 
local_unnamed_addr #[[ATTR0]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <2 x 
float> [[P0]], [[P0]]
-// CHECK-NEXT:    [[VECEXT_I:%.*]] = extractelement <2 x float> [[MUL_I]], i64 0
-// CHECK-NEXT:    [[VECEXT1_I:%.*]] = extractelement <2 x float> [[MUL_I]], 
i64 1
-// CHECK-NEXT:    [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn float 
[[VECEXT_I]], [[VECEXT1_I]]
-// CHECK-NEXT:    [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef float @llvm.sqrt.f32(float [[ADD_I]])
-// CHECK-NEXT:    ret float [[TMP0]]
-//
-float test_length_float2(float2 p0)
-{
-  return length(p0);
-}
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) float 
@_Z18test_length_float3Dv3_f(
-// CHECK-SAME: <3 x float> noundef nofpclass(nan inf) [[P0:%.*]]) 
local_unnamed_addr #[[ATTR0]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <3 x 
float> [[P0]], [[P0]]
-// CHECK-NEXT:    [[VECEXT_I:%.*]] = extractelement <3 x float> [[MUL_I]], i64 0
-// CHECK-NEXT:    [[VECEXT1_I:%.*]] = extractelement <3 x float> [[MUL_I]], 
i64 1
-// CHECK-NEXT:    [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn float 
[[VECEXT1_I]], [[VECEXT_I]]
-// CHECK-NEXT:    [[VECEXT1_I_1:%.*]] = extractelement <3 x float> [[MUL_I]], 
i64 2
-// CHECK-NEXT:    [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn float 
[[VECEXT1_I_1]], [[ADD_I]]
-// CHECK-NEXT:    [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef float @llvm.sqrt.f32(float [[ADD_I_1]])
-// CHECK-NEXT:    ret float [[TMP0]]
-//
-float test_length_float3(float3 p0)
-{
-  return length(p0);
-}
-
-
-// CHECK-LABEL: define noundef nofpclass(nan inf) float 
@_Z18test_length_float4Dv4_f(
-// CHECK-SAME: <4 x float> noundef nofpclass(nan inf) [[P0:%.*]]) 
local_unnamed_addr #[[ATTR0]] {
-// CHECK-NEXT:  [[ENTRY:.*:]]
-// CHECK-NEXT:    [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn <4 x 
float> [[P0]], [[P0]]
-// CHECK-NEXT:    [[VECEXT_I:%.*]] = extractelement <4 x float> [[MUL_I]], i64 0
-// CHECK-NEXT:    [[VECEXT1_I:%.*]] = extractelement <4 x float> [[MUL_I]], 
i64 1
-// CHECK-NEXT:    [[ADD_I:%.*]] = fadd reassoc nnan ninf nsz arcp afn float 
[[VECEXT1_I]], [[VECEXT_I]]
-// CHECK-NEXT:    [[VECEXT1_I_1:%.*]] = extractelement <4 x float> [[MUL_I]], 
i64 2
-// CHECK-NEXT:    [[ADD_I_1:%.*]] = fadd reassoc nnan ninf nsz arcp afn float 
[[VECEXT1_I_1]], [[ADD_I]]
-// CHECK-NEXT:    [[VECEXT1_I_2:%.*]] = extractelement <4 x float> [[MUL_I]], 
i64 3
-// CHECK-NEXT:    [[ADD_I_2:%.*]] = fadd reassoc nnan ninf nsz arcp afn float 
[[VECEXT1_I_2]], [[ADD_I_1]]
-// CHECK-NEXT:    [[TMP0:%.*]] = tail call reassoc nnan ninf nsz arcp afn 
noundef float @llvm.sqrt.f32(float [[ADD_I_2]])
-// CHECK-NEXT:    ret float [[TMP0]]
-//
-float test_length_float4(float4 p0)
-{
-  return length(p0);
-}
+// RUN: %clang_cc1 -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:   --check-prefixes=CHECK,NATIVE_HALF
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
+// RUN:   -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
+
+// NATIVE_HALF: define noundef nofpclass(nan inf) half @
+// NATIVE_HALF: call reassoc nnan ninf nsz arcp afn half @llvm.fabs.f16(half
+// NO_HALF: call reassoc nnan ninf nsz arcp afn float @llvm.fabs.f32(float
+// NATIVE_HALF: ret half
+// NO_HALF: ret float
+half test_length_half(half p0)
+{
+  return length(p0);
+}
+// NATIVE_HALF: define noundef nofpclass(nan inf) half @
+// NATIVE_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn half 
@llvm.dx.length.v2f16
+// NO_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn float 
@llvm.dx.length.v2f32(
+// NATIVE_HALF: ret half %hlsl.length
+// NO_HALF: ret float %hlsl.length
+half test_length_half2(half2 p0)
+{
+  return length(p0);
+}
+// NATIVE_HALF: define noundef nofpclass(nan inf) half @
+// NATIVE_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn half 
@llvm.dx.length.v3f16
+// NO_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn float 
@llvm.dx.length.v3f32(
+// NATIVE_HALF: ret half %hlsl.length
+// NO_HALF: ret float %hlsl.length
+half test_length_half3(half3 p0)
+{
+  return length(p0);
+}
+// NATIVE_HALF: define noundef nofpclass(nan inf) half @
+// NATIVE_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn half 
@llvm.dx.length.v4f16
+// NO_HALF: %hlsl.length = call reassoc nnan ninf nsz arcp afn float 
@llvm.dx.length.v4f32(
+// NATIVE_HALF: ret half %hlsl.length
+// NO_HALF: ret float %hlsl.length
+half test_length_half4(half4 p0)
+{
+  return length(p0);
+}
+
+// CHECK: define noundef nofpclass(nan inf) float @
+// CHECK: call reassoc nnan ninf nsz arcp afn float @llvm.fabs.f32(float
+// CHECK: ret float
+float test_length_float(float p0)
+{
+  return length(p0);
+}
+// CHECK: define noundef nofpclass(nan inf) float @
+// CHECK: %hlsl.length = call reassoc nnan ninf nsz arcp afn float 
@llvm.dx.length.v2f32(
+// CHECK: ret float %hlsl.length
+float test_length_float2(float2 p0)
+{
+  return length(p0);
+}
+// CHECK: define noundef nofpclass(nan inf) float @
+// CHECK: %hlsl.length = call reassoc nnan ninf nsz arcp afn float 
@llvm.dx.length.v3f32(
+// CHECK: ret float %hlsl.length
+float test_length_float3(float3 p0)
+{
+  return length(p0);
+}
+// CHECK: define noundef nofpclass(nan inf) float @
+// CHECK: %hlsl.length = call reassoc nnan ninf nsz arcp afn float 
@llvm.dx.length.v4f32(
+// CHECK: ret float %hlsl.length
+float test_length_float4(float4 p0)
+{
+  return length(p0);
+}

diff  --git a/clang/test/SemaHLSL/BuiltIns/length-errors.hlsl 
b/clang/test/SemaHLSL/BuiltIns/length-errors.hlsl
index a191f0419fbbaf..281faada6f5e94 100644
--- a/clang/test/SemaHLSL/BuiltIns/length-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/length-errors.hlsl
@@ -1,53 +1,32 @@
-// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only 
-disable-llvm-passes -verify
+// RUN: %clang_cc1 -finclude-default-header -triple 
dxil-pc-shadermodel6.6-library %s -fnative-half-type -disable-llvm-passes 
-verify -verify-ignore-unexpected
+
 
 void test_too_few_arg()
 {
-  return length();
-  // expected-error@-1 {{no matching function for call to 'length'}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: 
requires single argument 'X', but no arguments were provided}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: 
requires single argument 'X', but no arguments were provided}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'X', but no arguments were provided}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'X', but no arguments were provided}}
+  return __builtin_hlsl_length();
+  // expected-error@-1 {{too few arguments to function call, expected 1, have 
0}}
 }
 
 void test_too_many_arg(float2 p0)
 {
-  return length(p0, p0);
-  // expected-error@-1 {{no matching function for call to 'length'}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: 
requires single argument 'X', but 2 arguments were provided}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'X', but 2 arguments were provided}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: 
requires single argument 'X', but 2 arguments were provided}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not 
viable: requires single argument 'X', but 2 arguments were provided}}
-}
-
-float double_to_float_type(double p0) {
-  return length(p0);
-  // expected-error@-1  {{call to 'length' is ambiguous}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
+  return __builtin_hlsl_length(p0, p0);
+  // expected-error@-1 {{too many arguments to function call, expected 1, have 
2}}
 }
 
-
-float bool_to_float_type_promotion(bool p1)
+bool builtin_bool_to_float_type_promotion(bool p1)
 {
-  return length(p1);
-  // expected-error@-1  {{call to 'length' is ambiguous}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
+  return __builtin_hlsl_length(p1);
+  // expected-error@-1 {passing 'bool' to parameter of incompatible type 
'float'}}
 }
 
-float length_int_to_float_promotion(int p1)
+bool builtin_length_int_to_float_promotion(int p1)
 {
-  return length(p1);
-  // expected-error@-1  {{call to 'length' is ambiguous}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
+  return __builtin_hlsl_length(p1);
+  // expected-error@-1 {{passing 'int' to parameter of incompatible type 
'float'}}
 }
 
-float2 length_int2_to_float2_promotion(int2 p1)
+bool2 builtin_length_int2_to_float2_promotion(int2 p1)
 {
-  return length(p1);
-  // expected-error@-1  {{call to 'length' is ambiguous}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
-  // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function}}
+  return __builtin_hlsl_length(p1);
+  // expected-error@-1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of 
incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' 
(vector of 2 'float' values)}}
 }

diff  --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td 
b/llvm/include/llvm/IR/IntrinsicsDirectX.td
index ef48af5b42dbf1..3b1d1a88e01a8b 100644
--- a/llvm/include/llvm/IR/IntrinsicsDirectX.td
+++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td
@@ -93,6 +93,7 @@ def int_dx_isinf : 
DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i1
 def int_dx_lerp : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, 
LLVMMatchType<0>,LLVMMatchType<0>],
     [IntrNoMem]>;
 
+def int_dx_length : DefaultAttrsIntrinsic<[LLVMVectorElementType<0>], 
[llvm_anyfloat_ty], [IntrNoMem]>;
 def int_dx_imad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, 
LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
 def int_dx_umad : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, 
LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
 def int_dx_normalize : DefaultAttrsIntrinsic<[LLVMMatchType<0>], 
[llvm_anyfloat_ty], [IntrNoMem]>;

diff  --git a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp 
b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
index cf142806bb1df6..51dc3025f0c379 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -57,6 +57,7 @@ static bool isIntrinsicExpansion(Function &F) {
   case Intrinsic::dx_nclamp:
   case Intrinsic::dx_degrees:
   case Intrinsic::dx_lerp:
+  case Intrinsic::dx_length:
   case Intrinsic::dx_normalize:
   case Intrinsic::dx_fdot:
   case Intrinsic::dx_sdot:
@@ -291,6 +292,32 @@ static Value *expandAnyOrAllIntrinsic(CallInst *Orig,
   return Result;
 }
 
+static Value *expandLengthIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  IRBuilder<> Builder(Orig);
+  Type *Ty = X->getType();
+  Type *EltTy = Ty->getScalarType();
+
+  // Though dx.length does work on scalar type, we can optimize it to just emit
+  // fabs, in CGBuiltin.cpp. We shouldn't see a scalar type here because
+  // CGBuiltin.cpp should have emitted a fabs call.
+  Value *Elt = Builder.CreateExtractElement(X, (uint64_t)0);
+  auto *XVec = dyn_cast<FixedVectorType>(Ty);
+  unsigned XVecSize = XVec->getNumElements();
+  if (!(Ty->isVectorTy() && XVecSize > 1))
+    report_fatal_error(Twine("Invalid input type for length intrinsic"),
+                       /* gen_crash_diag=*/false);
+
+  Value *Sum = Builder.CreateFMul(Elt, Elt);
+  for (unsigned I = 1; I < XVecSize; I++) {
+    Elt = Builder.CreateExtractElement(X, I);
+    Value *Mul = Builder.CreateFMul(Elt, Elt);
+    Sum = Builder.CreateFAdd(Sum, Mul);
+  }
+  return Builder.CreateIntrinsic(EltTy, Intrinsic::sqrt, ArrayRef<Value 
*>{Sum},
+                                 nullptr, "elt.sqrt");
+}
+
 static Value *expandLerpIntrinsic(CallInst *Orig) {
   Value *X = Orig->getOperand(0);
   Value *Y = Orig->getOperand(1);
@@ -562,6 +589,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
   case Intrinsic::dx_lerp:
     Result = expandLerpIntrinsic(Orig);
     break;
+  case Intrinsic::dx_length:
+    Result = expandLengthIntrinsic(Orig);
+    break;
   case Intrinsic::dx_normalize:
     Result = expandNormalizeIntrinsic(Orig);
     break;

diff  --git a/llvm/test/CodeGen/DirectX/length.ll 
b/llvm/test/CodeGen/DirectX/length.ll
new file mode 100644
index 00000000000000..fc5868a7f6e82c
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/length.ll
@@ -0,0 +1,116 @@
+; RUN: opt -S  -dxil-intrinsic-expansion  < %s | FileCheck %s 
--check-prefixes=CHECK,EXPCHECK
+; RUN: opt -S  -dxil-intrinsic-expansion -dxil-op-lower 
-mtriple=dxil-pc-shadermodel6.3-library < %s | FileCheck %s 
--check-prefixes=CHECK,DOPCHECK
+
+; Make sure dxil operation function calls for length are generated for 
half/float.
+
+declare half @llvm.fabs.f16(half)
+declare half @llvm.dx.length.v2f16(<2 x half>)
+declare half @llvm.dx.length.v3f16(<3 x half>)
+declare half @llvm.dx.length.v4f16(<4 x half>)
+
+declare float @llvm.fabs.f32(float)
+declare float @llvm.dx.length.v2f32(<2 x float>)
+declare float @llvm.dx.length.v3f32(<3 x float>)
+declare float @llvm.dx.length.v4f32(<4 x float>)
+
+define noundef half @test_length_half2(<2 x half> noundef %p0) {
+entry:
+  ; CHECK: extractelement <2 x half> %{{.*}}, i64 0
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <2 x half> %{{.*}}, i64 1
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: fadd half %{{.*}}, %{{.*}}
+  ; EXPCHECK: call half @llvm.sqrt.f16(half %{{.*}})
+  ; DOPCHECK: call half @dx.op.unary.f16(i32 24, half %{{.*}})
+
+  %hlsl.length = call half @llvm.dx.length.v2f16(<2 x half> %p0)
+  ret half %hlsl.length
+}
+
+define noundef half @test_length_half3(<3 x half> noundef %p0) {
+entry:
+  ; CHECK: extractelement <3 x half> %{{.*}}, i64 0
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <3 x half> %{{.*}}, i64 1
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: fadd half %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <3 x half> %{{.*}}, i64 2
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: fadd half %{{.*}}, %{{.*}}
+  ; EXPCHECK: call half @llvm.sqrt.f16(half %{{.*}})
+  ; DOPCHECK: call half @dx.op.unary.f16(i32 24, half %{{.*}})
+
+  %hlsl.length = call half @llvm.dx.length.v3f16(<3 x half> %p0)
+  ret half %hlsl.length
+}
+
+define noundef half @test_length_half4(<4 x half> noundef %p0) {
+entry:
+  ; CHECK: extractelement <4 x half> %{{.*}}, i64 0
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <4 x half> %{{.*}}, i64 1
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: fadd half %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <4 x half> %{{.*}}, i64 2
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: fadd half %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <4 x half> %{{.*}}, i64 3
+  ; CHECK: fmul half %{{.*}}, %{{.*}}
+  ; CHECK: fadd half %{{.*}}, %{{.*}}
+  ; EXPCHECK: call half @llvm.sqrt.f16(half %{{.*}})
+  ; DOPCHECK:  call half @dx.op.unary.f16(i32 24, half %{{.*}})
+
+  %hlsl.length = call half @llvm.dx.length.v4f16(<4 x half> %p0)
+  ret half %hlsl.length
+}
+
+define noundef float @test_length_float2(<2 x float> noundef %p0) {
+entry:
+  ; CHECK: extractelement <2 x float> %{{.*}}, i64 0
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <2 x float> %{{.*}}, i64 1
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: fadd float %{{.*}}, %{{.*}}
+  ; EXPCHECK: call float @llvm.sqrt.f32(float %{{.*}})
+  ; DOPCHECK: call float @dx.op.unary.f32(i32 24, float %{{.*}})
+
+  %hlsl.length = call float @llvm.dx.length.v2f32(<2 x float> %p0)
+  ret float %hlsl.length
+}
+
+define noundef float @test_length_float3(<3 x float> noundef %p0) {
+entry:
+  ; CHECK: extractelement <3 x float> %{{.*}}, i64 0
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <3 x float> %{{.*}}, i64 1
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: fadd float %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <3 x float> %{{.*}}, i64 2
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: fadd float %{{.*}}, %{{.*}}
+  ; EXPCHECK: call float @llvm.sqrt.f32(float %{{.*}})
+  ; DOPCHECK: call float @dx.op.unary.f32(i32 24, float %{{.*}})
+
+  %hlsl.length = call float @llvm.dx.length.v3f32(<3 x float> %p0)
+  ret float %hlsl.length
+}
+
+define noundef float @test_length_float4(<4 x float> noundef %p0) {
+entry:
+  ; CHECK: extractelement <4 x float> %{{.*}}, i64 0
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <4 x float> %{{.*}}, i64 1
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: fadd float %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <4 x float> %{{.*}}, i64 2
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: fadd float %{{.*}}, %{{.*}}
+  ; CHECK: extractelement <4 x float> %{{.*}}, i64 3
+  ; CHECK: fmul float %{{.*}}, %{{.*}}
+  ; CHECK: fadd float %{{.*}}, %{{.*}}
+  ; EXPCHECK: call float @llvm.sqrt.f32(float %{{.*}})
+  ; DOPCHECK:  call float @dx.op.unary.f32(i32 24, float %{{.*}})
+
+  %hlsl.length = call float @llvm.dx.length.v4f32(<4 x float> %p0)
+  ret float %hlsl.length
+}

diff  --git a/llvm/test/CodeGen/DirectX/length_error.ll 
b/llvm/test/CodeGen/DirectX/length_error.ll
new file mode 100644
index 00000000000000..143b41fc506e1d
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/length_error.ll
@@ -0,0 +1,10 @@
+; RUN: not opt -S -dxil-intrinsic-expansion -dxil-op-lower 
-mtriple=dxil-pc-shadermodel6.3-library %s 2>&1 | FileCheck %s
+
+; DXIL operation length does not support double overload type
+; CHECK: Cannot create Sqrt operation: Invalid overload type
+
+define noundef double @test_length_double2(<2 x double> noundef %p0) {
+entry:
+  %hlsl.length = call double @llvm.dx.length.v2f32(<2 x double> %p0)
+  ret double %hlsl.length
+}

diff  --git a/llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error.ll 
b/llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error.ll
new file mode 100644
index 00000000000000..f722de2f9029e7
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error.ll
@@ -0,0 +1,10 @@
+; RUN: not opt -S -dxil-intrinsic-expansion -dxil-op-lower 
-mtriple=dxil-pc-shadermodel6.3-library %s 2>&1 | FileCheck %s
+
+; DXIL operation length does not support 1-element vector types.
+; CHECK: LLVM ERROR: Invalid input type for length intrinsic
+
+define noundef float @test_length_float(<1 x float> noundef %p0) {
+entry:
+  %hlsl.length = call float @llvm.dx.length.v1f32(<1 x float> %p0)
+  ret float %hlsl.length
+}

diff  --git 
a/llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error_scalar.ll 
b/llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error_scalar.ll
new file mode 100644
index 00000000000000..ac3a0513eb6b27
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/length_invalid_intrinsic_error_scalar.ll
@@ -0,0 +1,10 @@
+; RUN: not opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s 
2>&1 | FileCheck %s
+
+; DXIL operation length does not support scalar types
+; CHECK: error: invalid intrinsic signature
+
+define noundef float @test_length_float(float noundef %p0) {
+entry:
+  %hlsl.length = call float @llvm.dx.length.f32(float %p0)
+  ret float %hlsl.length
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to