tint/reader: Use ProgramBuilder helpers

Eases the migration to unkeywording types.

Bug: tint:1810
Change-Id: If9ccdbf963550774b85b8cd15df8e37c271cde93
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/118987
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 2023-02-08 12:35:19 +00:00 committed by Dawn LUCI CQ
parent 136ec1e3b4
commit 5b50790d70
5 changed files with 91 additions and 112 deletions

View File

@ -955,9 +955,8 @@ bool FunctionEmitter::Emit() {
return false;
}
builder_.AST().AddFunction(create<ast::Function>(
decl.source, builder_.Symbols().Register(decl.name), std::move(decl.params),
decl.return_type->Build(builder_), body, std::move(decl.attributes), utils::Empty));
builder_.Func(decl.source, decl.name, std::move(decl.params),
decl.return_type->Build(builder_), body, std::move(decl.attributes));
}
if (ep_info_ && !ep_info_->inner_name.empty()) {
@ -1395,7 +1394,6 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() {
}
}
auto* body = create<ast::BlockStatement>(source, stmts, utils::Empty);
AttributeList fn_attrs;
fn_attrs.Push(create<ast::StageAttribute>(source, ep_info_->stage));
@ -1409,9 +1407,8 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() {
}
}
builder_.AST().AddFunction(create<ast::Function>(
source, builder_.Symbols().Register(ep_info_->name), std::move(decl.params), return_type,
body, std::move(fn_attrs), AttributeList{}));
builder_.Func(source, ep_info_->name, std::move(decl.params), return_type, std::move(stmts),
std::move(fn_attrs));
return true;
}
@ -5779,21 +5776,18 @@ bool FunctionEmitter::EmitAtomicOp(const spvtools::opt::Instruction& inst) {
}
// Emit stub, will be removed by transform::SpirvAtomic
auto sym = builder_.Symbols().New(std::string("stub_") + sem::str(builtin));
auto* stub_deco = builder_.ASTNodes().Create<transform::SpirvAtomic::Stub>(
builder_.ID(), builder_.AllocateNodeID(), builtin);
auto* stub =
create<ast::Function>(Source{}, sym, std::move(params), ret_type,
auto* stub = builder_.Func(
Source{}, builder_.Symbols().New(std::string("stub_") + sem::str(builtin)),
std::move(params), ret_type,
/* body */ nullptr,
AttributeList{
stub_deco,
utils::Vector{
builder_.ASTNodes().Create<transform::SpirvAtomic::Stub>(
builder_.ID(), builder_.AllocateNodeID(), builtin),
builder_.Disable(ast::DisabledValidation::kFunctionHasNoBody),
},
AttributeList{});
builder_.AST().AddFunction(stub);
});
// Emit call to stub, will be replaced with call to atomic builtin by transform::SpirvAtomic
auto* call = builder_.Call(Source{}, sym, std::move(exprs));
auto* call = builder_.Call(Source{}, stub->symbol, std::move(exprs));
if (inst.type_id() != 0) {
auto* result_type = parser_impl_.ConvertType(inst.type_id());
TypedExpression expr{result_type, call};

View File

@ -1167,8 +1167,8 @@ const Type* ParserImpl::ConvertType(uint32_t type_id,
++num_non_writable_members;
}
const auto member_name = namer_.GetMemberName(type_id, member_index);
auto* ast_struct_member = create<ast::StructMember>(
Source{}, builder_.Symbols().Register(member_name), ast_member_ty->Build(builder_),
auto* ast_struct_member =
builder_.Member(Source{}, member_name, ast_member_ty->Build(builder_),
std::move(ast_member_decorations));
ast_members.Push(ast_struct_member);
}
@ -1390,7 +1390,6 @@ bool ParserImpl::EmitScalarSpecConstants() {
auto* ast_var =
MakeOverride(inst.result_id(), ast_type, ast_expr, std::move(spec_id_decos));
if (ast_var) {
builder_.AST().AddGlobalVariable(ast_var);
scalar_spec_constants_.insert(inst.result_id());
}
}
@ -1577,7 +1576,7 @@ const spvtools::opt::analysis::IntConstant* ParserImpl::GetArraySize(uint32_t va
return size->AsIntConstant();
}
ast::Var* ParserImpl::MakeVar(uint32_t id,
const ast::Var* ParserImpl::MakeVar(uint32_t id,
type::AddressSpace address_space,
const Type* storage_type,
const ast::Expression* initializer,
@ -1610,16 +1609,18 @@ ast::Var* ParserImpl::MakeVar(uint32_t id,
}
auto sym = builder_.Symbols().Register(namer_.Name(id));
return create<ast::Var>(Source{}, sym, storage_type->Build(builder_), address_space, access,
return builder_.Var(Source{}, sym, storage_type->Build(builder_), address_space, access,
initializer, decorations);
}
ast::Let* ParserImpl::MakeLet(uint32_t id, const Type* type, const ast::Expression* initializer) {
const ast::Let* ParserImpl::MakeLet(uint32_t id,
const Type* type,
const ast::Expression* initializer) {
auto sym = builder_.Symbols().Register(namer_.Name(id));
return create<ast::Let>(Source{}, sym, type->Build(builder_), initializer, utils::Empty);
return builder_.Let(Source{}, sym, type->Build(builder_), initializer, utils::Empty);
}
ast::Override* ParserImpl::MakeOverride(uint32_t id,
const ast::Override* ParserImpl::MakeOverride(uint32_t id,
const Type* type,
const ast::Expression* initializer,
AttributeList decorations) {
@ -1627,10 +1628,10 @@ ast::Override* ParserImpl::MakeOverride(uint32_t id,
return nullptr;
}
auto sym = builder_.Symbols().Register(namer_.Name(id));
return create<ast::Override>(Source{}, sym, type->Build(builder_), initializer, decorations);
return builder_.Override(Source{}, sym, type->Build(builder_), initializer, decorations);
}
ast::Parameter* ParserImpl::MakeParameter(uint32_t id,
const ast::Parameter* ParserImpl::MakeParameter(uint32_t id,
const Type* type,
AttributeList decorations) {
if (!ConvertDecorationsForVariable(id, &type, &decorations, false)) {
@ -1638,7 +1639,7 @@ ast::Parameter* ParserImpl::MakeParameter(uint32_t id,
}
auto sym = builder_.Symbols().Register(namer_.Name(id));
return create<ast::Parameter>(Source{}, sym, type->Build(builder_), decorations);
return builder_.Param(Source{}, sym, type->Build(builder_), decorations);
}
bool ParserImpl::ConvertDecorationsForVariable(uint32_t id,

View File

@ -429,7 +429,7 @@ class ParserImpl : Reader {
/// @param decorations the variable decorations
/// @returns a new Variable node, or null in the ignorable variable case and
/// in the error case
ast::Var* MakeVar(uint32_t id,
const ast::Var* MakeVar(uint32_t id,
type::AddressSpace address_space,
const Type* storage_type,
const ast::Expression* initializer,
@ -440,7 +440,7 @@ class ParserImpl : Reader {
/// @param type the type of the variable
/// @param initializer the variable initializer
/// @returns the AST 'let' node
ast::Let* MakeLet(uint32_t id, const Type* type, const ast::Expression* initializer);
const ast::Let* MakeLet(uint32_t id, const Type* type, const ast::Expression* initializer);
/// Creates an AST 'override' node for a SPIR-V ID, including any attached decorations.
/// @param id the SPIR-V result ID
@ -448,7 +448,7 @@ class ParserImpl : Reader {
/// @param initializer the variable initializer
/// @param decorations the variable decorations
/// @returns the AST 'override' node
ast::Override* MakeOverride(uint32_t id,
const ast::Override* MakeOverride(uint32_t id,
const Type* type,
const ast::Expression* initializer,
AttributeList decorations);
@ -459,7 +459,7 @@ class ParserImpl : Reader {
/// @param type the type of the parameter
/// @param decorations the parameter decorations
/// @returns the AST parameter node
ast::Parameter* MakeParameter(uint32_t id, const Type* type, AttributeList decorations);
const ast::Parameter* MakeParameter(uint32_t id, const Type* type, AttributeList decorations);
/// Returns true if a constant expression can be generated.
/// @param id the SPIR-V ID of the value
@ -885,7 +885,7 @@ class ParserImpl : Reader {
std::unordered_map<const spvtools::opt::Instruction*, const Type*> handle_type_;
/// Maps the SPIR-V ID of a module-scope variable to its AST variable.
utils::Hashmap<uint32_t, ast::Var*, 16> module_variable_;
utils::Hashmap<uint32_t, const ast::Var*, 16> module_variable_;
// Set of symbols of declared type that have been added, used to avoid
// adding duplicates.

View File

@ -505,8 +505,6 @@ Maybe<Void> ParserImpl::global_decl() {
return Failure::kErrored;
}
}
builder_.AST().AddGlobalVariable(gc.value);
return kSuccess;
}
@ -565,7 +563,6 @@ Maybe<Void> ParserImpl::global_decl() {
errored = true;
}
if (func.matched) {
builder_.AST().AddFunction(func.value);
return kSuccess;
}
@ -631,8 +628,8 @@ Maybe<const ast::Variable*> ParserImpl::global_variable_decl(AttributeList& attr
TINT_DEFER(attrs.Clear());
return create<ast::Var>(decl->source, // source
builder_.Symbols().Register(decl->name), // symbol
return builder_.Var(decl->source, // source
decl->name, // symbol
decl->type, // type
decl->address_space, // address space
decl->access, // access control
@ -646,7 +643,6 @@ Maybe<const ast::Variable*> ParserImpl::global_variable_decl(AttributeList& attr
// global_const_initializer
// : EQUAL const_expr
Maybe<const ast::Variable*> ParserImpl::global_constant_decl(AttributeList& attrs) {
bool is_const = false;
bool is_overridable = false;
const char* use = nullptr;
Source source;
@ -690,22 +686,15 @@ Maybe<const ast::Variable*> ParserImpl::global_constant_decl(AttributeList& attr
TINT_DEFER(attrs.Clear());
if (is_const) {
return create<ast::Const>(decl->source, // source
builder_.Symbols().Register(decl->name), // symbol
decl->type, // type
initializer, // initializer
std::move(attrs)); // attributes
}
if (is_overridable) {
return create<ast::Override>(decl->source, // source
builder_.Symbols().Register(decl->name), // symbol
return builder_.Override(decl->source, // source
decl->name, // symbol
decl->type, // type
initializer, // initializer
std::move(attrs)); // attributes
}
return create<ast::Const>(decl->source, // source
builder_.Symbols().Register(decl->name), // symbol
return builder_.GlobalConst(decl->source, // source
decl->name, // symbol
decl->type, // type
initializer, // initializer
std::move(attrs)); // attributes
@ -1451,7 +1440,7 @@ Expect<ParserImpl::StructMemberList> ParserImpl::expect_struct_body_decl() {
// struct_member
// : attribute* ident_with_type_specifier
Expect<ast::StructMember*> ParserImpl::expect_struct_member() {
Expect<const ast::StructMember*> ParserImpl::expect_struct_member() {
auto attrs = attribute_list();
if (attrs.errored) {
return Failure::kErrored;
@ -1462,8 +1451,7 @@ Expect<ast::StructMember*> ParserImpl::expect_struct_member() {
return Failure::kErrored;
}
return create<ast::StructMember>(decl->source, builder_.Symbols().Register(decl->name),
decl->type, std::move(attrs.value));
return builder_.Member(decl->source, decl->name, decl->type, std::move(attrs.value));
}
// const_assert_statement
@ -1523,9 +1511,8 @@ Maybe<const ast::Function*> ParserImpl::function_decl(AttributeList& attrs) {
TINT_DEFER(attrs.Clear());
return create<ast::Function>(header->source, builder_.Symbols().Register(header->name),
header->params, header->return_type, body.value, std::move(attrs),
header->return_type_attributes);
return builder_.Func(header->source, header->name, header->params, header->return_type,
body.value, std::move(attrs), header->return_type_attributes);
}
// function_header
@ -1618,7 +1605,7 @@ Expect<ParserImpl::ParameterList> ParserImpl::expect_param_list() {
// param
// : attribute_list* ident COLON type_specifier
Expect<ast::Parameter*> ParserImpl::expect_param() {
Expect<const ast::Parameter*> ParserImpl::expect_param() {
auto attrs = attribute_list();
auto decl = expect_ident_with_type_specifier("parameter");
@ -1626,8 +1613,8 @@ Expect<ast::Parameter*> ParserImpl::expect_param() {
return Failure::kErrored;
}
return create<ast::Parameter>(decl->source, // source
builder_.Symbols().Register(decl->name), // symbol
return builder_.Param(decl->source, // source
decl->name, // symbol
decl->type, // type
std::move(attrs.value)); // attributes
}
@ -1943,11 +1930,10 @@ Maybe<const ast::VariableDeclStatement*> ParserImpl::variable_statement() {
return add_error(peek(), "missing initializer for 'const' declaration");
}
auto* const_ = create<ast::Const>(typed_ident->source, // source
builder_.Symbols().Register(typed_ident->name), // symbol
auto* const_ = builder_.Const(typed_ident->source, // source
typed_ident->name, // symbol
typed_ident->type, // type
initializer.value, // initializer
utils::Empty); // attributes
initializer.value); // initializer
return create<ast::VariableDeclStatement>(decl_source, const_);
}
@ -1972,11 +1958,10 @@ Maybe<const ast::VariableDeclStatement*> ParserImpl::variable_statement() {
return add_error(peek(), "missing initializer for 'let' declaration");
}
auto* let = create<ast::Let>(typed_ident->source, // source
builder_.Symbols().Register(typed_ident->name), // symbol
auto* let = builder_.Let(typed_ident->source, // source
typed_ident->name, // symbol
typed_ident->type, // type
initializer.value, // initializer
utils::Empty); // attributes
initializer.value); // initializer
return create<ast::VariableDeclStatement>(decl_source, let);
}
@ -2004,13 +1989,12 @@ Maybe<const ast::VariableDeclStatement*> ParserImpl::variable_statement() {
initializer = initializer_expr.value;
}
auto* var = create<ast::Var>(decl_source, // source
builder_.Symbols().Register(decl->name), // symbol
auto* var = builder_.Var(decl_source, // source
decl->name, // symbol
decl->type, // type
decl->address_space, // address space
decl->access, // access control
initializer, // initializer
utils::Empty); // attributes
initializer); // initializer
return create<ast::VariableDeclStatement>(var->source, var);
}

View File

@ -474,7 +474,7 @@ class ParserImpl {
Expect<StructMemberList> expect_struct_body_decl();
/// Parses a `struct_member` grammar element, erroring on parse failure.
/// @returns the struct member or nullptr
Expect<ast::StructMember*> expect_struct_member();
Expect<const ast::StructMember*> expect_struct_member();
/// Parses a `function_decl` grammar element with the initial
/// `function_attribute_decl*` provided as `attrs`.
/// @param attrs the list of attributes for the function declaration. If attributes are consumed
@ -519,7 +519,7 @@ class ParserImpl {
Expect<ParameterList> expect_param_list();
/// Parses a `param` grammar element, erroring on parse failure.
/// @returns the parsed variable
Expect<ast::Parameter*> expect_param();
Expect<const ast::Parameter*> expect_param();
/// Parses a `pipeline_stage` grammar element, erroring if the next token does
/// not match a stage name.
/// @returns the pipeline stage.