Emit loop statement.
This CL emits the loop statement from the WGSL generator.
Bug: tint:4
Change-Id: I45be066ed306a71df053f7e61e6201a10a3d1056
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17283
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e7ae9cf..31cf5d2 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -419,6 +419,7 @@
writer/wgsl/generator_impl_import_test.cc
writer/wgsl/generator_impl_initializer_test.cc
writer/wgsl/generator_impl_kill_test.cc
+ writer/wgsl/generator_impl_loop_test.cc
writer/wgsl/generator_impl_member_accessor_test.cc
writer/wgsl/generator_impl_nop_test.cc
writer/wgsl/generator_impl_regardless_test.cc
diff --git a/src/ast/loop_statement.h b/src/ast/loop_statement.h
index 917eaa6..73e8b7f 100644
--- a/src/ast/loop_statement.h
+++ b/src/ast/loop_statement.h
@@ -62,6 +62,8 @@
const std::vector<std::unique_ptr<Statement>>& continuing() const {
return continuing_;
}
+ /// @returns true if there are continuing statements in the loop
+ bool has_continuing() const { return !continuing_.empty(); }
/// @returns true if this is a loop statement
bool IsLoop() const override { return true; }
diff --git a/src/ast/loop_statement_test.cc b/src/ast/loop_statement_test.cc
index d49636f..09455da 100644
--- a/src/ast/loop_statement_test.cc
+++ b/src/ast/loop_statement_test.cc
@@ -60,6 +60,25 @@
EXPECT_TRUE(l.IsLoop());
}
+TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) {
+ std::vector<std::unique_ptr<Statement>> body;
+ body.push_back(std::make_unique<KillStatement>());
+
+ LoopStatement l(std::move(body), {});
+ EXPECT_FALSE(l.has_continuing());
+}
+
+TEST_F(LoopStatementTest, HasContinuing_WithContinuing) {
+ std::vector<std::unique_ptr<Statement>> body;
+ body.push_back(std::make_unique<KillStatement>());
+
+ std::vector<std::unique_ptr<Statement>> continuing;
+ continuing.push_back(std::make_unique<NopStatement>());
+
+ LoopStatement l(std::move(body), std::move(continuing));
+ EXPECT_TRUE(l.has_continuing());
+}
+
TEST_F(LoopStatementTest, IsValid) {
std::vector<std::unique_ptr<Statement>> body;
body.push_back(std::make_unique<KillStatement>());
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index 4ad4db9..0837337 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -35,6 +35,7 @@
#include "src/ast/initializer_expression.h"
#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/regardless_statement.h"
#include "src/ast/relational_expression.h"
@@ -671,6 +672,9 @@
if (stmt->IsKill()) {
return EmitKill(stmt->AsKill());
}
+ if (stmt->IsLoop()) {
+ return EmitLoop(stmt->AsLoop());
+ }
if (stmt->IsNop()) {
return EmitNop(stmt->AsNop());
}
@@ -803,6 +807,43 @@
return true;
}
+bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
+ make_indent();
+
+ out_ << "loop {" << std::endl;
+ increment_indent();
+
+ for (const auto& s : stmt->body()) {
+ if (!EmitStatement(s.get())) {
+ return false;
+ }
+ }
+
+ if (stmt->has_continuing()) {
+ out_ << std::endl;
+
+ make_indent();
+ out_ << "continuing {" << std::endl;
+
+ increment_indent();
+ for (const auto& s : stmt->continuing()) {
+ if (!EmitStatement(s.get())) {
+ return false;
+ }
+ }
+
+ decrement_indent();
+ make_indent();
+ out_ << "}" << std::endl;
+ }
+
+ decrement_indent();
+ make_indent();
+ out_ << "}" << std::endl;
+
+ return true;
+}
+
bool GeneratorImpl::EmitNop(ast::NopStatement*) {
make_indent();
out_ << "nop;" << std::endl;
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h
index 62425f8..9b1b42b 100644
--- a/src/writer/wgsl/generator_impl.h
+++ b/src/writer/wgsl/generator_impl.h
@@ -138,6 +138,10 @@
/// @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
diff --git a/src/writer/wgsl/generator_impl_loop_test.cc b/src/writer/wgsl/generator_impl_loop_test.cc
new file mode 100644
index 0000000..beb2a5c
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_loop_test.cc
@@ -0,0 +1,73 @@
+// 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/kill_statement.h"
+#include "src/ast/loop_statement.h"
+#include "src/ast/nop_statement.h"
+#include "src/writer/wgsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace wgsl {
+namespace {
+
+using GeneratorImplTest = testing::Test;
+
+TEST_F(GeneratorImplTest, Emit_Loop) {
+ std::vector<std::unique_ptr<ast::Statement>> body;
+ body.push_back(std::make_unique<ast::KillStatement>());
+
+ ast::LoopStatement l(std::move(body), {});
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&l)) << g.error();
+ EXPECT_EQ(g.result(), R"( loop {
+ kill;
+ }
+)");
+}
+
+TEST_F(GeneratorImplTest, Emit_LoopWithContinuing) {
+ std::vector<std::unique_ptr<ast::Statement>> body;
+ body.push_back(std::make_unique<ast::KillStatement>());
+
+ std::vector<std::unique_ptr<ast::Statement>> continuing;
+ continuing.push_back(std::make_unique<ast::NopStatement>());
+
+ 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"( loop {
+ kill;
+
+ continuing {
+ nop;
+ }
+ }
+)");
+}
+
+} // namespace
+} // namespace wgsl
+} // namespace writer
+} // namespace tint