Add optional access to ptr<>

This also completes the work to resolve the access controls for each
storage type.

Fixed: tint:846
Change-Id: Iab24057ec14620a2978ec63c4a91ba12d1bc6e9b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53381
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton
2021-06-04 22:17:37 +00:00
committed by Tint LUCI CQ
parent 3db1820f0b
commit 1858854f7e
248 changed files with 7332 additions and 2027 deletions

View File

@@ -67,7 +67,7 @@ TEST_F(AstAliasTest, UnwrapAll_TwiceAliasPointerTwiceAlias) {
auto* u32 = create<U32>();
auto* a = create<Alias>(Sym("a_type"), u32);
auto* aa = create<Alias>(Sym("aa_type"), a);
auto* paa = create<Pointer>(aa, StorageClass::kUniform);
auto* paa = create<Pointer>(aa, StorageClass::kUniform, Access::kUndefined);
auto* apaa = create<Alias>(Sym("paa_type"), paa);
auto* aapaa = create<Alias>(Sym("aapaa_type"), apaa);

View File

@@ -24,14 +24,19 @@ namespace ast {
Pointer::Pointer(ProgramID program_id,
const Source& source,
Type* const subtype,
ast::StorageClass storage_class)
ast::StorageClass storage_class,
ast::Access access)
: Base(program_id, source),
subtype_(subtype),
storage_class_(storage_class) {}
storage_class_(storage_class),
access_(access) {}
std::string Pointer::type_name() const {
std::ostringstream out;
out << "__ptr_" << storage_class_ << subtype_->type_name();
if (access_ != ast::Access::kUndefined) {
out << "_" << access_;
}
return out.str();
}
@@ -41,7 +46,11 @@ std::string Pointer::FriendlyName(const SymbolTable& symbols) const {
if (storage_class_ != ast::StorageClass::kNone) {
out << storage_class_ << ", ";
}
out << subtype_->FriendlyName(symbols) << ">";
out << subtype_->FriendlyName(symbols);
if (access_ != ast::Access::kUndefined) {
out << ", " << access_;
}
out << ">";
return out.str();
}
@@ -53,7 +62,7 @@ Pointer* Pointer::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<Pointer>(src, ty, storage_class_);
return ctx->dst->create<Pointer>(src, ty, storage_class_, access_);
}
} // namespace ast

View File

@@ -17,6 +17,7 @@
#include <string>
#include "src/ast/access.h"
#include "src/ast/storage_class.h"
#include "src/ast/type.h"
@@ -31,19 +32,25 @@ class Pointer : public Castable<Pointer, Type> {
/// @param source the source of this node
/// @param subtype the pointee type
/// @param storage_class the storage class of the pointer
/// @param access the access control of the pointer
Pointer(ProgramID program_id,
const Source& source,
Type* const subtype,
ast::StorageClass storage_class);
ast::StorageClass storage_class,
ast::Access access);
/// Move constructor
Pointer(Pointer&&);
~Pointer() override;
/// @returns the pointee type
Type* type() const { return const_cast<Type*>(subtype_); }
/// @returns the storage class of the pointer
ast::StorageClass storage_class() const { return storage_class_; }
/// @returns the access control of the pointer
ast::Access access() const { return access_; }
/// @returns the name for this type
std::string type_name() const override;
@@ -60,6 +67,7 @@ class Pointer : public Castable<Pointer, Type> {
private:
Type const* const subtype_;
ast::StorageClass const storage_class_;
ast::Access const access_;
};
} // namespace ast

View File

@@ -25,27 +25,37 @@ using AstPointerTest = TestHelper;
TEST_F(AstPointerTest, Creation) {
auto* i32 = create<I32>();
auto* p = create<Pointer>(i32, ast::StorageClass::kStorage);
auto* p = create<Pointer>(i32, ast::StorageClass::kStorage, Access::kRead);
EXPECT_EQ(p->type(), i32);
EXPECT_EQ(p->storage_class(), ast::StorageClass::kStorage);
EXPECT_EQ(p->access(), Access::kRead);
}
TEST_F(AstPointerTest, TypeName) {
auto* i32 = create<I32>();
auto* p = create<Pointer>(i32, ast::StorageClass::kWorkgroup);
auto* p =
create<Pointer>(i32, ast::StorageClass::kWorkgroup, Access::kUndefined);
EXPECT_EQ(p->type_name(), "__ptr_workgroup__i32");
}
TEST_F(AstPointerTest, FriendlyNameWithStorageClass) {
TEST_F(AstPointerTest, TypeNameWithAccess) {
auto* i32 = create<I32>();
auto* p = create<Pointer>(i32, ast::StorageClass::kWorkgroup);
auto* p = create<Pointer>(i32, ast::StorageClass::kWorkgroup, Access::kRead);
EXPECT_EQ(p->type_name(), "__ptr_workgroup__i32_read");
}
TEST_F(AstPointerTest, FriendlyName) {
auto* i32 = create<I32>();
auto* p =
create<Pointer>(i32, ast::StorageClass::kWorkgroup, Access::kUndefined);
EXPECT_EQ(p->FriendlyName(Symbols()), "ptr<workgroup, i32>");
}
TEST_F(AstPointerTest, FriendlyNameWithoutStorageClass) {
TEST_F(AstPointerTest, FriendlyNameWithAccess) {
auto* i32 = create<I32>();
auto* p = create<Pointer>(i32, ast::StorageClass::kNone);
EXPECT_EQ(p->FriendlyName(Symbols()), "ptr<i32>");
auto* p =
create<Pointer>(i32, ast::StorageClass::kStorage, Access::kReadWrite);
EXPECT_EQ(p->FriendlyName(Symbols()), "ptr<storage, i32, read_write>");
}
} // namespace