[spirv-reader] Emit module-scope variables

Bug: tint:3
Change-Id: I54f35022c86d6c8df635bf86cc7bf39327674f6e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18460
Reviewed-by: dan sinclair <dsinclair@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ba455ac..7b96830 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -327,6 +327,7 @@
     reader/spirv/parser_impl_entry_point_test.cc
     reader/spirv/parser_impl_get_decorations_test.cc
     reader/spirv/parser_impl_import_test.cc
+    reader/spirv/parser_impl_module_var_test.cc
     reader/spirv/parser_impl_named_types_test.cc
     reader/spirv/parser_impl_user_name_test.cc
     reader/spirv/parser_impl_test.cc
diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc
index efa2d3e..c41b005 100644
--- a/src/reader/spirv/parser_impl.cc
+++ b/src/reader/spirv/parser_impl.cc
@@ -14,6 +14,7 @@
 
 #include "src/reader/spirv/parser_impl.h"
 
+#include <cassert>
 #include <cstring>
 #include <limits>
 #include <memory>
@@ -44,6 +45,8 @@
 #include "src/ast/type/u32_type.h"
 #include "src/ast/type/vector_type.h"
 #include "src/ast/type/void_type.h"
+#include "src/ast/variable.h"
+#include "src/ast/variable_decl_statement.h"
 #include "src/type_manager.h"
 
 namespace tint {
@@ -290,6 +293,9 @@
   if (!EmitAliasTypes()) {
     return false;
   }
+  if (!EmitModuleScopeVariables()) {
+    return false;
+  }
   // TODO(dneto): fill in the rest
   return true;
 }
@@ -604,6 +610,41 @@
   return success_;
 }
 
+bool ParserImpl::EmitModuleScopeVariables() {
+  if (!success_) {
+    return false;
+  }
+  for (const auto& type_or_value : module_->types_values()) {
+    if (type_or_value.opcode() != SpvOpVariable) {
+      continue;
+    }
+    const auto& var = type_or_value;
+    const auto spirv_storage_class = var.GetSingleWordInOperand(0);
+    auto ast_storage_class = enum_converter_.ToStorageClass(
+        static_cast<SpvStorageClass>(spirv_storage_class));
+    if (!success_) {
+      return false;
+    }
+    auto* ast_type = id_to_type_[var.type_id()];
+    if (ast_type == nullptr) {
+      return Fail() << "internal error: failed to register Tint AST type for "
+                       "SPIR-V type with ID: "
+                    << var.type_id();
+    }
+    auto* ast_store_type = ast_type->AsPointer()->type();
+    if (!namer_.HasName(var.result_id())) {
+      namer_.SuggestSanitizedName(var.result_id(),
+                                  "x_" + std::to_string(var.result_id()));
+    }
+    auto ast_var = std::make_unique<ast::Variable>(
+        namer_.GetName(var.result_id()), ast_storage_class, ast_store_type);
+    // TODO(dneto): decorated variables
+    // TODO(dneto): initializers (a.k.a. constructor expression)
+    ast_module_.AddGlobalVariable(std::move(ast_var));
+  }
+  return success_;
+}
+
 }  // namespace spirv
 }  // namespace reader
 }  // namespace tint
diff --git a/src/reader/spirv/parser_impl.h b/src/reader/spirv/parser_impl.h
index 4ddcf19..2c6a098 100644
--- a/src/reader/spirv/parser_impl.h
+++ b/src/reader/spirv/parser_impl.h
@@ -173,6 +173,11 @@
   /// @returns true if parser is still successful.
   bool EmitAliasTypes();
 
+  /// Emits module-scope variables.
+  /// This is a no-op if the parser has already failed.
+  /// @returns true if parser is still successful.
+  bool EmitModuleScopeVariables();
+
  private:
   /// Converts a specific SPIR-V type to a Tint type. Integer case
   ast::type::Type* ConvertType(const spvtools::opt::analysis::Integer* int_ty);
diff --git a/src/reader/spirv/parser_impl_module_var_test.cc b/src/reader/spirv/parser_impl_module_var_test.cc
new file mode 100644
index 0000000..1bee187
--- /dev/null
+++ b/src/reader/spirv/parser_impl_module_var_test.cc
@@ -0,0 +1,127 @@
+// 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 <string>
+
+#include "gmock/gmock.h"
+#include "src/reader/spirv/parser_impl.h"
+#include "src/reader/spirv/parser_impl_test_helper.h"
+#include "src/reader/spirv/spirv_tools_helpers_test.h"
+
+namespace tint {
+namespace reader {
+namespace spirv {
+namespace {
+
+using ::testing::HasSubstr;
+using ::testing::Not;
+
+TEST_F(SpvParserTest, ModuleScopeVar_NoVar) {
+  auto p = parser(test::Assemble(""));
+  EXPECT_TRUE(p->BuildAndParseInternalModule());
+  EXPECT_TRUE(p->error().empty());
+  const auto module_ast = p->module().to_str();
+  EXPECT_THAT(module_ast, Not(HasSubstr("Variable")));
+}
+
+TEST_F(SpvParserTest, ModuleScopeVar_BadStorageClass) {
+  auto p = parser(test::Assemble(R"(
+    %float = OpTypeFloat 32
+    %ptr = OpTypePointer CrossWorkgroup %float
+    %52 = OpVariable %ptr CrossWorkgroup
+  )"));
+  EXPECT_TRUE(p->BuildInternalModule());
+  // Normally we should run ParserImpl::RegisterTypes before emitting
+  // variables. But defensive coding in EmitModuleScopeVariables lets
+  // us catch this error.
+  EXPECT_FALSE(p->EmitModuleScopeVariables()) << p->error();
+  EXPECT_THAT(p->error(), HasSubstr("unknown SPIR-V storage class: 5"));
+}
+
+TEST_F(SpvParserTest, ModuleScopeVar_BadPointerType) {
+  auto p = parser(test::Assemble(R"(
+    %float = OpTypeFloat 32
+    %fn_ty = OpTypeFunction %float
+    %fn_ptr_ty = OpTypePointer Private %fn_ty
+    %52 = OpVariable %ptr Private
+  )"));
+  EXPECT_TRUE(p->BuildInternalModule());
+  // Normally we should run ParserImpl::RegisterTypes before emitting
+  // variables. But defensive coding in EmitModuleScopeVariables lets
+  // us catch this error.
+  EXPECT_FALSE(p->EmitModuleScopeVariables());
+  EXPECT_THAT(p->error(), HasSubstr("internal error: failed to register Tint "
+                                    "AST type for SPIR-V type with ID: 4"));
+}
+
+TEST_F(SpvParserTest, ModuleScopeVar_AnonWorkgroupVar) {
+  auto p = parser(test::Assemble(R"(
+    %float = OpTypeFloat 32
+    %ptr = OpTypePointer Workgroup %float
+    %52 = OpVariable %ptr Workgroup
+  )"));
+
+  EXPECT_TRUE(p->BuildAndParseInternalModule());
+  EXPECT_TRUE(p->error().empty());
+  const auto module_str = p->module().to_str();
+  EXPECT_THAT(module_str, HasSubstr(R"(
+  Variable{
+    x_52
+    workgroup
+    __f32
+  })"));
+}
+
+TEST_F(SpvParserTest, ModuleScopeVar_NamedWorkgroupVar) {
+  auto p = parser(test::Assemble(R"(
+    OpName %52 "the_counter"
+    %float = OpTypeFloat 32
+    %ptr = OpTypePointer Workgroup %float
+    %52 = OpVariable %ptr Workgroup
+  )"));
+
+  EXPECT_TRUE(p->BuildAndParseInternalModule());
+  EXPECT_TRUE(p->error().empty());
+  const auto module_str = p->module().to_str();
+  EXPECT_THAT(module_str, HasSubstr(R"(
+  Variable{
+    the_counter
+    workgroup
+    __f32
+  })"));
+}
+
+TEST_F(SpvParserTest, ModuleScopeVar_PrivateVar) {
+  auto p = parser(test::Assemble(R"(
+    OpName %52 "my_own_private_idaho"
+    %float = OpTypeFloat 32
+    %ptr = OpTypePointer Private %float
+    %52 = OpVariable %ptr Private
+  )"));
+
+  EXPECT_TRUE(p->BuildAndParseInternalModule());
+  EXPECT_TRUE(p->error().empty());
+  const auto module_str = p->module().to_str();
+  EXPECT_THAT(module_str, HasSubstr(R"(
+  Variable{
+    my_own_private_idaho
+    private
+    __f32
+  })"));
+}
+
+}  // namespace
+}  // namespace spirv
+}  // namespace reader
+}  // namespace tint