-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[ObjCARC] Change autorelease to release when the pool state is not changed between the autorelease and the pool pop #152353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AZero13
wants to merge
1
commit into
llvm:main
Choose a base branch
from
AZero13:pops
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-transforms Author: AZero13 (AZero13) ChangesFull diff: https://github.com/llvm/llvm-project/pull/152353.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp b/llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp
index 66a2c7632aadc..5598d5f221024 100644
--- a/llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp
+++ b/llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp
@@ -121,6 +121,44 @@ static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
/// \defgroup ARCOpt ARC Optimization.
/// @{
+/// Check if there is an autoreleasePoolPop after the given autorelease
+/// instruction in the same basic block with no intervening calls that
+/// could affect the autorelease pool.
+static bool HasFollowingAutoreleasePoolPop(Instruction *AutoreleaseInst) {
+ BasicBlock *BB = AutoreleaseInst->getParent();
+
+ // Look forward from the autorelease instruction
+ for (BasicBlock::iterator I = std::next(AutoreleaseInst->getIterator()),
+ E = BB->end();
+ I != E; ++I) {
+ ARCInstKind Class = GetBasicARCInstKind(&*I);
+
+ switch (Class) {
+ case ARCInstKind::AutoreleasepoolPop:
+ // Found a pool pop - the autorelease will be drained
+ return true;
+
+ case ARCInstKind::AutoreleasepoolPush:
+ // A new pool started - this autorelease won't be drained by a later pop
+ return false;
+
+ case ARCInstKind::CallOrUser:
+ case ARCInstKind::Call:
+ // Unknown call could affect autorelease pool state or return autoreleased
+ // objects
+ return false;
+
+ default:
+ // Known ObjC runtime calls and other instructions are safe to continue
+ // through
+ break;
+ }
+ }
+
+ // Reached end of basic block without finding a pool pop
+ return false;
+}
+
// TODO: On code like this:
//
// objc_retain(%x)
@@ -978,12 +1016,22 @@ void ObjCARCOpt::OptimizeIndividualCallImpl(Function &F, Instruction *Inst,
break;
}
- // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
+ // objc_autorelease(x) -> objc_release(x) if x is otherwise unused
+ // OR if this autorelease is followed by an autoreleasePoolPop.
if (IsAutorelease(Class) && Inst->use_empty()) {
CallInst *Call = cast<CallInst>(Inst);
const Value *Arg = Call->getArgOperand(0);
Arg = FindSingleUseIdentifiedObject(Arg);
- if (Arg) {
+ bool ShouldConvert = (Arg != nullptr);
+ const char *Reason = "since x is otherwise unused";
+
+ // Also convert if this autorelease is followed by a pool pop
+ if (!ShouldConvert && HasFollowingAutoreleasePoolPop(Inst)) {
+ ShouldConvert = true;
+ Reason = "since it's followed by autoreleasePoolPop";
+ }
+
+ if (ShouldConvert) {
Changed = true;
++NumAutoreleases;
@@ -997,8 +1045,8 @@ void ObjCARCOpt::OptimizeIndividualCallImpl(Function &F, Instruction *Inst,
MDNode::get(C, {}));
LLVM_DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
- "since x is otherwise unused.\nOld: "
- << *Call << "\nNew: " << *NewCall << "\n");
+ << Reason << ".\nOld: " << *Call
+ << "\nNew: " << *NewCall << "\n");
EraseInstruction(Call);
Inst = NewCall;
diff --git a/llvm/test/Transforms/ObjCARC/test_autorelease_pool.ll b/llvm/test/Transforms/ObjCARC/test_autorelease_pool.ll
index 896717f92146f..6c7cfeb78dd3a 100644
--- a/llvm/test/Transforms/ObjCARC/test_autorelease_pool.ll
+++ b/llvm/test/Transforms/ObjCARC/test_autorelease_pool.ll
@@ -44,7 +44,7 @@ define void @test_multiple_autoreleases() {
; CHECK-NEXT: call void @use_object(ptr [[OBJ1]])
; CHECK-NEXT: [[TMP1:%.*]] = call ptr @llvm.objc.autorelease(ptr [[OBJ1]]) #[[ATTR0]]
; CHECK-NEXT: call void @use_object(ptr [[OBJ2]])
-; CHECK-NEXT: [[TMP2:%.*]] = call ptr @llvm.objc.autorelease(ptr [[OBJ2]]) #[[ATTR0]]
+; CHECK-NEXT: call void @llvm.objc.release(ptr [[OBJ2]]) #[[ATTR0]], !clang.imprecise_release [[META0]]
; CHECK-NEXT: call void @llvm.objc.autoreleasePoolPop(ptr [[POOL]]) #[[ATTR0]]
; CHECK-NEXT: ret void
;
|
@jroelofs Ping |
jroelofs
reviewed
Aug 19, 2025
a1b36db
to
d287ebd
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
@jroelofs Sorry for the long wait, but finished! |
…anged between the autorelease and the pool pop Co-Authored-By: Jon Roelofs <[email protected]>
Please note I do not have merge permissions. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The last autorelease in a pool can be changed to release if we know nothing else will happen to it.