Remove BlockStatement::append()

Bug: tint:396
Bug: tint:390
Change-Id: I3b558a8961f9890f24d1aa3d6647ec095e5fe1cb
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35421
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-12-14 20:25:27 +00:00
committed by Commit Bot service account
parent ed70caf6a5
commit db5ce658b5
60 changed files with 2696 additions and 2046 deletions

View File

@@ -91,24 +91,21 @@ class BoundArrayAccessorsTest : public testing::Test {
};
struct ModuleBuilder : public ast::BuilderWithModule {
ModuleBuilder() : body_(create<ast::BlockStatement>(Source{})) {
mod->AddFunction(create<ast::Function>(
Source{}, mod->RegisterSymbol("func"), "func", ast::VariableList{},
ty.void_, body_, ast::FunctionDecorationList{}));
}
ast::Module Module() {
Build();
auto* body = create<ast::BlockStatement>(Source{}, statements);
mod->AddFunction(create<ast::Function>(
Source{}, mod->RegisterSymbol("func"), "func", ast::VariableList{},
ty.void_, body, ast::FunctionDecorationList{}));
return std::move(*mod);
}
protected:
virtual void Build() = 0;
void OnVariableBuilt(ast::Variable* var) override {
ASSERT_NE(body_, nullptr);
body_->append(create<ast::VariableDeclStatement>(Source{}, var));
statements.emplace_back(create<ast::VariableDeclStatement>(Source{}, var));
}
ast::BlockStatement* body_ = nullptr;
ast::StatementList statements;
};
TEST_F(BoundArrayAccessorsTest, Ptrs_Clamp) {

View File

@@ -53,16 +53,18 @@ struct ModuleBuilder : public ast::BuilderWithModule {
TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
struct Builder : ModuleBuilder {
void Build() override {
auto* block = create<ast::BlockStatement>(Source{});
block->append(create<ast::VariableDeclStatement>(
Source{}, Var("builtin_assignments_should_happen_before_this",
tint::ast::StorageClass::kFunction, ty.f32)));
auto* block = create<ast::BlockStatement>(
Source{},
ast::StatementList{
create<ast::VariableDeclStatement>(
Source{}, Var("builtin_assignments_should_happen_before_this",
tint::ast::StorageClass::kFunction, ty.f32)),
});
auto a_sym = mod->RegisterSymbol("non_entry_a");
mod->AddFunction(create<ast::Function>(
Source{}, a_sym, "non_entry_a", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{}));
auto entry_sym = mod->RegisterSymbol("entry");
@@ -77,7 +79,7 @@ TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
auto b_sym = mod->RegisterSymbol("non_entry_b");
mod->AddFunction(create<ast::Function>(
Source{}, b_sym, "non_entry_b", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{}));
}
};
@@ -131,13 +133,13 @@ TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
auto a_sym = mod->RegisterSymbol("non_entry_a");
mod->AddFunction(create<ast::Function>(
Source{}, a_sym, "non_entry_a", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{}));
auto entry_sym = mod->RegisterSymbol("entry");
mod->AddFunction(create<ast::Function>(
Source{}, entry_sym, "entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex,
Source{}),
@@ -146,7 +148,7 @@ TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
auto b_sym = mod->RegisterSymbol("non_entry_b");
mod->AddFunction(create<ast::Function>(
Source{}, b_sym, "non_entry_b", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{}));
}
};
@@ -193,7 +195,7 @@ TEST_F(EmitVertexPointSizeTest, NonVertexStage) {
auto frag_sym = mod->RegisterSymbol("fragment_entry");
auto* fragment_entry = create<ast::Function>(
Source{}, frag_sym, "fragment_entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kFragment,
Source{}),
@@ -203,7 +205,7 @@ TEST_F(EmitVertexPointSizeTest, NonVertexStage) {
auto comp_sym = mod->RegisterSymbol("compute_entry");
auto* compute_entry = create<ast::Function>(
Source{}, comp_sym, "compute_entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kCompute,
Source{}),

View File

@@ -154,24 +154,25 @@ Transform::Output FirstIndexOffset::Run(ast::Module* in) {
if (buffer_var == nullptr) {
return nullptr; // no transform need, just clone func
}
auto* body = ctx.mod->create<ast::BlockStatement>(
ctx.Clone(func->body()->source()));
ast::StatementList statements;
for (const auto& data : func->local_referenced_builtin_variables()) {
if (data.second->value() == ast::Builtin::kVertexIdx) {
body->append(CreateFirstIndexOffset(
statements.emplace_back(CreateFirstIndexOffset(
vertex_index_name, kFirstVertexName, buffer_var, ctx.mod));
} else if (data.second->value() == ast::Builtin::kInstanceIdx) {
body->append(CreateFirstIndexOffset(
statements.emplace_back(CreateFirstIndexOffset(
instance_index_name, kFirstInstanceName, buffer_var, ctx.mod));
}
}
for (auto* s : *func->body()) {
body->append(ctx.Clone(s));
statements.emplace_back(ctx.Clone(s));
}
return ctx.mod->create<ast::Function>(
ctx.Clone(func->source()), func->symbol(), func->name(),
ctx.Clone(func->params()), ctx.Clone(func->return_type()),
ctx.Clone(body), ctx.Clone(func->decorations()));
ctx.mod->create<ast::BlockStatement>(
ctx.Clone(func->body()->source()), statements),
ctx.Clone(func->decorations()));
});
in->Clone(&ctx);

View File

@@ -58,10 +58,11 @@ struct ModuleBuilder : public ast::BuilderWithModule {
}
ast::Function* AddFunction(const std::string& name,
ast::VariableList params = {}) {
ast::StatementList stmts) {
auto* func = create<ast::Function>(
Source{}, mod->RegisterSymbol(name), name, std::move(params), ty.u32,
create<ast::BlockStatement>(Source{}), ast::FunctionDecorationList());
Source{}, mod->RegisterSymbol(name), name, ast::VariableList{}, ty.u32,
create<ast::BlockStatement>(Source{}, stmts),
ast::FunctionDecorationList{});
mod->AddFunction(func);
return func;
}
@@ -73,10 +74,14 @@ TEST_F(FirstIndexOffsetTest, Error_AlreadyTransformed) {
struct Builder : public ModuleBuilder {
void Build() override {
AddBuiltinInput("vert_idx", ast::Builtin::kVertexIdx);
AddFunction("test")->body()->append(create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("vert_idx"), "vert_idx")));
AddFunction(
"test",
{
create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("vert_idx"), "vert_idx")),
});
}
};
@@ -116,10 +121,14 @@ TEST_F(FirstIndexOffsetTest, BasicModuleVertexIndex) {
struct Builder : public ModuleBuilder {
void Build() override {
AddBuiltinInput("vert_idx", ast::Builtin::kVertexIdx);
AddFunction("test")->body()->append(create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("vert_idx"), "vert_idx")));
AddFunction(
"test",
{
create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("vert_idx"), "vert_idx")),
});
}
};
@@ -194,10 +203,14 @@ TEST_F(FirstIndexOffsetTest, BasicModuleInstanceIndex) {
struct Builder : public ModuleBuilder {
void Build() override {
AddBuiltinInput("inst_idx", ast::Builtin::kInstanceIdx);
AddFunction("test")->body()->append(create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("inst_idx"), "inst_idx")));
AddFunction(
"test",
{
create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("inst_idx"), "inst_idx")),
});
}
};
@@ -272,8 +285,9 @@ TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
void Build() override {
AddBuiltinInput("inst_idx", ast::Builtin::kInstanceIdx);
AddBuiltinInput("vert_idx", ast::Builtin::kVertexIdx);
AddFunction("test")->body()->append(
create<ast::ReturnStatement>(Source{}, Expr(1u)));
AddFunction("test", {
create<ast::ReturnStatement>(Source{}, Expr(1u)),
});
}
};
@@ -348,18 +362,25 @@ TEST_F(FirstIndexOffsetTest, NestedCalls) {
struct Builder : public ModuleBuilder {
void Build() override {
AddBuiltinInput("vert_idx", ast::Builtin::kVertexIdx);
ast::Function* func1 = AddFunction("func1");
func1->body()->append(create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("vert_idx"), "vert_idx")));
ast::Function* func2 = AddFunction("func2");
func2->body()->append(create<ast::ReturnStatement>(
Source{}, create<ast::CallExpression>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("func1"), "func1"),
ast::ExpressionList{})));
AddFunction(
"func1",
{
create<ast::ReturnStatement>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("vert_idx"), "vert_idx")),
});
AddFunction(
"func2",
{
create<ast::ReturnStatement>(
Source{},
create<ast::CallExpression>(
Source{},
create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol("func1"), "func1"),
ast::ExpressionList{})),
});
}
};

View File

@@ -105,7 +105,7 @@ Transform::Output VertexPulling::Run(ast::Module* in) {
state.FindOrInsertInstanceIndexIfUsed();
state.ConvertVertexInputVariablesToPrivate();
state.AddVertexStorageBuffers();
state.AddVertexPullingPreamble(func);
func->body()->insert(0, state.CreateVertexPullingPreamble());
return out;
}
@@ -286,13 +286,11 @@ void VertexPulling::State::AddVertexStorageBuffers() {
mod->AddConstructedType(struct_type);
}
void VertexPulling::State::AddVertexPullingPreamble(
ast::Function* vertex_func) {
ast::BlockStatement* VertexPulling::State::CreateVertexPullingPreamble() {
// Assign by looking at the vertex descriptor to find attributes with matching
// location.
// A block statement allowing us to use append instead of insert
auto* block = mod->create<ast::BlockStatement>(Source{});
ast::StatementList stmts;
// Declare the |kPullingPosVarName| variable in the shader
auto* pos_declaration = mod->create<ast::VariableDeclStatement>(
@@ -308,7 +306,7 @@ void VertexPulling::State::AddVertexPullingPreamble(
// |kPullingPosVarName| refers to the byte location of the current read. We
// declare a variable in the shader to avoid having to reuse Expression
// objects.
block->append(pos_declaration);
stmts.emplace_back(pos_declaration);
for (uint32_t i = 0; i < cfg.vertex_state.size(); ++i) {
const VertexBufferLayoutDescriptor& buffer_layout = cfg.vertex_state[i];
@@ -339,9 +337,9 @@ void VertexPulling::State::AddVertexPullingPreamble(
// Update position of the read
auto* set_pos_expr = mod->create<ast::AssignmentStatement>(
Source{}, CreatePullingPositionIdent(), pos_value);
block->append(set_pos_expr);
stmts.emplace_back(set_pos_expr);
block->append(mod->create<ast::AssignmentStatement>(
stmts.emplace_back(mod->create<ast::AssignmentStatement>(
Source{},
mod->create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol(v->name()), v->name()),
@@ -349,7 +347,7 @@ void VertexPulling::State::AddVertexPullingPreamble(
}
}
vertex_func->body()->insert(0, block);
return mod->create<ast::BlockStatement>(Source{}, stmts);
}
ast::Expression* VertexPulling::State::GenUint(uint32_t value) {

View File

@@ -197,8 +197,8 @@ class VertexPulling : public Transform {
/// Adds storage buffer decorated variables for the vertex buffers
void AddVertexStorageBuffers();
/// Adds assignment to the variables from the buffers
void AddVertexPullingPreamble(ast::Function* vertex_func);
/// Creates and returns the assignment to the variables from the buffers
ast::BlockStatement* CreateVertexPullingPreamble();
/// Generates an expression holding a constant uint
/// @param value uint value

View File

@@ -47,8 +47,9 @@ class VertexPullingHelper {
// Create basic module with an entry point and vertex function
void InitBasicModule() {
auto* func = create<ast::Function>(
Source{}, mod_->RegisterSymbol("main"), "main", ast::VariableList{},
mod_->create<ast::type::Void>(), create<ast::BlockStatement>(Source{}),
Source{}, mod()->RegisterSymbol("main"), "main", ast::VariableList{},
mod_->create<ast::type::Void>(),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{create<ast::StageDecoration>(
ast::PipelineStage::kVertex, Source{})});
mod()->AddFunction(func);
@@ -135,7 +136,8 @@ TEST_F(VertexPullingTest, Error_InvalidEntryPoint) {
TEST_F(VertexPullingTest, Error_EntryPointWrongStage) {
auto* func = create<ast::Function>(
Source{}, mod()->RegisterSymbol("main"), "main", ast::VariableList{},
mod()->create<ast::type::Void>(), create<ast::BlockStatement>(Source{}),
mod()->create<ast::type::Void>(),
create<ast::BlockStatement>(Source{}, ast::StatementList{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kFragment, Source{}),
});