[clang] [llvm] [HLSL] Move `normalize` implementation to header files (PR #216228)

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 13 18:57:42 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Kaitlin Peng (kmpeng)

<details>
<summary>Changes</summary>

Closes #<!-- -->128043.

This PR replaces the previous implementation of `normalize` with a new one inside the header files. It also adds the `__builtin_spirv_normalize` SPIR-V builtin (+ relevant tests) to use as a fast path in the header implementation so SPIR-V can still emit the GL/CL extension instructions.

Assisted-by: Claude Opus 4.8

---

Patch is 62.69 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/216228.diff


21 Files Affected:

- (modified) clang/include/clang/Basic/Builtins.td (-6) 
- (modified) clang/include/clang/Basic/BuiltinsSPIRVCommon.td (+1) 
- (modified) clang/include/clang/Basic/HLSLIntrinsics.td (+2-1) 
- (modified) clang/lib/CodeGen/CGHLSLBuiltins.cpp (-11) 
- (modified) clang/lib/CodeGen/CGHLSLRuntime.h (-1) 
- (modified) clang/lib/CodeGen/TargetBuiltins/SPIR.cpp (+8) 
- (modified) clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h (+8) 
- (modified) clang/lib/Sema/SemaHLSL.cpp (-12) 
- (modified) clang/lib/Sema/SemaSPIRV.cpp (+17) 
- (removed) clang/test/CodeGenHLSL/builtins/normalize-builtin.hlsl (-16) 
- (modified) clang/test/CodeGenHLSL/builtins/normalize-overloads.hlsl (+189-89) 
- (modified) clang/test/CodeGenHLSL/builtins/normalize.hlsl (+68-47) 
- (added) clang/test/CodeGenSPIRV/Builtins/normalize.c (+41) 
- (removed) clang/test/SemaHLSL/BuiltIns/normalize-errors.hlsl (-31) 
- (added) clang/test/SemaSPIRV/BuiltIns/normalize-errors.c (+23) 
- (modified) llvm/include/llvm/IR/IntrinsicsDirectX.td (-1) 
- (modified) llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp (-41) 
- (removed) llvm/test/CodeGen/DirectX/normalize.ll (-112) 
- (removed) llvm/test/CodeGen/DirectX/normalize_error.ll (-10) 
- (modified) llvm/test/CodeGen/SPIRV/hlsl-intrinsics/normalize.ll (+20) 
- (added) llvm/test/CodeGen/SPIRV/opencl/normalize.ll (+52) 


``````````diff
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 88aa8aee01e4b..9f7aa9bdb9571 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -5713,12 +5713,6 @@ def HLSLMul : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
-def HLSLNormalize : LangBuiltin<"HLSL_LANG"> {
-  let Spellings = ["__builtin_hlsl_normalize"];
-  let Attributes = [NoThrow, Const, CustomTypeChecking];
-  let Prototype = "void(...)";
-}
-
 def HLSLTranspose : LangBuiltin<"HLSL_LANG"> {
   let Spellings = ["__builtin_hlsl_transpose"];
   let Attributes = [NoThrow, Const, CustomTypeChecking];
diff --git a/clang/include/clang/Basic/BuiltinsSPIRVCommon.td b/clang/include/clang/Basic/BuiltinsSPIRVCommon.td
index 448223a176ab4..91ad12599971f 100644
--- a/clang/include/clang/Basic/BuiltinsSPIRVCommon.td
+++ b/clang/include/clang/Basic/BuiltinsSPIRVCommon.td
@@ -20,6 +20,7 @@ def subgroup_local_invocation_id : SPIRVBuiltin<"uint32_t()", [NoThrow, Const]>;
 
 def distance : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
 def length : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
+def normalize : SPIRVBuiltin<"void(...)", [NoThrow, Const, CustomTypeChecking]>;
 def smoothstep : SPIRVBuiltin<"void(...)", [NoThrow, Const, CustomTypeChecking]>;
 
 def group_barrier : SPIRVBuiltin<"void()", [NoThrow]>;
diff --git a/clang/include/clang/Basic/HLSLIntrinsics.td b/clang/include/clang/Basic/HLSLIntrinsics.td
index fa5cb896e795a..91a542cf269e8 100644
--- a/clang/include/clang/Basic/HLSLIntrinsics.td
+++ b/clang/include/clang/Basic/HLSLIntrinsics.td
@@ -1284,7 +1284,7 @@ The return value is the \a I parameter.
 }
 
 // Returns the normalized unit vector of the specified floating-point vector.
-def hlsl_normalize : HLSLOneArgBuiltin<"normalize", "__builtin_hlsl_normalize"> {
+def hlsl_normalize : HLSLOneArgDetail<"normalize", "normalize_impl"> {
   let Doc = [{
 \fn T normalize(T x)
 \brief Returns the normalized unit vector of the specified floating-point
@@ -1293,6 +1293,7 @@ vector.
 
 Normalize is based on the following formula: x / length(x).
 }];
+  let ParamNames = ["x"];
   let VaryingTypes = [HalfTy, FloatTy];
   let VaryingMatDims = [];
 }
diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
index 83c0bb2ac684b..38c2b58cea0c6 100644
--- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp
+++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp
@@ -1137,17 +1137,6 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
         CGM.getHLSLRuntime().getFirstBitLowIntrinsic(), ArrayRef<Value *>{X},
         nullptr, "hlsl.firstbitlow");
   }
-  case Builtin::BI__builtin_hlsl_normalize: {
-    Value *X = EmitScalarExpr(E->getArg(0));
-
-    assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
-           "normalize operand must have a float representation");
-
-    return Builder.CreateIntrinsic(
-        /*ReturnType=*/X->getType(),
-        CGM.getHLSLRuntime().getNormalizeIntrinsic(), ArrayRef<Value *>{X},
-        nullptr, "hlsl.normalize");
-  }
   case Builtin::BI__builtin_hlsl_elementwise_f16tof32: {
     return handleElementwiseF16ToF32(*this, E);
   }
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index 0b282f19cfcbc..a4ac00893c532 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -128,7 +128,6 @@ class CGHLSLRuntime {
                                    flattened_thread_id_in_group)
   GENERATE_HLSL_INTRINSIC_FUNCTION(IsInf, isinf)
   GENERATE_HLSL_INTRINSIC_FUNCTION(IsNaN, isnan)
-  GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
diff --git a/clang/lib/CodeGen/TargetBuiltins/SPIR.cpp b/clang/lib/CodeGen/TargetBuiltins/SPIR.cpp
index b2732e2ae674e..66f22d28e2a13 100644
--- a/clang/lib/CodeGen/TargetBuiltins/SPIR.cpp
+++ b/clang/lib/CodeGen/TargetBuiltins/SPIR.cpp
@@ -45,6 +45,14 @@ Value *CodeGenFunction::EmitSPIRVBuiltinExpr(unsigned BuiltinID,
         /*ReturnType=*/X->getType()->getScalarType(), Intrinsic::spv_length,
         ArrayRef<Value *>{X}, nullptr, "spv.length");
   }
+  case SPIRV::BI__builtin_spirv_normalize: {
+    Value *X = EmitScalarExpr(E->getArg(0));
+    assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
+           "normalize operand must have a float representation");
+    return Builder.CreateIntrinsic(
+        /*ReturnType=*/X->getType(), Intrinsic::spv_normalize,
+        ArrayRef<Value *>{X}, nullptr, "spv.normalize");
+  }
   case SPIRV::BI__builtin_spirv_reflect: {
     Value *I = EmitScalarExpr(E->getArg(0));
     Value *N = EmitScalarExpr(E->getArg(1));
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
index 977059a9fdae0..6d941231971c8 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
@@ -28,6 +28,14 @@ length_impl(vector<T, N> X) {
 #endif
 }
 
+template <typename T> constexpr T normalize_impl(T X) {
+#if (__has_builtin(__builtin_spirv_normalize))
+  return __builtin_spirv_normalize(X);
+#else
+  return X * rsqrt(dot(X, X));
+#endif
+}
+
 constexpr float dot2add_impl(half2 a, half2 b, float c) {
 #if (__has_builtin(__builtin_dx_dot2add))
   return __builtin_dx_dot2add(a, b, c);
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 184339044e5bf..7eca8cae1f27e 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -4514,18 +4514,6 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
     TheCall->setType(RetTy);
     break;
   }
-  case Builtin::BI__builtin_hlsl_normalize: {
-    if (SemaRef.checkArgCount(TheCall, 1))
-      return true;
-    if (CheckAllArgTypesAreCorrect(&SemaRef, TheCall,
-                                   CheckFloatOrHalfRepresentation))
-      return true;
-    ExprResult A = TheCall->getArg(0);
-    QualType ArgTyA = A.get()->getType();
-    // return type is the same as the input type
-    TheCall->setType(ArgTyA);
-    break;
-  }
   case Builtin::BI__builtin_elementwise_fma: {
     if (SemaRef.checkArgCount(TheCall, 3) ||
         CheckAllArgsHaveSameType(&SemaRef, TheCall)) {
diff --git a/clang/lib/Sema/SemaSPIRV.cpp b/clang/lib/Sema/SemaSPIRV.cpp
index 8c2af5053bde2..7c1abcad17ac3 100644
--- a/clang/lib/Sema/SemaSPIRV.cpp
+++ b/clang/lib/Sema/SemaSPIRV.cpp
@@ -246,6 +246,23 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI,
     TheCall->setType(RetTy);
     break;
   }
+  case SPIRV::BI__builtin_spirv_normalize: {
+    if (SemaRef.checkArgCount(TheCall, 1))
+      return true;
+
+    ExprResult A = TheCall->getArg(0);
+    QualType ArgTyA = A.get()->getType();
+    if (!ArgTyA->hasFloatingRepresentation()) {
+      SemaRef.Diag(A.get()->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+          << /* ordinal */ 1 << /* scalar or vector */ 5 << /* no int */ 0
+          << /* fp */ 1 << ArgTyA;
+      return true;
+    }
+
+    QualType RetTy = ArgTyA;
+    TheCall->setType(RetTy);
+    break;
+  }
   case SPIRV::BI__builtin_spirv_reflect: {
     if (SemaRef.checkArgCount(TheCall, 2))
       return true;
diff --git a/clang/test/CodeGenHLSL/builtins/normalize-builtin.hlsl b/clang/test/CodeGenHLSL/builtins/normalize-builtin.hlsl
deleted file mode 100644
index 46bfb44c9b2a1..0000000000000
--- a/clang/test/CodeGenHLSL/builtins/normalize-builtin.hlsl
+++ /dev/null
@@ -1,16 +0,0 @@
-// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -fnative-int16-type -emit-llvm -disable-llvm-passes -o - | FileCheck %s
-
-
-// CHECK-LABEL: builtin_normalize_half
-// CHECK: %hlsl.normalize = call reassoc nnan ninf nsz arcp afn half @llvm.dx.normalize.f16(half %{{.*}})
-// CHECK: ret half  %hlsl.normalize
-half builtin_normalize_half(half p0) {
-  return __builtin_hlsl_normalize(p0);
-}
-
-// CHECK-LABEL: builtin_normalize_float
-// CHECK: %hlsl.normalize = call reassoc nnan ninf nsz arcp afn float @llvm.dx.normalize.f32(float %{{.*}})
-// CHECK: ret float  %hlsl.normalize
-float builtin_normalize_float (float p0) {
-  return __builtin_hlsl_normalize(p0);
-}
diff --git a/clang/test/CodeGenHLSL/builtins/normalize-overloads.hlsl b/clang/test/CodeGenHLSL/builtins/normalize-overloads.hlsl
index 8ed3ff26529d6..2256621892f04 100644
--- a/clang/test/CodeGenHLSL/builtins/normalize-overloads.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/normalize-overloads.hlsl
@@ -1,199 +1,299 @@
 // RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple \
 // RUN:   dxil-pc-shadermodel6.3-library %s -emit-llvm \
-// RUN:   -Wdeprecated-declarations -o - | FileCheck %s --check-prefixes=CHECK \
-// RUN:   -DFNATTRS="hidden noundef nofpclass(nan inf)" -DTARGET=dx
+// RUN:   -Wdeprecated-declarations -o - | FileCheck %s --check-prefixes=CHECK,DXCHECK
 // RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple \
 // RUN:   spirv-unknown-vulkan-library %s -emit-llvm \
-// RUN:   -Wdeprecated-declarations -o - | FileCheck %s --check-prefixes=CHECK \
-// RUN:   -DFNATTRS="hidden spir_func noundef nofpclass(nan inf)" -DTARGET=spv
+// RUN:   -Wdeprecated-declarations -o - | FileCheck %s --check-prefixes=CHECK,SPVCHECK
 // RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s  \
 // RUN:   -verify -verify-ignore-unexpected=note
 // RUN: %clang_cc1 -std=hlsl202x -finclude-default-header -x hlsl -triple spirv-unknown-vulkan-library %s  \
 // RUN:   -verify -verify-ignore-unexpected=note
 
-// CHECK: define [[FNATTRS]] float @_Z21test_normalize_doubled(
-// CHECK:    [[CONVI:%.*]] = fptrunc {{.*}} double %{{.*}} to float
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} float @llvm.[[TARGET]].normalize.f32(float [[CONVI]])
-// CHECK:    ret float [[HLSLNORMALIZEI]]
+// CHECK-LABEL: test_normalize_double
+// CHECK: [[CONVI:%.*]] = fptrunc {{.*}} double %{{.*}} to float
+// DXCHECK: [[MUL:%.*]] = fmul {{.*}} float %{{.*}}, %{{.*}}
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[MUL]])
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} float %{{.*}}, [[RSQRT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} float @llvm.spv.normalize.f32(float %{{.*}})
+// CHECK-NEXT: ret float [[RET]]
 float test_normalize_double(double p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x 64 bit API lowering for normalize is deprecated. Explicitly cast parameters to 32 or 16 bit types.}}
     return normalize(p0);
 }
-// CHECK: define [[FNATTRS]] <2 x float> @_Z22test_normalize_double2Dv2_d(
-// CHECK:    [[CONVI:%.*]] = fptrunc {{.*}} <2 x double> %{{.*}} to <2 x float>
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} <2 x float> @llvm.[[TARGET]].normalize.v2f32(<2 x float> [[CONVI]])
-// CHECK:    ret <2 x float> [[HLSLNORMALIZEI]]
+
+// CHECK-LABEL: test_normalize_double2
+// CHECK: [[CONVI:%.*]] = fptrunc {{.*}} <2 x double> %{{.*}} to <2 x float>
+// DXCHECK: [[DOT:%.*]] = call {{.*}} float @llvm.dx.fdot.v2f32(<2 x float> %{{.*}}, <2 x float> %{{.*}})
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[DOT]])
+// DXCHECK-NEXT: [[SPLATINSERT:%.*]] = insertelement <2 x float> poison, float [[RSQRT]], i64 0
+// DXCHECK-NEXT: [[SPLAT:%.*]] = shufflevector <2 x float> [[SPLATINSERT]], <2 x float> poison, <2 x i32> zeroinitializer
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} <2 x float> %{{.*}}, [[SPLAT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} <2 x float> @llvm.spv.normalize.v2f32(<2 x float> %{{.*}})
+// CHECK-NEXT: ret <2 x float> [[RET]]
 float2 test_normalize_double2(double2 p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x 64 bit API lowering for normalize is deprecated. Explicitly cast parameters to 32 or 16 bit types.}}
     return normalize(p0);
 }
-// CHECK: define [[FNATTRS]] <3 x float> @_Z22test_normalize_double3Dv3_d(
-// CHECK:    [[CONVI:%.*]] = fptrunc {{.*}} <3 x double> %{{.*}} to <3 x float>
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} <3 x float> @llvm.[[TARGET]].normalize.v3f32(<3 x float> [[CONVI]])
-// CHECK:    ret <3 x float> [[HLSLNORMALIZEI]]
+
+// CHECK-LABEL: test_normalize_double3
+// CHECK: [[CONVI:%.*]] = fptrunc {{.*}} <3 x double> %{{.*}} to <3 x float>
+// DXCHECK: [[DOT:%.*]] = call {{.*}} float @llvm.dx.fdot.v3f32(<3 x float> %{{.*}}, <3 x float> %{{.*}})
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[DOT]])
+// DXCHECK-NEXT: [[SPLATINSERT:%.*]] = insertelement <3 x float> poison, float [[RSQRT]], i64 0
+// DXCHECK-NEXT: [[SPLAT:%.*]] = shufflevector <3 x float> [[SPLATINSERT]], <3 x float> poison, <3 x i32> zeroinitializer
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} <3 x float> %{{.*}}, [[SPLAT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} <3 x float> @llvm.spv.normalize.v3f32(<3 x float> %{{.*}})
+// CHECK-NEXT: ret <3 x float> [[RET]]
 float3 test_normalize_double3(double3 p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x 64 bit API lowering for normalize is deprecated. Explicitly cast parameters to 32 or 16 bit types.}}
     return normalize(p0);
 }
-// CHECK: define [[FNATTRS]] <4 x float> @_Z19test_length_double4Dv4_d(
-// CHECK:    [[CONVI:%.*]] = fptrunc {{.*}} <4 x double> %{{.*}} to <4 x float>
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} <4 x float> @llvm.[[TARGET]].normalize.v4f32(<4 x float> [[CONVI]])
-// CHECK:    ret <4 x float> [[HLSLNORMALIZEI]]
-float4 test_length_double4(double4 p0)
+
+// CHECK-LABEL: test_normalize_double4
+// CHECK: [[CONVI:%.*]] = fptrunc {{.*}} <4 x double> %{{.*}} to <4 x float>
+// DXCHECK: [[DOT:%.*]] = call {{.*}} float @llvm.dx.fdot.v4f32(<4 x float> %{{.*}}, <4 x float> %{{.*}})
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[DOT]])
+// DXCHECK-NEXT: [[SPLATINSERT:%.*]] = insertelement <4 x float> poison, float [[RSQRT]], i64 0
+// DXCHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x float> [[SPLATINSERT]], <4 x float> poison, <4 x i32> zeroinitializer
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} <4 x float> %{{.*}}, [[SPLAT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} <4 x float> @llvm.spv.normalize.v4f32(<4 x float> %{{.*}})
+// CHECK-NEXT: ret <4 x float> [[RET]]
+float4 test_normalize_double4(double4 p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x 64 bit API lowering for normalize is deprecated. Explicitly cast parameters to 32 or 16 bit types.}}
     return normalize(p0);
 }
 
-// CHECK: define [[FNATTRS]] float @_Z18test_normalize_inti(
-// CHECK:    [[CONVI:%.*]] = sitofp {{.*}} i32 %{{.*}} to float
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} float @llvm.[[TARGET]].normalize.f32(float [[CONVI]])
-// CHECK:    ret float [[HLSLNORMALIZEI]]
+// CHECK-LABEL: test_normalize_int
+// CHECK: [[CONVI:%.*]] = sitofp {{.*}} i32 %{{.*}} to float
+// DXCHECK: [[MUL:%.*]] = fmul {{.*}} float %{{.*}}, %{{.*}}
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[MUL]])
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} float %{{.*}}, [[RSQRT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} float @llvm.spv.normalize.f32(float %{{.*}})
+// CHECK-NEXT: ret float [[RET]]
 float test_normalize_int(int p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x int lowering for normalize is deprecated. Explicitly cast parameters to float types.}}
     return normalize(p0);
 }
-// CHECK: define [[FNATTRS]] <2 x float> @_Z19test_normalize_int2Dv2_i(
-// CHECK:    [[CONVI:%.*]] = sitofp {{.*}} <2 x i32> %{{.*}} to <2 x float>
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} <2 x float> @llvm.[[TARGET]].normalize.v2f32(<2 x float> [[CONVI]])
-// CHECK:    ret <2 x float> [[HLSLNORMALIZEI]]
+
+// CHECK-LABEL: test_normalize_int2
+// CHECK: [[CONVI:%.*]] = sitofp {{.*}} <2 x i32> %{{.*}} to <2 x float>
+// DXCHECK: [[DOT:%.*]] = call {{.*}} float @llvm.dx.fdot.v2f32(<2 x float> %{{.*}}, <2 x float> %{{.*}})
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[DOT]])
+// DXCHECK-NEXT: [[SPLATINSERT:%.*]] = insertelement <2 x float> poison, float [[RSQRT]], i64 0
+// DXCHECK-NEXT: [[SPLAT:%.*]] = shufflevector <2 x float> [[SPLATINSERT]], <2 x float> poison, <2 x i32> zeroinitializer
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} <2 x float> %{{.*}}, [[SPLAT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} <2 x float> @llvm.spv.normalize.v2f32(<2 x float> %{{.*}})
+// CHECK-NEXT: ret <2 x float> [[RET]]
 float2 test_normalize_int2(int2 p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x int lowering for normalize is deprecated. Explicitly cast parameters to float types.}}
     return normalize(p0);
 }
-// CHECK: define [[FNATTRS]] <3 x float> @_Z19test_normalize_int3Dv3_i(
-// CHECK:    [[CONVI:%.*]] = sitofp {{.*}} <3 x i32> %{{.*}} to <3 x float>
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} <3 x float> @llvm.[[TARGET]].normalize.v3f32(<3 x float> [[CONVI]])
-// CHECK:    ret <3 x float> [[HLSLNORMALIZEI]]
+
+// CHECK-LABEL: test_normalize_int3
+// CHECK: [[CONVI:%.*]] = sitofp {{.*}} <3 x i32> %{{.*}} to <3 x float>
+// DXCHECK: [[DOT:%.*]] = call {{.*}} float @llvm.dx.fdot.v3f32(<3 x float> %{{.*}}, <3 x float> %{{.*}})
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[DOT]])
+// DXCHECK-NEXT: [[SPLATINSERT:%.*]] = insertelement <3 x float> poison, float [[RSQRT]], i64 0
+// DXCHECK-NEXT: [[SPLAT:%.*]] = shufflevector <3 x float> [[SPLATINSERT]], <3 x float> poison, <3 x i32> zeroinitializer
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} <3 x float> %{{.*}}, [[SPLAT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} <3 x float> @llvm.spv.normalize.v3f32(<3 x float> %{{.*}})
+// CHECK-NEXT: ret <3 x float> [[RET]]
 float3 test_normalize_int3(int3 p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x int lowering for normalize is deprecated. Explicitly cast parameters to float types.}}
     return normalize(p0);
 }
-// CHECK: define [[FNATTRS]] <4 x float> @_Z16test_length_int4Dv4_i(
-// CHECK:    [[CONVI:%.*]] = sitofp {{.*}} <4 x i32> %{{.*}} to <4 x float>
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} <4 x float> @llvm.[[TARGET]].normalize.v4f32(<4 x float> [[CONVI]])
-// CHECK:    ret <4 x float> [[HLSLNORMALIZEI]]
-float4 test_length_int4(int4 p0)
+
+// CHECK-LABEL: test_normalize_int4
+// CHECK: [[CONVI:%.*]] = sitofp {{.*}} <4 x i32> %{{.*}} to <4 x float>
+// DXCHECK: [[DOT:%.*]] = call {{.*}} float @llvm.dx.fdot.v4f32(<4 x float> %{{.*}}, <4 x float> %{{.*}})
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[DOT]])
+// DXCHECK-NEXT: [[SPLATINSERT:%.*]] = insertelement <4 x float> poison, float [[RSQRT]], i64 0
+// DXCHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x float> [[SPLATINSERT]], <4 x float> poison, <4 x i32> zeroinitializer
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} <4 x float> %{{.*}}, [[SPLAT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} <4 x float> @llvm.spv.normalize.v4f32(<4 x float> %{{.*}})
+// CHECK-NEXT: ret <4 x float> [[RET]]
+float4 test_normalize_int4(int4 p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x int lowering for normalize is deprecated. Explicitly cast parameters to float types.}}
     return normalize(p0);
 }
 
-// CHECK: define [[FNATTRS]] float @_Z19test_normalize_uintj(
-// CHECK:    [[CONVI:%.*]] = uitofp {{.*}} i32 %{{.*}} to float
-// CHECK:    [[HLSLNORMALIZEI:%.*]] = call {{.*}} float @llvm.[[TARGET]].normalize.f32(float [[CONVI]])
-// CHECK:    ret float [[HLSLNORMALIZEI]]
+// CHECK-LABEL: test_normalize_uint
+// CHECK: [[CONVI:%.*]] = uitofp {{.*}} i32 %{{.*}} to float
+// DXCHECK: [[MUL:%.*]] = fmul {{.*}} float %{{.*}}, %{{.*}}
+// DXCHECK-NEXT: [[RSQRT:%.*]] = call {{.*}} float @llvm.dx.rsqrt.f32(float [[MUL]])
+// DXCHECK-NEXT: [[RET:%.*]] = fmul {{.*}} float %{{.*}}, [[RSQRT]]
+// SPVCHECK: [[RET:%.*]] = call {{.*}} float @llvm.spv.normalize.f32(float %{{.*}})
+// CHECK-NEXT: ret float [[RET]]
 float test_normalize_uint(uint p0)
 {
 // expected-warning at +1 {{'normalize' is deprecated: In 202x int lowering for normalize is deprecated. Explicitly cast parameters to float types.}}
     return normalize(p0);
 }
 
-// CHECK: define [[FNATTRS]] <2 x float> @_Z20test_normalize_uint...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/216228


More information about the cfe-commits mailing list