tint: Fix MSL generation of '&' and '|' with booleans

The bitwise-and and bitwise-or binary operators on booleans result in an integer.
Explicitly cast this back to a boolean.

Fixed: tint:1540
Fixed: tint:1541
Change-Id: I395176f291e6080c88b8cff18e14ed6cd1234074
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/90501
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton
2022-05-16 12:02:52 +00:00
committed by Dawn LUCI CQ
parent 35f0fcaac0
commit e6b6777c8e
26 changed files with 316 additions and 0 deletions

View File

@@ -513,6 +513,22 @@ bool GeneratorImpl::EmitBinary(std::ostream& out, const ast::BinaryExpression* e
return true;
}
// Handle '&' and '|' of booleans.
if ((expr->IsAnd() || expr->IsOr()) && lhs_type->Is<sem::Bool>()) {
out << "bool";
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
if (!emit_op()) {
return false;
}
if (!EmitExpression(out, expr->rhs)) {
return false;
}
return true;
}
// Emit as usual
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {

View File

@@ -171,5 +171,31 @@ TEST_F(MslBinaryTest, ModVec3F32) {
EXPECT_EQ(out.str(), "fmod(left, right)");
}
TEST_F(MslBinaryTest, BoolAnd) {
auto* left = Var("left", nullptr, Expr(true));
auto* right = Var("right", nullptr, Expr(false));
auto* expr = create<ast::BinaryExpression>(ast::BinaryOp::kAnd, Expr(left), Expr(right));
WrapInFunction(left, right, expr);
GeneratorImpl& gen = Build();
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "bool(left & right)");
}
TEST_F(MslBinaryTest, BoolOr) {
auto* left = Var("left", nullptr, Expr(true));
auto* right = Var("right", nullptr, Expr(false));
auto* expr = create<ast::BinaryExpression>(ast::BinaryOp::kOr, Expr(left), Expr(right));
WrapInFunction(left, right, expr);
GeneratorImpl& gen = Build();
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "bool(left | right)");
}
} // namespace
} // namespace tint::writer::msl