Skip to content

Commit

Permalink
[AllocOpt] fix iterator invalidation
Browse files Browse the repository at this point in the history
We might previously accidentally visit this use after deletion, if the
orig_inst ended up back in the workqueue.

Fixes #41916
  • Loading branch information
vtjnash committed Aug 30, 2021
1 parent 915a212 commit 09fc4cc
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/llvm-alloc-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,14 +1149,16 @@ void Optimizer::optimizeTag(CallInst *orig_inst)
{
auto tag = orig_inst->getArgOperand(2);
// `julia.typeof` is only legal on the original pointer, no need to scan recursively
for (auto user: orig_inst->users()) {
for (auto &use: orig_inst->uses()) {
auto user = use.getUser();
if (auto call = dyn_cast<CallInst>(user)) {
auto callee = call->getCalledOperand();
if (pass.typeof_func == callee) {
call->replaceAllUsesWith(tag);
// Push to the removed instructions to trigger `finalize` to
// return the correct result.
// Also so that we don't have to worry about iterator invalidation...
use = UndefValue::get(use->getType());
removed.push_back(call);
}
}
Expand Down

0 comments on commit 09fc4cc

Please sign in to comment.