transforms: Add a ProgramBuilder::Sym() helper
This creates a new, unnamed symbol. Change-Id: I0ea11e812296c3215ba624a18777d3e7233e2ae4 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49441 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Ben Clayton <bclayton@google.com> Commit-Queue: Ben Clayton <bclayton@google.com> Auto-Submit: James Price <jrprice@google.com>
This commit is contained in:
parent
77f7f5d369
commit
65ae64d009
|
@ -878,6 +878,9 @@ class ProgramBuilder {
|
||||||
// AST helper methods
|
// AST helper methods
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// @return a new unnamed symbol
|
||||||
|
Symbol Sym() { return Symbols().New(); }
|
||||||
|
|
||||||
/// @param name the symbol string
|
/// @param name the symbol string
|
||||||
/// @return a Symbol with the given name
|
/// @return a Symbol with the given name
|
||||||
Symbol Sym(const std::string& name) { return Symbols().Register(name); }
|
Symbol Sym(const std::string& name) { return Symbols().Register(name); }
|
||||||
|
|
|
@ -80,7 +80,7 @@ Output CalculateArrayLength::Run(const Program* in, const DataMap&) {
|
||||||
std::unordered_map<sem::StructType*, Symbol> buffer_size_intrinsics;
|
std::unordered_map<sem::StructType*, Symbol> buffer_size_intrinsics;
|
||||||
auto get_buffer_size_intrinsic = [&](sem::StructType* buffer_type) {
|
auto get_buffer_size_intrinsic = [&](sem::StructType* buffer_type) {
|
||||||
return utils::GetOrCreate(buffer_size_intrinsics, buffer_type, [&] {
|
return utils::GetOrCreate(buffer_size_intrinsics, buffer_type, [&] {
|
||||||
auto name = ctx.dst->Symbols().New();
|
auto name = ctx.dst->Sym();
|
||||||
auto* func = ctx.dst->create<ast::Function>(
|
auto* func = ctx.dst->create<ast::Function>(
|
||||||
name,
|
name,
|
||||||
ast::VariableList{
|
ast::VariableList{
|
||||||
|
@ -183,7 +183,7 @@ Output CalculateArrayLength::Run(const Program* in, const DataMap&) {
|
||||||
// Construct the variable that'll hold the result of
|
// Construct the variable that'll hold the result of
|
||||||
// RWByteAddressBuffer.GetDimensions()
|
// RWByteAddressBuffer.GetDimensions()
|
||||||
auto* buffer_size_result = ctx.dst->Decl(ctx.dst->Var(
|
auto* buffer_size_result = ctx.dst->Decl(ctx.dst->Var(
|
||||||
ctx.dst->Symbols().New(), ctx.dst->ty.u32(),
|
ctx.dst->Sym(), ctx.dst->ty.u32(),
|
||||||
ast::StorageClass::kFunction, ctx.dst->Expr(0u)));
|
ast::StorageClass::kFunction, ctx.dst->Expr(0u)));
|
||||||
|
|
||||||
// Call storage_buffer.GetDimensions(buffer_size_result)
|
// Call storage_buffer.GetDimensions(buffer_size_result)
|
||||||
|
@ -199,7 +199,7 @@ Output CalculateArrayLength::Run(const Program* in, const DataMap&) {
|
||||||
// total_storage_buffer_size - array_offset
|
// total_storage_buffer_size - array_offset
|
||||||
// array_length = ----------------------------------------
|
// array_length = ----------------------------------------
|
||||||
// array_stride
|
// array_stride
|
||||||
auto name = ctx.dst->Symbols().New();
|
auto name = ctx.dst->Sym();
|
||||||
uint32_t array_offset = array_member_sem->Offset();
|
uint32_t array_offset = array_member_sem->Offset();
|
||||||
uint32_t array_stride = array_member_sem->Size();
|
uint32_t array_stride = array_member_sem->Size();
|
||||||
auto* array_length_var = ctx.dst->Decl(ctx.dst->Const(
|
auto* array_length_var = ctx.dst->Decl(ctx.dst->Const(
|
||||||
|
|
|
@ -99,7 +99,7 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
|
||||||
|
|
||||||
if (!func->params().empty()) {
|
if (!func->params().empty()) {
|
||||||
// Collect all parameters and build a list of new struct members.
|
// Collect all parameters and build a list of new struct members.
|
||||||
auto new_struct_param_symbol = ctx.dst->Symbols().New();
|
auto new_struct_param_symbol = ctx.dst->Sym();
|
||||||
ast::StructMemberList new_struct_members;
|
ast::StructMemberList new_struct_members;
|
||||||
for (auto* param : func->params()) {
|
for (auto* param : func->params()) {
|
||||||
auto param_name = ctx.Clone(param->symbol());
|
auto param_name = ctx.Clone(param->symbol());
|
||||||
|
@ -175,7 +175,7 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
|
||||||
StructMemberComparator);
|
StructMemberComparator);
|
||||||
|
|
||||||
// Create the new struct type.
|
// Create the new struct type.
|
||||||
auto in_struct_name = ctx.dst->Symbols().New();
|
auto in_struct_name = ctx.dst->Sym();
|
||||||
auto* in_struct =
|
auto* in_struct =
|
||||||
ctx.dst->create<sem::StructType>(ctx.dst->create<ast::Struct>(
|
ctx.dst->create<sem::StructType>(ctx.dst->create<ast::Struct>(
|
||||||
in_struct_name, new_struct_members, ast::DecorationList{}));
|
in_struct_name, new_struct_members, ast::DecorationList{}));
|
||||||
|
@ -221,7 +221,7 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
|
||||||
StructMemberComparator);
|
StructMemberComparator);
|
||||||
|
|
||||||
// Create the new struct type.
|
// Create the new struct type.
|
||||||
auto out_struct_name = ctx.dst->Symbols().New();
|
auto out_struct_name = ctx.dst->Sym();
|
||||||
auto* out_struct =
|
auto* out_struct =
|
||||||
ctx.dst->create<sem::StructType>(ctx.dst->create<ast::Struct>(
|
ctx.dst->create<sem::StructType>(ctx.dst->create<ast::Struct>(
|
||||||
out_struct_name, new_struct_members, ast::DecorationList{}));
|
out_struct_name, new_struct_members, ast::DecorationList{}));
|
||||||
|
@ -242,7 +242,7 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
|
||||||
if (!ret->value()->Is<ast::IdentifierExpression>()) {
|
if (!ret->value()->Is<ast::IdentifierExpression>()) {
|
||||||
// Create a const to hold the return value expression to avoid
|
// Create a const to hold the return value expression to avoid
|
||||||
// re-evaluating it multiple times.
|
// re-evaluating it multiple times.
|
||||||
auto temp = ctx.dst->Symbols().New();
|
auto temp = ctx.dst->Sym();
|
||||||
auto* temp_var = ctx.dst->Decl(
|
auto* temp_var = ctx.dst->Decl(
|
||||||
ctx.dst->Const(temp, ctx.Clone(ret_type), new_ret_value()));
|
ctx.dst->Const(temp, ctx.Clone(ret_type), new_ret_value()));
|
||||||
ctx.InsertBefore(ret_sem->Block()->statements(), ret, temp_var);
|
ctx.InsertBefore(ret_sem->Block()->statements(), ret, temp_var);
|
||||||
|
|
|
@ -425,7 +425,7 @@ struct State {
|
||||||
ast::Function* func = nullptr;
|
ast::Function* func = nullptr;
|
||||||
if (auto* intrinsic = IntrinsicLoadFor(ctx.dst, el_ty)) {
|
if (auto* intrinsic = IntrinsicLoadFor(ctx.dst, el_ty)) {
|
||||||
func = ctx.dst->create<ast::Function>(
|
func = ctx.dst->create<ast::Function>(
|
||||||
ctx.dst->Symbols().New(), params, ctx.Clone(el_ty), nullptr,
|
ctx.dst->Sym(), params, ctx.Clone(el_ty), nullptr,
|
||||||
ast::DecorationList{intrinsic}, ast::DecorationList{});
|
ast::DecorationList{intrinsic}, ast::DecorationList{});
|
||||||
} else {
|
} else {
|
||||||
ast::ExpressionList values;
|
ast::ExpressionList values;
|
||||||
|
@ -458,7 +458,7 @@ struct State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func = ctx.dst->create<ast::Function>(
|
func = ctx.dst->create<ast::Function>(
|
||||||
ctx.dst->Symbols().New(), params, ctx.Clone(el_ty),
|
ctx.dst->Sym(), params, ctx.Clone(el_ty),
|
||||||
ctx.dst->Block(
|
ctx.dst->Block(
|
||||||
ctx.dst->Return(ctx.dst->create<ast::TypeConstructorExpression>(
|
ctx.dst->Return(ctx.dst->create<ast::TypeConstructorExpression>(
|
||||||
ctx.Clone(el_ty), values))),
|
ctx.Clone(el_ty), values))),
|
||||||
|
@ -489,7 +489,7 @@ struct State {
|
||||||
ast::Function* func = nullptr;
|
ast::Function* func = nullptr;
|
||||||
if (auto* intrinsic = IntrinsicStoreFor(ctx.dst, el_ty)) {
|
if (auto* intrinsic = IntrinsicStoreFor(ctx.dst, el_ty)) {
|
||||||
func = ctx.dst->create<ast::Function>(
|
func = ctx.dst->create<ast::Function>(
|
||||||
ctx.dst->Symbols().New(), params, ctx.dst->ty.void_(), nullptr,
|
ctx.dst->Sym(), params, ctx.dst->ty.void_(), nullptr,
|
||||||
ast::DecorationList{intrinsic}, ast::DecorationList{});
|
ast::DecorationList{intrinsic}, ast::DecorationList{});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -531,8 +531,8 @@ struct State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func = ctx.dst->create<ast::Function>(
|
func = ctx.dst->create<ast::Function>(
|
||||||
ctx.dst->Symbols().New(), params, ctx.dst->ty.void_(),
|
ctx.dst->Sym(), params, ctx.dst->ty.void_(), ctx.dst->Block(body),
|
||||||
ctx.dst->Block(body), ast::DecorationList{}, ast::DecorationList{});
|
ast::DecorationList{}, ast::DecorationList{});
|
||||||
}
|
}
|
||||||
|
|
||||||
InsertGlobal(ctx, insert_after, func);
|
InsertGlobal(ctx, insert_after, func);
|
||||||
|
|
|
@ -134,11 +134,11 @@ Output FirstIndexOffset::Run(const Program* in, const DataMap& data) {
|
||||||
offset += 4;
|
offset += 4;
|
||||||
}
|
}
|
||||||
auto struct_type =
|
auto struct_type =
|
||||||
ctx.dst->Structure(ctx.dst->Symbols().New(), std::move(members),
|
ctx.dst->Structure(ctx.dst->Sym(), std::move(members),
|
||||||
{ctx.dst->create<ast::StructBlockDecoration>()});
|
{ctx.dst->create<ast::StructBlockDecoration>()});
|
||||||
|
|
||||||
// Create a global to hold the uniform buffer
|
// Create a global to hold the uniform buffer
|
||||||
Symbol buffer_name = ctx.dst->Symbols().New();
|
Symbol buffer_name = ctx.dst->Sym();
|
||||||
ctx.dst->Global(buffer_name, struct_type, ast::StorageClass::kUniform,
|
ctx.dst->Global(buffer_name, struct_type, ast::StorageClass::kUniform,
|
||||||
nullptr,
|
nullptr,
|
||||||
ast::DecorationList{
|
ast::DecorationList{
|
||||||
|
|
|
@ -98,7 +98,7 @@ void Hlsl::PromoteInitializersToConstVar(CloneContext& ctx) const {
|
||||||
auto* src_ty = src_sem_expr->Type();
|
auto* src_ty = src_sem_expr->Type();
|
||||||
if (src_ty->IsAnyOf<sem::ArrayType, sem::StructType>()) {
|
if (src_ty->IsAnyOf<sem::ArrayType, sem::StructType>()) {
|
||||||
// Create a new symbol for the constant
|
// Create a new symbol for the constant
|
||||||
auto dst_symbol = ctx.dst->Symbols().New();
|
auto dst_symbol = ctx.dst->Sym();
|
||||||
// Clone the type
|
// Clone the type
|
||||||
auto* dst_ty = ctx.Clone(src_ty);
|
auto* dst_ty = ctx.Clone(src_ty);
|
||||||
// Clone the initializer
|
// Clone the initializer
|
||||||
|
|
|
@ -906,7 +906,7 @@ Output Renamer::Run(const Program* in, const DataMap&) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto sym_out = ctx.dst->Symbols().New();
|
auto sym_out = ctx.dst->Sym();
|
||||||
remappings.emplace(name_in, ctx.dst->Symbols().NameFor(sym_out));
|
remappings.emplace(name_in, ctx.dst->Symbols().NameFor(sym_out));
|
||||||
return sym_out;
|
return sym_out;
|
||||||
});
|
});
|
||||||
|
|
|
@ -154,7 +154,7 @@ void Spirv::HandleEntryPointIOTypes(CloneContext& ctx) const {
|
||||||
|
|
||||||
if (!func->return_type()->Is<sem::Void>()) {
|
if (!func->return_type()->Is<sem::Void>()) {
|
||||||
ast::StatementList stores;
|
ast::StatementList stores;
|
||||||
auto store_value_symbol = ctx.dst->Symbols().New();
|
auto store_value_symbol = ctx.dst->Sym();
|
||||||
HoistToOutputVariables(
|
HoistToOutputVariables(
|
||||||
ctx, func, func->return_type(), func->return_type(),
|
ctx, func, func->return_type(), func->return_type(),
|
||||||
func->return_type_decorations(), {}, store_value_symbol, stores);
|
func->return_type_decorations(), {}, store_value_symbol, stores);
|
||||||
|
@ -162,7 +162,7 @@ void Spirv::HandleEntryPointIOTypes(CloneContext& ctx) const {
|
||||||
// Create a function that writes a return value to all output variables.
|
// Create a function that writes a return value to all output variables.
|
||||||
auto* store_value =
|
auto* store_value =
|
||||||
ctx.dst->Param(store_value_symbol, ctx.Clone(func->return_type()));
|
ctx.dst->Param(store_value_symbol, ctx.Clone(func->return_type()));
|
||||||
auto return_func_symbol = ctx.dst->Symbols().New();
|
auto return_func_symbol = ctx.dst->Sym();
|
||||||
auto* return_func = ctx.dst->create<ast::Function>(
|
auto* return_func = ctx.dst->create<ast::Function>(
|
||||||
return_func_symbol, ast::VariableList{store_value},
|
return_func_symbol, ast::VariableList{store_value},
|
||||||
ctx.dst->ty.void_(), ctx.dst->create<ast::BlockStatement>(stores),
|
ctx.dst->ty.void_(), ctx.dst->create<ast::BlockStatement>(stores),
|
||||||
|
@ -262,7 +262,7 @@ Symbol Spirv::HoistToInputVariables(
|
||||||
return !deco->IsAnyOf<ast::BuiltinDecoration,
|
return !deco->IsAnyOf<ast::BuiltinDecoration,
|
||||||
ast::LocationDecoration>();
|
ast::LocationDecoration>();
|
||||||
});
|
});
|
||||||
auto global_var_symbol = ctx.dst->Symbols().New();
|
auto global_var_symbol = ctx.dst->Sym();
|
||||||
auto* global_var =
|
auto* global_var =
|
||||||
ctx.dst->Var(global_var_symbol, ctx.Clone(declared_ty),
|
ctx.dst->Var(global_var_symbol, ctx.Clone(declared_ty),
|
||||||
ast::StorageClass::kInput, nullptr, new_decorations);
|
ast::StorageClass::kInput, nullptr, new_decorations);
|
||||||
|
@ -279,7 +279,7 @@ Symbol Spirv::HoistToInputVariables(
|
||||||
init_value_names.emplace_back(member_var);
|
init_value_names.emplace_back(member_var);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto func_var_symbol = ctx.dst->Symbols().New();
|
auto func_var_symbol = ctx.dst->Sym();
|
||||||
if (func->body()->empty()) {
|
if (func->body()->empty()) {
|
||||||
// The return value should never get used.
|
// The return value should never get used.
|
||||||
return func_var_symbol;
|
return func_var_symbol;
|
||||||
|
@ -315,7 +315,7 @@ void Spirv::HoistToOutputVariables(CloneContext& ctx,
|
||||||
return !deco->IsAnyOf<ast::BuiltinDecoration,
|
return !deco->IsAnyOf<ast::BuiltinDecoration,
|
||||||
ast::LocationDecoration>();
|
ast::LocationDecoration>();
|
||||||
});
|
});
|
||||||
auto global_var_symbol = ctx.dst->Symbols().New();
|
auto global_var_symbol = ctx.dst->Sym();
|
||||||
auto* global_var =
|
auto* global_var =
|
||||||
ctx.dst->Var(global_var_symbol, ctx.Clone(declared_ty),
|
ctx.dst->Var(global_var_symbol, ctx.Clone(declared_ty),
|
||||||
ast::StorageClass::kOutput, nullptr, new_decorations);
|
ast::StorageClass::kOutput, nullptr, new_decorations);
|
||||||
|
|
|
@ -487,11 +487,10 @@ struct State {
|
||||||
new_members.push_back(
|
new_members.push_back(
|
||||||
ctx.dst->Member(member_sym, member_type, std::move(member_decos)));
|
ctx.dst->Member(member_sym, member_type, std::move(member_decos)));
|
||||||
}
|
}
|
||||||
auto new_struct =
|
auto new_struct = ctx.dst->Structure(ctx.dst->Sym(), new_members);
|
||||||
ctx.dst->Structure(ctx.dst->Symbols().New(), new_members);
|
|
||||||
|
|
||||||
// Create a new function parameter with this struct.
|
// Create a new function parameter with this struct.
|
||||||
auto* new_param = ctx.dst->Param(ctx.dst->Symbols().New(), new_struct);
|
auto* new_param = ctx.dst->Param(ctx.dst->Sym(), new_struct);
|
||||||
new_function_parameters.push_back(new_param);
|
new_function_parameters.push_back(new_param);
|
||||||
|
|
||||||
// Copy values from the new parameter to the function-scope variable.
|
// Copy values from the new parameter to the function-scope variable.
|
||||||
|
|
Loading…
Reference in New Issue