From e88c0f6f8a8dd91e843b345d4785d546d4ed7faf Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 30 Jun 2020 17:40:28 +0000 Subject: [PATCH] [msl-writer] Emit loop statements. This CL adds the code to convert a loop/continuing statement into MSL. Bug: tint:8 Change-Id: I9fa595908c6d834d9543b583c8baf8c19f8cae6c Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24122 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/msl/generator_impl.cc | 57 +++++++ src/writer/msl/generator_impl.h | 5 + src/writer/msl/generator_impl_loop_test.cc | 173 +++++++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 src/writer/msl/generator_impl_loop_test.cc diff --git a/BUILD.gn b/BUILD.gn index 4643fca889..7c1303989a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -897,6 +897,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_identifier_test.cc", "src/writer/msl/generator_impl_if_test.cc", "src/writer/msl/generator_impl_kill_test.cc", + "src/writer/msl/generator_impl_loop_test.cc", "src/writer/msl/generator_impl_member_accessor_test.cc", "src/writer/msl/generator_impl_return_test.cc", "src/writer/msl/generator_impl_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5a64fba7c2..82f26d0c9b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -505,6 +505,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_identifier_test.cc writer/msl/generator_impl_if_test.cc writer/msl/generator_impl_kill_test.cc + writer/msl/generator_impl_loop_test.cc writer/msl/generator_impl_member_accessor_test.cc writer/msl/generator_impl_return_test.cc writer/msl/generator_impl_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 04eba00e63..466688edfd 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -27,6 +27,7 @@ #include "src/ast/function.h" #include "src/ast/identifier_expression.h" #include "src/ast/if_statement.h" +#include "src/ast/loop_statement.h" #include "src/ast/member_accessor_expression.h" #include "src/ast/return_statement.h" #include "src/ast/sint_literal.h" @@ -407,6 +408,59 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) { return true; } +bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { + loop_emission_counter_++; + + if (stmt->has_continuing()) { + make_indent(); + + // Continuing variables get their own scope. + out_ << "{" << std::endl; + increment_indent(); + + make_indent(); + out_ << "bool tint_msl_is_first_" << loop_emission_counter_ << " = true;" + << std::endl; + } + + make_indent(); + out_ << "for(;;) {" << std::endl; + increment_indent(); + + if (stmt->has_continuing()) { + make_indent(); + out_ << "if (!tint_msl_is_first_" << loop_emission_counter_ << ")"; + + if (!EmitStatementBlockAndNewline(stmt->continuing())) { + return false; + } + + make_indent(); + out_ << "tint_msl_is_first_" << loop_emission_counter_ << " = false;" + << std::endl; + out_ << std::endl; + } + + for (const auto& s : stmt->body()) { + if (!EmitStatement(s.get())) { + return false; + } + } + + decrement_indent(); + make_indent(); + out_ << "}" << std::endl; + + // Close the scope for any continuing variables. + if (stmt->has_continuing()) { + decrement_indent(); + make_indent(); + out_ << "}" << std::endl; + } + + return true; +} + bool GeneratorImpl::EmitKill(ast::KillStatement*) { make_indent(); // TODO(dsinclair): Verify this is correct when the kill semantics are defined @@ -519,6 +573,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsKill()) { return EmitKill(stmt->AsKill()); } + if (stmt->IsLoop()) { + return EmitLoop(stmt->AsLoop()); + } if (stmt->IsReturn()) { return EmitReturn(stmt->AsReturn()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 77391b5cfa..d74ff04345 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -104,6 +104,10 @@ class GeneratorImpl : public TextGenerator { /// @param lit the literal to emit /// @returns true if the literal was successfully emitted bool EmitLiteral(ast::Literal* lit); + /// Handles a loop statement + /// @param stmt the statement to emit + /// @returns true if the statement was emtited + bool EmitLoop(ast::LoopStatement* stmt); /// Handles a member accessor expression /// @param expr the member accessor expression /// @returns true if the member accessor was emitted @@ -147,6 +151,7 @@ class GeneratorImpl : public TextGenerator { private: const ast::Module* module_ = nullptr; + uint32_t loop_emission_counter_ = 0; }; } // namespace msl diff --git a/src/writer/msl/generator_impl_loop_test.cc b/src/writer/msl/generator_impl_loop_test.cc new file mode 100644 index 0000000000..cbbbfb17cf --- /dev/null +++ b/src/writer/msl/generator_impl_loop_test.cc @@ -0,0 +1,173 @@ +// 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 + +#include "gtest/gtest.h" +#include "src/ast/assignment_statement.h" +#include "src/ast/float_literal.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/kill_statement.h" +#include "src/ast/loop_statement.h" +#include "src/ast/return_statement.h" +#include "src/ast/type/f32_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_Loop) { + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::LoopStatement l(std::move(body), {}); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&l)) << g.error(); + EXPECT_EQ(g.result(), R"( for(;;) { + discard_fragment(); + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_LoopWithContinuing) { + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::StatementList continuing; + continuing.push_back(std::make_unique()); + + ast::LoopStatement l(std::move(body), std::move(continuing)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&l)) << g.error(); + EXPECT_EQ(g.result(), R"( { + bool tint_msl_is_first_1 = true; + for(;;) { + if (!tint_msl_is_first_1) { + return; + } + tint_msl_is_first_1 = false; + + discard_fragment(); + } + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) { + ast::type::F32Type f32; + + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::StatementList continuing; + continuing.push_back(std::make_unique()); + + auto inner = std::make_unique(std::move(body), + std::move(continuing)); + + body.push_back(std::move(inner)); + + auto lhs = std::make_unique("lhs"); + auto rhs = std::make_unique("rhs"); + continuing.push_back(std::make_unique( + std::move(lhs), std::move(rhs))); + + ast::LoopStatement outer(std::move(body), std::move(continuing)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&outer)) << g.error(); + EXPECT_EQ(g.result(), R"( { + bool tint_msl_is_first_1 = true; + for(;;) { + if (!tint_msl_is_first_1) { + lhs = rhs; + } + tint_msl_is_first_1 = false; + + { + bool tint_msl_is_first_2 = true; + for(;;) { + if (!tint_msl_is_first_2) { + return; + } + tint_msl_is_first_2 = false; + + discard_fragment(); + } + } + } + } +)"); +} + +// TODO(dsinclair): Make this work when we have variable declarations. +TEST_F(MslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) { + ast::type::F32Type f32; + + auto var = std::make_unique( + "lhs", ast::StorageClass::kFunction, &f32); + var->set_constructor(std::make_unique( + std::make_unique(&f32, 2.4))); + + ast::StatementList body; + body.push_back(std::make_unique(std::move(var))); + body.push_back(std::make_unique( + std::make_unique("other", ast::StorageClass::kFunction, + &f32))); + + ast::StatementList continuing; + auto lhs = std::make_unique("lhs"); + auto rhs = std::make_unique("rhs"); + continuing.push_back(std::make_unique( + std::move(lhs), std::move(rhs))); + + ast::LoopStatement outer(std::move(body), std::move(continuing)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&outer)) << g.error(); + EXPECT_EQ(g.result(), R"( { + float lhs; + bool tint_msl_is_first_1 = true; + for(;;) { + if (!tint_msl_is_first_1) { + lhs = rhs; + } + tint_msl_is_first_1 = false; + + lhs = 2.4; + float other; + } + } +)"); +} +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint