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>
This commit is contained in:
Ben Clayton 2021-03-30 20:55:08 +00:00 committed by Commit Bot service account
parent 971774945a
commit 55bc5409c2
4 changed files with 70 additions and 4 deletions

View File

@ -805,6 +805,7 @@ if(${TINT_BUILD_TESTS})
writer/hlsl/generator_impl_type_test.cc writer/hlsl/generator_impl_type_test.cc
writer/hlsl/generator_impl_unary_op_test.cc writer/hlsl/generator_impl_unary_op_test.cc
writer/hlsl/generator_impl_variable_decl_statement_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/namer_test.cc
writer/hlsl/test_helper.cc writer/hlsl/test_helper.cc
writer/hlsl/test_helper.h writer/hlsl/test_helper.h

View File

@ -1781,14 +1781,16 @@ bool GeneratorImpl::EmitEntryPointData(
auto* decl = var->Declaration(); auto* decl = var->Declaration();
auto* unwrapped_type = var->Type()->UnwrapAll(); 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) { if (!emitted_globals.emplace(decl->symbol()).second) {
continue; // Global already emitted 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(), "")) { if (!EmitType(out, var->Type(), "")) {
return false; return false;
} }

View File

@ -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

View File

@ -588,6 +588,7 @@ source_set("tint_unittests_hlsl_writer_src") {
"../src/writer/hlsl/generator_impl_type_test.cc", "../src/writer/hlsl/generator_impl_type_test.cc",
"../src/writer/hlsl/generator_impl_unary_op_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_variable_decl_statement_test.cc",
"../src/writer/hlsl/generator_impl_workgroup_var_test.cc",
"../src/writer/hlsl/namer_test.cc", "../src/writer/hlsl/namer_test.cc",
"../src/writer/hlsl/test_helper.cc", "../src/writer/hlsl/test_helper.cc",
"../src/writer/hlsl/test_helper.h", "../src/writer/hlsl/test_helper.h",