writer/hlsl: Emit workgroup variables

Fixed: tint:688
Change-Id: I3f0f3fd24734421f8d36a94c501aed9881ec10b0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/46379
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 599c114..826a4b3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -805,6 +805,7 @@
       writer/hlsl/generator_impl_type_test.cc
       writer/hlsl/generator_impl_unary_op_test.cc
       writer/hlsl/generator_impl_variable_decl_statement_test.cc
+      writer/hlsl/generator_impl_workgroup_var_test.cc
       writer/hlsl/namer_test.cc
       writer/hlsl/test_helper.cc
       writer/hlsl/test_helper.h
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index e0d6f95..c019ca7 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -1781,14 +1781,16 @@
       auto* decl = var->Declaration();
 
       auto* unwrapped_type = var->Type()->UnwrapAll();
-      if (!unwrapped_type->IsAnyOf<type::Texture, type::Sampler>()) {
-        continue;  // Not interested in this type
-      }
-
       if (!emitted_globals.emplace(decl->symbol()).second) {
         continue;  // Global already emitted
       }
 
+      if (var->StorageClass() == ast::StorageClass::kWorkgroup) {
+        out << "groupshared ";
+      } else if (!unwrapped_type->IsAnyOf<type::Texture, type::Sampler>()) {
+        continue;  // Not interested in this type
+      }
+
       if (!EmitType(out, var->Type(), "")) {
         return false;
       }
diff --git a/src/writer/hlsl/generator_impl_workgroup_var_test.cc b/src/writer/hlsl/generator_impl_workgroup_var_test.cc
new file mode 100644
index 0000000..268fe0a
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_workgroup_var_test.cc
@@ -0,0 +1,62 @@
+// Copyright 2021 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 "gmock/gmock.h"
+#include "src/ast/constant_id_decoration.h"
+#include "src/ast/stage_decoration.h"
+#include "src/writer/hlsl/test_helper.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+using ::testing::HasSubstr;
+
+using HlslGeneratorImplTest_WorkgroupVar = TestHelper;
+
+TEST_F(HlslGeneratorImplTest_WorkgroupVar, Basic) {
+  Global("wg", ty.f32(), ast::StorageClass::kWorkgroup);
+
+  Func("main", {}, ty.void_(),
+       {create<ast::AssignmentStatement>(Expr("wg"), Expr(1.2f))},
+       {create<ast::StageDecoration>(ast::PipelineStage::kCompute)});
+  GeneratorImpl& gen = Build();
+
+  ASSERT_TRUE(gen.Generate(out)) << gen.error();
+  EXPECT_THAT(result(), HasSubstr("groupshared float wg;\n"));
+
+  Validate();
+}
+
+TEST_F(HlslGeneratorImplTest_WorkgroupVar, Aliased) {
+  auto* alias = ty.alias("F32", ty.f32());
+  AST().AddConstructedType(alias);
+
+  Global("wg", alias, ast::StorageClass::kWorkgroup);
+
+  Func("main", {}, ty.void_(),
+       {create<ast::AssignmentStatement>(Expr("wg"), Expr(1.2f))},
+       {create<ast::StageDecoration>(ast::PipelineStage::kCompute)});
+  GeneratorImpl& gen = Build();
+
+  ASSERT_TRUE(gen.Generate(out)) << gen.error();
+  EXPECT_THAT(result(), HasSubstr("groupshared F32 wg;\n"));
+
+  Validate();
+}
+
+}  // namespace
+}  // namespace hlsl
+}  // namespace writer
+}  // namespace tint
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 7bc5663..9ec26a1 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -588,6 +588,7 @@
     "../src/writer/hlsl/generator_impl_type_test.cc",
     "../src/writer/hlsl/generator_impl_unary_op_test.cc",
     "../src/writer/hlsl/generator_impl_variable_decl_statement_test.cc",
+    "../src/writer/hlsl/generator_impl_workgroup_var_test.cc",
     "../src/writer/hlsl/namer_test.cc",
     "../src/writer/hlsl/test_helper.cc",
     "../src/writer/hlsl/test_helper.h",