[spirv-writer] Load array accessors if required.

If an array accessor index is a variable it needs to be loaded before we
can use it to access the array. This CL adds the call to generate the
load for the index.

Bug: tint:5
Change-Id: I74143d2eb746727e857218d54126e1f5fd785bdf
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20780
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-05-01 19:05:21 +00:00 committed by dan sinclair
parent cf2af15d94
commit cf3517812c
2 changed files with 49 additions and 0 deletions

View File

@ -510,6 +510,7 @@ bool Builder::GenerateArrayAccessor(ast::ArrayAccessorExpression* expr,
if (idx_id == 0) {
return 0;
}
idx_id = GenerateLoadIfNeeded(expr->idx_expr()->result_type(), idx_id);
// If the source is a pointer we access chain into it.
if (info->source_type->IsPointer()) {

View File

@ -85,6 +85,54 @@ TEST_F(BuilderTest, ArrayAccessor) {
)");
}
TEST_F(BuilderTest, Accessor_Array_LoadIndex) {
ast::type::I32Type i32;
ast::type::F32Type f32;
ast::type::VectorType vec3(&f32, 3);
// ary : vec3<f32>;
// idx : i32;
// ary[idx] -> ptr<f32>
ast::Variable var("ary", ast::StorageClass::kFunction, &vec3);
ast::Variable idx("idx", ast::StorageClass::kFunction, &i32);
auto ary = std::make_unique<ast::IdentifierExpression>("ary");
auto idx_expr = std::make_unique<ast::IdentifierExpression>("idx");
ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx_expr));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(&var);
td.RegisterVariableForTesting(&idx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateFunctionVariable(&var)) << b.error();
ASSERT_TRUE(b.GenerateFunctionVariable(&idx)) << b.error();
EXPECT_EQ(b.GenerateAccessorExpression(&expr), 10u);
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32
%3 = OpTypeVector %4 3
%2 = OpTypePointer Function %3
%7 = OpTypeInt 32 1
%6 = OpTypePointer Function %7
%9 = OpTypePointer Function %4
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].variables()),
R"(%1 = OpVariable %2 Function
%5 = OpVariable %6 Function
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%8 = OpLoad %7 %5
%10 = OpAccessChain %9 %1 %8
)");
}
TEST_F(BuilderTest, ArrayAccessor_Dynamic) {
ast::type::I32Type i32;
ast::type::F32Type f32;