[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 <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 4643fca..7c13039 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -897,6 +897,7 @@
     "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 5a64fba..82f26d0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -505,6 +505,7 @@
     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 04eba00..466688e 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 @@
   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 @@
   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 77391b5..d74ff04 100644
--- a/src/writer/msl/generator_impl.h
+++ b/src/writer/msl/generator_impl.h
@@ -104,6 +104,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
@@ -147,6 +151,7 @@
 
  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 0000000..cbbbfb1
--- /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 <memory>
+
+#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::KillStatement>());
+
+  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::KillStatement>());
+
+  ast::StatementList continuing;
+  continuing.push_back(std::make_unique<ast::ReturnStatement>());
+
+  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::KillStatement>());
+
+  ast::StatementList continuing;
+  continuing.push_back(std::make_unique<ast::ReturnStatement>());
+
+  auto inner = std::make_unique<ast::LoopStatement>(std::move(body),
+                                                    std::move(continuing));
+
+  body.push_back(std::move(inner));
+
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+  continuing.push_back(std::make_unique<ast::AssignmentStatement>(
+      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<ast::Variable>(
+      "lhs", ast::StorageClass::kFunction, &f32);
+  var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>(
+      std::make_unique<ast::FloatLiteral>(&f32, 2.4)));
+
+  ast::StatementList body;
+  body.push_back(std::make_unique<ast::VariableDeclStatement>(std::move(var)));
+  body.push_back(std::make_unique<ast::VariableDeclStatement>(
+      std::make_unique<ast::Variable>("other", ast::StorageClass::kFunction,
+                                      &f32)));
+
+  ast::StatementList continuing;
+  auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+  auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+  continuing.push_back(std::make_unique<ast::AssignmentStatement>(
+      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