Update StructMember{Offset,Size}Attribute to expressions.

This CL updates the StructMember Offset and Size attributes to
store expressions instead of uint32_t values.

Bug: tint:1633
Change-Id: I771b64fbd27a398ffbcb3f8cc2cbb7fa2606983e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/101640
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Auto-Submit: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair
2022-09-09 14:49:09 +00:00
committed by Dawn LUCI CQ
parent f0209f58ea
commit 93df967003
27 changed files with 157 additions and 95 deletions

View File

@@ -25,8 +25,8 @@ namespace tint::ast {
StructMemberOffsetAttribute::StructMemberOffsetAttribute(ProgramID pid,
NodeID nid,
const Source& src,
uint32_t o)
: Base(pid, nid, src), offset(o) {}
const ast::Expression* exp)
: Base(pid, nid, src), expr(exp) {}
StructMemberOffsetAttribute::~StructMemberOffsetAttribute() = default;
@@ -37,7 +37,8 @@ std::string StructMemberOffsetAttribute::Name() const {
const StructMemberOffsetAttribute* StructMemberOffsetAttribute::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source);
return ctx->dst->create<StructMemberOffsetAttribute>(src, offset);
auto expr_ = ctx->Clone(expr);
return ctx->dst->create<StructMemberOffsetAttribute>(src, expr_);
}
} // namespace tint::ast

View File

@@ -18,6 +18,7 @@
#include <string>
#include "src/tint/ast/attribute.h"
#include "src/tint/ast/expression.h"
namespace tint::ast {
@@ -37,8 +38,11 @@ class StructMemberOffsetAttribute final : public Castable<StructMemberOffsetAttr
/// @param pid the identifier of the program that owns this node
/// @param nid the unique node identifier
/// @param src the source of this node
/// @param offset the offset value
StructMemberOffsetAttribute(ProgramID pid, NodeID nid, const Source& src, uint32_t offset);
/// @param expr the offset expression
StructMemberOffsetAttribute(ProgramID pid,
NodeID nid,
const Source& src,
const ast::Expression* expr);
~StructMemberOffsetAttribute() override;
/// @returns the WGSL name for the attribute
@@ -50,8 +54,8 @@ class StructMemberOffsetAttribute final : public Castable<StructMemberOffsetAttr
/// @return the newly cloned node
const StructMemberOffsetAttribute* Clone(CloneContext* ctx) const override;
/// The offset value
const uint32_t offset;
/// The offset expression
const ast::Expression* const expr;
};
} // namespace tint::ast

View File

@@ -17,11 +17,13 @@
namespace tint::ast {
namespace {
using namespace tint::number_suffixes; // NOLINT
using StructMemberOffsetAttributeTest = TestHelper;
TEST_F(StructMemberOffsetAttributeTest, Creation) {
auto* d = create<StructMemberOffsetAttribute>(2u);
EXPECT_EQ(2u, d->offset);
auto* d = MemberOffset(2_u);
ASSERT_TRUE(d->expr->Is<ast::IntLiteralExpression>());
EXPECT_EQ(2u, d->expr->As<ast::IntLiteralExpression>()->value);
}
} // namespace

View File

@@ -26,8 +26,8 @@ namespace tint::ast {
StructMemberSizeAttribute::StructMemberSizeAttribute(ProgramID pid,
NodeID nid,
const Source& src,
uint32_t sz)
: Base(pid, nid, src), size(sz) {}
const ast::Expression* exp)
: Base(pid, nid, src), expr(exp) {}
StructMemberSizeAttribute::~StructMemberSizeAttribute() = default;
@@ -38,7 +38,8 @@ std::string StructMemberSizeAttribute::Name() const {
const StructMemberSizeAttribute* StructMemberSizeAttribute::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source);
return ctx->dst->create<StructMemberSizeAttribute>(src, size);
auto expr_ = ctx->Clone(expr);
return ctx->dst->create<StructMemberSizeAttribute>(src, expr_);
}
} // namespace tint::ast

View File

@@ -19,6 +19,7 @@
#include <string>
#include "src/tint/ast/attribute.h"
#include "src/tint/ast/expression.h"
namespace tint::ast {
@@ -29,8 +30,11 @@ class StructMemberSizeAttribute final : public Castable<StructMemberSizeAttribut
/// @param pid the identifier of the program that owns this node
/// @param nid the unique node identifier
/// @param src the source of this node
/// @param size the size value
StructMemberSizeAttribute(ProgramID pid, NodeID nid, const Source& src, uint32_t size);
/// @param expr the size expression
StructMemberSizeAttribute(ProgramID pid,
NodeID nid,
const Source& src,
const ast::Expression* expr);
~StructMemberSizeAttribute() override;
/// @returns the WGSL name for the attribute
@@ -42,8 +46,8 @@ class StructMemberSizeAttribute final : public Castable<StructMemberSizeAttribut
/// @return the newly cloned node
const StructMemberSizeAttribute* Clone(CloneContext* ctx) const override;
/// The size value
const uint32_t size;
/// The size expression
const ast::Expression* const expr;
};
} // namespace tint::ast

View File

@@ -19,11 +19,13 @@
namespace tint::ast {
namespace {
using namespace tint::number_suffixes; // NOLINT
using StructMemberSizeAttributeTest = TestHelper;
TEST_F(StructMemberSizeAttributeTest, Creation) {
auto* d = create<StructMemberSizeAttribute>(2u);
EXPECT_EQ(2u, d->size);
auto* d = MemberSize(2_u);
ASSERT_TRUE(d->expr->Is<ast::IntLiteralExpression>());
EXPECT_EQ(2u, d->expr->As<ast::IntLiteralExpression>()->value);
}
} // namespace

View File

@@ -18,10 +18,11 @@
namespace tint::ast {
namespace {
using namespace tint::number_suffixes; // NOLINT
using StructMemberTest = TestHelper;
TEST_F(StructMemberTest, Creation) {
auto* st = Member("a", ty.i32(), utils::Vector{MemberSize(4)});
auto* st = Member("a", ty.i32(), utils::Vector{MemberSize(4_a)});
EXPECT_EQ(st->symbol, Symbol(1, ID()));
EXPECT_TRUE(st->type->Is<ast::I32>());
EXPECT_EQ(st->attributes.Length(), 1u);
@@ -66,7 +67,7 @@ TEST_F(StructMemberTest, Assert_Null_Attribute) {
EXPECT_FATAL_FAILURE(
{
ProgramBuilder b;
b.Member("a", b.ty.i32(), utils::Vector{b.MemberSize(4), nullptr});
b.Member("a", b.ty.i32(), utils::Vector{b.MemberSize(4_a), nullptr});
},
"internal compiler error");
}
@@ -76,7 +77,7 @@ TEST_F(StructMemberTest, Assert_DifferentProgramID_Symbol) {
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.Member(b2.Sym("a"), b1.ty.i32(), utils::Vector{b1.MemberSize(4)});
b1.Member(b2.Sym("a"), b1.ty.i32(), utils::Vector{b1.MemberSize(4_a)});
},
"internal compiler error");
}
@@ -86,7 +87,7 @@ TEST_F(StructMemberTest, Assert_DifferentProgramID_Attribute) {
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.Member("a", b1.ty.i32(), utils::Vector{b2.MemberSize(4)});
b1.Member("a", b1.ty.i32(), utils::Vector{b2.MemberSize(4_a)});
},
"internal compiler error");
}