Skip to content

Commit

Permalink
[IRGen] Don't call objc_retainAutoreleasedReturnValue() without interop.
Browse files Browse the repository at this point in the history
When ObjC interop is not enabled, we shouldn't be emitting calls to
`objc_retainAutoreleasedReturnValue()` as that function might not exist.

Call `swift_retain()` instead, to balance the `swift_release()` of the
returned value.

Fixes #47846, #45359.

rdar://23335318
  • Loading branch information
al45tair committed Mar 1, 2024
1 parent f1adf99 commit d0c35cf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,10 @@ class SyncCallEmission final : public CallEmission {
if (fnConv.getNumDirectSILResults() == 1
&& (fnConv.getDirectSILResults().begin()->getConvention()
== ResultConvention::Autoreleased)) {
result = emitObjCRetainAutoreleasedReturnValue(IGF, result);
if (IGF.IGM.Context.LangOpts.EnableObjCInterop)
result = emitObjCRetainAutoreleasedReturnValue(IGF, result);
else
IGF.emitNativeStrongRetain(result, IGF.getDefaultAtomicity());
}

auto origFnType = getCallee().getOrigFunctionType();
Expand Down
6 changes: 6 additions & 0 deletions test/IRGen/Inputs/CFBridgedType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#define CF_BRIDGED_TYPE(T) __attribute__((objc_bridge(T)))

typedef struct CF_BRIDGED_TYPE(id) __CFBridgedType *CFBridgedTypeRef;

__attribute__((cf_audited_transfer))
CFBridgedTypeRef returnsACFBridgedType(void);
8 changes: 6 additions & 2 deletions test/IRGen/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ module SynthesizedProtocol {
}

module PointerAuth {
header "ptrauth_field_fptr_import.h"
}
header "ptrauth_field_fptr_import.h"
}

module CFBridgedType {
header "CFBridgedType.h"
}
2 changes: 1 addition & 1 deletion test/IRGen/autorelease.sil
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %s -emit-ir | %FileCheck -check-prefix CHECK -check-prefix CHECK-%target-ptrsize -check-prefix %target-cpu -DINT=i%target-ptrsize %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %s -enable-objc-interop -emit-ir | %FileCheck -check-prefix CHECK -check-prefix CHECK-%target-ptrsize -check-prefix %target-cpu -DINT=i%target-ptrsize %s

// rdar://16565958

Expand Down
24 changes: 24 additions & 0 deletions test/IRGen/cf_objc_retainAutoreleasedReturnValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %target-swift-frontend -module-name cf_objc_retainAutoreleasedReturnValue -I %S/Inputs %s -enable-objc-interop -emit-ir | %FileCheck %s
// RUN: %target-swift-frontend -module-name cf_objc_retainAutoreleasedReturnValue -I %S/Inputs %s -emit-ir | %FileCheck %s --check-prefix=NO_INTEROP

import CFBridgedType

@inline(never)
public func foo() {
let _ = returnsACFBridgedType()
}

// With interop enabled, this should use objc_retainAutoreleasedReturnValue()

// CHECK-LABEL: define protected swiftcc void @"$s37cf_objc_retainAutoreleasedReturnValue3fooyyF"()
// CHECK: entry:
// CHECK: %0 = call {{.*}}@returnsACFBridgedType()
// CHECK: %1 = notail call ptr @llvm.objc.retainAutoreleasedReturnValue(ptr %0)

// Without interop, it should call swift_retain() instead.

// NO_INTEROP-LABEL: define protected swiftcc void @"$s37cf_objc_retainAutoreleasedReturnValue3fooyyF"()
// NO_INTEROP: entry:
// NO_INTEROP: %0 = call {{.*}}@returnsACFBridgedType()
// NO_INTEROP: %1 = call ptr @swift_retain(ptr returned %0)

0 comments on commit d0c35cf

Please sign in to comment.