diff --git a/BUILD.gn b/BUILD.gn index f088ced83d..27adc0f7d2 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -552,6 +552,7 @@ source_set("tint_unittests_spv_reader_src") { source_set("tint_unittests_spv_writer_src") { sources = [ "src/writer/spirv/binary_writer_test.cc", + "src/writer/spirv/builder_accessor_expression_test.cc", "src/writer/spirv/builder_assign_test.cc", "src/writer/spirv/builder_binary_expression_test.cc", "src/writer/spirv/builder_call_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 84280dd5ec..b53a9fb7c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -425,6 +425,7 @@ endif() if(${TINT_BUILD_SPV_WRITER}) list(APPEND TINT_TEST_SRCS writer/spirv/binary_writer_test.cc + writer/spirv/builder_accessor_expression_test.cc writer/spirv/builder_assign_test.cc writer/spirv/builder_binary_expression_test.cc writer/spirv/builder_call_test.cc diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 6f8032a99c..4d8e140dc7 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -17,6 +17,7 @@ #include #include "spirv/unified1/spirv.h" +#include "src/ast/array_accessor_expression.h" #include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" #include "src/ast/binding_decoration.h" @@ -32,6 +33,7 @@ #include "src/ast/int_literal.h" #include "src/ast/location_decoration.h" #include "src/ast/loop_statement.h" +#include "src/ast/member_accessor_expression.h" #include "src/ast/return_statement.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/set_decoration.h" @@ -198,6 +200,9 @@ bool Builder::GenerateEntryPoint(ast::EntryPoint* ep) { } uint32_t Builder::GenerateExpression(ast::Expression* expr) { + if (expr->IsArrayAccessor()) { + return GenerateAccessorExpression(expr->AsArrayAccessor()); + } if (expr->IsBinary()) { return GenerateBinaryExpression(expr->AsBinary()); } @@ -436,6 +441,77 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) { return true; } +uint32_t Builder::GenerateAccessorExpression(ast::Expression* expr) { + assert(expr->IsArrayAccessor() || expr->IsMemberAccessor()); + + auto result = result_op(); + auto result_id = result.to_i(); + + std::vector idx_list; + + ast::Expression* source = expr; + while (true) { + if (source->IsArrayAccessor()) { + auto* ary_accessor = source->AsArrayAccessor(); + source = ary_accessor->array(); + + auto idx = GenerateExpression(ary_accessor->idx_expr()); + if (idx == 0) { + return 0; + } + idx_list.insert(idx_list.begin(), Operand::Int(idx)); + + } else if (source->IsMemberAccessor()) { + auto* mem_accessor = source->AsMemberAccessor(); + source = mem_accessor->structure(); + + auto* data_type = mem_accessor->structure()->result_type(); + if (data_type->IsStruct()) { + auto* strct = data_type->AsStruct()->impl(); + auto name = mem_accessor->member()->name(); + + uint32_t i = 0; + for (; i < strct->members().size(); ++i) { + const auto& member = strct->members()[i]; + if (member->name() != name) { + continue; + } + + break; + } + idx_list.insert(idx_list.begin(), Operand::Int(i)); + + } else if (data_type->IsVector()) { + // TODO(dsinclair): Handle swizzle + } else { + error_ = "invalid type for member accessor"; + return 0; + } + } else { + break; + } + } + + auto source_id = GenerateExpression(source); + if (source_id == 0) { + return 0; + } + + // The access chain results in a pointer, so wrap the return type. + ast::type::PointerType ptr(expr->result_type(), ast::StorageClass::kFunction); + auto type_id = GenerateTypeIfNeeded(&ptr); + if (type_id == 0) { + return 0; + } + + idx_list.insert(idx_list.begin(), Operand::Int(source_id)); + idx_list.insert(idx_list.begin(), result); + idx_list.insert(idx_list.begin(), Operand::Int(type_id)); + push_function_inst(spv::Op::OpAccessChain, idx_list); + + return result_id; +} + uint32_t Builder::GenerateIdentifierExpression( ast::IdentifierExpression* expr) { uint32_t val = 0; diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h index 02e3a19cdb..6d0fd69b01 100644 --- a/src/writer/spirv/builder.h +++ b/src/writer/spirv/builder.h @@ -177,6 +177,10 @@ class Builder { /// @param var the variable to generate /// @returns true if the variable is emited. bool GenerateGlobalVariable(ast::Variable* var); + /// Generates an array accessor expression + /// @param expr the expresssion to generate + /// @returns the id of the expression or 0 on failure + uint32_t GenerateAccessorExpression(ast::Expression* expr); /// Generates an identifier expression /// @param expr the expresssion to generate /// @returns the id of the expression or 0 on failure diff --git a/src/writer/spirv/builder_accessor_expression_test.cc b/src/writer/spirv/builder_accessor_expression_test.cc new file mode 100644 index 0000000000..53275f30d5 --- /dev/null +++ b/src/writer/spirv/builder_accessor_expression_test.cc @@ -0,0 +1,131 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "gtest/gtest.h" +#include "src/ast/array_accessor_expression.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/int_literal.h" +#include "src/ast/module.h" +#include "src/ast/scalar_constructor_expression.h" +#include "src/ast/type/array_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/variable.h" +#include "src/context.h" +#include "src/type_determiner.h" +#include "src/writer/spirv/builder.h" +#include "src/writer/spirv/spv_dump.h" + +namespace tint { +namespace writer { +namespace spirv { +namespace { + +using BuilderTest = testing::Test; + +TEST_F(BuilderTest, ArrayAccessor) { + ast::type::I32Type i32; + ast::type::F32Type f32; + ast::type::VectorType vec3(&f32, 3); + + ast::Variable var("ary", ast::StorageClass::kFunction, &vec3); + + auto ary = std::make_unique("ary"); + auto idx_expr = std::make_unique( + std::make_unique(&i32, 1)); + + ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx_expr)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + td.RegisterVariableForTesting(&var); + ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + ASSERT_TRUE(b.GenerateFunctionVariable(&var)) << b.error(); + + EXPECT_EQ(b.GenerateAccessorExpression(&expr), 5u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32 +%3 = OpTypeVector %4 3 +%2 = OpTypePointer Function %3 +%6 = OpTypeInt 32 1 +%7 = OpConstant %6 1 +%8 = OpTypePointer Function %4 +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].variables()), + R"(%1 = OpVariable %2 Function +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), + R"(%5 = OpAccessChain %8 %1 %7 +)"); +} + +TEST_F(BuilderTest, ArrayAccessor_MultiLevel) { + ast::type::I32Type i32; + ast::type::F32Type f32; + ast::type::VectorType vec3(&f32, 3); + ast::type::ArrayType ary4(&vec3, 4); + + // ary = array, 4> + // ary[3][2]; + + ast::Variable var("ary", ast::StorageClass::kFunction, &ary4); + + ast::ArrayAccessorExpression expr( + std::make_unique( + std::make_unique("ary"), + std::make_unique( + std::make_unique(&i32, 3))), + std::make_unique( + std::make_unique(&i32, 2))); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + td.RegisterVariableForTesting(&var); + ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); + + Builder b(&mod); + b.push_function(Function{}); + ASSERT_TRUE(b.GenerateFunctionVariable(&var)) << b.error(); + + EXPECT_EQ(b.GenerateAccessorExpression(&expr), 8u); + + EXPECT_EQ(DumpInstructions(b.types()), R"(%5 = OpTypeFloat 32 +%4 = OpTypeVector %5 3 +%6 = OpTypeInt 32 0 +%7 = OpConstant %6 4 +%3 = OpTypeArray %4 %7 +%2 = OpTypePointer Function %3 +%9 = OpTypeInt 32 1 +%10 = OpConstant %9 2 +%11 = OpConstant %9 3 +%12 = OpTypePointer Function %5 +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].variables()), + R"(%1 = OpVariable %2 Function +)"); + EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), + R"(%8 = OpAccessChain %12 %1 %11 %10 +)"); +} + +} // namespace +} // namespace spirv +} // namespace writer +} // namespace tint