[hlsl-writer] Add alias type support.

This CL adds support for emitting type aliases to the HLSL backend.

Bug: tint:7
Change-Id: Ibd2c2d3bbe6c9a86033e379b4e1cb494259b4df2
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25800
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2020-07-29 18:56:25 +00:00
parent 4a11b5600d
commit 0e48082549
5 changed files with 114 additions and 0 deletions

View File

@ -1041,6 +1041,7 @@ source_set("tint_unittests_msl_writer_src") {
source_set("tint_unittests_hlsl_writer_src") {
sources = [
"src/writer/hlsl/generator_impl_alias_type_test.cc",
"src/writer/hlsl/generator_impl_assign_test.cc",
"src/writer/hlsl/generator_impl_binary_test.cc",
"src/writer/hlsl/generator_impl_break_test.cc",

View File

@ -549,6 +549,7 @@ endif()
if (${TINT_BUILD_HLSL_WRITER})
list(APPEND TINT_TEST_SRCS
writer/hlsl/generator_impl_alias_type_test.cc
writer/hlsl/generator_impl_assign_test.cc
writer/hlsl/generator_impl_binary_test.cc
writer/hlsl/generator_impl_break_test.cc

View File

@ -57,6 +57,16 @@ bool GeneratorImpl::Generate() {
for (const auto& global : module_->global_variables()) {
global_variables_.set(global->name(), global.get());
}
for (auto* const alias : module_->alias_types()) {
if (!EmitAliasType(alias)) {
return false;
}
}
if (!module_->alias_types().empty()) {
out_ << std::endl;
}
return true;
}
@ -81,6 +91,17 @@ std::string GeneratorImpl::current_ep_var_name(VarType type) {
return name;
}
bool GeneratorImpl::EmitAliasType(const ast::type::AliasType* alias) {
make_indent();
out_ << "typedef ";
if (!EmitType(alias->type(), "")) {
return false;
}
out_ << " " << namer_.NameFor(alias->name()) << ";" << std::endl;
return true;
}
bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
make_indent();

View File

@ -38,6 +38,10 @@ class GeneratorImpl : public TextGenerator {
/// @returns true on successful generation; false otherwise
bool Generate();
/// Handles generating an alias
/// @param alias the alias to generate
/// @returns true if the alias was emitted
bool EmitAliasType(const ast::type::AliasType* alias);
/// Handles an assignment statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully

View File

@ -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/module.h"
#include "src/ast/struct.h"
#include "src/ast/struct_member.h"
#include "src/ast/struct_member_decoration.h"
#include "src/ast/struct_member_offset_decoration.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/struct_type.h"
#include "src/writer/hlsl/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslGeneratorImplTest = testing::Test;
TEST_F(HlslGeneratorImplTest, EmitAliasType_F32) {
ast::type::F32Type f32;
ast::type::AliasType alias("a", &f32);
ast::Module m;
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
EXPECT_EQ(g.result(), R"(typedef float a;
)");
}
TEST_F(HlslGeneratorImplTest, EmitAliasType_NameCollision) {
ast::type::F32Type f32;
ast::type::AliasType alias("float", &f32);
ast::Module m;
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
EXPECT_EQ(g.result(), R"(typedef float float_tint_0;
)");
}
TEST_F(HlslGeneratorImplTest, EmitAliasType_Struct) {
ast::type::I32Type i32;
ast::type::F32Type f32;
ast::StructMemberList members;
members.push_back(std::make_unique<ast::StructMember>(
"a", &f32, ast::StructMemberDecorationList{}));
ast::StructMemberDecorationList b_deco;
b_deco.push_back(std::make_unique<ast::StructMemberOffsetDecoration>(4));
members.push_back(
std::make_unique<ast::StructMember>("b", &i32, std::move(b_deco)));
auto str = std::make_unique<ast::Struct>();
str->set_members(std::move(members));
ast::type::StructType s(std::move(str));
ast::type::AliasType alias("a", &s);
ast::Module m;
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
EXPECT_EQ(g.result(), R"(typedef struct {
float a;
int b;
} a;
)");
}
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint