Author: Dan Brown Date: 2026-08-28T13:51:54-07:00 New Revision: 0ad77a642a08e5f8b50167bdaba716680f34c51d
URL: https://github.com/llvm/llvm-project/commit/0ad77a642a08e5f8b50167bdaba716680f34c51d DIFF: https://github.com/llvm/llvm-project/commit/0ad77a642a08e5f8b50167bdaba716680f34c51d.diff LOG: [HLSL] Add matrix support for degrees(), radians() (#218573) Resolves #184480, #184485 Assisted-by: Claude Opus 4 Added: clang/test/CodeGenHLSL/builtins/degrees_mat.hlsl clang/test/CodeGenHLSL/builtins/radians_mat.hlsl Modified: clang/include/clang/Basic/HLSLIntrinsics.td clang/lib/Headers/hlsl/hlsl_detail.h clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h Removed: ################################################################################ diff --git a/clang/include/clang/Basic/HLSLIntrinsics.td b/clang/include/clang/Basic/HLSLIntrinsics.td index ebfbe5e18c464..4ef818edb3259 100644 --- a/clang/include/clang/Basic/HLSLIntrinsics.td +++ b/clang/include/clang/Basic/HLSLIntrinsics.td @@ -694,15 +694,15 @@ prevision partial derivative of the input value. } // Converts the specified value from radians to degrees. -def hlsl_degrees : HLSLOneArgDetail<"degrees", "degrees_impl"> { +def hlsl_degrees : HLSLOneArgInlineBuiltin<"degrees"> { let Doc = [{ \fn T degrees(T x) \brief Converts the specified value from radians to degrees. \param x The specified input value. }]; let ParamNames = ["x"]; + let Body = "return x * (__detail::elem_type_t<decltype(x)>)(180.L / __detail::Pi);"; let VaryingTypes = [HalfTy, FloatTy]; - let VaryingMatDims = []; } // Returns the cross product of two floating-point, 3D vectors. def hlsl_cross : HLSLTwoArgDetail<"cross", "cross_impl"> { @@ -1394,14 +1394,14 @@ def hlsl_quad_read_across_diagonal : HLSLOneArgBuiltin<"QuadReadAcrossDiagonal", } // Converts the specified value from degrees to radians. -def hlsl_radians : HLSLOneArgDetail<"radians", "radians_impl"> { +def hlsl_radians : HLSLOneArgInlineBuiltin<"radians"> { let Doc = [{ \fn T radians(T Val) \brief Converts the specified value from degrees to radians. }]; let ParamNames = ["Val"]; + let Body = "return Val * (__detail::elem_type_t<decltype(Val)>)(__detail::Pi / 180.L);"; let VaryingTypes = [HalfTy, FloatTy]; - let VaryingMatDims = []; } // Calculates a fast, approximate, per-component reciprocal ie 1 / x. diff --git a/clang/lib/Headers/hlsl/hlsl_detail.h b/clang/lib/Headers/hlsl/hlsl_detail.h index 8ce925bb8fbe4..bc9d23af95c17 100644 --- a/clang/lib/Headers/hlsl/hlsl_detail.h +++ b/clang/lib/Headers/hlsl/hlsl_detail.h @@ -53,6 +53,17 @@ template <typename T> struct is_arithmetic { static const bool Value = __is_arithmetic(T); }; +template <typename T> struct elem_type { + using Type = T; +}; +template <typename T, int N> struct elem_type<vector<T, N>> { + using Type = T; +}; +template <typename T, int R, int C> struct elem_type<matrix<T, R, C>> { + using Type = T; +}; +template <typename T> using elem_type_t = typename elem_type<T>::Type; + } // namespace __detail } // namespace hlsl #endif //_HLSL_HLSL_DETAILS_H_ diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h index 977059a9fdae0..d96199f206504 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h @@ -180,14 +180,6 @@ template <typename T> constexpr T fwidth_impl(T input) { #endif } -template <typename T> constexpr T degrees_impl(T Val) { - return Val * (T)(180.L / Pi); -} - -template <typename T> constexpr T radians_impl(T Val) { - return Val * (T)(Pi / 180.L); -} - } // namespace __detail } // namespace hlsl diff --git a/clang/test/CodeGenHLSL/builtins/degrees_mat.hlsl b/clang/test/CodeGenHLSL/builtins/degrees_mat.hlsl new file mode 100644 index 0000000000000..f9423237a8c22 --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/degrees_mat.hlsl @@ -0,0 +1,155 @@ +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \ +// RUN: dxil-pc-shadermodel6.3-library %s \ +// RUN: -fnative-half-type -fnative-int16-type -emit-llvm -o - \ +// RUN: | FileCheck %s + +// CHECK-LABEL: test_degrees_half1x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <2 x half> +half1x2 test_degrees_half1x2(half1x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half1x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <3 x half> +half1x3 test_degrees_half1x3(half1x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half1x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <4 x half> +half1x4 test_degrees_half1x4(half1x4 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half2x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <2 x half> +half2x1 test_degrees_half2x1(half2x1 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half2x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <4 x half> +half2x2 test_degrees_half2x2(half2x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half2x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <6 x half> +half2x3 test_degrees_half2x3(half2x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half2x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <8 x half> +half2x4 test_degrees_half2x4(half2x4 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half3x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <3 x half> +half3x1 test_degrees_half3x1(half3x1 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half3x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <6 x half> +half3x2 test_degrees_half3x2(half3x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half3x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <9 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <9 x half> +half3x3 test_degrees_half3x3(half3x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half3x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <12 x half> +half3x4 test_degrees_half3x4(half3x4 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half4x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <4 x half> +half4x1 test_degrees_half4x1(half4x1 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half4x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <8 x half> +half4x2 test_degrees_half4x2(half4x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half4x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <12 x half> +half4x3 test_degrees_half4x3(half4x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_half4x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <16 x half> {{.*}}, splat (half 5.728130e+01) +// CHECK: ret <16 x half> +half4x4 test_degrees_half4x4(half4x4 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float1x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <2 x float> +float1x2 test_degrees_float1x2(float1x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float1x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <3 x float> +float1x3 test_degrees_float1x3(float1x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float1x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <4 x float> +float1x4 test_degrees_float1x4(float1x4 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float2x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <2 x float> +float2x1 test_degrees_float2x1(float2x1 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float2x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <4 x float> +float2x2 test_degrees_float2x2(float2x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float2x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <6 x float> +float2x3 test_degrees_float2x3(float2x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float2x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <8 x float> +float2x4 test_degrees_float2x4(float2x4 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float3x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <3 x float> +float3x1 test_degrees_float3x1(float3x1 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float3x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <6 x float> +float3x2 test_degrees_float3x2(float3x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float3x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <9 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <9 x float> +float3x3 test_degrees_float3x3(float3x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float3x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <12 x float> +float3x4 test_degrees_float3x4(float3x4 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float4x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <4 x float> +float4x1 test_degrees_float4x1(float4x1 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float4x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <8 x float> +float4x2 test_degrees_float4x2(float4x2 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float4x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <12 x float> +float4x3 test_degrees_float4x3(float4x3 p0) { return degrees(p0); } + +// CHECK-LABEL: test_degrees_float4x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <16 x float> {{.*}}, splat (float f0x42652EE1) +// CHECK: ret <16 x float> +float4x4 test_degrees_float4x4(float4x4 p0) { return degrees(p0); } + diff --git a/clang/test/CodeGenHLSL/builtins/radians_mat.hlsl b/clang/test/CodeGenHLSL/builtins/radians_mat.hlsl new file mode 100644 index 0000000000000..231e8b5ea0258 --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/radians_mat.hlsl @@ -0,0 +1,155 @@ +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \ +// RUN: dxil-pc-shadermodel6.3-library %s \ +// RUN: -fnative-half-type -fnative-int16-type -emit-llvm -o - \ +// RUN: | FileCheck %s + +// CHECK-LABEL: test_radians_half1x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <2 x half> +half1x2 test_radians_half1x2(half1x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half1x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <3 x half> +half1x3 test_radians_half1x3(half1x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half1x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <4 x half> +half1x4 test_radians_half1x4(half1x4 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half2x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <2 x half> +half2x1 test_radians_half2x1(half2x1 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half2x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <4 x half> +half2x2 test_radians_half2x2(half2x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half2x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <6 x half> +half2x3 test_radians_half2x3(half2x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half2x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <8 x half> +half2x4 test_radians_half2x4(half2x4 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half3x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <3 x half> +half3x1 test_radians_half3x1(half3x1 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half3x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <6 x half> +half3x2 test_radians_half3x2(half3x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half3x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <9 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <9 x half> +half3x3 test_radians_half3x3(half3x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half3x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <12 x half> +half3x4 test_radians_half3x4(half3x4 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half4x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <4 x half> +half4x1 test_radians_half4x1(half4x1 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half4x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <8 x half> +half4x2 test_radians_half4x2(half4x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half4x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <12 x half> +half4x3 test_radians_half4x3(half4x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_half4x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <16 x half> {{.*}}, splat (half 1.745610e-02) +// CHECK: ret <16 x half> +half4x4 test_radians_half4x4(half4x4 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float1x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <2 x float> +float1x2 test_radians_float1x2(float1x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float1x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <3 x float> +float1x3 test_radians_float1x3(float1x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float1x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <4 x float> +float1x4 test_radians_float1x4(float1x4 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float2x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <2 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <2 x float> +float2x1 test_radians_float2x1(float2x1 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float2x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <4 x float> +float2x2 test_radians_float2x2(float2x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float2x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <6 x float> +float2x3 test_radians_float2x3(float2x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float2x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <8 x float> +float2x4 test_radians_float2x4(float2x4 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float3x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <3 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <3 x float> +float3x1 test_radians_float3x1(float3x1 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float3x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <6 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <6 x float> +float3x2 test_radians_float3x2(float3x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float3x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <9 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <9 x float> +float3x3 test_radians_float3x3(float3x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float3x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <12 x float> +float3x4 test_radians_float3x4(float3x4 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float4x1 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <4 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <4 x float> +float4x1 test_radians_float4x1(float4x1 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float4x2 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <8 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <8 x float> +float4x2 test_radians_float4x2(float4x2 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float4x3 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <12 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <12 x float> +float4x3 test_radians_float4x3(float4x3 p0) { return radians(p0); } + +// CHECK-LABEL: test_radians_float4x4 +// CHECK: fmul reassoc nnan ninf nsz arcp afn <16 x float> {{.*}}, splat (float f0x3C8EFA35) +// CHECK: ret <16 x float> +float4x4 test_radians_float4x4(float4x4 p0) { return radians(p0); } + _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
