[llvm] [ObjCARC] Move autorelease-to-release conversion to pool pop site instead of converting in place (PR #152353)
Akira Hatanaka via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 17 16:55:55 PDT 2026
================
@@ -601,6 +606,60 @@ class ObjCARCOpt {
};
} // end anonymous namespace
+/// Find the autoreleasePoolPop that will drain the given autorelease
+/// instruction in the same basic block, skipping over nested pools.
+///
+/// Since objc_autorelease does not change the refcount (it only registers the
+/// object for a deferred release at pool drain), we can move the release to
+/// just before the pool pop instead of converting in place. This avoids the
+/// need to check for uses of the pointer between the autorelease and the pop.
+Instruction *
+ObjCARCOpt::FindFollowingAutoreleasePoolPop(Instruction *AutoreleaseInst) {
+ assert(GetBasicARCInstKind(AutoreleaseInst) == ARCInstKind::Autorelease);
+
+ auto It = FollowingPoolPopCache.find(AutoreleaseInst);
+ if (It != FollowingPoolPopCache.end()) {
+ // Note: The cached value is a raw pointer to a pool pop. This is safe
+ // because this pass never erases autoreleasePoolPop instructions.
+ return It->second;
+ }
+
+ BasicBlock *BB = AutoreleaseInst->getParent();
+ Instruction *Result = nullptr;
+
+ SmallVector<SmallVector<Instruction *, 2>, 4> AutoreleasesByDepth(1);
+
----------------
ahatanak wrote:
A few optional simplifications:
1. Insert `AutoreleaseInst` into `AutoreleasesByDepth[0]` before the loop starts. It's just another depth-0 sibling like any other autorelease the scan finds, so this lets you remove the special-cased `FollowingPoolPopCache[AutoreleaseInst] = Result;` after the loop.
2. Move the `if (Depth == 0)` check to right after `AutoreleasesByDepth[Depth].clear()`, and return `&*I` immediately from there instead of breaking. This unifies the depth-0 case with the general case, instead of treating it separately.
3. With change 2, once you fall off the end of the loop, every remaining instruction across all of `AutoreleasesByDepth` (including `AutoreleaseInst` itself) is known to have no following pop in this block, so the trailing loop can assign nullptr directly instead of `Result`. You can then drop the `Result` local and return `&*I` / `nullptr` directly instead.
4. In the `AutoreleasepoolPush` branch, assert that `AutoreleasesByDepth[Depth]` is empty when `Depth != (int)AutoreleasesByDepth.size()`. You are reusing a bucket, which has been cleared earlier, rather than growing the vector.
https://github.com/llvm/llvm-project/pull/152353
More information about the llvm-commits
mailing list