From 5b30ad3cc8778529e7ec36800a5c6ffb70cae07c Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Wed, 1 Sep 2021 13:53:16 -0400 Subject: [PATCH] [AllocOpt] fix iterator invalidation (#42059) We might previously accidentally visit this use after deletion, if the orig_inst ended up back in the workqueue. Fixes #41916 --- src/llvm-alloc-opt.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/llvm-alloc-opt.cpp b/src/llvm-alloc-opt.cpp index ec7060bd10a5ea..18b54b117c3231 100644 --- a/src/llvm-alloc-opt.cpp +++ b/src/llvm-alloc-opt.cpp @@ -1149,6 +1149,7 @@ 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 + size_t last_deleted = removed.size(); for (auto user: orig_inst->users()) { if (auto call = dyn_cast(user)) { auto callee = call->getCalledOperand(); @@ -1161,6 +1162,8 @@ void Optimizer::optimizeTag(CallInst *orig_inst) } } } + while (last_deleted < removed.size()) + removed[last_deleted++]->replaceUsesOfWith(orig_inst, UndefValue::get(orig_inst->getType())); } void Optimizer::splitOnStack(CallInst *orig_inst)