[msl-writer] Add emission of if statements.

This Cl adds emission of `if`, `else if` and `else` statements to the
MSL backend.

Bug: tint:8
Change-Id: I8c22d70f2afa0a1d86cf475f5c98127504a6dc0e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23840
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-26 21:27:59 +00:00 committed by David Neto
parent b1de84316c
commit 28d4a94bee
6 changed files with 190 additions and 67 deletions

View File

@ -852,7 +852,6 @@ source_set("tint_unittests_wgsl_writer_src") {
"src/writer/wgsl/generator_impl_cast_test.cc", "src/writer/wgsl/generator_impl_cast_test.cc",
"src/writer/wgsl/generator_impl_constructor_test.cc", "src/writer/wgsl/generator_impl_constructor_test.cc",
"src/writer/wgsl/generator_impl_continue_test.cc", "src/writer/wgsl/generator_impl_continue_test.cc",
"src/writer/wgsl/generator_impl_else_test.cc",
"src/writer/wgsl/generator_impl_entry_point_test.cc", "src/writer/wgsl/generator_impl_entry_point_test.cc",
"src/writer/wgsl/generator_impl_fallthrough_test.cc", "src/writer/wgsl/generator_impl_fallthrough_test.cc",
"src/writer/wgsl/generator_impl_function_test.cc", "src/writer/wgsl/generator_impl_function_test.cc",
@ -892,6 +891,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_constructor_test.cc", "src/writer/msl/generator_impl_constructor_test.cc",
"src/writer/msl/generator_impl_function_test.cc", "src/writer/msl/generator_impl_function_test.cc",
"src/writer/msl/generator_impl_identifier_test.cc", "src/writer/msl/generator_impl_identifier_test.cc",
"src/writer/msl/generator_impl_if_test.cc",
"src/writer/msl/generator_impl_return_test.cc", "src/writer/msl/generator_impl_return_test.cc",
"src/writer/msl/generator_impl_test.cc", "src/writer/msl/generator_impl_test.cc",
"src/writer/msl/generator_impl_type_test.cc", "src/writer/msl/generator_impl_type_test.cc",

View File

@ -472,7 +472,6 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_cast_test.cc writer/wgsl/generator_impl_cast_test.cc
writer/wgsl/generator_impl_constructor_test.cc writer/wgsl/generator_impl_constructor_test.cc
writer/wgsl/generator_impl_continue_test.cc writer/wgsl/generator_impl_continue_test.cc
writer/wgsl/generator_impl_else_test.cc
writer/wgsl/generator_impl_entry_point_test.cc writer/wgsl/generator_impl_entry_point_test.cc
writer/wgsl/generator_impl_fallthrough_test.cc writer/wgsl/generator_impl_fallthrough_test.cc
writer/wgsl/generator_impl_function_test.cc writer/wgsl/generator_impl_function_test.cc
@ -500,6 +499,7 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator_impl_constructor_test.cc writer/msl/generator_impl_constructor_test.cc
writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_function_test.cc
writer/msl/generator_impl_identifier_test.cc writer/msl/generator_impl_identifier_test.cc
writer/msl/generator_impl_if_test.cc
writer/msl/generator_impl_return_test.cc writer/msl/generator_impl_return_test.cc
writer/msl/generator_impl_test.cc writer/msl/generator_impl_test.cc
writer/msl/generator_impl_type_test.cc writer/msl/generator_impl_type_test.cc

View File

@ -19,9 +19,11 @@
#include "src/ast/binary_expression.h" #include "src/ast/binary_expression.h"
#include "src/ast/bool_literal.h" #include "src/ast/bool_literal.h"
#include "src/ast/cast_expression.h" #include "src/ast/cast_expression.h"
#include "src/ast/else_statement.h"
#include "src/ast/float_literal.h" #include "src/ast/float_literal.h"
#include "src/ast/function.h" #include "src/ast/function.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/if_statement.h"
#include "src/ast/return_statement.h" #include "src/ast/return_statement.h"
#include "src/ast/sint_literal.h" #include "src/ast/sint_literal.h"
#include "src/ast/type/alias_type.h" #include "src/ast/type/alias_type.h"
@ -345,6 +347,42 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
return true; return true;
} }
bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
if (stmt->HasCondition()) {
out_ << " else if (";
if (!EmitExpression(stmt->condition())) {
return false;
}
out_ << ")";
} else {
out_ << " else";
}
return EmitStatementBlock(stmt->body());
}
bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
make_indent();
out_ << "if (";
if (!EmitExpression(stmt->condition())) {
return false;
}
out_ << ")";
if (!EmitStatementBlock(stmt->body())) {
return false;
}
for (const auto& e : stmt->else_statements()) {
if (!EmitElse(e.get())) {
return false;
}
}
out_ << std::endl;
return true;
}
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
make_indent(); make_indent();
@ -390,6 +428,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) { if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign()); return EmitAssign(stmt->AsAssign());
} }
if (stmt->IsIf()) {
return EmitIf(stmt->AsIf());
}
if (stmt->IsReturn()) { if (stmt->IsReturn()) {
return EmitReturn(stmt->AsReturn()); return EmitReturn(stmt->AsReturn());
} }

View File

@ -60,6 +60,10 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the constructor expression /// @param expr the constructor expression
/// @returns true if the expression was emitted /// @returns true if the expression was emitted
bool EmitConstructor(ast::ConstructorExpression* expr); bool EmitConstructor(ast::ConstructorExpression* expr);
/// Handles generating an else statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitElse(ast::ElseStatement* stmt);
/// Handles generate an Expression /// Handles generate an Expression
/// @param expr the expression /// @param expr the expression
/// @returns true if the expression was emitted /// @returns true if the expression was emitted
@ -72,6 +76,10 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the identifier expression /// @param expr the identifier expression
/// @returns true if the identifeir was emitted /// @returns true if the identifeir was emitted
bool EmitIdentifier(ast::IdentifierExpression* expr); bool EmitIdentifier(ast::IdentifierExpression* expr);
/// Handles an if statement
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitIf(ast::IfStatement* stmt);
/// Handles a literal /// Handles a literal
/// @param lit the literal to emit /// @param lit the literal to emit
/// @returns true if the literal was successfully emitted /// @returns true if the literal was successfully emitted

View File

@ -0,0 +1,139 @@
// 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/else_statement.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/if_statement.h"
#include "src/ast/return_statement.h"
#include "src/writer/msl/generator_impl.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = testing::Test;
TEST_F(MslGeneratorImplTest, Emit_If) {
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
ast::StatementList body;
body.push_back(std::make_unique<ast::ReturnStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
ast::StatementList else_body;
else_body.push_back(std::make_unique<ast::ReturnStatement>());
ast::ElseStatementList elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
std::move(else_body)));
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
ast::StatementList body;
body.push_back(std::make_unique<ast::ReturnStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
return;
} else if (else_cond) {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_IfWithElse) {
ast::StatementList else_body;
else_body.push_back(std::make_unique<ast::ReturnStatement>());
ast::ElseStatementList elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
ast::StatementList body;
body.push_back(std::make_unique<ast::ReturnStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
return;
} else {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
ast::StatementList else_body;
else_body.push_back(std::make_unique<ast::ReturnStatement>());
ast::StatementList else_body_2;
else_body_2.push_back(std::make_unique<ast::ReturnStatement>());
ast::ElseStatementList elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
std::move(else_body)));
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
ast::StatementList body;
body.push_back(std::make_unique<ast::ReturnStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
return;
} else if (else_cond) {
return;
} else {
return;
}
)");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint

View File

@ -1,65 +0,0 @@
// 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 "gtest/gtest.h"
#include "src/ast/else_statement.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/kill_statement.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
using WgslGeneratorImplTest = testing::Test;
TEST_F(WgslGeneratorImplTest, Emit_Else) {
ast::StatementList body;
body.push_back(std::make_unique<ast::KillStatement>());
ast::ElseStatement e(std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( else {
kill;
})");
}
TEST_F(WgslGeneratorImplTest, Emit_ElseWithCondition) {
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
ast::StatementList body;
body.push_back(std::make_unique<ast::KillStatement>());
ast::ElseStatement e(std::move(cond), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( elseif (cond) {
kill;
})");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint