Add a symbol to the Function AST node.

This Cl adds a Symbol representing the function name to the function
AST. The symbol is added alongside the name for now. When all usages of
the function name are removed then the string version will be removed
from the constructor.

Change-Id: Ib2450e5fe531e988b25bb7d2937acc6af2187871
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35220
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
Auto-Submit: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair
2020-12-11 18:24:53 +00:00
committed by Commit Bot service account
parent cd9e5f6e91
commit a41132fcd8
48 changed files with 923 additions and 658 deletions

View File

@@ -51,7 +51,7 @@ namespace {
template <typename T = ast::Expression>
T* FindVariable(ast::Module* mod, std::string name) {
if (auto* func = mod->FindFunctionByName("func")) {
if (auto* func = mod->FindFunctionBySymbol(mod->RegisterSymbol("func"))) {
for (auto* stmt : *func->body()) {
if (auto* decl = stmt->As<ast::VariableDeclStatement>()) {
if (auto* var = decl->variable()) {
@@ -92,9 +92,9 @@ class BoundArrayAccessorsTest : public testing::Test {
struct ModuleBuilder : public ast::BuilderWithModule {
ModuleBuilder() : body_(create<ast::BlockStatement>()) {
mod->AddFunction(create<ast::Function>(Source{}, "func",
ast::VariableList{}, ty.void_, body_,
ast::FunctionDecorationList{}));
mod->AddFunction(create<ast::Function>(
Source{}, mod->RegisterSymbol("func"), "func", ast::VariableList{},
ty.void_, body_, ast::FunctionDecorationList{}));
}
ast::Module Module() {

View File

@@ -58,23 +58,26 @@ TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
Var("builtin_assignments_should_happen_before_this",
tint::ast::StorageClass::kFunction, ty.f32)));
mod->AddFunction(
create<ast::Function>(Source{}, "non_entry_a", ast::VariableList{},
ty.void_, create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{}));
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{}),
ast::FunctionDecorationList{}));
auto entry_sym = mod->RegisterSymbol("entry");
auto* entry = create<ast::Function>(
Source{}, "entry", ast::VariableList{}, ty.void_, block,
Source{}, entry_sym, "entry", ast::VariableList{}, ty.void_, block,
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex,
Source{}),
});
mod->AddFunction(entry);
mod->AddFunction(
create<ast::Function>(Source{}, "non_entry_b", ast::VariableList{},
ty.void_, create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{}));
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{}),
ast::FunctionDecorationList{}));
}
};
@@ -82,7 +85,7 @@ TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{
auto expected = R"(Module{
Variable{
Decorations{
BuiltinDecoration{pointsize}
@@ -91,11 +94,13 @@ TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
out
__f32
}
Function non_entry_a -> __void
Function )" + result.module.RegisterSymbol("non_entry_a").to_str() +
R"( -> __void
()
{
}
Function entry -> __void
Function )" + result.module.RegisterSymbol("entry").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{
@@ -111,7 +116,8 @@ TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
}
}
}
Function non_entry_b -> __void
Function )" + result.module.RegisterSymbol("non_entry_b").to_str() +
R"( -> __void
()
{
}
@@ -123,23 +129,26 @@ TEST_F(EmitVertexPointSizeTest, VertexStageBasic) {
TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
struct Builder : ModuleBuilder {
void Build() override {
mod->AddFunction(
create<ast::Function>(Source{}, "non_entry_a", ast::VariableList{},
ty.void_, create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{}));
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{}),
ast::FunctionDecorationList{}));
mod->AddFunction(
create<ast::Function>(Source{}, "entry", ast::VariableList{},
ty.void_, create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(
ast::PipelineStage::kVertex, Source{}),
}));
auto entry_sym = mod->RegisterSymbol("entry");
mod->AddFunction(create<ast::Function>(
Source{}, entry_sym, "entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex,
Source{}),
}));
mod->AddFunction(
create<ast::Function>(Source{}, "non_entry_b", ast::VariableList{},
ty.void_, create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{}));
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{}),
ast::FunctionDecorationList{}));
}
};
@@ -147,7 +156,7 @@ TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{
auto expected = R"(Module{
Variable{
Decorations{
BuiltinDecoration{pointsize}
@@ -156,11 +165,13 @@ TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
out
__f32
}
Function non_entry_a -> __void
Function )" + result.module.RegisterSymbol("non_entry_a").to_str() +
R"( -> __void
()
{
}
Function entry -> __void
Function )" + result.module.RegisterSymbol("entry").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{
@@ -169,7 +180,8 @@ TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
ScalarConstructor[__f32]{1.000000}
}
}
Function non_entry_b -> __void
Function )" + result.module.RegisterSymbol("non_entry_b").to_str() +
R"( -> __void
()
{
}
@@ -181,8 +193,9 @@ TEST_F(EmitVertexPointSizeTest, VertexStageEmpty) {
TEST_F(EmitVertexPointSizeTest, NonVertexStage) {
struct Builder : ModuleBuilder {
void Build() override {
auto frag_sym = mod->RegisterSymbol("fragment_entry");
auto* fragment_entry = create<ast::Function>(
Source{}, "fragment_entry", ast::VariableList{}, ty.void_,
Source{}, frag_sym, "fragment_entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kFragment,
@@ -190,13 +203,14 @@ TEST_F(EmitVertexPointSizeTest, NonVertexStage) {
});
mod->AddFunction(fragment_entry);
auto* compute_entry =
create<ast::Function>(Source{}, "compute_entry", ast::VariableList{},
ty.void_, create<ast::BlockStatement>(Source{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(
ast::PipelineStage::kCompute, Source{}),
});
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{}),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kCompute,
Source{}),
});
mod->AddFunction(compute_entry);
}
};
@@ -205,13 +219,15 @@ TEST_F(EmitVertexPointSizeTest, NonVertexStage) {
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{
Function fragment_entry -> __void
auto expected = R"(Module{
Function )" + result.module.RegisterSymbol("fragment_entry").to_str() +
R"( -> __void
StageDecoration{fragment}
()
{
}
Function compute_entry -> __void
Function )" + result.module.RegisterSymbol("compute_entry").to_str() +
R"( -> __void
StageDecoration{compute}
()
{

View File

@@ -169,9 +169,9 @@ Transform::Output FirstIndexOffset::Run(ast::Module* in) {
body->append(ctx.Clone(s));
}
return ctx.mod->create<ast::Function>(
ctx.Clone(func->source()), func->name(), ctx.Clone(func->params()),
ctx.Clone(func->return_type()), ctx.Clone(body),
ctx.Clone(func->decorations()));
ctx.Clone(func->source()), func->symbol(), func->name(),
ctx.Clone(func->params()), ctx.Clone(func->return_type()),
ctx.Clone(body), ctx.Clone(func->decorations()));
});
in->Clone(&ctx);

View File

@@ -58,9 +58,9 @@ struct ModuleBuilder : public ast::BuilderWithModule {
ast::Function* AddFunction(const std::string& name,
ast::VariableList params = {}) {
auto* func = create<ast::Function>(Source{}, name, std::move(params),
ty.u32, create<ast::BlockStatement>(),
ast::FunctionDecorationList());
auto* func = create<ast::Function>(
Source{}, mod->RegisterSymbol(name), name, std::move(params), ty.u32,
create<ast::BlockStatement>(), ast::FunctionDecorationList());
mod->AddFunction(func);
return func;
}
@@ -154,7 +154,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleVertexIndex) {
uniform
__struct_TintFirstIndexOffsetData
}
Function test -> __u32
Function tint_symbol_1 -> __u32
()
{
VariableDeclStatement{
@@ -229,7 +229,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleInstanceIndex) {
uniform
__struct_TintFirstIndexOffsetData
}
Function test -> __u32
Function tint_symbol_1 -> __u32
()
{
VariableDeclStatement{
@@ -317,7 +317,7 @@ TEST_F(FirstIndexOffsetTest, BasicModuleBothIndex) {
uniform
__struct_TintFirstIndexOffsetData
}
Function test -> __u32
Function tint_symbol_1 -> __u32
()
{
Return{
@@ -389,7 +389,7 @@ TEST_F(FirstIndexOffsetTest, NestedCalls) {
uniform
__struct_TintFirstIndexOffsetData
}
Function func1 -> __u32
Function tint_symbol_1 -> __u32
()
{
VariableDeclStatement{
@@ -415,7 +415,7 @@ TEST_F(FirstIndexOffsetTest, NestedCalls) {
}
}
}
Function func2 -> __u32
Function tint_symbol_2 -> __u32
()
{
Return{

View File

@@ -84,8 +84,8 @@ Transform::Output VertexPulling::Run(ast::Module* in) {
}
// Find entry point
auto* func = mod->FindFunctionByNameAndStage(cfg.entry_point_name,
ast::PipelineStage::kVertex);
auto* func = mod->FindFunctionBySymbolAndStage(
mod->GetSymbol(cfg.entry_point_name), ast::PipelineStage::kVertex);
if (func == nullptr) {
diag::Diagnostic err;
err.severity = diag::Severity::Error;
@@ -94,9 +94,6 @@ Transform::Output VertexPulling::Run(ast::Module* in) {
return out;
}
// Save the vertex function
auto* vertex_func = mod->FindFunctionByName(func->name());
// TODO(idanr): Need to check shader locations in descriptor cover all
// attributes
@@ -108,7 +105,7 @@ Transform::Output VertexPulling::Run(ast::Module* in) {
state.FindOrInsertInstanceIndexIfUsed();
state.ConvertVertexInputVariablesToPrivate();
state.AddVertexStorageBuffers();
state.AddVertexPullingPreamble(vertex_func);
state.AddVertexPullingPreamble(func);
return out;
}

View File

@@ -47,8 +47,8 @@ class VertexPullingHelper {
// Create basic module with an entry point and vertex function
void InitBasicModule() {
auto* func = create<ast::Function>(
Source{}, "main", ast::VariableList{}, mod_->create<ast::type::Void>(),
create<ast::BlockStatement>(),
Source{}, mod_->RegisterSymbol("main"), "main", ast::VariableList{},
mod_->create<ast::type::Void>(), create<ast::BlockStatement>(),
ast::FunctionDecorationList{create<ast::StageDecoration>(
ast::PipelineStage::kVertex, Source{})});
mod()->AddFunction(func);
@@ -134,8 +134,8 @@ TEST_F(VertexPullingTest, Error_InvalidEntryPoint) {
TEST_F(VertexPullingTest, Error_EntryPointWrongStage) {
auto* func = create<ast::Function>(
Source{}, "main", ast::VariableList{}, mod()->create<ast::type::Void>(),
create<ast::BlockStatement>(),
Source{}, mod()->RegisterSymbol("main"), "main", ast::VariableList{},
mod()->create<ast::type::Void>(), create<ast::BlockStatement>(),
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kFragment, Source{}),
});
@@ -152,7 +152,8 @@ TEST_F(VertexPullingTest, BasicModule) {
InitBasicModule();
InitTransform({});
auto result = manager()->Run(mod());
ASSERT_FALSE(result.diagnostics.contains_errors());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
}
TEST_F(VertexPullingTest, OneAttribute) {
@@ -164,7 +165,8 @@ TEST_F(VertexPullingTest, OneAttribute) {
InitTransform({{{4, InputStepMode::kVertex, {{VertexFormat::kF32, 0, 0}}}}});
auto result = manager()->Run(mod());
ASSERT_FALSE(result.diagnostics.contains_errors());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
EXPECT_EQ(R"(Module{
TintVertexData Struct{
@@ -193,7 +195,8 @@ TEST_F(VertexPullingTest, OneAttribute) {
storage_buffer
__struct_TintVertexData
}
Function main -> __void
Function )" + result.module.GetSymbol("main").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{
@@ -250,7 +253,8 @@ TEST_F(VertexPullingTest, OneInstancedAttribute) {
{{{4, InputStepMode::kInstance, {{VertexFormat::kF32, 0, 0}}}}});
auto result = manager()->Run(mod());
ASSERT_FALSE(result.diagnostics.contains_errors());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
EXPECT_EQ(R"(Module{
TintVertexData Struct{
@@ -279,7 +283,8 @@ TEST_F(VertexPullingTest, OneInstancedAttribute) {
storage_buffer
__struct_TintVertexData
}
Function main -> __void
Function )" + result.module.GetSymbol("main").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{
@@ -336,7 +341,8 @@ TEST_F(VertexPullingTest, OneAttributeDifferentOutputSet) {
transform()->SetPullingBufferBindingSet(5);
auto result = manager()->Run(mod());
ASSERT_FALSE(result.diagnostics.contains_errors());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
EXPECT_EQ(R"(Module{
TintVertexData Struct{
@@ -365,7 +371,8 @@ TEST_F(VertexPullingTest, OneAttributeDifferentOutputSet) {
storage_buffer
__struct_TintVertexData
}
Function main -> __void
Function )" + result.module.GetSymbol("main").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{
@@ -451,7 +458,8 @@ TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
{4, InputStepMode::kInstance, {{VertexFormat::kF32, 0, 1}}}}});
auto result = manager()->Run(mod());
ASSERT_FALSE(result.diagnostics.contains_errors());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
EXPECT_EQ(R"(Module{
TintVertexData Struct{
@@ -502,7 +510,8 @@ TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
storage_buffer
__struct_TintVertexData
}
Function main -> __void
Function )" + result.module.GetSymbol("main").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{
@@ -592,7 +601,8 @@ TEST_F(VertexPullingTest, TwoAttributesSameBuffer) {
{{VertexFormat::kF32, 0, 0}, {VertexFormat::kVec4F32, 0, 1}}}}});
auto result = manager()->Run(mod());
ASSERT_FALSE(result.diagnostics.contains_errors());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
EXPECT_EQ(R"(Module{
TintVertexData Struct{
@@ -626,7 +636,8 @@ TEST_F(VertexPullingTest, TwoAttributesSameBuffer) {
storage_buffer
__struct_TintVertexData
}
Function main -> __void
Function )" + result.module.GetSymbol("main").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{
@@ -778,7 +789,8 @@ TEST_F(VertexPullingTest, FloatVectorAttributes) {
{16, InputStepMode::kVertex, {{VertexFormat::kVec4F32, 0, 2}}}}});
auto result = manager()->Run(mod());
ASSERT_FALSE(result.diagnostics.contains_errors());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
EXPECT_EQ(R"(Module{
TintVertexData Struct{
@@ -835,7 +847,8 @@ TEST_F(VertexPullingTest, FloatVectorAttributes) {
storage_buffer
__struct_TintVertexData
}
Function main -> __void
Function )" + result.module.GetSymbol("main").to_str() +
R"( -> __void
StageDecoration{vertex}
()
{