CloneContext: Drop the first CloneContext* parameter from the ReplaceAll() callback

You have to have the CloneContext in order to call ReplaceAll() in the first place. The overhead of capturing the pointer in the closure is negligible.

Cleans up the callsites.

Change-Id: I3a0fd808517d69f19756f590f3426e5ba226c57e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/42840
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2021-02-26 19:33:56 +00:00
committed by Commit Bot service account
parent d5638c93c5
commit a8b20bec7b
6 changed files with 88 additions and 87 deletions

View File

@@ -58,11 +58,11 @@ BoundArrayAccessors::~BoundArrayAccessors() = default;
Transform::Output BoundArrayAccessors::Run(const Program* in) {
ProgramBuilder out;
CloneContext(&out, in)
.ReplaceAll([&](CloneContext* ctx, ast::ArrayAccessorExpression* expr) {
return Transform(expr, ctx);
})
.Clone();
CloneContext ctx(&out, in);
ctx.ReplaceAll([&](ast::ArrayAccessorExpression* expr) {
return Transform(expr, &ctx);
});
ctx.Clone();
return Output(Program(std::move(out)));
}

View File

@@ -64,24 +64,23 @@ Transform::Output EmitVertexPointSize::Run(const Program* in) {
out.AST().AddGlobalVariable(pointsize_var);
// Add the pointsize assignment statement to the front of all vertex stages.
CloneContext(&out, in)
.ReplaceAll(
[&](CloneContext* ctx, ast::Function* func) -> ast::Function* {
if (func->pipeline_stage() != ast::PipelineStage::kVertex) {
return nullptr; // Just clone func
}
CloneContext ctx(&out, in);
ctx.ReplaceAll([&](ast::Function* func) -> ast::Function* {
if (func->pipeline_stage() != ast::PipelineStage::kVertex) {
return nullptr; // Just clone func
}
// Build the AST expression & statement for assigning pointsize one.
auto* one = out.create<ast::ScalarConstructorExpression>(
Source{}, out.create<ast::FloatLiteral>(Source{}, f32, 1.0f));
auto* pointsize_ident = out.create<ast::IdentifierExpression>(
Source{}, out.Symbols().Register(kPointSizeVar));
auto* pointsize_assign = out.create<ast::AssignmentStatement>(
Source{}, pointsize_ident, one);
// Build the AST expression & statement for assigning pointsize one.
auto* one = out.create<ast::ScalarConstructorExpression>(
Source{}, out.create<ast::FloatLiteral>(Source{}, f32, 1.0f));
auto* pointsize_ident = out.create<ast::IdentifierExpression>(
Source{}, out.Symbols().Register(kPointSizeVar));
auto* pointsize_assign =
out.create<ast::AssignmentStatement>(Source{}, pointsize_ident, one);
return CloneWithStatementsAtStart(ctx, func, {pointsize_assign});
})
.Clone();
return CloneWithStatementsAtStart(&ctx, func, {pointsize_assign});
});
ctx.Clone();
return Output(Program(std::move(out)));
}

View File

@@ -131,53 +131,52 @@ Transform::Output FirstIndexOffset::Run(const Program* in) {
// add a CreateFirstIndexOffset() statement to each function that uses one of
// these builtins.
CloneContext(&out, in)
.ReplaceAll([&](CloneContext* ctx, ast::Variable* var) -> ast::Variable* {
for (ast::VariableDecoration* dec : var->decorations()) {
if (auto* blt_dec = dec->As<ast::BuiltinDecoration>()) {
ast::Builtin blt_type = blt_dec->value();
if (blt_type == ast::Builtin::kVertexIndex) {
vertex_index_sym = var->symbol();
has_vertex_index_ = true;
return clone_variable_with_new_name(
ctx, var,
kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
} else if (blt_type == ast::Builtin::kInstanceIndex) {
instance_index_sym = var->symbol();
has_instance_index_ = true;
return clone_variable_with_new_name(
ctx, var,
kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
}
CloneContext ctx(&out, in);
ctx.ReplaceAll([&](ast::Variable* var) -> ast::Variable* {
for (ast::VariableDecoration* dec : var->decorations()) {
if (auto* blt_dec = dec->As<ast::BuiltinDecoration>()) {
ast::Builtin blt_type = blt_dec->value();
if (blt_type == ast::Builtin::kVertexIndex) {
vertex_index_sym = var->symbol();
has_vertex_index_ = true;
return clone_variable_with_new_name(
&ctx, var,
kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
} else if (blt_type == ast::Builtin::kInstanceIndex) {
instance_index_sym = var->symbol();
has_instance_index_ = true;
return clone_variable_with_new_name(
&ctx, var,
kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
}
}
}
return nullptr; // Just clone var
});
ctx.ReplaceAll( // Note: This happens in the same pass as the rename above
// which determines the original builtin variable names,
// but this should be fine, as variables are cloned first.
[&](ast::Function* func) -> ast::Function* {
maybe_create_buffer_var(ctx.dst);
if (buffer_var == nullptr) {
return nullptr; // no transform need, just clone func
}
auto* func_sem = in->Sem().Get(func);
ast::StatementList statements;
for (const auto& data : func_sem->LocalReferencedBuiltinVariables()) {
if (data.second->value() == ast::Builtin::kVertexIndex) {
statements.emplace_back(
CreateFirstIndexOffset(in->Symbols().NameFor(vertex_index_sym),
kFirstVertexName, buffer_var, ctx.dst));
} else if (data.second->value() == ast::Builtin::kInstanceIndex) {
statements.emplace_back(CreateFirstIndexOffset(
in->Symbols().NameFor(instance_index_sym), kFirstInstanceName,
buffer_var, ctx.dst));
}
}
return nullptr; // Just clone var
})
.ReplaceAll( // Note: This happens in the same pass as the rename above
// which determines the original builtin variable names,
// but this should be fine, as variables are cloned first.
[&](CloneContext* ctx, ast::Function* func) -> ast::Function* {
maybe_create_buffer_var(ctx->dst);
if (buffer_var == nullptr) {
return nullptr; // no transform need, just clone func
}
auto* func_sem = in->Sem().Get(func);
ast::StatementList statements;
for (const auto& data :
func_sem->LocalReferencedBuiltinVariables()) {
if (data.second->value() == ast::Builtin::kVertexIndex) {
statements.emplace_back(CreateFirstIndexOffset(
in->Symbols().NameFor(vertex_index_sym), kFirstVertexName,
buffer_var, ctx->dst));
} else if (data.second->value() == ast::Builtin::kInstanceIndex) {
statements.emplace_back(CreateFirstIndexOffset(
in->Symbols().NameFor(instance_index_sym),
kFirstInstanceName, buffer_var, ctx->dst));
}
}
return CloneWithStatementsAtStart(ctx, func, statements);
})
.Clone();
return CloneWithStatementsAtStart(&ctx, func, statements);
});
ctx.Clone();
return Output(
Program(std::move(out)),

View File

@@ -113,7 +113,7 @@ Transform::Output VertexPulling::Run(const Program* in) {
for (auto& replacement : state.location_replacements) {
ctx.Replace(replacement.from, replacement.to);
}
ctx.ReplaceAll([&](CloneContext*, ast::Function* f) -> ast::Function* {
ctx.ReplaceAll([&](ast::Function* f) -> ast::Function* {
if (f == func) {
return CloneWithStatementsAtStart(&ctx, f,
{state.CreateVertexPullingPreamble()});