From 0d0d0b290d305b0be1471bdb2d93e7cd7261273a Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 14 Apr 2020 15:03:24 +0000 Subject: [PATCH] [spirv-writer] Add binary not equal. This CL adds support for the not equal comparison. Bug: tint:5 Change-Id: Idb91a02b546e49d67d6dc8570c754e611643a1f2 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/19403 Reviewed-by: Ryan Harrison --- src/writer/spirv/builder.cc | 30 ++++++++----------- .../spirv/builder_binary_expression_test.cc | 18 ++++++----- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index a3e634deab..37cdbdaef3 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -559,31 +559,25 @@ uint32_t Builder::GenerateBinaryExpression(ast::BinaryExpression* expr) { auto result = result_op(); auto result_id = result.to_i(); - auto lhs_type = expr->lhs()->result_type(); - - auto expr_type = expr->result_type(); - auto type_id = GenerateTypeIfNeeded(expr_type); + auto type_id = GenerateTypeIfNeeded(expr->result_type()); if (type_id == 0) { return 0; } + // Handle int and float and the vectors of those types. Other types + // should have been rejected by validation. + auto lhs_type = expr->lhs()->result_type(); + bool lhs_is_float_or_vec = + lhs_type->IsF32() || + (lhs_type->IsVector() && lhs_type->AsVector()->type()->IsF32()); + spv::Op op = spv::Op::OpNop; if (expr->IsAdd()) { - // This handles int and float and the vectors of those types. Other types - // should have been rejected by validation. - op = spv::Op::OpIAdd; - if (expr_type->IsF32() || - (expr_type->IsVector() && expr_type->AsVector()->type()->IsF32())) { - op = spv::Op::OpFAdd; - } + op = lhs_is_float_or_vec ? spv::Op::OpFAdd : spv::Op::OpIAdd; } else if (expr->IsEqual()) { - // This handles int and float and the vectors of those types. Other types - // should have been rejected by validation. - op = spv::Op::OpIEqual; - if (lhs_type->IsF32() || - (lhs_type->IsVector() && lhs_type->AsVector()->type()->IsF32())) { - op = spv::Op::OpFOrdEqual; - } + op = lhs_is_float_or_vec ? spv::Op::OpFOrdEqual : spv::Op::OpIEqual; + } else if (expr->IsNotEqual()) { + op = lhs_is_float_or_vec ? spv::Op::OpFOrdNotEqual : spv::Op::OpINotEqual; } else { return 0; } diff --git a/src/writer/spirv/builder_binary_expression_test.cc b/src/writer/spirv/builder_binary_expression_test.cc index 6b0848f3be..41fa16ca56 100644 --- a/src/writer/spirv/builder_binary_expression_test.cc +++ b/src/writer/spirv/builder_binary_expression_test.cc @@ -276,10 +276,11 @@ TEST_P(BinaryCompareIntegerTest, Vector) { EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), "%5 = " + param.name + " %6 %4 %4\n"); } -INSTANTIATE_TEST_SUITE_P(BuilderTest, - BinaryCompareIntegerTest, - testing::Values(BinaryData{ast::BinaryOp::kEqual, - "OpIEqual"})); +INSTANTIATE_TEST_SUITE_P( + BuilderTest, + BinaryCompareIntegerTest, + testing::Values(BinaryData{ast::BinaryOp::kEqual, "OpIEqual"}, + BinaryData{ast::BinaryOp::kNotEqual, "OpINotEqual"})); using BinaryCompareFloatTest = testing::TestWithParam; TEST_P(BinaryCompareFloatTest, Scalar) { @@ -357,10 +358,11 @@ TEST_P(BinaryCompareFloatTest, Vector) { EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), "%5 = " + param.name + " %6 %4 %4\n"); } -INSTANTIATE_TEST_SUITE_P(BuilderTest, - BinaryCompareFloatTest, - testing::Values(BinaryData{ast::BinaryOp::kEqual, - "OpFOrdEqual"})); +INSTANTIATE_TEST_SUITE_P( + BuilderTest, + BinaryCompareFloatTest, + testing::Values(BinaryData{ast::BinaryOp::kEqual, "OpFOrdEqual"}, + BinaryData{ast::BinaryOp::kNotEqual, "OpFOrdNotEqual"})); } // namespace } // namespace spirv