From 02ada29af2cd13c33341ef5639dcc0d01a3a523a Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 4 May 2021 18:13:21 +0000 Subject: [PATCH] Add Transform::CreateASTTypeFor() Reconstructs the AST nodes needed to build the given semantic type. Bug: tint:724 Change-Id: Iadf97a47b68088a6a1eb1e6871fb3a7248676417 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49745 Commit-Queue: Ben Clayton Reviewed-by: Antonio Maiorano --- src/CMakeLists.txt | 1 + src/transform/transform.cc | 44 ++++++++++++ src/transform/transform.h | 8 +++ src/transform/transform_test.cc | 122 ++++++++++++++++++++++++++++++++ test/BUILD.gn | 1 + 5 files changed, 176 insertions(+) create mode 100644 src/transform/transform_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aff37fcf79..064c2f944b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -564,6 +564,7 @@ if(${TINT_BUILD_TESTS}) sem/intrinsic_test.cc symbol_table_test.cc symbol_test.cc + transform/transform_test.cc test_main.cc sem/access_control_type_test.cc sem/alias_type_test.cc diff --git a/src/transform/transform.cc b/src/transform/transform.cc index a06d75ed90..d971ab9a3d 100644 --- a/src/transform/transform.cc +++ b/src/transform/transform.cc @@ -71,5 +71,49 @@ ast::DecorationList Transform::RemoveDecorations( return new_decorations; } +ast::Type* Transform::CreateASTTypeFor(CloneContext* ctx, const sem::Type* ty) { + if (ty->Is()) { + return ctx->dst->create(); + } + if (ty->Is()) { + return ctx->dst->create(); + } + if (ty->Is()) { + return ctx->dst->create(); + } + if (ty->Is()) { + return ctx->dst->create(); + } + if (ty->Is()) { + return ctx->dst->create(); + } + if (auto* m = ty->As()) { + auto* el = CreateASTTypeFor(ctx, m->type()); + return ctx->dst->create(el, m->rows(), m->columns()); + } + if (auto* v = ty->As()) { + auto* el = CreateASTTypeFor(ctx, v->type()); + return ctx->dst->create(el, v->size()); + } + if (auto* a = ty->As()) { + auto* el = CreateASTTypeFor(ctx, a->type()); + auto decos = ctx->Clone(a->decorations()); + return ctx->dst->create(el, a->size(), std::move(decos)); + } + if (auto* ac = ty->As()) { + auto* el = CreateASTTypeFor(ctx, ac->type()); + return ctx->dst->create(ac->access_control(), el); + } + if (auto* a = ty->As()) { + return ctx->dst->create(ctx->Clone(a->symbol())); + } + if (auto* s = ty->As()) { + return ctx->dst->create(ctx->Clone(s->impl()->name())); + } + TINT_UNREACHABLE(ctx->dst->Diagnostics()) + << "Unhandled type: " << ty->TypeInfo().name; + return nullptr; +} + } // namespace transform } // namespace tint diff --git a/src/transform/transform.h b/src/transform/transform.h index 95ba235cb3..3506af106a 100644 --- a/src/transform/transform.h +++ b/src/transform/transform.h @@ -179,6 +179,14 @@ class Transform { CloneContext* ctx, const ast::DecorationList& in, std::function should_remove); + + /// CreateASTTypeFor constructs new ast::Type nodes that reconstructs the + /// semantic type `ty`. + /// @param ctx the clone context + /// @param ty the semantic type to reconstruct + /// @returns a ast::Type that when resolved, will produce the semantic type + /// `ty`. + static ast::Type* CreateASTTypeFor(CloneContext* ctx, const sem::Type* ty); }; } // namespace transform diff --git a/src/transform/transform_test.cc b/src/transform/transform_test.cc new file mode 100644 index 0000000000..f287f57dd7 --- /dev/null +++ b/src/transform/transform_test.cc @@ -0,0 +1,122 @@ +// 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 "src/transform/transform.h" +#include "src/clone_context.h" +#include "src/program_builder.h" + +#include "gtest/gtest.h" + +namespace tint { +namespace transform { +namespace { + +// Inherit from Transform so we have access to protected methods +struct CreateASTTypeForTest : public testing::Test, public Transform { + Output Run(const Program*, const DataMap&) override { return {}; } + + ast::Type* create( + std::function create_sem_type) { + ProgramBuilder sem_type_builder; + auto* sem_type = create_sem_type(sem_type_builder); + Program program(std::move(sem_type_builder)); + CloneContext ctx(&ast_type_builder, &program, false); + return CreateASTTypeFor(&ctx, sem_type); + } + + ProgramBuilder ast_type_builder; +}; + +TEST_F(CreateASTTypeForTest, Basic) { + EXPECT_TRUE(create([](ProgramBuilder& b) { + return b.create(); + })->Is()); + EXPECT_TRUE(create([](ProgramBuilder& b) { + return b.create(); + })->Is()); + EXPECT_TRUE(create([](ProgramBuilder& b) { + return b.create(); + })->Is()); + EXPECT_TRUE(create([](ProgramBuilder& b) { + return b.create(); + })->Is()); + EXPECT_TRUE(create([](ProgramBuilder& b) { + return b.create(); + })->Is()); +} + +TEST_F(CreateASTTypeForTest, Matrix) { + auto* mat = create([](ProgramBuilder& b) { + return b.create(b.create(), 2, 3); + }); + ASSERT_TRUE(mat->Is()); + ASSERT_TRUE(mat->As()->type()->Is()); + ASSERT_EQ(mat->As()->columns(), 3u); + ASSERT_EQ(mat->As()->rows(), 2u); +} + +TEST_F(CreateASTTypeForTest, Vector) { + auto* vec = create([](ProgramBuilder& b) { + return b.create(b.create(), 2); + }); + ASSERT_TRUE(vec->Is()); + ASSERT_TRUE(vec->As()->type()->Is()); + ASSERT_EQ(vec->As()->size(), 2u); +} + +TEST_F(CreateASTTypeForTest, Array) { + auto* arr = create([](ProgramBuilder& b) { + return b.create(b.create(), 4, + ast::DecorationList{ + b.create(32u), + }); + }); + ASSERT_TRUE(arr->Is()); + ASSERT_TRUE(arr->As()->type()->Is()); + ASSERT_EQ(arr->As()->size(), 4u); + ASSERT_EQ(arr->As()->decorations().size(), 1u); + ASSERT_TRUE( + arr->As()->decorations()[0]->Is()); + ASSERT_EQ(arr->As() + ->decorations()[0] + ->As() + ->stride(), + 32u); +} + +TEST_F(CreateASTTypeForTest, AccessControl) { + auto* ac = create([](ProgramBuilder& b) { + auto str = b.Structure("S", {}, {}); + return b.create(ast::AccessControl::kReadOnly, str); + }); + ASSERT_TRUE(ac->Is()); + EXPECT_EQ(ac->As()->access_control(), + ast::AccessControl::kReadOnly); + EXPECT_TRUE(ac->As()->type()->Is()); +} + +TEST_F(CreateASTTypeForTest, Struct) { + auto* str = create([](ProgramBuilder& b) { + auto* impl = b.Structure("S", {}, {}).ast; + return b.create(const_cast(impl)); + }); + ASSERT_TRUE(str->Is()); + EXPECT_EQ( + ast_type_builder.Symbols().NameFor(str->As()->name()), + "S"); +} + +} // namespace +} // namespace transform +} // namespace tint diff --git a/test/BUILD.gn b/test/BUILD.gn index e999f6c54d..c7bed0386a 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -297,6 +297,7 @@ tint_unittests_source_set("tint_unittests_core_src") { "../src/transform/first_index_offset_test.cc", "../src/transform/renamer_test.cc", "../src/transform/single_entry_point_test.cc", + "../src/transform/transform_test.cc", "../src/transform/vertex_pulling_test.cc", "../src/utils/command_test.cc", "../src/utils/get_or_create_test.cc",