Skip to content

[cxx-interop] Emit retain/release correctly for FRT's that are stored… #61533

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 1 commit into from
Oct 27, 2022
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
21 changes: 19 additions & 2 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
#include "ScalarTypeInfo.h"
#include "StructLayout.h"
#include "SwitchBuilder.h"
#include "ClassTypeInfo.h"

using namespace swift;
using namespace irgen;
Expand Down Expand Up @@ -2467,9 +2468,17 @@ namespace {
void retainRefcountedPayload(IRGenFunction &IGF,
llvm::Value *ptr) const {
switch (CopyDestroyKind) {
case NullableRefcounted:
case NullableRefcounted: {
if (Refcounting == ReferenceCounting::Custom) {
Explosion e;
e.add(ptr);
getPayloadTypeInfo().as<ClassTypeInfo>().strongRetain(IGF, e, IGF.getDefaultAtomicity());
return;
}

IGF.emitStrongRetain(ptr, Refcounting, IGF.getDefaultAtomicity());
return;
}
case ForwardToPayload:
case POD:
case Normal:
Expand All @@ -2495,9 +2504,17 @@ namespace {
void releaseRefcountedPayload(IRGenFunction &IGF,
llvm::Value *ptr) const {
switch (CopyDestroyKind) {
case NullableRefcounted:
case NullableRefcounted: {
if (Refcounting == ReferenceCounting::Custom) {
Explosion e;
e.add(ptr);
getPayloadTypeInfo().as<ClassTypeInfo>().strongRelease(IGF, e, IGF.getDefaultAtomicity());
return;
}

IGF.emitStrongRelease(ptr, Refcounting, IGF.getDefaultAtomicity());
return;
}
case ForwardToPayload:
case POD:
case Normal:
Expand Down
10 changes: 9 additions & 1 deletion test/Interop/Cxx/foreign-reference/reference-counted.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none -Xfrontend -disable-llvm-verify -g)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none -Xfrontend -disable-llvm-verify)
//
// REQUIRES: executable_test
// TODO: This should work without ObjC interop in the future rdar://97497120
Expand Down Expand Up @@ -29,6 +29,14 @@ ReferenceCountedTestSuite.test("Local") {
expectEqual(finalLocalRefCount, 0)
}

var globalOptional: NS.LocalCount? = nil

ReferenceCountedTestSuite.test("Global optional holding local ref count") {
expectEqual(finalLocalRefCount, 0)
globalOptional = NS.LocalCount.create()
expectEqual(finalLocalRefCount, 1)
}

@inline(never)
func globalTest1() {
var x = GlobalCount.create()
Expand Down