tint/utils: Make Hashmap::Find() safer to use

Don't return a raw pointer to the map entry's value, instead return a new Reference which re-looks up the entry if the map is mutated.

Change-Id: I031749785faeac98e2a129a776493cb0371a5cb9
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/110540
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2022-11-23 21:04:25 +00:00
committed by Dawn LUCI CQ
parent 597ad53029
commit 7c6e229a18
19 changed files with 187 additions and 113 deletions

View File

@@ -129,7 +129,7 @@ Transform::ApplyResult DecomposeStridedMatrix::Apply(const Program* src,
std::unordered_map<MatrixInfo, Symbol, MatrixInfo::Hasher> mat_to_arr;
ctx.ReplaceAll([&](const ast::AssignmentStatement* stmt) -> const ast::Statement* {
if (auto* access = src->Sem().Get<sem::StructMemberAccess>(stmt->lhs)) {
if (auto* info = decomposed.Find(access->Member()->Declaration())) {
if (auto info = decomposed.Find(access->Member()->Declaration())) {
auto fn = utils::GetOrCreate(mat_to_arr, *info, [&] {
auto name =
b.Symbols().New("mat" + std::to_string(info->matrix->columns()) + "x" +
@@ -168,7 +168,7 @@ Transform::ApplyResult DecomposeStridedMatrix::Apply(const Program* src,
std::unordered_map<MatrixInfo, Symbol, MatrixInfo::Hasher> arr_to_mat;
ctx.ReplaceAll([&](const ast::MemberAccessorExpression* expr) -> const ast::Expression* {
if (auto* access = src->Sem().Get<sem::StructMemberAccess>(expr)) {
if (auto* info = decomposed.Find(access->Member()->Declaration())) {
if (auto info = decomposed.Find(access->Member()->Declaration())) {
auto fn = utils::GetOrCreate(arr_to_mat, *info, [&] {
auto name =
b.Symbols().New("arr_to_mat" + std::to_string(info->matrix->columns()) +

View File

@@ -140,7 +140,7 @@ struct SimplifyPointers::State {
// variable identifier.
ctx.ReplaceAll([&](const ast::Expression* expr) -> const ast::Expression* {
// Look to see if we need to swap this Expression with a saved variable.
if (auto* saved_var = saved_vars.Find(expr)) {
if (auto saved_var = saved_vars.Find(expr)) {
return ctx.dst->Expr(*saved_var);
}

View File

@@ -401,7 +401,7 @@ struct Std140::State {
return Switch(
ty, //
[&](const sem::Struct* str) -> const ast::Type* {
if (auto* std140 = std140_structs.Find(str)) {
if (auto std140 = std140_structs.Find(str)) {
return b.create<ast::TypeName>(*std140);
}
return nullptr;
@@ -695,7 +695,7 @@ struct Std140::State {
// call, or by reassembling a std140 matrix from column vector members.
utils::Vector<const ast::Expression*, 8> args;
for (auto* member : str->Members()) {
if (auto* col_members = std140_mat_members.Find(member)) {
if (auto col_members = std140_mat_members.Find(member)) {
// std140 decomposed matrix. Reassemble.
auto* mat_ty = CreateASTTypeFor(ctx, member->Type());
auto mat_args =

View File

@@ -161,7 +161,7 @@ Transform::ApplyResult TruncateInterstageVariables::Apply(const Program* src,
ctx.ReplaceAll(
[&](const ast::ReturnStatement* return_statement) -> const ast::ReturnStatement* {
auto* return_sem = sem.Get(return_statement);
if (auto* mapping_fn_sym =
if (auto mapping_fn_sym =
entry_point_functions_to_truncate_functions.Find(return_sem->Function())) {
return b.Return(return_statement->source,
b.Call(*mapping_fn_sym, ctx.Clone(return_statement->value)));

View File

@@ -97,7 +97,7 @@ struct Unshadow::State {
ctx.ReplaceAll(
[&](const ast::IdentifierExpression* ident) -> const tint::ast::IdentifierExpression* {
if (auto* user = sem.Get<sem::VariableUser>(ident)) {
if (auto* renamed = renamed_to.Find(user->Variable())) {
if (auto renamed = renamed_to.Find(user->Variable())) {
return b.Expr(*renamed);
}
}

View File

@@ -135,7 +135,7 @@ struct HoistToDeclBefore::State {
/// automatically called.
/// @warning the returned reference is invalid if this is called a second time, or the
/// #for_loops map is mutated.
LoopInfo& ForLoop(const sem::ForLoopStatement* for_loop) {
auto ForLoop(const sem::ForLoopStatement* for_loop) {
if (for_loops.IsEmpty()) {
RegisterForLoopTransform();
}
@@ -147,7 +147,7 @@ struct HoistToDeclBefore::State {
/// automatically called.
/// @warning the returned reference is invalid if this is called a second time, or the
/// #for_loops map is mutated.
LoopInfo& WhileLoop(const sem::WhileStatement* while_loop) {
auto WhileLoop(const sem::WhileStatement* while_loop) {
if (while_loops.IsEmpty()) {
RegisterWhileLoopTransform();
}
@@ -159,7 +159,7 @@ struct HoistToDeclBefore::State {
/// automatically called.
/// @warning the returned reference is invalid if this is called a second time, or the
/// #else_ifs map is mutated.
ElseIfInfo& ElseIf(const ast::IfStatement* else_if) {
auto ElseIf(const ast::IfStatement* else_if) {
if (else_ifs.IsEmpty()) {
RegisterElseIfTransform();
}
@@ -172,7 +172,7 @@ struct HoistToDeclBefore::State {
auto& sem = ctx.src->Sem();
if (auto* fl = sem.Get(stmt)) {
if (auto* info = for_loops.Find(fl)) {
if (auto info = for_loops.Find(fl)) {
auto* for_loop = fl->Declaration();
// For-loop needs to be decomposed to a loop.
// Build the loop body's statements.
@@ -222,7 +222,7 @@ struct HoistToDeclBefore::State {
auto& sem = ctx.src->Sem();
if (auto* w = sem.Get(stmt)) {
if (auto* info = while_loops.Find(w)) {
if (auto info = while_loops.Find(w)) {
auto* while_loop = w->Declaration();
// While needs to be decomposed to a loop.
// Build the loop body's statements.
@@ -259,7 +259,7 @@ struct HoistToDeclBefore::State {
void RegisterElseIfTransform() const {
// Decompose 'else-if' statements into 'else { if }' blocks.
ctx.ReplaceAll([&](const ast::IfStatement* stmt) -> const ast::Statement* {
if (auto* info = else_ifs.Find(stmt)) {
if (auto info = else_ifs.Find(stmt)) {
// Build the else block's body statements, starting with let decls for the
// conditional expression.
auto body_stmts = Build(info->cond_decls);
@@ -291,10 +291,10 @@ struct HoistToDeclBefore::State {
if (else_if && else_if->Parent()->Is<sem::IfStatement>()) {
// Insertion point is an 'else if' condition.
// Need to convert 'else if' to 'else { if }'.
auto& else_if_info = ElseIf(else_if->Declaration());
auto else_if_info = ElseIf(else_if->Declaration());
// Index the map to convert this else if, even if `stmt` is nullptr.
auto& decls = else_if_info.cond_decls;
auto& decls = else_if_info->cond_decls;
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
decls.Push(std::forward<BUILDER>(builder));
}
@@ -306,7 +306,7 @@ struct HoistToDeclBefore::State {
// For-loop needs to be decomposed to a loop.
// Index the map to convert this for-loop, even if `stmt` is nullptr.
auto& decls = ForLoop(fl).cond_decls;
auto& decls = ForLoop(fl)->cond_decls;
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
decls.Push(std::forward<BUILDER>(builder));
}
@@ -318,7 +318,7 @@ struct HoistToDeclBefore::State {
// While needs to be decomposed to a loop.
// Index the map to convert this while, even if `stmt` is nullptr.
auto& decls = WhileLoop(w).cond_decls;
auto& decls = WhileLoop(w)->cond_decls;
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
decls.Push(std::forward<BUILDER>(builder));
}
@@ -354,7 +354,7 @@ struct HoistToDeclBefore::State {
// For-loop needs to be decomposed to a loop.
// Index the map to convert this for-loop, even if `stmt` is nullptr.
auto& decls = ForLoop(fl).cont_decls;
auto& decls = ForLoop(fl)->cont_decls;
if constexpr (!std::is_same_v<BUILDER, Decompose>) {
decls.Push(std::forward<BUILDER>(builder));
}