resolver: Optimize type dispatch with Switch()

Bug: tint:1383
Change-Id: Ia02c7ddd3e46d36134f5430e4f22df04993b2158
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/81104
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Kokoro: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton 2022-02-18 22:06:33 +00:00 committed by Tint LUCI CQ
parent 473b6087ac
commit 38f1e9c75c
1 changed files with 161 additions and 180 deletions

View File

@ -139,34 +139,31 @@ class DependencyScanner {
/// dependencies of each global. /// dependencies of each global.
void Scan(Global* global) { void Scan(Global* global) {
TINT_SCOPED_ASSIGNMENT(current_global_, global); TINT_SCOPED_ASSIGNMENT(current_global_, global);
Switch(
if (auto* str = global->node->As<ast::Struct>()) { global->node,
Declare(str->name, str); [&](const ast::Struct* str) {
for (auto* member : str->members) { Declare(str->name, str);
TraverseType(member->type); for (auto* member : str->members) {
} TraverseType(member->type);
return; }
} },
if (auto* alias = global->node->As<ast::Alias>()) { [&](const ast::Alias* alias) {
Declare(alias->name, alias); Declare(alias->name, alias);
TraverseType(alias->type); TraverseType(alias->type);
return; },
} [&](const ast::Function* func) {
if (auto* func = global->node->As<ast::Function>()) { Declare(func->symbol, func);
Declare(func->symbol, func); TraverseAttributes(func->attributes);
TraverseAttributes(func->attributes); TraverseFunction(func);
TraverseFunction(func); },
return; [&](const ast::Variable* var) {
} Declare(var->symbol, var);
if (auto* var = global->node->As<ast::Variable>()) { TraverseType(var->type);
Declare(var->symbol, var); if (var->constructor) {
TraverseType(var->type); TraverseExpression(var->constructor);
if (var->constructor) { }
TraverseExpression(var->constructor); },
} [&](Default) { UnhandledNode(diagnostics_, global->node); });
return;
}
UnhandledNode(diagnostics_, global->node);
} }
private: private:
@ -208,78 +205,72 @@ class DependencyScanner {
/// Traverses the statement, performing symbol resolution and determining /// Traverses the statement, performing symbol resolution and determining
/// global dependencies. /// global dependencies.
void TraverseStatement(const ast::Statement* stmt) { void TraverseStatement(const ast::Statement* stmt) {
if (stmt == nullptr) { if (!stmt) {
return; return;
} }
if (auto* b = stmt->As<ast::AssignmentStatement>()) { Switch(
TraverseExpression(b->lhs); stmt, //
TraverseExpression(b->rhs); [&](const ast::AssignmentStatement* a) {
return; TraverseExpression(a->lhs);
} TraverseExpression(a->rhs);
if (auto* b = stmt->As<ast::BlockStatement>()) { },
scope_stack_.Push(); [&](const ast::BlockStatement* b) {
TINT_DEFER(scope_stack_.Pop()); scope_stack_.Push();
TraverseStatements(b->statements); TINT_DEFER(scope_stack_.Pop());
return; TraverseStatements(b->statements);
} },
if (auto* r = stmt->As<ast::CallStatement>()) { [&](const ast::CallStatement* r) { //
TraverseExpression(r->expr); TraverseExpression(r->expr);
return; },
} [&](const ast::ForLoopStatement* l) {
if (auto* l = stmt->As<ast::ForLoopStatement>()) { scope_stack_.Push();
scope_stack_.Push(); TINT_DEFER(scope_stack_.Pop());
TINT_DEFER(scope_stack_.Pop()); TraverseStatement(l->initializer);
TraverseStatement(l->initializer); TraverseExpression(l->condition);
TraverseExpression(l->condition); TraverseStatement(l->continuing);
TraverseStatement(l->continuing); TraverseStatement(l->body);
TraverseStatement(l->body); },
return; [&](const ast::LoopStatement* l) {
} scope_stack_.Push();
if (auto* l = stmt->As<ast::LoopStatement>()) { TINT_DEFER(scope_stack_.Pop());
scope_stack_.Push(); TraverseStatements(l->body->statements);
TINT_DEFER(scope_stack_.Pop()); TraverseStatement(l->continuing);
TraverseStatements(l->body->statements); },
TraverseStatement(l->continuing); [&](const ast::IfStatement* i) {
return; TraverseExpression(i->condition);
} TraverseStatement(i->body);
if (auto* i = stmt->As<ast::IfStatement>()) { for (auto* e : i->else_statements) {
TraverseExpression(i->condition); TraverseExpression(e->condition);
TraverseStatement(i->body); TraverseStatement(e->body);
for (auto* e : i->else_statements) { }
TraverseExpression(e->condition); },
TraverseStatement(e->body); [&](const ast::ReturnStatement* r) { //
} TraverseExpression(r->value);
return; },
} [&](const ast::SwitchStatement* s) {
if (auto* r = stmt->As<ast::ReturnStatement>()) { TraverseExpression(s->condition);
TraverseExpression(r->value); for (auto* c : s->body) {
return; for (auto* sel : c->selectors) {
} TraverseExpression(sel);
if (auto* s = stmt->As<ast::SwitchStatement>()) { }
TraverseExpression(s->condition); TraverseStatement(c->body);
for (auto* c : s->body) { }
for (auto* sel : c->selectors) { },
TraverseExpression(sel); [&](const ast::VariableDeclStatement* v) {
} if (auto* shadows = scope_stack_.Get(v->variable->symbol)) {
TraverseStatement(c->body); graph_.shadows.emplace(v->variable, shadows);
} }
return; TraverseType(v->variable->type);
} TraverseExpression(v->variable->constructor);
if (auto* v = stmt->As<ast::VariableDeclStatement>()) { Declare(v->variable->symbol, v->variable);
if (auto* shadows = scope_stack_.Get(v->variable->symbol)) { },
graph_.shadows.emplace(v->variable, shadows); [&](Default) {
} if (!stmt->IsAnyOf<ast::BreakStatement, ast::ContinueStatement,
TraverseType(v->variable->type); ast::DiscardStatement,
TraverseExpression(v->variable->constructor); ast::FallthroughStatement>()) {
Declare(v->variable->symbol, v->variable); UnhandledNode(diagnostics_, stmt);
return; }
} });
if (stmt->IsAnyOf<ast::BreakStatement, ast::ContinueStatement,
ast::DiscardStatement, ast::FallthroughStatement>()) {
return;
}
UnhandledNode(diagnostics_, stmt);
} }
/// Adds the symbol definition to the current scope, raising an error if two /// Adds the symbol definition to the current scope, raising an error if two
@ -302,21 +293,23 @@ class DependencyScanner {
} }
ast::TraverseExpressions( ast::TraverseExpressions(
root, diagnostics_, [&](const ast::Expression* expr) { root, diagnostics_, [&](const ast::Expression* expr) {
if (auto* ident = expr->As<ast::IdentifierExpression>()) { Switch(
AddDependency(ident, ident->symbol, "identifier", "references"); expr,
} [&](const ast::IdentifierExpression* ident) {
if (auto* call = expr->As<ast::CallExpression>()) { AddDependency(ident, ident->symbol, "identifier", "references");
if (call->target.name) { },
AddDependency(call->target.name, call->target.name->symbol, [&](const ast::CallExpression* call) {
"function", "calls"); if (call->target.name) {
} AddDependency(call->target.name, call->target.name->symbol,
if (call->target.type) { "function", "calls");
TraverseType(call->target.type); }
} if (call->target.type) {
} TraverseType(call->target.type);
if (auto* cast = expr->As<ast::BitcastExpression>()) { }
TraverseType(cast->type); },
} [&](const ast::BitcastExpression* cast) {
TraverseType(cast->type);
});
return ast::TraverseAction::Descend; return ast::TraverseAction::Descend;
}); });
} }
@ -324,50 +317,44 @@ class DependencyScanner {
/// Traverses the type node, performing symbol resolution and determining /// Traverses the type node, performing symbol resolution and determining
/// global dependencies. /// global dependencies.
void TraverseType(const ast::Type* ty) { void TraverseType(const ast::Type* ty) {
if (ty == nullptr) { if (!ty) {
return; return;
} }
if (auto* arr = ty->As<ast::Array>()) { Switch(
TraverseType(arr->type); ty, //
TraverseExpression(arr->count); [&](const ast::Array* arr) {
return; TraverseType(arr->type); //
} TraverseExpression(arr->count);
if (auto* atomic = ty->As<ast::Atomic>()) { },
TraverseType(atomic->type); [&](const ast::Atomic* atomic) { //
return; TraverseType(atomic->type);
} },
if (auto* mat = ty->As<ast::Matrix>()) { [&](const ast::Matrix* mat) { //
TraverseType(mat->type); TraverseType(mat->type);
return; },
} [&](const ast::Pointer* ptr) { //
if (auto* ptr = ty->As<ast::Pointer>()) { TraverseType(ptr->type);
TraverseType(ptr->type); },
return; [&](const ast::TypeName* tn) { //
} AddDependency(tn, tn->name, "type", "references");
if (auto* tn = ty->As<ast::TypeName>()) { },
AddDependency(tn, tn->name, "type", "references"); [&](const ast::Vector* vec) { //
return; TraverseType(vec->type);
} },
if (auto* vec = ty->As<ast::Vector>()) { [&](const ast::SampledTexture* tex) { //
TraverseType(vec->type); TraverseType(tex->type);
return; },
} [&](const ast::MultisampledTexture* tex) { //
if (auto* tex = ty->As<ast::SampledTexture>()) { TraverseType(tex->type);
TraverseType(tex->type); },
return; [&](Default) {
} if (!ty->IsAnyOf<ast::Void, ast::Bool, ast::I32, ast::U32, ast::F32,
if (auto* tex = ty->As<ast::MultisampledTexture>()) { ast::DepthTexture, ast::DepthMultisampledTexture,
TraverseType(tex->type); ast::StorageTexture, ast::ExternalTexture,
return; ast::Sampler>()) {
} UnhandledNode(diagnostics_, ty);
if (ty->IsAnyOf<ast::Void, ast::Bool, ast::I32, ast::U32, ast::F32, }
ast::DepthTexture, ast::DepthMultisampledTexture, });
ast::StorageTexture, ast::ExternalTexture,
ast::Sampler>()) {
return;
}
UnhandledNode(diagnostics_, ty);
} }
/// Traverses the attribute list, performing symbol resolution and /// Traverses the attribute list, performing symbol resolution and
@ -490,17 +477,15 @@ struct DependencyAnalysis {
/// @note will raise an ICE if the node is not a type, function or variable /// @note will raise an ICE if the node is not a type, function or variable
/// declaration /// declaration
Symbol SymbolOf(const ast::Node* node) const { Symbol SymbolOf(const ast::Node* node) const {
if (auto* td = node->As<ast::TypeDecl>()) { return Switch(
return td->name; node, //
} [&](const ast::TypeDecl* td) { return td->name; },
if (auto* func = node->As<ast::Function>()) { [&](const ast::Function* func) { return func->symbol; },
return func->symbol; [&](const ast::Variable* var) { return var->symbol; },
} [&](Default) {
if (auto* var = node->As<ast::Variable>()) { UnhandledNode(diagnostics_, node);
return var->symbol; return Symbol{};
} });
UnhandledNode(diagnostics_, node);
return {};
} }
/// @param node the ast::Node of the global declaration /// @param node the ast::Node of the global declaration
@ -516,20 +501,16 @@ struct DependencyAnalysis {
/// @note will raise an ICE if the node is not a type, function or variable /// @note will raise an ICE if the node is not a type, function or variable
/// declaration /// declaration
std::string KindOf(const ast::Node* node) { std::string KindOf(const ast::Node* node) {
if (node->Is<ast::Struct>()) { return Switch(
return "struct"; node, //
} [&](const ast::Struct*) { return "struct"; },
if (node->Is<ast::Alias>()) { [&](const ast::Alias*) { return "alias"; },
return "alias"; [&](const ast::Function*) { return "function"; },
} [&](const ast::Variable* var) { return var->is_const ? "let" : "var"; },
if (node->Is<ast::Function>()) { [&](Default) {
return "function"; UnhandledNode(diagnostics_, node);
} return "<error>";
if (auto* var = node->As<ast::Variable>()) { });
return var->is_const ? "let" : "var";
}
UnhandledNode(diagnostics_, node);
return {};
} }
/// Traverses `module`, collecting all the global declarations and populating /// Traverses `module`, collecting all the global declarations and populating