transform/EntryPointIO: Use InsertFront

This removes the need to check for an empty function body and means we
can unconditionally build the function constant initializers, instead
of deferring this work via captured lambdas.

Change-Id: I1571a065fb5885e2b29f0ffb41940a71e5b3f121
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53281
Auto-Submit: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
This commit is contained in:
James Price 2021-06-04 14:03:28 +00:00 committed by Tint LUCI CQ
parent cd5492c910
commit 3604e80321
2 changed files with 19 additions and 27 deletions

View File

@ -16,7 +16,6 @@
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include <vector>
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/sem/block_statement.h" #include "src/sem/block_statement.h"
@ -109,11 +108,11 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
auto* param_ty = param->Type(); auto* param_ty = param->Type();
auto* param_declared_ty = param->Declaration()->type(); auto* param_declared_ty = param->Declaration()->type();
std::function<ast::Expression*()> func_const_initializer; ast::Expression* func_const_initializer = nullptr;
if (auto* str = param_ty->As<sem::Struct>()) { if (auto* str = param_ty->As<sem::Struct>()) {
// Pull out all struct members and build initializer list. // Pull out all struct members and build initializer list.
std::vector<Symbol> member_names; ast::ExpressionList init_values;
for (auto* member : str->Members()) { for (auto* member : str->Members()) {
if (member->Type()->Is<sem::Struct>()) { if (member->Type()->Is<sem::Struct>()) {
TINT_ICE(ctx.dst->Diagnostics()) << "nested pipeline IO struct"; TINT_ICE(ctx.dst->Diagnostics()) << "nested pipeline IO struct";
@ -129,19 +128,12 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
auto* member_type = ctx.Clone(member->Declaration()->type()); auto* member_type = ctx.Clone(member->Declaration()->type());
new_struct_members.push_back( new_struct_members.push_back(
ctx.dst->Member(member_name, member_type, new_decorations)); ctx.dst->Member(member_name, member_type, new_decorations));
member_names.emplace_back(member_name); init_values.push_back(
ctx.dst->MemberAccessor(new_struct_param_symbol, member_name));
} }
func_const_initializer = [&ctx, new_struct_param_symbol, func_const_initializer =
param_declared_ty, member_names]() { ctx.dst->Construct(ctx.Clone(param_declared_ty), init_values);
ast::ExpressionList init_values;
for (auto name : member_names) {
init_values.push_back(
ctx.dst->MemberAccessor(new_struct_param_symbol, name));
}
return ctx.dst->Construct(ctx.Clone(param_declared_ty),
init_values);
};
} else { } else {
ast::DecorationList new_decorations = RemoveDecorations( ast::DecorationList new_decorations = RemoveDecorations(
&ctx, param->Declaration()->decorations(), &ctx, param->Declaration()->decorations(),
@ -151,23 +143,15 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
}); });
new_struct_members.push_back(ctx.dst->Member( new_struct_members.push_back(ctx.dst->Member(
param_name, ctx.Clone(param_declared_ty), new_decorations)); param_name, ctx.Clone(param_declared_ty), new_decorations));
func_const_initializer = [&ctx, new_struct_param_symbol, func_const_initializer =
param_name]() { ctx.dst->MemberAccessor(new_struct_param_symbol, param_name);
return ctx.dst->MemberAccessor(new_struct_param_symbol, param_name);
};
}
if (func_ast->body()->empty()) {
// Don't generate a function-scope const if the function is empty.
continue;
} }
// Create a function-scope const to replace the parameter. // Create a function-scope const to replace the parameter.
// Initialize it with the value extracted from the new struct parameter. // Initialize it with the value extracted from the new struct parameter.
auto* func_const = ctx.dst->Const( auto* func_const = ctx.dst->Const(
param_name, ctx.Clone(param_declared_ty), func_const_initializer()); param_name, ctx.Clone(param_declared_ty), func_const_initializer);
ctx.InsertBefore(func_ast->body()->statements(), ctx.InsertFront(func_ast->body()->statements(),
*func_ast->body()->begin(),
ctx.dst->WrapInStatement(func_const)); ctx.dst->WrapInStatement(func_const));
// Replace all uses of the function parameter with the function const. // Replace all uses of the function parameter with the function const.

View File

@ -107,6 +107,9 @@ struct tint_symbol_1 {
[[stage(fragment)]] [[stage(fragment)]]
fn frag_main(tint_symbol : tint_symbol_1) { fn frag_main(tint_symbol : tint_symbol_1) {
let loc1 : f32 = tint_symbol.loc1;
let loc2 : vec4<u32> = tint_symbol.loc2;
let coord : vec4<f32> = tint_symbol.coord;
} }
)"; )";
@ -583,6 +586,10 @@ struct tint_symbol_3 {
[[stage(fragment)]] [[stage(fragment)]]
fn frag_main(tint_symbol_2 : tint_symbol_3) { fn frag_main(tint_symbol_2 : tint_symbol_3) {
let ff : bool = tint_symbol_2.ff;
let c : i32 = tint_symbol_2.c;
let inputs : FragmentInputExtra = FragmentInputExtra(tint_symbol_2.d, tint_symbol_2.pos, tint_symbol_2.a);
let b : u32 = tint_symbol_2.b;
} }
)"; )";
@ -606,6 +613,7 @@ struct tint_symbol_2 {
[[stage(fragment)]] [[stage(fragment)]]
fn tint_symbol_1(tint_symbol : tint_symbol_2) { fn tint_symbol_1(tint_symbol : tint_symbol_2) {
let col : f32 = tint_symbol.col;
} }
)"; )";