[PATCH] D156618: [IR] Fix a memory leak if Function::dropAllReferences() is followed by setHungoffOperand

Liqiang Tao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 08:18:35 PDT 2023


taolq updated this revision to Diff 556785.
taolq added a comment.

add a new function to implement deleteBody, do not call User::dropAllReference


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156618/new/

https://reviews.llvm.org/D156618

Files:
  llvm/include/llvm/IR/Function.h
  llvm/lib/IR/Function.cpp


Index: llvm/lib/IR/Function.cpp
===================================================================
--- llvm/lib/IR/Function.cpp
+++ llvm/lib/IR/Function.cpp
@@ -548,6 +548,32 @@
   clearMetadata();
 }
 
+// This function do almost all the same things as Function::dropAllReferences(),
+// except setting Op<0..3> with PointerNull.
+void Function::deleteBodyImpl() {
+  setIsMaterializable(false);
+
+  for (BasicBlock &BB : *this)
+    BB.dropAllReferences();
+
+  // Delete all basic blocks. They are now unused, except possibly by
+  // blockaddresses, but BasicBlock's destructor takes care of those.
+  while (!BasicBlocks.empty())
+    BasicBlocks.begin()->eraseFromParent();
+
+  // The code needs to match Function::allocHungoffUselist().
+  if (getNumOperands()) {
+    auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0));
+    Op<0>().set(CPN);
+    Op<1>().set(CPN);
+    Op<2>().set(CPN);
+    setValueSubclassData(getSubclassDataFromValue() & ~0xe);
+  }
+
+  // Metadata is stored in a side-table.
+  clearMetadata();
+}
+
 void Function::addAttributeAtIndex(unsigned i, Attribute Attr) {
   AttributeSets = AttributeSets.addAttributeAtIndex(getContext(), i, Attr);
 }
Index: llvm/include/llvm/IR/Function.h
===================================================================
--- llvm/include/llvm/IR/Function.h
+++ llvm/include/llvm/IR/Function.h
@@ -116,6 +116,8 @@
 
   void clearArguments();
 
+  void deleteBodyImpl();
+
   /// Function ctor - If the (optional) Module argument is specified, the
   /// function is automatically inserted into the end of the function list for
   /// the module.
@@ -667,7 +669,7 @@
   /// the linkage to external.
   ///
   void deleteBody() {
-    dropAllReferences();
+    deleteBodyImpl();
     setLinkage(ExternalLinkage);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156618.556785.patch
Type: text/x-patch
Size: 1806 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230914/3c3956ba/attachment.bin>


More information about the llvm-commits mailing list