From 1a42b3804f0ed1c4958c4f17216543a1623e3452 Mon Sep 17 00:00:00 2001 From: Jeremy Morse Date: Thu, 8 Feb 2024 10:27:34 +0000 Subject: [PATCH] [DebugInfo][RemoveDIs] Erase ranges of instructions individually (#81007) The BasicBlock::erase method simply removes a range of instructions from the instlist by unlinking them. However, now that we're attaching debug-info directly to instructions, some cleanup is required, so use eraseFromParent on each instruction instead. This is less efficient, but rare, and seemingly only WASM EH Prepare uses this method of BasicBlock. Detected via a memory leak check in asan. (asan is always the final boss for whatever I do). --- llvm/lib/IR/BasicBlock.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp index bb55f48df4b314..fe9d0d08c5fe97 100644 --- a/llvm/lib/IR/BasicBlock.cpp +++ b/llvm/lib/IR/BasicBlock.cpp @@ -677,7 +677,9 @@ BasicBlock *BasicBlock::splitBasicBlockBefore(iterator I, const Twine &BBName) { BasicBlock::iterator BasicBlock::erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt) { - return InstList.erase(FromIt, ToIt); + for (Instruction &I : make_early_inc_range(make_range(FromIt, ToIt))) + I.eraseFromParent(); + return ToIt; } void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) {