From 8c7f0da8c86925b6641c12ac63800f0c1605b70a Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 17 Jun 2021 15:48:39 +0000 Subject: [PATCH] ast: Add atomic type Bug: tint:892 Change-Id: I00ab3e819a60762ff72b58f8a8255e56adadd71f Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54644 Kokoro: Kokoro Reviewed-by: James Price --- src/BUILD.gn | 2 ++ src/CMakeLists.txt | 3 +++ src/ast/atomic.cc | 51 +++++++++++++++++++++++++++++++++++ src/ast/atomic.h | 60 ++++++++++++++++++++++++++++++++++++++++++ src/ast/atomic_test.cc | 46 ++++++++++++++++++++++++++++++++ src/program_builder.h | 14 ++++++++++ test/BUILD.gn | 1 + 7 files changed, 177 insertions(+) create mode 100644 src/ast/atomic.cc create mode 100644 src/ast/atomic.h create mode 100644 src/ast/atomic_test.cc diff --git a/src/BUILD.gn b/src/BUILD.gn index 6200be87a6..992f5fa730 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -304,6 +304,8 @@ libtint_source_set("libtint_core_all_src") { "ast/assignment_statement.cc", "ast/assignment_statement.h", "ast/ast_type.cc", # TODO(bclayton) - rename to type.cc + "ast/atomic.cc", + "ast/atomic.h", "ast/binary_expression.cc", "ast/binary_expression.h", "ast/binding_decoration.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bde7627fc3..bf41f5a7db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,6 +48,8 @@ set(TINT_LIB_SRCS ast/array.h ast/assignment_statement.cc ast/assignment_statement.h + ast/atomic.cc + ast/atomic.h ast/binary_expression.cc ast/binary_expression.h ast/binding_decoration.cc @@ -515,6 +517,7 @@ if(${TINT_BUILD_TESTS}) ast/array_accessor_expression_test.cc ast/array_test.cc ast/assignment_statement_test.cc + ast/atomic_test.cc ast/binary_expression_test.cc ast/binding_decoration_test.cc ast/bitcast_expression_test.cc diff --git a/src/ast/atomic.cc b/src/ast/atomic.cc new file mode 100644 index 0000000000..9f4c2dc7b8 --- /dev/null +++ b/src/ast/atomic.cc @@ -0,0 +1,51 @@ +// 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/ast/atomic.h" + +#include "src/program_builder.h" + +TINT_INSTANTIATE_TYPEINFO(tint::ast::Atomic); + +namespace tint { +namespace ast { + +Atomic::Atomic(ProgramID program_id, const Source& source, Type* const subtype) + : Base(program_id, source), subtype_(subtype) {} + +std::string Atomic::type_name() const { + std::ostringstream out; + out << "__atomic" << subtype_->type_name(); + return out.str(); +} + +std::string Atomic::FriendlyName(const SymbolTable& symbols) const { + std::ostringstream out; + out << "atomic<" << subtype_->FriendlyName(symbols) << ">"; + return out.str(); +} + +Atomic::Atomic(Atomic&&) = default; + +Atomic::~Atomic() = default; + +Atomic* Atomic::Clone(CloneContext* ctx) const { + // Clone arguments outside of create() call to have deterministic ordering + auto src = ctx->Clone(source()); + auto* ty = ctx->Clone(type()); + return ctx->dst->create(src, ty); +} + +} // namespace ast +} // namespace tint diff --git a/src/ast/atomic.h b/src/ast/atomic.h new file mode 100644 index 0000000000..edd41d6652 --- /dev/null +++ b/src/ast/atomic.h @@ -0,0 +1,60 @@ +// 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. + +#ifndef SRC_AST_ATOMIC_H_ +#define SRC_AST_ATOMIC_H_ + +#include + +#include "src/ast/type.h" + +namespace tint { +namespace ast { + +/// An atomic type. +class Atomic : public Castable { + public: + /// Constructor + /// @param program_id the identifier of the program that owns this node + /// @param source the source of this node + /// @param subtype the pointee type + Atomic(ProgramID program_id, const Source& source, Type* const subtype); + /// Move constructor + Atomic(Atomic&&); + ~Atomic() override; + + /// @returns the pointee type + Type* type() const { return const_cast(subtype_); } + + /// @returns the name for this type + std::string type_name() const override; + + /// @param symbols the program's symbol table + /// @returns the name for this type that closely resembles how it would be + /// declared in WGSL. + std::string FriendlyName(const SymbolTable& symbols) const override; + + /// Clones this type and all transitive types using the `CloneContext` `ctx`. + /// @param ctx the clone context + /// @return the newly cloned type + Atomic* Clone(CloneContext* ctx) const override; + + private: + Type const* const subtype_; +}; + +} // namespace ast +} // namespace tint + +#endif // SRC_AST_ATOMIC_H_ diff --git a/src/ast/atomic_test.cc b/src/ast/atomic_test.cc new file mode 100644 index 0000000000..0d28d5b1ac --- /dev/null +++ b/src/ast/atomic_test.cc @@ -0,0 +1,46 @@ +// 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/ast/atomic.h" + +#include "src/ast/i32.h" +#include "src/ast/test_helper.h" + +namespace tint { +namespace ast { +namespace { + +using AstAtomicTest = TestHelper; + +TEST_F(AstAtomicTest, Creation) { + auto* i32 = create(); + auto* p = create(i32); + EXPECT_EQ(p->type(), i32); +} + +TEST_F(AstAtomicTest, TypeName) { + auto* i32 = create(); + auto* p = create(i32); + EXPECT_EQ(p->type_name(), "__atomic__i32"); +} + +TEST_F(AstAtomicTest, FriendlyName) { + auto* i32 = create(); + auto* p = create(i32); + EXPECT_EQ(p->FriendlyName(Symbols()), "atomic"); +} + +} // namespace +} // namespace ast +} // namespace tint diff --git a/src/program_builder.h b/src/program_builder.h index 2a47d51dfd..c6c58394d7 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -22,6 +22,7 @@ #include "src/ast/array.h" #include "src/ast/array_accessor_expression.h" #include "src/ast/assignment_statement.h" +#include "src/ast/atomic.h" #include "src/ast/binary_expression.h" #include "src/ast/bool.h" #include "src/ast/bool_literal.h" @@ -737,6 +738,19 @@ class ProgramBuilder { return pointer(Of(), storage_class, access); } + /// @param source the Source of the node + /// @param type the type of the atomic + /// @return the atomic to `type` + ast::Atomic* atomic(const Source& source, ast::Type* type) const { + return builder->create(source, type); + } + + /// @return the atomic to type `T` + template + ast::Atomic* atomic() const { + return atomic(Of()); + } + /// @param kind the kind of sampler /// @returns the sampler ast::Sampler* sampler(ast::SamplerKind kind) const { diff --git a/test/BUILD.gn b/test/BUILD.gn index 8ec1c358f6..63fceec982 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -149,6 +149,7 @@ tint_unittests_source_set("tint_unittests_core_src") { "../src/ast/array_accessor_expression_test.cc", "../src/ast/array_test.cc", "../src/ast/assignment_statement_test.cc", + "../src/ast/atomic_test.cc", "../src/ast/binary_expression_test.cc", "../src/ast/binding_decoration_test.cc", "../src/ast/bitcast_expression_test.cc",