https://github.com/farzonl created https://github.com/llvm/llvm-project/pull/243998
fixes https://github.com/llvm/llvm-project/issues/184494 fixes https://github.com/llvm/llvm-project/issues/184493 Implemented matrix support for all and any using header-only helpers that reuse vector reductions This PR was assisted vy copilot fusion >From e70c691684a01d750d999de77b00a1e1aa90d26a Mon Sep 17 00:00:00 2001 From: Farzon Lotfi <[email protected]> Date: Wed, 18 Nov 2026 15:35:34 -0500 Subject: [PATCH] [HLSL][Matrix] Add all and any support fixes https://github.com/llvm/llvm-project/issues/184494 fixes https://github.com/llvm/llvm-project/issues/184493 Implemented matrix support for all and any using header-only helpers that reuse vector reductions --- clang/include/clang/Basic/HLSLIntrinsics.td | 10 ++-- .../lib/Headers/hlsl/hlsl_intrinsic_helpers.h | 8 +++ clang/test/CodeGenHLSL/builtins/all_mat.hlsl | 50 +++++++++++++++++++ clang/test/CodeGenHLSL/builtins/any_mat.hlsl | 50 +++++++++++++++++++ 4 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 clang/test/CodeGenHLSL/builtins/all_mat.hlsl create mode 100644 clang/test/CodeGenHLSL/builtins/any_mat.hlsl diff --git a/clang/include/clang/Basic/HLSLIntrinsics.td b/clang/include/clang/Basic/HLSLIntrinsics.td index 00ccf34d5ee836..f2578b3780c4cd 100644 --- a/clang/include/clang/Basic/HLSLIntrinsics.td +++ b/clang/include/clang/Basic/HLSLIntrinsics.td @@ -386,7 +386,7 @@ def hlsl_all : HLSLBuiltin<"all"> { \fn bool all(T x) \brief Returns True if all components of the \a x parameter are non-zero; otherwise, false. -\param x The input value. +\param x The input scalar, vector, or matrix. }]; let ParamNames = ["V"]; let Body = "return V != 0;"; @@ -396,7 +396,7 @@ otherwise, false. let VaryingScalar = 1; } -def hlsl_all_vector : HLSLBuiltin<"all"> { +def hlsl_all_aggregate : HLSLBuiltin<"all"> { let ParamNames = ["V"]; let DetailFunc = "all_impl"; let Args = [Varying]; @@ -404,6 +404,7 @@ def hlsl_all_vector : HLSLBuiltin<"all"> { let VaryingTypes = AllTypesWithBool; let VaryingVecSizes = [2, 3, 4]; let VaryingLongVector = 1; + let VaryingMatDims = AllMatDims; } // Returns the boolean AND of two boolean scalars, vectors, or matrices. @@ -423,7 +424,7 @@ def hlsl_any : HLSLBuiltin<"any"> { \fn bool any(T x) \brief Returns True if any components of the \a x parameter are non-zero; otherwise, false. -\param x The input value. +\param x The input scalar, vector, or matrix. }]; let ParamNames = ["V"]; let Body = "return V != 0;"; @@ -433,7 +434,7 @@ otherwise, false. let VaryingScalar = 1; } -def hlsl_any_vector : HLSLBuiltin<"any"> { +def hlsl_any_aggregate : HLSLBuiltin<"any"> { let ParamNames = ["V"]; let DetailFunc = "any_impl"; let Args = [Varying]; @@ -441,6 +442,7 @@ def hlsl_any_vector : HLSLBuiltin<"any"> { let VaryingTypes = AllTypesWithBool; let VaryingVecSizes = [2, 3, 4]; let VaryingLongVector = 1; + let VaryingMatDims = AllMatDims; } // Reinterprets a cast value (two 32-bit values) into a double. diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h index 5bcce0f68c3865..ab8cf12491bb68 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h @@ -17,11 +17,19 @@ template <typename T, int N> constexpr bool all_impl(vector<T, N> V) { __builtin_convertvector(V != static_cast<T>(0), vector<bool, N>)); } +template <typename T, int R, int C> constexpr bool all_impl(matrix<T, R, C> V) { + return all_impl((vector<T, R * C>)V); +} + template <typename T, int N> constexpr bool any_impl(vector<T, N> V) { return __builtin_reduce_or( __builtin_convertvector(V != static_cast<T>(0), vector<bool, N>)); } +template <typename T, int R, int C> constexpr bool any_impl(matrix<T, R, C> V) { + return any_impl((vector<T, R * C>)V); +} + template <typename T> constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T> length_impl(T X) { diff --git a/clang/test/CodeGenHLSL/builtins/all_mat.hlsl b/clang/test/CodeGenHLSL/builtins/all_mat.hlsl new file mode 100644 index 00000000000000..4c28f162ce38af --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/all_mat.hlsl @@ -0,0 +1,50 @@ +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \ +// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \ +// RUN: -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,NATIVE_HALF +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \ +// RUN: spirv-unknown-vulkan-library %s -emit-llvm -o - \ +// RUN: | FileCheck %s --check-prefixes=CHECK,NO_HALF + +// CHECK-LABEL: test_all_half2x3 +// NATIVE_HALF: [[CMP:%.*]] = fcmp {{.*}} une <6 x half> {{.*}}, zeroinitializer +// NO_HALF: [[CMP:%.*]] = fcmp {{.*}} une <6 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.and.v6i1(<6 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_all_half2x3(half2x3 value) { return all(value); } + +// CHECK-LABEL: test_all_half3x4 +// NATIVE_HALF: [[CMP:%.*]] = fcmp {{.*}} une <12 x half> {{.*}}, zeroinitializer +// NO_HALF: [[CMP:%.*]] = fcmp {{.*}} une <12 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.and.v12i1(<12 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_all_half3x4(half3x4 value) { return all(value); } + +// CHECK-LABEL: test_all_half4x4 +// NATIVE_HALF: [[CMP:%.*]] = fcmp {{.*}} une <16 x half> {{.*}}, zeroinitializer +// NO_HALF: [[CMP:%.*]] = fcmp {{.*}} une <16 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.and.v16i1(<16 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_all_half4x4(half4x4 value) { return all(value); } + +// CHECK-LABEL: test_all_float2x3 +// CHECK: [[CMP:%.*]] = fcmp {{.*}} une <6 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.and.v6i1(<6 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_all_float2x3(float2x3 value) { return all(value); } + +// CHECK-LABEL: test_all_double3x4 +// CHECK: [[CMP:%.*]] = fcmp {{.*}} une <12 x double> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.and.v12i1(<12 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_all_double3x4(double3x4 value) { return all(value); } + +// CHECK-LABEL: test_all_int4x4 +// CHECK: [[CMP:%.*]] = icmp ne <16 x i32> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.and.v16i1(<16 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_all_int4x4(int4x4 value) { return all(value); } + +// CHECK-LABEL: test_all_bool2x3 +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.and.v6i1(<6 x i1> %{{.*}}) +// CHECK: ret i1 [[REDUCE]] +bool test_all_bool2x3(bool2x3 value) { return all(value); } diff --git a/clang/test/CodeGenHLSL/builtins/any_mat.hlsl b/clang/test/CodeGenHLSL/builtins/any_mat.hlsl new file mode 100644 index 00000000000000..e9917c7d36c7da --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/any_mat.hlsl @@ -0,0 +1,50 @@ +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \ +// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \ +// RUN: -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,NATIVE_HALF +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \ +// RUN: spirv-unknown-vulkan-library %s -emit-llvm -o - \ +// RUN: | FileCheck %s --check-prefixes=CHECK,NO_HALF + +// CHECK-LABEL: test_any_half2x3 +// NATIVE_HALF: [[CMP:%.*]] = fcmp {{.*}} une <6 x half> {{.*}}, zeroinitializer +// NO_HALF: [[CMP:%.*]] = fcmp {{.*}} une <6 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.or.v6i1(<6 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_any_half2x3(half2x3 value) { return any(value); } + +// CHECK-LABEL: test_any_half3x4 +// NATIVE_HALF: [[CMP:%.*]] = fcmp {{.*}} une <12 x half> {{.*}}, zeroinitializer +// NO_HALF: [[CMP:%.*]] = fcmp {{.*}} une <12 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.or.v12i1(<12 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_any_half3x4(half3x4 value) { return any(value); } + +// CHECK-LABEL: test_any_half4x4 +// NATIVE_HALF: [[CMP:%.*]] = fcmp {{.*}} une <16 x half> {{.*}}, zeroinitializer +// NO_HALF: [[CMP:%.*]] = fcmp {{.*}} une <16 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.or.v16i1(<16 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_any_half4x4(half4x4 value) { return any(value); } + +// CHECK-LABEL: test_any_float2x3 +// CHECK: [[CMP:%.*]] = fcmp {{.*}} une <6 x float> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.or.v6i1(<6 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_any_float2x3(float2x3 value) { return any(value); } + +// CHECK-LABEL: test_any_double3x4 +// CHECK: [[CMP:%.*]] = fcmp {{.*}} une <12 x double> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.or.v12i1(<12 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_any_double3x4(double3x4 value) { return any(value); } + +// CHECK-LABEL: test_any_int4x4 +// CHECK: [[CMP:%.*]] = icmp ne <16 x i32> {{.*}}, zeroinitializer +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.or.v16i1(<16 x i1> [[CMP]]) +// CHECK: ret i1 [[REDUCE]] +bool test_any_int4x4(int4x4 value) { return any(value); } + +// CHECK-LABEL: test_any_bool2x3 +// CHECK: [[REDUCE:%.*]] = call {{.*}}i1 @llvm.vector.reduce.or.v6i1(<6 x i1> %{{.*}}) +// CHECK: ret i1 [[REDUCE]] +bool test_any_bool2x3(bool2x3 value) { return any(value); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
