[PATCH] D156618: [IR] Fix a memory leak in Function::dropAllReferences()

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 30 19:51:43 PDT 2023


MaskRay accepted this revision.
MaskRay added a comment.
This revision is now accepted and ready to land.

> [IR] Fix a memory leak in Function::dropAllReferences()

Thanks for the patch. I wonder whether it's slightly unfair to call it a memory leak, but I do agree that `dropAllReferences` should be changed, as it fails to maintain a constraint: when `NumUserOperands==0`, the hung-off operand list should not be allocated.

It seems unfair because all call sites, `dropAllReferences` is followed by the deletion of the `Function`, and there will be no leak.
However, if we call `setHungoffOperand` (like `setPersonality`), there will be a leak.



================
Comment at: llvm/lib/IR/Function.cpp:541
   if (getNumOperands()) {
-    User::dropAllReferences();
-    setNumHungOffUseOperands(0);
+    auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
+    Op<0>().set(CPN);
----------------
Change this to `PointerType::get(getContext(), 0)` after `getInt1PtrTy` removal.

Add a comment `The code needs to match allocHungoffUselist.` or similar.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156618



More information about the llvm-commits mailing list