Skip to content

Commit

Permalink
[FIX][Pass] concurrent modification in RemoveUnusedVars (apache#32)
Browse files Browse the repository at this point in the history
* [Pass] Fix concurrent modification in RemoveUnusedVars

When running RemoveUnusedVars (i.e. remove_all_unused), in some cases the map users will raise Concurrent modification error. This commit fixed it by changing the logic to "iterate the map first and update it later".

* change the algorithm by store keys first

* Add an ICHECK before getting map value

* change the information of the ICHECK
  • Loading branch information
SiriusNEO authored Nov 27, 2022
1 parent e86b00b commit 91fcc47
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/relax/ir/binding_rewrite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class RemoveUnusedVars : public ExprMutator {
do {
prev_size = unused.size();

std::vector<Var> users_keys;
for (const auto& kv : users) {
// var -> [users...]
// var is unused iff
Expand All @@ -212,17 +213,21 @@ class RemoveUnusedVars : public ExprMutator {
if (kv.second.empty() && // kv.first is not used by fn outputs.
fn_outputs.end() == std::find(fn_outputs.begin(), fn_outputs.end(), kv.first)) {
unused.push_back(kv.first);
} else {
users_keys.push_back(kv.first);
}
}

for (size_t i = prev_size; i < unused.size(); ++i) {
users.erase(unused[i]);
// remove def site.
for (auto kv : users) { // remove use site.
auto it = std::find(kv.second.begin(), kv.second.end(), unused[i]);
if (it != kv.second.end()) {
kv.second.erase(it);
users.Set(kv.first, std::move(kv.second));
for (const auto& key: users_keys) { // remove use site.
ICHECK(users.count(key)) << "the key " << key << " is expected to be in the mapping users.";
Array<Var> cur_users = users[key];
auto it = std::find(cur_users.begin(), cur_users.end(), unused[i]);
if (it != cur_users.end()) {
cur_users.erase(it);
users.Set(key, std::move(cur_users));
}
}
}
Expand Down

0 comments on commit 91fcc47

Please sign in to comment.