Skip to content

[cxx-interop][Runtime] Initialize metadata of a Swift array of C++ references correctly #73615

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
May 16, 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
1 change: 0 additions & 1 deletion include/swift/ABI/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ struct TargetMetadata {
case MetadataKind::Class:
case MetadataKind::ObjCClassWrapper:
case MetadataKind::ForeignClass:
case MetadataKind::ForeignReferenceType:
return true;

default:
Expand Down
5 changes: 3 additions & 2 deletions lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,8 @@ SILCombiner::propagateConcreteTypeOfInitExistential(FullApplySite Apply) {
/// getContiguousArrayStorageType<Int>(for:)
/// => metatype @thick ContiguousArrayStorage<Int>.Type
/// We know that `getContiguousArrayStorageType` will not return the AnyObject
/// type optimization for any non class or objc existential type instantiation.
/// type optimization for any non class or objc existential type instantiation
/// or a C++ foreign reference type.
static bool shouldReplaceCallByMetadataConstructor(CanType storageMetaTy) {
auto metaTy = dyn_cast<MetatypeType>(storageMetaTy);
if (!metaTy || metaTy->getRepresentation() != MetatypeRepresentation::Thick)
Expand All @@ -1451,7 +1452,7 @@ static bool shouldReplaceCallByMetadataConstructor(CanType storageMetaTy) {
if (ty->getStructOrBoundGenericStruct() || ty->getEnumOrBoundGenericEnum() ||
isa<BuiltinVectorType>(ty) || isa<BuiltinIntegerType>(ty) ||
isa<BuiltinFloatType>(ty) || isa<TupleType>(ty) ||
isa<AnyFunctionType>(ty) ||
isa<AnyFunctionType>(ty) || ty->isForeignReferenceType() ||
(ty->isAnyExistentialType() && !ty->isObjCExistentialType()))
return true;

Expand Down
4 changes: 4 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,10 @@ shouldReplaceCallByContiguousArrayStorageAnyObject(SILFunction &F,
auto ty = genericArgs[0]->getCanonicalType();
if (!ty->getClassOrBoundGenericClass() && !ty->isObjCExistentialType())
return std::nullopt;
// C++ foreign reference types have custom release/retain operations and are
// not AnyObjects.
if (ty->isForeignReferenceType())
return std::nullopt;

auto anyObjectTy = ctxt.getAnyObjectType();
auto arrayStorageTy =
Expand Down
5 changes: 4 additions & 1 deletion test/Interop/Cxx/foreign-reference/not-any-object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Test {

inline void* operator new(unsigned long, void* p) { return p; }

struct __attribute__((swift_attr("import_reference"))) Empty {
struct __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:immortal")))
__attribute__((swift_attr("release:immortal"))) Empty {
static Empty *create() { return new (malloc(sizeof(Empty))) Empty(); }
};

Expand All @@ -27,3 +29,4 @@ public func test(_ _: AnyObject) {}

// TODO: make this a better error.
test(Empty.create()) // expected-error {{type of expression is ambiguous without a type annotation}}
test([Empty.create()][0]) // expected-error {{argument type 'Any' expected to be an instance of a class or class-constrained type}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %target-swift-emit-irgen %s -I %S/Inputs -cxx-interoperability-mode=default -Xcc -fignore-exceptions -disable-availability-checking | %FileCheck %s
// XFAIL: OS=linux-android, OS=linux-androideabi
// XFAIL: OS=windows-msvc

import ReferenceCounted

Expand Down Expand Up @@ -51,3 +52,17 @@ public func getNullable(wantNullptr: Bool) -> GlobalCountNullableInit? {
// CHECK: lifetime.cont:
// CHECK: ret i64 %2
// CHECK-NEXT: }


public func getArrayOfLocalCount() -> [NS.LocalCount] {
return [NS.LocalCount.create()]
}

// CHECK: define {{.*}}swiftcc ptr @"$s4main20getArrayOfLocalCountSaySo2NSO0eF0VGyF"()
// CHECK-NEXT: entry:
// CHECK-NEXT: %0 = call swiftcc %swift.metadata_response @"$sSo2NSO10LocalCountVMa"(i64 0)
// CHECK-NEXT: %1 = extractvalue %swift.metadata_response %0, 0
// CHECK-NEXT: %2 = call swiftcc { ptr, ptr } @"$ss27_allocateUninitializedArrayySayxG_BptBwlF"(i64 1, ptr %1)
// CHECK: %5 = call ptr @{{_ZN2NS10LocalCount6createEv|"\?create\@LocalCount\@NS\@\@SAPEAU12\@XZ"}}()
// CHECK-NEXT: call void @{{_Z8LCRetainPN2NS10LocalCountE|"\?LCRetain\@\@YAXPEAULocalCount\@NS\@\@\@Z"}}(ptr %5)
// CHECK: }
14 changes: 14 additions & 0 deletions test/Interop/Cxx/foreign-reference/reference-counted.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@ ReferenceCountedTestSuite.test("Global") {
expectEqual(globalCount, 0)
}

var globalArray: [GlobalCount] = []

ReferenceCountedTestSuite.test("Global array") {
expectEqual(globalCount, 0)

globalArray = [GlobalCount.create()]
#if NO_OPTIMIZATIONS
expectEqual(globalCount, 1)
#endif

globalArray = []
expectEqual(globalCount, 0)
}

runAllTests()