From 28f77647041426a6470321b048d37471926eda0a Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 2 Nov 2020 19:43:08 +0000 Subject: [PATCH] [spirv-writer] Generate load in Unary Op generation. When generating a unary operation we need to make sure we correctly generate the load otherwise the resulting SPIR-V will be invalid. This CL adds the required GenerateLoadIfNeeded call into the unary generation. Bug: tint292 Change-Id: Ia04314726afdda8f63a78e8e52f996681373db6e Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31620 Commit-Queue: dan sinclair Commit-Queue: David Neto Reviewed-by: David Neto --- src/writer/spirv/builder.cc | 1 + .../spirv/builder_binary_expression_test.cc | 38 +++++++++++++++++++ .../spirv/builder_unary_op_expression_test.cc | 37 ++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 1d7ad02224..5cf43ee020 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -1072,6 +1072,7 @@ uint32_t Builder::GenerateUnaryOpExpression(ast::UnaryOpExpression* expr) { if (val_id == 0) { return 0; } + val_id = GenerateLoadIfNeeded(expr->expr()->result_type(), val_id); auto type_id = GenerateTypeIfNeeded(expr->result_type()); if (type_id == 0) { diff --git a/src/writer/spirv/builder_binary_expression_test.cc b/src/writer/spirv/builder_binary_expression_test.cc index a49241c8d9..6fa9f12496 100644 --- a/src/writer/spirv/builder_binary_expression_test.cc +++ b/src/writer/spirv/builder_binary_expression_test.cc @@ -124,6 +124,44 @@ TEST_P(BinaryArithSignedIntegerTest, Vector) { EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), "%5 = " + param.name + " %1 %4 %4\n"); } +TEST_P(BinaryArithSignedIntegerTest, Scalar_Loads) { + auto param = GetParam(); + + ast::type::I32Type i32; + + ast::Variable var("param", ast::StorageClass::kFunction, &i32); + + auto lhs = std::make_unique("param"); + auto rhs = std::make_unique("param"); + + ast::BinaryExpression expr(param.op, std::move(lhs), std::move(rhs)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + td.RegisterVariableForTesting(&var); + EXPECT_TRUE(td.DetermineResultType(&expr)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_TRUE(b.GenerateFunctionVariable(&var)) << b.error(); + EXPECT_EQ(b.GenerateBinaryExpression(&expr), 7u) << b.error(); + ASSERT_FALSE(b.has_error()) << b.error(); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeInt 32 1 +%2 = OpTypePointer Function %3 +%4 = OpConstantNull %3 +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].variables()), + R"(%1 = OpVariable %2 Function %4 +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), + R"(%5 = OpLoad %3 %1 +%6 = OpLoad %3 %1 +%7 = )" + param.name + + R"( %3 %5 %6 +)"); +} INSTANTIATE_TEST_SUITE_P( BuilderTest, BinaryArithSignedIntegerTest, diff --git a/src/writer/spirv/builder_unary_op_expression_test.cc b/src/writer/spirv/builder_unary_op_expression_test.cc index 2ca7f094e1..db07ff6e59 100644 --- a/src/writer/spirv/builder_unary_op_expression_test.cc +++ b/src/writer/spirv/builder_unary_op_expression_test.cc @@ -23,6 +23,7 @@ #include "src/ast/type/bool_type.h" #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" +#include "src/ast/type/vector_type.h" #include "src/ast/unary_op_expression.h" #include "src/context.h" #include "src/type_determiner.h" @@ -111,6 +112,42 @@ TEST_F(BuilderTest, UnaryOp_Not) { )"); } +TEST_F(BuilderTest, UnaryOp_LoadRequired) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 3); + + ast::Variable var("param", ast::StorageClass::kFunction, &vec); + + ast::UnaryOpExpression expr( + ast::UnaryOp::kNegation, + std::make_unique("param")); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + td.RegisterVariableForTesting(&var); + EXPECT_TRUE(td.DetermineResultType(&expr)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + EXPECT_TRUE(b.GenerateFunctionVariable(&var)) << b.error(); + EXPECT_EQ(b.GenerateUnaryOpExpression(&expr), 6u) << b.error(); + ASSERT_FALSE(b.has_error()) << b.error(); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32 +%3 = OpTypeVector %4 3 +%2 = OpTypePointer Function %3 +%5 = OpConstantNull %3 +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].variables()), + R"(%1 = OpVariable %2 Function %5 +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), + R"(%7 = OpLoad %3 %1 +%6 = OpFNegate %3 %7 +)"); +} + } // namespace } // namespace spirv } // namespace writer