[spirv-writer] Add as casts.

This CL adds the conversion of `as<f32>(b)` to SPIR-V.

Bug: tint:5
Change-Id: If1e04db2fe5520940527f4dcf52a89628b11b518
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23461
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 25a43e8..bc65b11 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -689,6 +689,7 @@
   sources = [
     "src/writer/spirv/binary_writer_test.cc",
     "src/writer/spirv/builder_accessor_expression_test.cc",
+    "src/writer/spirv/builder_as_expression_test.cc",
     "src/writer/spirv/builder_assign_test.cc",
     "src/writer/spirv/builder_binary_expression_test.cc",
     "src/writer/spirv/builder_call_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9fa9927..e046cda 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -416,6 +416,7 @@
   list(APPEND TINT_TEST_SRCS
     writer/spirv/binary_writer_test.cc
     writer/spirv/builder_accessor_expression_test.cc
+    writer/spirv/builder_as_expression_test.cc
     writer/spirv/builder_assign_test.cc
     writer/spirv/builder_binary_expression_test.cc
     writer/spirv/builder_call_test.cc
diff --git a/src/type_determiner.cc b/src/type_determiner.cc
index eda58f2..40cdf98 100644
--- a/src/type_determiner.cc
+++ b/src/type_determiner.cc
@@ -391,6 +391,10 @@
 }
 
 bool TypeDeterminer::DetermineAs(ast::AsExpression* expr) {
+  if (!DetermineResultType(expr->expr())) {
+    return false;
+  }
+
   expr->set_result_type(expr->type());
   return true;
 }
diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc
index df4829a..c0dbf31 100644
--- a/src/writer/spirv/builder.cc
+++ b/src/writer/spirv/builder.cc
@@ -19,6 +19,7 @@
 
 #include "spirv/unified1/spirv.h"
 #include "src/ast/array_accessor_expression.h"
+#include "src/ast/as_expression.h"
 #include "src/ast/assignment_statement.h"
 #include "src/ast/binary_expression.h"
 #include "src/ast/binding_decoration.h"
@@ -339,6 +340,9 @@
   if (expr->IsArrayAccessor()) {
     return GenerateAccessorExpression(expr->AsArrayAccessor());
   }
+  if (expr->IsAs()) {
+    return GenerateAsExpression(expr->AsAs());
+  }
   if (expr->IsBinary()) {
     return GenerateBinaryExpression(expr->AsBinary());
   }
@@ -1354,6 +1358,36 @@
   return result_id;
 }
 
+uint32_t Builder::GenerateAsExpression(ast::AsExpression* as) {
+  auto result = result_op();
+  auto result_id = result.to_i();
+
+  auto result_type_id = GenerateTypeIfNeeded(as->result_type());
+  if (result_type_id == 0) {
+    return 0;
+  }
+
+  auto val_id = GenerateExpression(as->expr());
+  if (val_id == 0) {
+    return 0;
+  }
+  val_id = GenerateLoadIfNeeded(as->expr()->result_type(), val_id);
+
+  // Bitcast does not allow same types, just emit a CopyObject
+  auto* to_type = as->result_type()->UnwrapPtrIfNeeded();
+  auto* from_type = as->expr()->result_type()->UnwrapPtrIfNeeded();
+  if (to_type->type_name() == from_type->type_name()) {
+    push_function_inst(spv::Op::OpCopyObject, {Operand::Int(result_type_id),
+                                               result, Operand::Int(val_id)});
+    return result_id;
+  }
+
+  push_function_inst(spv::Op::OpBitcast, {Operand::Int(result_type_id), result,
+                                          Operand::Int(val_id)});
+
+  return result_id;
+}
+
 uint32_t Builder::GenerateCastExpression(ast::CastExpression* cast) {
   auto result = result_op();
   auto result_id = result.to_i();
diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h
index 61a2e36..094add4 100644
--- a/src/writer/spirv/builder.h
+++ b/src/writer/spirv/builder.h
@@ -283,6 +283,10 @@
   /// @param lit the literal to generate
   /// @returns the ID on success or 0 on failure
   uint32_t GenerateLiteralIfNeeded(ast::Literal* lit);
+  /// Generates an as expression
+  /// @param expr the expression to generate
+  /// @returns the expression ID on success or 0 otherwise
+  uint32_t GenerateAsExpression(ast::AsExpression* expr);
   /// Generates a binary expression
   /// @param expr the expression to generate
   /// @returns the expression ID on success or 0 otherwise
diff --git a/src/writer/spirv/builder_as_expression_test.cc b/src/writer/spirv/builder_as_expression_test.cc
new file mode 100644
index 0000000..c650e53
--- /dev/null
+++ b/src/writer/spirv/builder_as_expression_test.cc
@@ -0,0 +1,87 @@
+// 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/as_expression.h"
+#include "src/ast/float_literal.h"
+#include "src/ast/module.h"
+#include "src/ast/scalar_constructor_expression.h"
+#include "src/ast/type/f32_type.h"
+#include "src/ast/type/u32_type.h"
+#include "src/context.h"
+#include "src/type_determiner.h"
+#include "src/writer/spirv/builder.h"
+#include "src/writer/spirv/spv_dump.h"
+
+namespace tint {
+namespace writer {
+namespace spirv {
+namespace {
+
+using BuilderTest = testing::Test;
+
+TEST_F(BuilderTest, As) {
+  ast::type::U32Type u32;
+  ast::type::F32Type f32;
+
+  ast::AsExpression as(&u32,
+                       std::make_unique<ast::ScalarConstructorExpression>(
+                           std::make_unique<ast::FloatLiteral>(&f32, 2.4)));
+
+  Context ctx;
+  ast::Module mod;
+  TypeDeterminer td(&ctx, &mod);
+  ASSERT_TRUE(td.DetermineResultType(&as)) << td.error();
+
+  Builder b(&mod);
+  b.push_function(Function{});
+  EXPECT_EQ(b.GenerateAsExpression(&as), 1u);
+
+  EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
+%3 = OpTypeFloat 32
+%4 = OpConstant %3 2.4000001
+)");
+  EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
+            R"(%1 = OpBitcast %2 %4
+)");
+}
+
+TEST_F(BuilderTest, As_DuplicateType) {
+  ast::type::F32Type f32;
+
+  ast::AsExpression as(&f32,
+                       std::make_unique<ast::ScalarConstructorExpression>(
+                           std::make_unique<ast::FloatLiteral>(&f32, 2.4)));
+
+  Context ctx;
+  ast::Module mod;
+  TypeDeterminer td(&ctx, &mod);
+  ASSERT_TRUE(td.DetermineResultType(&as)) << td.error();
+
+  Builder b(&mod);
+  b.push_function(Function{});
+  EXPECT_EQ(b.GenerateAsExpression(&as), 1u);
+
+  EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
+%3 = OpConstant %2 2.4000001
+)");
+  EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
+            R"(%1 = OpCopyObject %2 %3
+)");
+}
+
+}  // namespace
+}  // namespace spirv
+}  // namespace writer
+}  // namespace tint