[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 <dsinclair@chromium.org>
Commit-Queue: David Neto <dneto@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-11-02 19:43:08 +00:00 committed by Commit Bot service account
parent 2e6cc992c8
commit 28f7764704
3 changed files with 76 additions and 0 deletions

View File

@ -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) {

View File

@ -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<ast::IdentifierExpression>("param");
auto rhs = std::make_unique<ast::IdentifierExpression>("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,

View File

@ -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<ast::IdentifierExpression>("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