tint: Add StructMember attributes to sem.

Removes the need to examine AST attributes.

Change-Id: Iaaa6b10fd56baf732057c4c3960c1bfc5bbdeaa6
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/129621
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton
2023-04-27 17:58:25 +00:00
committed by Dawn LUCI CQ
parent 333cea405c
commit 576ba1c493
23 changed files with 223 additions and 211 deletions

View File

@@ -179,7 +179,7 @@ StructMember::StructMember(tint::Source source,
uint32_t offset,
uint32_t align,
uint32_t size,
std::optional<uint32_t> location)
const StructMemberAttributes& attributes)
: source_(source),
name_(name),
type_(type),
@@ -187,7 +187,7 @@ StructMember::StructMember(tint::Source source,
offset_(offset),
align_(align),
size_(size),
location_(location) {}
attributes_(attributes) {}
StructMember::~StructMember() = default;
@@ -195,7 +195,7 @@ StructMember* StructMember::Clone(CloneContext& ctx) const {
auto sym = ctx.dst.st->Register(name_.Name());
auto* ty = type_->Clone(ctx);
return ctx.dst.mgr->Get<StructMember>(source_, sym, ty, index_, offset_, align_, size_,
location_);
attributes_);
}
} // namespace tint::type

View File

@@ -22,6 +22,7 @@
#include <unordered_set>
#include "src/tint/builtin/address_space.h"
#include "src/tint/builtin/interpolation.h"
#include "src/tint/symbol.h"
#include "src/tint/type/node.h"
#include "src/tint/type/type.h"
@@ -163,6 +164,18 @@ class Struct : public utils::Castable<Struct, Type> {
utils::Vector<const Struct*, 2> concrete_types_;
};
/// Attributes that can be applied to the StructMember
struct StructMemberAttributes {
/// The value of a `@location` attribute
std::optional<uint32_t> location;
/// The value of a `@builtin` attribute
std::optional<builtin::BuiltinValue> builtin;
/// The values of a `@interpolate` attribute
std::optional<builtin::Interpolation> interpolation;
/// True if the member was annotated with `@invariant`
bool invariant = false;
};
/// StructMember holds the type information for structure members.
class StructMember : public utils::Castable<StructMember, Node> {
public:
@@ -174,7 +187,7 @@ class StructMember : public utils::Castable<StructMember, Node> {
/// @param offset the byte offset from the base of the structure
/// @param align the byte alignment of the member
/// @param size the byte size of the member
/// @param location the location attribute, if present
/// @param attributes the optional attributes
StructMember(tint::Source source,
Symbol name,
const type::Type* type,
@@ -182,7 +195,7 @@ class StructMember : public utils::Castable<StructMember, Node> {
uint32_t offset,
uint32_t align,
uint32_t size,
std::optional<uint32_t> location);
const StructMemberAttributes& attributes);
/// Destructor
~StructMember() override;
@@ -215,8 +228,8 @@ class StructMember : public utils::Castable<StructMember, Node> {
/// @returns byte size
uint32_t Size() const { return size_; }
/// @returns the location, if set
std::optional<uint32_t> Location() const { return location_; }
/// @returns the optional attributes
const StructMemberAttributes& Attributes() const { return attributes_; }
/// @param ctx the clone context
/// @returns a clone of this struct member
@@ -231,7 +244,7 @@ class StructMember : public utils::Castable<StructMember, Node> {
const uint32_t offset_;
const uint32_t align_;
const uint32_t size_;
const std::optional<uint32_t> location_;
const StructMemberAttributes attributes_;
};
} // namespace tint::type

View File

@@ -101,10 +101,8 @@ TEST_F(TypeStructTest, Location) {
auto* sem = p.Sem().Get(st);
ASSERT_EQ(2u, sem->Members().Length());
EXPECT_TRUE(sem->Members()[0]->Location().has_value());
EXPECT_EQ(sem->Members()[0]->Location().value(), 1u);
EXPECT_FALSE(sem->Members()[1]->Location().has_value());
EXPECT_EQ(sem->Members()[0]->Attributes().location, 1u);
EXPECT_FALSE(sem->Members()[1]->Attributes().location.has_value());
}
TEST_F(TypeStructTest, IsConstructable) {
@@ -207,12 +205,15 @@ TEST_F(TypeStructTest, HasFixedFootprint) {
}
TEST_F(TypeStructTest, Clone) {
type::StructMemberAttributes attrs_location_2;
attrs_location_2.location = 2;
auto* s = create<Struct>(
Source{}, Sym("my_struct"),
utils::Vector{create<StructMember>(Source{}, Sym("b"), create<Vector>(create<F32>(), 3u),
0u, 0u, 16u, 12u, std::optional<uint32_t>{2}),
0u, 0u, 16u, 12u, attrs_location_2),
create<StructMember>(Source{}, Sym("a"), create<I32>(), 1u, 16u, 4u, 4u,
std::optional<uint32_t>())},
type::StructMemberAttributes{})},
4u /* align */, 8u /* size */, 16u /* size_no_padding */);
ProgramID id;

View File

@@ -56,7 +56,7 @@ struct TypeTest : public TestHelper {
/* offset */ 0u,
/* align */ 4u,
/* size */ 4u,
/* location */ std::nullopt),
/* attributes */ type::StructMemberAttributes{}),
},
/* align*/ 4u,
/* size*/ 4u,
@@ -72,7 +72,7 @@ struct TypeTest : public TestHelper {
/* offset */ 0u,
/* align */ 4u,
/* size */ 4u,
/* location */ std::nullopt),
/* attributes */ type::StructMemberAttributes{}),
},
/* align*/ 4u,
/* size*/ 4u,
@@ -88,7 +88,7 @@ struct TypeTest : public TestHelper {
/* offset */ 0u,
/* align */ 4u,
/* size */ 4u,
/* location */ std::nullopt),
/* attributes */ type::StructMemberAttributes{}),
},
/* align*/ 4u,
/* size*/ 4u,