Skip to content

[IRGen] Don't call objc_retainAutoreleasedReturnValue() without interop. #72011

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
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
4 changes: 3 additions & 1 deletion test/IRGen/autorelease.sil
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// 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

// REQUIRES: objc_codegen

// rdar://16565958

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

// We need to require objc_codegen to avoid this test on WASM.
// (That's why the other half of this test is in a separate file.)

// REQUIRES: objc_codegen

import CFBridgedType

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

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

// CHECK-LABEL: define {{.*}}swiftcc void @"$s37cf_objc_retainAutoreleasedReturnValue3fooyyF"()
// CHECK: entry:
// CHECK: %0 = call {{.*}}@returnsACFBridgedType()
// CHECK: %1 = notail call ptr @llvm.objc.retainAutoreleasedReturnValue(ptr %0)
18 changes: 18 additions & 0 deletions test/IRGen/cf_objc_retainAutoreleasedReturnValue2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-swift-frontend -module-name cf_objc_retainAutoreleasedReturnValue -I %S/Inputs %s -disable-objc-interop -emit-ir | %FileCheck %s

// This is in a separate file because *this* one works on WASM.
// (cf_objc_retainAutoreleasedReturnValue.swift does not.)

import CFBridgedType

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

// With interop disabled, this should use swift_retain().

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