<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 31, 2018, at 9:15 AM, Akira Hatanaka <<a href="mailto:ahatanaka@apple.com" class="">ahatanaka@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jan 30, 2018, at 4:32 PM, Saleem Abdulrasool <<a href="mailto:compnerd@compnerd.org" class="">compnerd@compnerd.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div dir="auto" class="">Thanks for the note here. I’ll try to take a look at that, but, yes, the frontend change itself is correct. I’ve seen many, many places where the ObjCARC passes break down in the backend, and I’ve filed a few bugs on it in the hope that someone from Apple would take a look at some point. I’m happy to work on it if someone could help with some of the issues that crop up.</div><div dir="auto" class=""><br class=""></div></div></div></blockquote><div class=""><br class=""></div>Sure, I can help you.</div></div></div></blockquote><div><br class=""></div><div>Great! That would be quite helpful.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class=""><div dir="auto" class="">Instruction bundles would only work partially and not really solve the problem. The issue is that even the Post RA scheduler can break apart call sequences, reordering things. The Machine Constant Island Pass is another place this breaks down, as it can create water in between the sequence. The MI passes tend to expect the bundles to have been broken up, and we need something which is kept all the way through the MC level.</div><br class=""></div></div></blockquote><div class=""><br class=""></div>Yes, that’s true, instruction bundles work only when the code is in LLVM IR. Maybe we can use MachineInstr bundles to prevent backend passes from inserting instructions between the calls?</div></div></div></blockquote><div><br class=""></div><div>I suppose that I should have been more clear. The problem is that the AFAICT, the mid-end optimizations would need to be taught about the bundle. If we pipe that all the way through to the MI layer, then we can use MIBundles. Again, we face the same problem of the MI passes needing to understand the bundle. I think that in order to ensure that the bundles never get split apart, we may have to teach the entire pipeline to peer through and possibly reconstruct these bundles. I am not sure if that is preferable as it spreads the knowledge of the ABI handshake all the way through and adds additional special casing to other passes. Am I misunderstanding something here?</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class=""><div class="gmail_quote"><div class="">On Tue, Jan 30, 2018 at 12:38 PM Akira Hatanaka <<a href="mailto:ahatanaka@apple.com" class="">ahatanaka@apple.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Saleem,<br class="">
<br class="">
I had to revert this patch since this patch caused crashes in code that was working fine before.<br class="">
<br class="">
As I mentioned in the commit message, I believe this patch is correct, but it causes the objc_retainAutoreleasedReturnValue/objc_autoreleaseReturnValue handshake to fail in some cases because middle-end and backend passes insert instructions between the call to the function returning an object and the call to objc_retainAutoreleasedReturnValue. We probably should find a way to prevent inserting instructions between the calls (maybe using instruction bundles), but we haven’t had the time to implement the fix. Feel free to fix the bug if you’d like to do so.<br class="">
<br class="">
> On Feb 11, 2017, at 1:34 PM, Saleem Abdulrasool via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank" class="">cfe-commits@lists.llvm.org</a>> wrote:<br class="">
><br class="">
> Author: compnerd<br class="">
> Date: Sat Feb 11 15:34:18 2017<br class="">
> New Revision: 294872<br class="">
><br class="">
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=294872&view=rev" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-project?rev=294872&view=rev</a><br class="">
> Log:<br class="">
> CodeGen: annotate ObjC ARC functions with ABI constraints<br class="">
><br class="">
> Certain ARC runtime functions have an ABI contract of being forwarding.<br class="">
> Annotate the functions with the appropriate `returned` attribute on the<br class="">
> arguments. This hoists some of the runtime ABI contract information<br class="">
> into the frontend rather than the backend transformations.<br class="">
><br class="">
> The test adjustments are to mark the returned function parameter as<br class="">
> such. The minor change to the IR output is due to the fact that the<br class="">
> returned reference of the object causes it to extend the lifetime of the<br class="">
> object by returning an autoreleased return value. The result is that<br class="">
> the explicit objc_autorelease call is no longer formed, as autorelease<br class="">
> elision is now possible on the return.<br class="">
><br class="">
> Modified:<br class="">
> cfe/trunk/lib/CodeGen/CGObjC.cpp<br class="">
> cfe/trunk/test/CodeGenObjC/arc.m<br class="">
><br class="">
> Modified: cfe/trunk/lib/CodeGen/CGObjC.cpp<br class="">
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjC.cpp?rev=294872&r1=294871&r2=294872&view=diff" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjC.cpp?rev=294872&r1=294871&r2=294872&view=diff</a><br class="">
> ==============================================================================<br class="">
> --- cfe/trunk/lib/CodeGen/CGObjC.cpp (original)<br class="">
> +++ cfe/trunk/lib/CodeGen/CGObjC.cpp Sat Feb 11 15:34:18 2017<br class="">
> @@ -1802,6 +1802,22 @@ void CodeGenFunction::EmitARCIntrinsicUs<br class="">
> }<br class="">
><br class="">
><br class="">
> +static bool IsForwarding(StringRef Name) {<br class="">
> + return llvm::StringSwitch<bool>(Name)<br class="">
> + .Cases("objc_autoreleaseReturnValue", // ARCInstKind::AutoreleaseRV<br class="">
> + "objc_autorelease", // ARCInstKind::Autorelease<br class="">
> + "objc_retainAutoreleaseReturnValue", // ARCInstKind::FusedRetainAutoreleaseRV<br class="">
> + "objc_retainAutoreleasedReturnValue", // ARCInstKind::RetainRV<br class="">
> + "objc_retainAutorelease", // ARCInstKind::FusedRetainAutorelease<br class="">
> + "objc_retainedObject", // ARCInstKind::NoopCast<br class="">
> + "objc_retain", // ARCInstKind::Retain<br class="">
> + "objc_unretainedObject", // ARCInstKind::NoopCast<br class="">
> + "objc_unretainedPointer", // ARCInstKind::NoopCast<br class="">
> + "objc_unsafeClaimAutoreleasedReturnValue", // ARCInstKind::ClaimRV<br class="">
> + true)<br class="">
> + .Default(false);<br class="">
> +}<br class="">
> +<br class="">
> static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,<br class="">
> llvm::FunctionType *FTy,<br class="">
> StringRef Name) {<br class="">
> @@ -1819,6 +1835,13 @@ static llvm::Constant *createARCRuntimeF<br class="">
> // performance.<br class="">
> F->addFnAttr(llvm::Attribute::NonLazyBind);<br class="">
> }<br class="">
> +<br class="">
> + if (IsForwarding(Name)) {<br class="">
> + llvm::AttrBuilder B;<br class="">
> + B.addAttribute(llvm::Attribute::Returned);<br class="">
> +<br class="">
> + F->arg_begin()->addAttr(llvm::AttributeSet::get(F->getContext(), 1, B));<br class="">
> + }<br class="">
> }<br class="">
><br class="">
> return RTF;<br class="">
><br class="">
> Modified: cfe/trunk/test/CodeGenObjC/arc.m<br class="">
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/arc.m?rev=294872&r1=294871&r2=294872&view=diff" rel="noreferrer" target="_blank" class="">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/arc.m?rev=294872&r1=294871&r2=294872&view=diff</a><br class="">
> ==============================================================================<br class="">
> --- cfe/trunk/test/CodeGenObjC/arc.m (original)<br class="">
> +++ cfe/trunk/test/CodeGenObjC/arc.m Sat Feb 11 15:34:18 2017<br class="">
> @@ -7,30 +7,30 @@<br class="">
> // RUN: %clang_cc1 -fobjc-runtime=macosx-10.7.0 -triple x86_64-apple-darwin11 -Wno-objc-root-class -Wno-incompatible-pointer-types -Wno-arc-unsafe-retained-assign -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -o - %s | FileCheck -check-prefix=ARC-NATIVE %s<br class="">
><br class="">
> // ARC-ALIEN: declare extern_weak void @objc_storeStrong(i8**, i8*)<br class="">
> -// ARC-ALIEN: declare extern_weak i8* @objc_retain(i8*)<br class="">
> -// ARC-ALIEN: declare extern_weak i8* @objc_autoreleaseReturnValue(i8*)<br class="">
> +// ARC-ALIEN: declare extern_weak i8* @objc_retain(i8* returned)<br class="">
> +// ARC-ALIEN: declare extern_weak i8* @objc_autoreleaseReturnValue(i8* returned)<br class="">
> // ARC-ALIEN: declare i8* @objc_msgSend(i8*, i8*, ...) [[NLB:#[0-9]+]]<br class="">
> // ARC-ALIEN: declare extern_weak void @objc_release(i8*)<br class="">
> -// ARC-ALIEN: declare extern_weak i8* @objc_retainAutoreleasedReturnValue(i8*)<br class="">
> +// ARC-ALIEN: declare extern_weak i8* @objc_retainAutoreleasedReturnValue(i8* returned)<br class="">
> // ARC-ALIEN: declare extern_weak i8* @objc_initWeak(i8**, i8*)<br class="">
> // ARC-ALIEN: declare extern_weak i8* @objc_storeWeak(i8**, i8*)<br class="">
> // ARC-ALIEN: declare extern_weak i8* @objc_loadWeakRetained(i8**)<br class="">
> // ARC-ALIEN: declare extern_weak void @objc_destroyWeak(i8**)<br class="">
> -// ARC-ALIEN: declare extern_weak i8* @objc_autorelease(i8*)<br class="">
> -// ARC-ALIEN: declare extern_weak i8* @objc_retainAutorelease(i8*)<br class="">
> +// declare extern_weak i8* @objc_autorelease(i8*)<br class="">
> +// ARC-ALIEN: declare extern_weak i8* @objc_retainAutorelease(i8* returned)<br class="">
><br class="">
> // ARC-NATIVE: declare void @objc_storeStrong(i8**, i8*)<br class="">
> -// ARC-NATIVE: declare i8* @objc_retain(i8*) [[NLB:#[0-9]+]]<br class="">
> -// ARC-NATIVE: declare i8* @objc_autoreleaseReturnValue(i8*)<br class="">
> +// ARC-NATIVE: declare i8* @objc_retain(i8* returned) [[NLB:#[0-9]+]]<br class="">
> +// ARC-NATIVE: declare i8* @objc_autoreleaseReturnValue(i8* returned)<br class="">
> // ARC-NATIVE: declare i8* @objc_msgSend(i8*, i8*, ...) [[NLB]]<br class="">
> // ARC-NATIVE: declare void @objc_release(i8*) [[NLB]]<br class="">
> -// ARC-NATIVE: declare i8* @objc_retainAutoreleasedReturnValue(i8*)<br class="">
> +// ARC-NATIVE: declare i8* @objc_retainAutoreleasedReturnValue(i8* returned)<br class="">
> // ARC-NATIVE: declare i8* @objc_initWeak(i8**, i8*)<br class="">
> // ARC-NATIVE: declare i8* @objc_storeWeak(i8**, i8*)<br class="">
> // ARC-NATIVE: declare i8* @objc_loadWeakRetained(i8**)<br class="">
> // ARC-NATIVE: declare void @objc_destroyWeak(i8**)<br class="">
> -// ARC-NATIVE: declare i8* @objc_autorelease(i8*)<br class="">
> -// ARC-NATIVE: declare i8* @objc_retainAutorelease(i8*)<br class="">
> +// declare i8* @objc_autorelease(i8*)<br class="">
> +// ARC-NATIVE: declare i8* @objc_retainAutorelease(i8* returned)<br class="">
><br class="">
> // CHECK-LABEL: define void @test0<br class="">
> void test0(id x) {<br class="">
> @@ -1504,7 +1504,9 @@ void test68(void) {<br class="">
> // CHECK: [[SELF:%.*]] = alloca [[TEST69:%.*]]*, align 8<br class="">
> // CHECK: [[T0:%.*]] = load [[TEST69]]*, [[TEST69]]** [[SELF]], align 8<br class="">
> // CHECK-NEXT: [[T1:%.*]] = bitcast [[TEST69]]* [[T0]] to i8*<br class="">
> -// CHECK-NEXT: ret i8* [[T1]]<br class="">
> +// CHECK-NEXT: [[RETAIN:%.*]] = call i8* @objc_retain(i8* [[T1]])<br class="">
> +// CHECK-NEXT: [[AUTORELEASE:%.*]] = tail call i8* @objc_autoreleaseReturnValue(i8* [[RETAIN]])<br class="">
> +// CHECK-NEXT: ret i8* [[AUTORELEASE]]<br class="">
><br class="">
> // <a href="rdar://problem/10907547" class="">rdar://problem/10907547</a><br class="">
> void test70(id i) {<br class="">
><br class="">
><br class="">
> _______________________________________________<br class="">
> cfe-commits mailing list<br class="">
> <a href="mailto:cfe-commits@lists.llvm.org" target="_blank" class="">cfe-commits@lists.llvm.org</a><br class="">
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br class="">
<br class="">
</blockquote></div></div><div dir="ltr" class="">-- <br class=""></div><div class="gmail_signature" data-smartmail="gmail_signature">Saleem Abdulrasool<br class="">compnerd (at) compnerd (dot) org</div>
</div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>