[clang] [HLSL][Matrix] Add all and any support (PR #243998)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 18 12:49:54 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-hlsl
Author: Farzon Lotfi (farzonl)
<details>
<summary>Changes</summary>
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 by copilot fusion
---
Full diff: https://github.com/llvm/llvm-project/pull/243998.diff
4 Files Affected:
- (modified) clang/include/clang/Basic/HLSLIntrinsics.td (+6-4)
- (modified) clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h (+8)
- (added) clang/test/CodeGenHLSL/builtins/all_mat.hlsl (+50)
- (added) clang/test/CodeGenHLSL/builtins/any_mat.hlsl (+50)
``````````diff
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); }
``````````
</details>
https://github.com/llvm/llvm-project/pull/243998
More information about the cfe-commits
mailing list