[ir][spirv-writer] Implement any builtin
This maps to OpAny, but is a passthrough when the operand is a scalar.
Bug: tint:1906
Change-Id: If0ad1851888c62b8a94800ab9b6f2dfbd679d114
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/140002
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
diff --git a/src/tint/writer/spirv/ir/generator_impl_ir.cc b/src/tint/writer/spirv/ir/generator_impl_ir.cc
index bd6ecec..61613da 100644
--- a/src/tint/writer/spirv/ir/generator_impl_ir.cc
+++ b/src/tint/writer/spirv/ir/generator_impl_ir.cc
@@ -1089,6 +1089,12 @@
values_.Add(builtin->Result(), Value(builtin->Args()[0]));
return;
}
+ if (builtin->Func() == builtin::Function::kAny &&
+ builtin->Args()[0]->Type()->Is<type::Bool>()) {
+ // any() is a passthrough for a scalar argument.
+ values_.Add(builtin->Result(), Value(builtin->Args()[0]));
+ return;
+ }
auto id = Value(builtin);
@@ -1117,6 +1123,9 @@
glsl_ext_inst(GLSLstd450SAbs);
}
break;
+ case builtin::Function::kAny:
+ op = spv::Op::OpAny;
+ break;
case builtin::Function::kAcos:
glsl_ext_inst(GLSLstd450Acos);
break;
diff --git a/src/tint/writer/spirv/ir/generator_impl_ir_builtin_test.cc b/src/tint/writer/spirv/ir/generator_impl_ir_builtin_test.cc
index 84fb93a..0ab0a39 100644
--- a/src/tint/writer/spirv/ir/generator_impl_ir_builtin_test.cc
+++ b/src/tint/writer/spirv/ir/generator_impl_ir_builtin_test.cc
@@ -131,6 +131,34 @@
)");
}
+// Test that any of an scalar just folds away.
+TEST_F(SpvGeneratorImplTest, Builtin_Any_Scalar) {
+ auto* arg = b.FunctionParam("arg", ty.bool_());
+ auto* func = b.Function("foo", ty.bool_());
+ func->SetParams({arg});
+ b.With(func->Block(), [&] {
+ auto* result = b.Call(ty.bool_(), builtin::Function::kAny, arg);
+ b.Return(func, result);
+ });
+
+ ASSERT_TRUE(Generate()) << Error() << output_;
+ EXPECT_INST("OpReturnValue %arg");
+}
+
+TEST_F(SpvGeneratorImplTest, Builtin_Any_Vector) {
+ auto* arg = b.FunctionParam("arg", ty.vec4<bool>());
+ auto* func = b.Function("foo", ty.bool_());
+ func->SetParams({arg});
+ b.With(func->Block(), [&] {
+ auto* result = b.Call(ty.bool_(), builtin::Function::kAny, arg);
+ b.Return(func, result);
+ mod.SetName(result, "result");
+ });
+
+ ASSERT_TRUE(Generate()) << Error() << output_;
+ EXPECT_INST("%result = OpAny %bool %arg");
+}
+
TEST_F(SpvGeneratorImplTest, Builtin_Length_vec4f) {
auto* arg = b.FunctionParam("arg", ty.vec4<f32>());
auto* func = b.Function("foo", ty.f32());