From 75597036cb6078afba8da782a061caa671d0a74e 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 (cherry picked from commit d8a8db23a5208480fe7cd56b413c9743ea58db83) --- 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 3bb11988b730e..f4a60cf739fc3 100644 --- a/src/llvm-alloc-opt.cpp +++ b/src/llvm-alloc-opt.cpp @@ -1156,6 +1156,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(); @@ -1168,6 +1169,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)