[msl-writer] Add variable declarations.

This CL adds variable declarations to the MSL backend.

Bug: tint:7
Change-Id: Icf63ad44a217213e5036eb76429e25a3031822eb
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24540
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-08 20:52:46 +00:00 committed by dan sinclair
parent d0f7381090
commit 45101598a1
7 changed files with 222 additions and 8 deletions

View File

@ -907,6 +907,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_test.cc",
"src/writer/msl/generator_impl_type_test.cc",
"src/writer/msl/generator_impl_unary_op_test.cc",
"src/writer/msl/generator_impl_variable_decl_statement_test.cc",
"src/writer/msl/namer_test.cc",
]

View File

@ -515,6 +515,7 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator_impl_test.cc
writer/msl/generator_impl_type_test.cc
writer/msl/generator_impl_unary_op_test.cc
writer/msl/generator_impl_variable_decl_statement_test.cc
writer/msl/namer_test.cc
)
endif()

View File

@ -46,6 +46,7 @@
#include "src/ast/type/void_type.h"
#include "src/ast/uint_literal.h"
#include "src/ast/unary_op_expression.h"
#include "src/ast/variable_decl_statement.h"
namespace tint {
namespace writer {
@ -314,22 +315,48 @@ bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
out_ << "(";
bool first = true;
for (const auto& e : expr->values()) {
if (!first) {
out_ << ", ";
}
first = false;
if (!EmitExpression(e.get())) {
// If the type constructor is empty then we need to construct with the zero
// value for all components.
if (expr->values().empty()) {
if (!EmitZeroValue(expr->type())) {
return false;
}
} else {
bool first = true;
for (const auto& e : expr->values()) {
if (!first) {
out_ << ", ";
}
first = false;
if (!EmitExpression(e.get())) {
return false;
}
}
}
out_ << ")";
return true;
}
bool GeneratorImpl::EmitZeroValue(ast::type::Type* type) {
if (type->IsBool()) {
out_ << "false";
} else if (type->IsF32()) {
out_ << "0.0f";
} else if (type->IsI32()) {
out_ << "0";
} else if (type->IsU32()) {
out_ << "0u";
} else if (type->IsVector()) {
return EmitZeroValue(type->AsVector()->type());
} else {
error_ = "Invalid type for zero emission: " + type->type_name();
return false;
}
return true;
}
bool GeneratorImpl::EmitScalarConstructor(
ast::ScalarConstructorExpression* expr) {
return EmitLiteral(expr->literal());
@ -647,6 +674,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsSwitch()) {
return EmitSwitch(stmt->AsSwitch());
}
if (stmt->IsVariableDecl()) {
return EmitVariable(stmt->AsVariableDecl()->variable());
}
error_ = "unknown statement type: " + stmt->str();
return false;
@ -781,6 +811,36 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) {
return true;
}
bool GeneratorImpl::EmitVariable(ast::Variable* var) {
make_indent();
// TODO(dsinclair): Handle variable decorations
if (var->IsDecorated()) {
error_ = "Variable decorations are not handled yet";
return false;
}
if (var->is_const()) {
out_ << "const ";
}
if (!EmitType(var->type(), var->name())) {
return false;
}
if (!var->type()->IsArray()) {
out_ << " " << var->name();
}
if (var->constructor() != nullptr) {
out_ << " = ";
if (!EmitExpression(var->constructor())) {
return false;
}
}
out_ << ";" << std::endl;
return true;
}
} // namespace msl
} // namespace writer
} // namespace tint

View File

@ -157,6 +157,14 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the expression to emit
/// @returns true if the expression was emitted
bool EmitUnaryOp(ast::UnaryOpExpression* expr);
/// Handles generating a variable
/// @param var the variable to generate
/// @returns true if the variable was emitted
bool EmitVariable(ast::Variable* var);
/// Emits the zero value for the given type
/// @param type the type to emit the value for
/// @returns true if the zero value was successfully emitted.
bool EmitZeroValue(ast::type::Type* type);
private:
Namer namer_;

View File

@ -157,6 +157,18 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Vec) {
EXPECT_EQ(g.result(), "float3(1.00000000f, 2.00000000f, 3.00000000f)");
}
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Vec_Empty) {
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList values;
ast::TypeConstructorExpression expr(&vec, std::move(values));
GeneratorImpl g;
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "float3(0.0f)");
}
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) {
ast::type::F32Type f32;
ast::type::MatrixType mat(&f32, 3, 2);

View File

@ -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 <memory>
#include <vector>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/type/array_type.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/vector_type.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decl_statement.h"
#include "src/writer/msl/generator_impl.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = testing::Test;
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement) {
ast::type::F32Type f32;
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
ast::VariableDeclStatement stmt(std::move(var));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a;\n");
}
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
ast::type::F32Type f32;
ast::type::ArrayType ary(&f32, 5);
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &ary);
ast::VariableDeclStatement stmt(std::move(var));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a[5];\n");
}
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
ast::type::F32Type f32;
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
ast::VariableDeclStatement stmt(std::move(var));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a;\n");
}
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
ast::type::F32Type f32;
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &f32);
ast::VariableDeclStatement stmt(std::move(var));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a;\n");
}
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
auto ident = std::make_unique<ast::IdentifierExpression>("initializer");
ast::type::F32Type f32;
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
var->set_constructor(std::move(ident));
ast::VariableDeclStatement stmt(std::move(var));
GeneratorImpl g;
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), R"(float a = initializer;
)");
}
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList values;
auto zero_vec =
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(values));
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &vec);
var->set_constructor(std::move(zero_vec));
ast::VariableDeclStatement stmt(std::move(var));
GeneratorImpl g;
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), R"(float3 a = float3(0.0f);
)");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint

View File

@ -16,6 +16,7 @@
#include <vector>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decl_statement.h"