https://github.com/citymarina updated 
https://github.com/llvm/llvm-project/pull/138696

>From e83258e216f89992a7c46eaf3478c7b31e1113dc Mon Sep 17 00:00:00 2001
From: Marina Taylor <marina_tay...@apple.com>
Date: Wed, 19 Mar 2025 18:25:09 +0000
Subject: [PATCH] [ObjC] Support objc_claimAutoreleasedReturnValue.

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 <ah...@bougacha.org>
---
 llvm/include/llvm/Analysis/ObjCARCUtil.h      | 15 ++++++++++
 llvm/include/llvm/IR/Intrinsics.td            |  9 ++++--
 llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp |  8 +++---
 llvm/lib/IR/Verifier.cpp                      |  2 ++
 .../Target/AArch64/AArch64ISelLowering.cpp    |  6 ++--
 .../AArch64/GISel/AArch64CallLowering.cpp     |  4 +--
 llvm/test/CodeGen/AArch64/call-rv-marker.ll   | 28 +++++++++++++++++++
 7 files changed, 60 insertions(+), 12 deletions(-)

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..eb727c55b1282 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..fb6e2e479646e 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -9542,10 +9542,10 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
     Ops.insert(Ops.begin() + 1, GA);
 
     // We may or may not need to emit both the marker and the retain/claim 
call.
-    // 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..9bef102e8abf1 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp
@@ -1366,10 +1366,8 @@ bool AArch64CallLowering::lowerCall(MachineIRBuilder 
&MIRBuilder,
     ++CalleeOpNo;
 
     // We may or may not need to emit both the marker and the retain/claim 
call.
-    // 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());
diff --git a/llvm/test/CodeGen/AArch64/call-rv-marker.ll 
b/llvm/test/CodeGen/AArch64/call-rv-marker.ll
index ee8cbe45da083..b22370a295749 100644
--- a/llvm/test/CodeGen/AArch64/call-rv-marker.ll
+++ b/llvm/test/CodeGen/AArch64/call-rv-marker.ll
@@ -544,7 +544,35 @@ define dso_local void @rv_marker_multiarg(i64 %a, i64 %b, 
i64 %c) {
   ret void
 }
 
+define dso_local ptr @rv_marker_claim() {
+; SELDAG-LABEL: rv_marker_claim:
+; SELDAG:       ; %bb.0: ; %entry
+; SELDAG-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; SELDAG-NEXT:    .cfi_def_cfa_offset 16
+; SELDAG-NEXT:    .cfi_offset w30, -8
+; SELDAG-NEXT:    .cfi_offset w29, -16
+; SELDAG-NEXT:    bl _foo1
+; SELDAG-NEXT:    bl _objc_claimAutoreleasedReturnValue
+; SELDAG-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; SELDAG-NEXT:    ret
+;
+; GISEL-LABEL: rv_marker_claim:
+; GISEL:       ; %bb.0: ; %entry
+; GISEL-NEXT:    stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; GISEL-NEXT:    .cfi_def_cfa_offset 16
+; GISEL-NEXT:    .cfi_offset w30, -8
+; GISEL-NEXT:    .cfi_offset w29, -16
+; GISEL-NEXT:    bl _foo1
+; GISEL-NEXT:    bl _objc_claimAutoreleasedReturnValue
+; GISEL-NEXT:    ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; GISEL-NEXT:    ret
+entry:
+  %call = call ptr @foo1() [ "clang.arc.attachedcall"(ptr 
@objc_claimAutoreleasedReturnValue) ]
+  ret ptr %call
+}
+
 declare ptr @objc_retainAutoreleasedReturnValue(ptr)
+declare ptr @objc_claimAutoreleasedReturnValue(ptr)
 declare ptr @objc_unsafeClaimAutoreleasedReturnValue(ptr)
 declare i32 @__gxx_personality_v0(...)
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add 
tests below this line:

_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to