diff --git a/src/tint/intrinsics.def b/src/tint/intrinsics.def index b72426e38d..1894f8bbad 100644 --- a/src/tint/intrinsics.def +++ b/src/tint/intrinsics.def @@ -410,8 +410,8 @@ fn acos(T) -> T fn acos(vec) -> vec fn acosh(T) -> T fn acosh(vec) -> vec -fn all(bool) -> bool -fn all(vec) -> bool +@const fn all(bool) -> bool +@const fn all(vec) -> bool @const fn any(bool) -> bool @const fn any(vec) -> bool fn arrayLength(ptr, A>) -> u32 diff --git a/src/tint/resolver/const_eval.cc b/src/tint/resolver/const_eval.cc index 5d94e1cb7c..f18c0d5e95 100644 --- a/src/tint/resolver/const_eval.cc +++ b/src/tint/resolver/const_eval.cc @@ -1570,6 +1570,12 @@ ConstEval::Result ConstEval::OpShiftLeft(const sem::Type* ty, return r; } +ConstEval::Result ConstEval::all(const sem::Type* ty, + utils::VectorRef args, + const Source&) { + return CreateElement(builder, ty, !args[0]->AnyZero()); +} + ConstEval::Result ConstEval::any(const sem::Type* ty, utils::VectorRef args, const Source&) { diff --git a/src/tint/resolver/const_eval.h b/src/tint/resolver/const_eval.h index 9aacec5370..d6f5594927 100644 --- a/src/tint/resolver/const_eval.h +++ b/src/tint/resolver/const_eval.h @@ -377,6 +377,15 @@ class ConstEval { // Builtins //////////////////////////////////////////////////////////////////////////// + /// all builtin + /// @param ty the expression type + /// @param args the input arguments + /// @param source the source location of the conversion + /// @return the result value, or null if the value cannot be calculated + Result all(const sem::Type* ty, + utils::VectorRef args, + const Source& source); + /// any builtin /// @param ty the expression type /// @param args the input arguments diff --git a/src/tint/resolver/const_eval_builtin_test.cc b/src/tint/resolver/const_eval_builtin_test.cc index 7b78035956..b0fa42524d 100644 --- a/src/tint/resolver/const_eval_builtin_test.cc +++ b/src/tint/resolver/const_eval_builtin_test.cc @@ -146,6 +146,35 @@ INSTANTIATE_TEST_SUITE_P( // C({1.0_a, 0_a}, kPiOver2), }))); +static std::vector AllCases() { + return { + C({Val(true)}, Val(true)), + C({Val(false)}, Val(false)), + + C({Vec(true, true)}, Val(true)), + C({Vec(true, false)}, Val(false)), + C({Vec(false, true)}, Val(false)), + C({Vec(false, false)}, Val(false)), + + C({Vec(true, true, true)}, Val(true)), + C({Vec(false, true, true)}, Val(false)), + C({Vec(true, false, true)}, Val(false)), + C({Vec(true, true, false)}, Val(false)), + C({Vec(false, false, false)}, Val(false)), + + C({Vec(true, true, true, true)}, Val(true)), + C({Vec(false, true, true, true)}, Val(false)), + C({Vec(true, false, true, true)}, Val(false)), + C({Vec(true, true, false, true)}, Val(false)), + C({Vec(true, true, true, false)}, Val(false)), + C({Vec(false, false, false, false)}, Val(false)), + }; +} +INSTANTIATE_TEST_SUITE_P( // + All, + ResolverConstEvalBuiltinTest, + testing::Combine(testing::Values(sem::BuiltinType::kAll), testing::ValuesIn(AllCases()))); + static std::vector AnyCases() { return { C({Val(true)}, Val(true)), diff --git a/src/tint/resolver/intrinsic_table.inl b/src/tint/resolver/intrinsic_table.inl index dde2982c2e..021d993b5a 100644 --- a/src/tint/resolver/intrinsic_table.inl +++ b/src/tint/resolver/intrinsic_table.inl @@ -13430,7 +13430,7 @@ constexpr OverloadInfo kOverloads[] = { /* parameters */ &kParameters[996], /* return matcher indices */ &kMatcherIndices[41], /* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline), - /* const eval */ nullptr, + /* const eval */ &ConstEval::all, }, { /* [425] */ @@ -13442,7 +13442,7 @@ constexpr OverloadInfo kOverloads[] = { /* parameters */ &kParameters[1017], /* return matcher indices */ &kMatcherIndices[41], /* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline), - /* const eval */ nullptr, + /* const eval */ &ConstEval::all, }, { /* [426] */ diff --git a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.dxc.hlsl b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.dxc.hlsl index 47ffe9f5e6..8aa391f4f7 100644 --- a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.dxc.hlsl +++ b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.dxc.hlsl @@ -1,5 +1,5 @@ void all_353d6a() { - bool res = all(true); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.fxc.hlsl b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.fxc.hlsl index 47ffe9f5e6..8aa391f4f7 100644 --- a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.fxc.hlsl +++ b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.fxc.hlsl @@ -1,5 +1,5 @@ void all_353d6a() { - bool res = all(true); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.msl b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.msl index d661e0c530..78fd78c35f 100644 --- a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.msl +++ b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.msl @@ -2,7 +2,7 @@ using namespace metal; void all_353d6a() { - bool res = all(true); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.spvasm b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.spvasm index ce7b6fe9d8..4f2129be8c 100644 --- a/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.spvasm +++ b/test/tint/builtins/gen/literal/all/353d6a.wgsl.expected.spvasm @@ -1,7 +1,7 @@ ; SPIR-V ; Version: 1.3 ; Generator: Google Tint Compiler; 0 -; Bound: 33 +; Bound: 32 ; Schema: 0 OpCapability Shader OpMemoryModel Logical GLSL450 @@ -33,34 +33,34 @@ %bool = OpTypeBool %true = OpConstantTrue %bool %_ptr_Function_bool = OpTypePointer Function %bool - %18 = OpConstantNull %bool - %19 = OpTypeFunction %v4float + %17 = OpConstantNull %bool + %18 = OpTypeFunction %v4float %float_1 = OpConstant %float 1 %all_353d6a = OpFunction %void None %9 %12 = OpLabel - %res = OpVariable %_ptr_Function_bool Function %18 + %res = OpVariable %_ptr_Function_bool Function %17 OpStore %res %true OpReturn OpFunctionEnd -%vertex_main_inner = OpFunction %v4float None %19 - %21 = OpLabel - %22 = OpFunctionCall %void %all_353d6a +%vertex_main_inner = OpFunction %v4float None %18 + %20 = OpLabel + %21 = OpFunctionCall %void %all_353d6a OpReturnValue %5 OpFunctionEnd %vertex_main = OpFunction %void None %9 - %24 = OpLabel - %25 = OpFunctionCall %v4float %vertex_main_inner - OpStore %value %25 + %23 = OpLabel + %24 = OpFunctionCall %v4float %vertex_main_inner + OpStore %value %24 OpStore %vertex_point_size %float_1 OpReturn OpFunctionEnd %fragment_main = OpFunction %void None %9 - %28 = OpLabel - %29 = OpFunctionCall %void %all_353d6a + %27 = OpLabel + %28 = OpFunctionCall %void %all_353d6a OpReturn OpFunctionEnd %compute_main = OpFunction %void None %9 - %31 = OpLabel - %32 = OpFunctionCall %void %all_353d6a + %30 = OpLabel + %31 = OpFunctionCall %void %all_353d6a OpReturn OpFunctionEnd diff --git a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.dxc.hlsl b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.dxc.hlsl index d426a74362..eeb115eb12 100644 --- a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.dxc.hlsl +++ b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.dxc.hlsl @@ -1,5 +1,5 @@ void all_986c7b() { - bool res = all((true).xxxx); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.fxc.hlsl b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.fxc.hlsl index d426a74362..eeb115eb12 100644 --- a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.fxc.hlsl +++ b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.fxc.hlsl @@ -1,5 +1,5 @@ void all_986c7b() { - bool res = all((true).xxxx); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.glsl b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.glsl index e8bd17d04b..21a76c4f5f 100644 --- a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.glsl +++ b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.glsl @@ -1,7 +1,7 @@ #version 310 es void all_986c7b() { - bool res = all(bvec4(true)); + bool res = true; } vec4 vertex_main() { @@ -21,7 +21,7 @@ void main() { precision mediump float; void all_986c7b() { - bool res = all(bvec4(true)); + bool res = true; } void fragment_main() { @@ -35,7 +35,7 @@ void main() { #version 310 es void all_986c7b() { - bool res = all(bvec4(true)); + bool res = true; } void compute_main() { diff --git a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.msl b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.msl index 6465c14724..93c5215f39 100644 --- a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.msl +++ b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.msl @@ -2,7 +2,7 @@ using namespace metal; void all_986c7b() { - bool res = all(bool4(true)); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.spvasm b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.spvasm index 1a1b1c594c..f335207fe8 100644 --- a/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.spvasm +++ b/test/tint/builtins/gen/literal/all/986c7b.wgsl.expected.spvasm @@ -1,7 +1,7 @@ ; SPIR-V ; Version: 1.3 ; Generator: Google Tint Compiler; 0 -; Bound: 35 +; Bound: 32 ; Schema: 0 OpCapability Shader OpMemoryModel Logical GLSL450 @@ -31,39 +31,36 @@ %void = OpTypeVoid %9 = OpTypeFunction %void %bool = OpTypeBool - %v4bool = OpTypeVector %bool 4 %true = OpConstantTrue %bool - %17 = OpConstantComposite %v4bool %true %true %true %true %_ptr_Function_bool = OpTypePointer Function %bool - %20 = OpConstantNull %bool - %21 = OpTypeFunction %v4float + %17 = OpConstantNull %bool + %18 = OpTypeFunction %v4float %float_1 = OpConstant %float 1 %all_986c7b = OpFunction %void None %9 %12 = OpLabel - %res = OpVariable %_ptr_Function_bool Function %20 - %13 = OpAll %bool %17 - OpStore %res %13 + %res = OpVariable %_ptr_Function_bool Function %17 + OpStore %res %true OpReturn OpFunctionEnd -%vertex_main_inner = OpFunction %v4float None %21 - %23 = OpLabel - %24 = OpFunctionCall %void %all_986c7b +%vertex_main_inner = OpFunction %v4float None %18 + %20 = OpLabel + %21 = OpFunctionCall %void %all_986c7b OpReturnValue %5 OpFunctionEnd %vertex_main = OpFunction %void None %9 - %26 = OpLabel - %27 = OpFunctionCall %v4float %vertex_main_inner - OpStore %value %27 + %23 = OpLabel + %24 = OpFunctionCall %v4float %vertex_main_inner + OpStore %value %24 OpStore %vertex_point_size %float_1 OpReturn OpFunctionEnd %fragment_main = OpFunction %void None %9 + %27 = OpLabel + %28 = OpFunctionCall %void %all_986c7b + OpReturn + OpFunctionEnd +%compute_main = OpFunction %void None %9 %30 = OpLabel %31 = OpFunctionCall %void %all_986c7b OpReturn OpFunctionEnd -%compute_main = OpFunction %void None %9 - %33 = OpLabel - %34 = OpFunctionCall %void %all_986c7b - OpReturn - OpFunctionEnd diff --git a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.dxc.hlsl b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.dxc.hlsl index 3b9e55e2ab..2ea2bab535 100644 --- a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.dxc.hlsl +++ b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.dxc.hlsl @@ -1,5 +1,5 @@ void all_bd2dba() { - bool res = all((true).xxx); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.fxc.hlsl b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.fxc.hlsl index 3b9e55e2ab..2ea2bab535 100644 --- a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.fxc.hlsl +++ b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.fxc.hlsl @@ -1,5 +1,5 @@ void all_bd2dba() { - bool res = all((true).xxx); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.glsl b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.glsl index d37d4a8c6f..69d19deccd 100644 --- a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.glsl +++ b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.glsl @@ -1,7 +1,7 @@ #version 310 es void all_bd2dba() { - bool res = all(bvec3(true)); + bool res = true; } vec4 vertex_main() { @@ -21,7 +21,7 @@ void main() { precision mediump float; void all_bd2dba() { - bool res = all(bvec3(true)); + bool res = true; } void fragment_main() { @@ -35,7 +35,7 @@ void main() { #version 310 es void all_bd2dba() { - bool res = all(bvec3(true)); + bool res = true; } void compute_main() { diff --git a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.msl b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.msl index 49413efbdc..bb3cd3dd12 100644 --- a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.msl +++ b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.msl @@ -2,7 +2,7 @@ using namespace metal; void all_bd2dba() { - bool res = all(bool3(true)); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.spvasm b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.spvasm index 2d82b87f56..dcc734293e 100644 --- a/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.spvasm +++ b/test/tint/builtins/gen/literal/all/bd2dba.wgsl.expected.spvasm @@ -1,7 +1,7 @@ ; SPIR-V ; Version: 1.3 ; Generator: Google Tint Compiler; 0 -; Bound: 35 +; Bound: 32 ; Schema: 0 OpCapability Shader OpMemoryModel Logical GLSL450 @@ -31,39 +31,36 @@ %void = OpTypeVoid %9 = OpTypeFunction %void %bool = OpTypeBool - %v3bool = OpTypeVector %bool 3 %true = OpConstantTrue %bool - %17 = OpConstantComposite %v3bool %true %true %true %_ptr_Function_bool = OpTypePointer Function %bool - %20 = OpConstantNull %bool - %21 = OpTypeFunction %v4float + %17 = OpConstantNull %bool + %18 = OpTypeFunction %v4float %float_1 = OpConstant %float 1 %all_bd2dba = OpFunction %void None %9 %12 = OpLabel - %res = OpVariable %_ptr_Function_bool Function %20 - %13 = OpAll %bool %17 - OpStore %res %13 + %res = OpVariable %_ptr_Function_bool Function %17 + OpStore %res %true OpReturn OpFunctionEnd -%vertex_main_inner = OpFunction %v4float None %21 - %23 = OpLabel - %24 = OpFunctionCall %void %all_bd2dba +%vertex_main_inner = OpFunction %v4float None %18 + %20 = OpLabel + %21 = OpFunctionCall %void %all_bd2dba OpReturnValue %5 OpFunctionEnd %vertex_main = OpFunction %void None %9 - %26 = OpLabel - %27 = OpFunctionCall %v4float %vertex_main_inner - OpStore %value %27 + %23 = OpLabel + %24 = OpFunctionCall %v4float %vertex_main_inner + OpStore %value %24 OpStore %vertex_point_size %float_1 OpReturn OpFunctionEnd %fragment_main = OpFunction %void None %9 + %27 = OpLabel + %28 = OpFunctionCall %void %all_bd2dba + OpReturn + OpFunctionEnd +%compute_main = OpFunction %void None %9 %30 = OpLabel %31 = OpFunctionCall %void %all_bd2dba OpReturn OpFunctionEnd -%compute_main = OpFunction %void None %9 - %33 = OpLabel - %34 = OpFunctionCall %void %all_bd2dba - OpReturn - OpFunctionEnd diff --git a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.dxc.hlsl b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.dxc.hlsl index edc68272aa..5def9402f0 100644 --- a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.dxc.hlsl +++ b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.dxc.hlsl @@ -1,5 +1,5 @@ void all_f46790() { - bool res = all((true).xx); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.fxc.hlsl b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.fxc.hlsl index edc68272aa..5def9402f0 100644 --- a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.fxc.hlsl +++ b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.fxc.hlsl @@ -1,5 +1,5 @@ void all_f46790() { - bool res = all((true).xx); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.glsl b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.glsl index 2a50d2b25c..d515e55161 100644 --- a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.glsl +++ b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.glsl @@ -1,7 +1,7 @@ #version 310 es void all_f46790() { - bool res = all(bvec2(true)); + bool res = true; } vec4 vertex_main() { @@ -21,7 +21,7 @@ void main() { precision mediump float; void all_f46790() { - bool res = all(bvec2(true)); + bool res = true; } void fragment_main() { @@ -35,7 +35,7 @@ void main() { #version 310 es void all_f46790() { - bool res = all(bvec2(true)); + bool res = true; } void compute_main() { diff --git a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.msl b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.msl index 71d9104c41..449bdae7a5 100644 --- a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.msl +++ b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.msl @@ -2,7 +2,7 @@ using namespace metal; void all_f46790() { - bool res = all(bool2(true)); + bool res = true; } struct tint_symbol { diff --git a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.spvasm b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.spvasm index e9e5bbdd78..4201ee768c 100644 --- a/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.spvasm +++ b/test/tint/builtins/gen/literal/all/f46790.wgsl.expected.spvasm @@ -1,7 +1,7 @@ ; SPIR-V ; Version: 1.3 ; Generator: Google Tint Compiler; 0 -; Bound: 35 +; Bound: 32 ; Schema: 0 OpCapability Shader OpMemoryModel Logical GLSL450 @@ -31,39 +31,36 @@ %void = OpTypeVoid %9 = OpTypeFunction %void %bool = OpTypeBool - %v2bool = OpTypeVector %bool 2 %true = OpConstantTrue %bool - %17 = OpConstantComposite %v2bool %true %true %_ptr_Function_bool = OpTypePointer Function %bool - %20 = OpConstantNull %bool - %21 = OpTypeFunction %v4float + %17 = OpConstantNull %bool + %18 = OpTypeFunction %v4float %float_1 = OpConstant %float 1 %all_f46790 = OpFunction %void None %9 %12 = OpLabel - %res = OpVariable %_ptr_Function_bool Function %20 - %13 = OpAll %bool %17 - OpStore %res %13 + %res = OpVariable %_ptr_Function_bool Function %17 + OpStore %res %true OpReturn OpFunctionEnd -%vertex_main_inner = OpFunction %v4float None %21 - %23 = OpLabel - %24 = OpFunctionCall %void %all_f46790 +%vertex_main_inner = OpFunction %v4float None %18 + %20 = OpLabel + %21 = OpFunctionCall %void %all_f46790 OpReturnValue %5 OpFunctionEnd %vertex_main = OpFunction %void None %9 - %26 = OpLabel - %27 = OpFunctionCall %v4float %vertex_main_inner - OpStore %value %27 + %23 = OpLabel + %24 = OpFunctionCall %v4float %vertex_main_inner + OpStore %value %24 OpStore %vertex_point_size %float_1 OpReturn OpFunctionEnd %fragment_main = OpFunction %void None %9 + %27 = OpLabel + %28 = OpFunctionCall %void %all_f46790 + OpReturn + OpFunctionEnd +%compute_main = OpFunction %void None %9 %30 = OpLabel %31 = OpFunctionCall %void %all_f46790 OpReturn OpFunctionEnd -%compute_main = OpFunction %void None %9 - %33 = OpLabel - %34 = OpFunctionCall %void %all_f46790 - OpReturn - OpFunctionEnd