ast: Add atomic type

Bug: tint:892
Change-Id: I00ab3e819a60762ff72b58f8a8255e56adadd71f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54644
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-06-17 15:48:39 +00:00 committed by Ben Clayton
parent 75500a0218
commit 8c7f0da8c8
7 changed files with 177 additions and 0 deletions

View File

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

View File

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

51
src/ast/atomic.cc Normal file
View File

@ -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<Atomic>(src, ty);
}
} // namespace ast
} // namespace tint

60
src/ast/atomic.h Normal file
View File

@ -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 <string>
#include "src/ast/type.h"
namespace tint {
namespace ast {
/// An atomic type.
class Atomic : public Castable<Atomic, Type> {
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<Type*>(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_

46
src/ast/atomic_test.cc Normal file
View File

@ -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<I32>();
auto* p = create<Atomic>(i32);
EXPECT_EQ(p->type(), i32);
}
TEST_F(AstAtomicTest, TypeName) {
auto* i32 = create<I32>();
auto* p = create<Atomic>(i32);
EXPECT_EQ(p->type_name(), "__atomic__i32");
}
TEST_F(AstAtomicTest, FriendlyName) {
auto* i32 = create<I32>();
auto* p = create<Atomic>(i32);
EXPECT_EQ(p->FriendlyName(Symbols()), "atomic<i32>");
}
} // namespace
} // namespace ast
} // namespace tint

View File

@ -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<T>(), 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<ast::Atomic>(source, type);
}
/// @return the atomic to type `T`
template <typename T>
ast::Atomic* atomic() const {
return atomic(Of<T>());
}
/// @param kind the kind of sampler
/// @returns the sampler
ast::Sampler* sampler(ast::SamplerKind kind) const {

View File

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