Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[ObjC] Support objc_claimAutoreleasedReturnValue #138696

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

Merged

Conversation

citymarina
Copy link
Contributor

This adds basic support for objc_claimAutoreleasedReturnValue, which is mostly equivalent to objc_retainAutoreleasedReturnValue, with the difference that it doesn't require the marker nop to be emitted between it and the call it was attached to.

To achieve that, this also teaches the AArch64 attachedcall bundle lowering to pick whether the marker should be emitted or not based on whether the attachedcall target is claimARV or retainARV.

@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

@llvm/pr-subscribers-llvm-analysis
@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-llvm-ir

Author: Marina Taylor (citymarina)

Changes

This adds basic support for objc_claimAutoreleasedReturnValue, which is mostly equivalent to objc_retainAutoreleasedReturnValue, with the difference that it doesn't require the marker nop to be emitted between it and the call it was attached to.

To achieve that, this also teaches the AArch64 attachedcall bundle lowering to pick whether the marker should be emitted or not based on whether the attachedcall target is claimARV or retainARV.


Full diff: https://github.com/llvm/llvm-project/pull/138696.diff

6 Files Affected:

  • (modified) llvm/include/llvm/Analysis/ObjCARCUtil.h (+15)
  • (modified) llvm/include/llvm/IR/Intrinsics.td (+7-2)
  • (modified) llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp (+4-4)
  • (modified) llvm/lib/IR/Verifier.cpp (+2)
  • (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+3-1)
  • (modified) llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp (+1-1)
diff --git a/llvm/include/llvm/Analysis/ObjCARCUtil.h b/llvm/include/llvm/Analysis/ObjCARCUtil.h
index 62e13eb2a5722..7dc0c50809f06 100644
--- a/llvm/include/llvm/Analysis/ObjCARCUtil.h
+++ b/llvm/include/llvm/Analysis/ObjCARCUtil.h
@@ -48,6 +48,21 @@ inline std::optional<Function *> getAttachedARCFunction(const CallBase *CB) {
   return cast<Function>(B->Inputs[0]);
 }
 
+/// This function determines whether the clang_arc_attachedcall should be
+/// emitted with or without the marker.
+/// Concretely, this is the difference between:
+///   objc_retainAutoreleasedReturnValue
+/// and
+///  objc_claimAutoreleasedReturnValue
+/// retainRV (and unsafeClaimRV) requires a marker, but claimRV does not.
+inline bool attachedCallOpBundleNeedsMarker(const CallBase *CB) {
+  // FIXME: do this on ARCRuntimeEntryPoints, and do the todo above ARCInstKind
+  if (std::optional<Function *> Fn = getAttachedARCFunction(CB))
+    if ((*Fn)->getName() == "objc_claimAutoreleasedReturnValue")
+      return false;
+  return true;
+}
+
 /// Check whether the function is retainRV/unsafeClaimRV.
 inline bool isRetainOrClaimRV(ARCInstKind Kind) {
   return Kind == ARCInstKind::RetainRV || Kind == ARCInstKind::UnsafeClaimRV;
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index a174ccbf61002..53c6e6619764c 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -777,10 +777,12 @@ def int_objc_loadWeakRetained               : Intrinsic<[llvm_ptr_ty],
 def int_objc_moveWeak                       : Intrinsic<[],
                                                         [llvm_ptr_ty,
                                                          llvm_ptr_ty]>;
+
 def int_objc_release                        : Intrinsic<[], [llvm_ptr_ty]>;
 def int_objc_retain                         : Intrinsic<[llvm_ptr_ty],
                                                         [llvm_ptr_ty],
                                                         [Returned<ArgIndex<0>>]>;
+
 def int_objc_retainAutorelease              : Intrinsic<[llvm_ptr_ty],
                                                         [llvm_ptr_ty],
                                                         [Returned<ArgIndex<0>>]>;
@@ -789,6 +791,11 @@ def int_objc_retainAutoreleaseReturnValue   : Intrinsic<[llvm_ptr_ty],
                                                         [Returned<ArgIndex<0>>]>;
 def int_objc_retainAutoreleasedReturnValue  : Intrinsic<[llvm_ptr_ty],
                                                         [llvm_ptr_ty]>;
+def int_objc_unsafeClaimAutoreleasedReturnValue : Intrinsic<[llvm_ptr_ty],
+                                                            [llvm_ptr_ty]>;
+def int_objc_claimAutoreleasedReturnValue   : Intrinsic<[llvm_ptr_ty],
+                                                        [llvm_ptr_ty]>;
+
 def int_objc_retainBlock                    : Intrinsic<[llvm_ptr_ty],
                                                         [llvm_ptr_ty]>;
 def int_objc_storeStrong                    : Intrinsic<[],
@@ -802,8 +809,6 @@ def int_objc_clang_arc_use                  : Intrinsic<[],
 def int_objc_clang_arc_noop_use : DefaultAttrsIntrinsic<[],
                                                         [llvm_vararg_ty],
                                                         [IntrInaccessibleMemOnly]>;
-def int_objc_unsafeClaimAutoreleasedReturnValue : Intrinsic<[llvm_ptr_ty],
-                                                            [llvm_ptr_ty]>;
 def int_objc_retainedObject                 : Intrinsic<[llvm_ptr_ty],
                                                         [llvm_ptr_ty]>;
 def int_objc_unretainedObject               : Intrinsic<[llvm_ptr_ty],
diff --git a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
index 9dc1764b49e46..1b71a9aa8f2fe 100644
--- a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
+++ b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
@@ -160,10 +160,7 @@ static bool lowerObjCCall(Function &F, const char *NewFn,
     auto *CB = cast<CallBase>(U.getUser());
 
     if (CB->getCalledFunction() != &F) {
-      objcarc::ARCInstKind Kind = objcarc::getAttachedARCFunctionKind(CB);
-      (void)Kind;
-      assert((Kind == objcarc::ARCInstKind::RetainRV ||
-              Kind == objcarc::ARCInstKind::UnsafeClaimRV) &&
+      assert((*objcarc::getAttachedARCFunction(CB) == &F) &&
              "use expected to be the argument of operand bundle "
              "\"clang.arc.attachedcall\"");
       U.set(FCache.getCallee());
@@ -529,6 +526,9 @@ bool PreISelIntrinsicLowering::lowerIntrinsics(Module &M) const {
     case Intrinsic::objc_retainAutoreleasedReturnValue:
       Changed |= lowerObjCCall(F, "objc_retainAutoreleasedReturnValue");
       break;
+    case Intrinsic::objc_claimAutoreleasedReturnValue:
+      Changed |= lowerObjCCall(F, "objc_claimAutoreleasedReturnValue");
+      break;
     case Intrinsic::objc_retainBlock:
       Changed |= lowerObjCCall(F, "objc_retainBlock");
       break;
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index a798808d79656..c95497672913b 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -7282,11 +7282,13 @@ void Verifier::verifyAttachedCallBundle(const CallBase &Call,
 
   if (IID) {
     Check((IID == Intrinsic::objc_retainAutoreleasedReturnValue ||
+           IID == Intrinsic::objc_claimAutoreleasedReturnValue ||
            IID == Intrinsic::objc_unsafeClaimAutoreleasedReturnValue),
           "invalid function argument", Call);
   } else {
     StringRef FnName = Fn->getName();
     Check((FnName == "objc_retainAutoreleasedReturnValue" ||
+           FnName == "objc_claimAutoreleasedReturnValue" ||
            FnName == "objc_unsafeClaimAutoreleasedReturnValue"),
           "invalid function argument", Call);
   }
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 5024bda5677fb..648eef9133426 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -9545,7 +9545,9 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
     // Do what the frontend tells us: if the rvmarker module flag is present,
     // emit the marker.  Always emit the call regardless.
     // Tell the pseudo expansion using an additional boolean op.
-    SDValue DoEmitMarker = DAG.getTargetConstant(true, DL, MVT::i32);
+    bool ShouldEmitMarker = objcarc::attachedCallOpBundleNeedsMarker(CLI.CB);
+    SDValue DoEmitMarker =
+        DAG.getTargetConstant(ShouldEmitMarker, DL, MVT::i32);
     Ops.insert(Ops.begin() + 2, DoEmitMarker);
   } else if (CallConv == CallingConv::ARM64EC_Thunk_X64) {
     Opc = AArch64ISD::CALL_ARM64EC_TO_X64;
diff --git a/llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp b/llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp
index 91e453657f2f6..38e2f10777593 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp
@@ -1369,7 +1369,7 @@ bool AArch64CallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
     // Do what the frontend tells us: if the rvmarker module flag is present,
     // emit the marker.  Always emit the call regardless.
     // Tell the pseudo expansion using an additional boolean op.
-    MIB.addImm(true);
+    MIB.addImm(objcarc::attachedCallOpBundleNeedsMarker(Info.CB));
     ++CalleeOpNo;
   } else if (Info.CFIType) {
     MIB->setCFIType(MF, Info.CFIType->getZExtValue());

@citymarina
Copy link
Contributor Author

Thanks for the feedback, I'm working on an update to address the comments, but currently juggling too many tasks.

This adds basic support for objc_claimAutoreleasedReturnValue,
which is mostly equivalent to objc_retainAutoreleasedReturnValue,
with the difference that it doesn't require the marker nop to be
emitted between it and the call it was attached to.

To achieve that, this also teaches the AArch64 attachedcall bundle
lowering to pick whether the marker should be emitted or not
based on whether the attachedcall target is claimARV or retainARV.

Co-authored-by: Ahmed Bougacha <ahmed@bougacha.org>
@citymarina citymarina force-pushed the users/citymarina/objc-claim-2b branch from b410f3b to e83258e Compare May 12, 2025 15:47
@citymarina citymarina requested a review from jroelofs May 12, 2025 17:08
@citymarina citymarina merged commit d33e5c5 into users/citymarina/objc-claim-2a May 13, 2025
11 checks passed
@citymarina citymarina deleted the users/citymarina/objc-claim-2b branch May 13, 2025 11:06
@citymarina citymarina restored the users/citymarina/objc-claim-2b branch May 13, 2025 12:29
@citymarina citymarina deleted the users/citymarina/objc-claim-2b branch May 13, 2025 16:39
@AZero13
Copy link
Contributor

AZero13 commented May 14, 2025

What did you mean by // FIXME: do this on ARCRuntimeEntryPoints, and do the todo above ARCInstKind

Like you want to do the same check there? How can I help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.