Skip to content

IRGen: lazy initialize ForeignClassMetadata #22857

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
Mar 8, 2019
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
44 changes: 37 additions & 7 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,21 @@ static void emitMetadataCompletionFunction(IRGenModule &IGM,
IGF.Builder.CreateRet(returnValue);
}

static bool needsForeignMetadataCompletionFunction(StructDecl *decl) {
static bool needsForeignMetadataCompletionFunction(IRGenModule &IGM,
StructDecl *decl) {
// Currently, foreign structs never need a completion function.
return false;
}

static bool needsForeignMetadataCompletionFunction(EnumDecl *decl) {
static bool needsForeignMetadataCompletionFunction(IRGenModule &IGM,
EnumDecl *decl) {
// Currently, foreign enums never need a completion function.
return false;
}

static bool needsForeignMetadataCompletionFunction(ClassDecl *decl) {
return decl->hasSuperclass();
static bool needsForeignMetadataCompletionFunction(IRGenModule &IGM,
ClassDecl *decl) {
return IGM.getOptions().LazyInitializeClassMetadata || decl->hasSuperclass();
}

/*****************************************************************************/
Expand Down Expand Up @@ -1171,7 +1174,7 @@ namespace {
}

bool needsForeignMetadataCompletionFunction() {
return ::needsForeignMetadataCompletionFunction(Type);
return ::needsForeignMetadataCompletionFunction(IGM, Type);
}

/// Add an SingletonMetadataInitialization structure to the descriptor.
Expand Down Expand Up @@ -3804,7 +3807,7 @@ namespace {
}

bool needsMetadataCompletionFunction() {
return needsForeignMetadataCompletionFunction(Target);
return needsForeignMetadataCompletionFunction(IGM, Target);
}

void createMetadataCompletionFunction() {
Expand Down Expand Up @@ -3837,10 +3840,19 @@ namespace {
public:
ForeignClassMetadataBuilder(IRGenModule &IGM, ClassDecl *target,
ConstantStructBuilder &B)
: ForeignMetadataBuilderBase(IGM, target, B) {}
: ForeignMetadataBuilderBase(IGM, target, B) {
if (IGM.getOptions().LazyInitializeClassMetadata)
CanBeConstant = false;
}

void emitInitializeMetadata(IRGenFunction &IGF, llvm::Value *metadata,
MetadataDependencyCollector *collector) {
if (!Target->hasSuperclass()) {
assert(IGM.getOptions().LazyInitializeClassMetadata &&
"should have superclass if not lazy initializing class metadata");
return;
}

// Emit a reference to the superclass.
auto superclass = IGF.emitAbstractTypeMetadataRef(
Target->getSuperclass()->getCanonicalType());
Expand All @@ -3858,6 +3870,24 @@ namespace {
// Visitor methods.

void addValueWitnessTable() {
// The runtime will fill in the default VWT during allocation for the
// foreign class metadata.
//
// As of Swift 5.1, the runtime will fill in a default VWT during
// allocation of foreign class metadata. We rely on this for correctness
// on COFF, where we can't necessarily reference the stanard VWT from the
// metadata candidate, but it is a good optimization everywhere.
//
// The default VWT uses ObjC-compatible reference counting if ObjC interop
// is enabled and Swift-compatible reference counting otherwise. That is
// currently always good enough for foreign classes, so we can
// unconditionally rely on the default VWT.
//
// FIXME: take advantage of this on other targets when targeting a
// sufficiently recent runtime.
if (IGM.getOptions().LazyInitializeClassMetadata)
return B.addNullPointer(IGM.WitnessTablePtrTy);

// Without Objective-C interop, foreign classes must still use
// Swift native reference counting.
auto type = (IGM.ObjCInterop
Expand Down
11 changes: 11 additions & 0 deletions stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3655,6 +3655,17 @@ class ForeignMetadataCacheEntry
state = PrivateMetadataState::NonTransitiveComplete;
}
} else {
if (candidate->getValueWitnesses() == nullptr) {
assert(isa<ForeignClassMetadata>(candidate) &&
"cannot set default value witnesses for non-class foreign types");
// Fill in the default VWT if it was not set in the candidate at build
// time.
#if SWIFT_OBJC_INTEROP
candidate->setValueWitnesses(&VALUE_WITNESS_SYM(BO));
#else
candidate->setValueWitnesses(&VALUE_WITNESS_SYM(Bo));
#endif
}
state = inferStateForMetadata(candidate);
}

Expand Down
16 changes: 13 additions & 3 deletions test/IRGen/cf.sil
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/chex.py < %s > %t/cf.sil
// RUN: %target-swift-frontend -enable-objc-interop -sdk %S/Inputs %t/cf.sil -emit-ir -import-cf-types | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize %t/cf.sil -DINT=i%target-ptrsize
// RUN: %target-swift-frontend -enable-objc-interop -sdk %S/Inputs %t/cf.sil -emit-ir -import-cf-types | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize %t/cf.sil -DINT=i%target-ptrsize -check-prefix CHECK-%target-import-type -check-prefix CHECK-%target-import-type-%target-ptrsize

// REQUIRES: CPU=i386 || CPU=x86_64

Expand All @@ -9,7 +9,14 @@
// CHECK: [[MUTABLE_REFRIGERATOR:%TSo24CCMutableRefrigeratorRefa]] = type
// CHECK: [[OBJC:%objc_object]] = type

// CHECK: @"$sSo17CCRefrigeratorRefaN" = linkonce_odr hidden constant <{ {{.*}} }> <{ i8** @"$sBOWV", [[INT]] 515, {{.*}}"$sSo17CCRefrigeratorRefaMn", [[TYPE]]* null, i8* null }>
// CHECK: @"$sSo17CCRefrigeratorRefaN" = linkonce_odr hidden
// CHECK-DIRECT-SAME: constant
// CHECK-INDIRECT-SAME: global
// CHECK-SAME: <{ {{.*}} }> <{
// -- value witness table
// CHECK-DIRECT-SAME: i8** @"$sBOWV",
// CHECK-INDIRECT-SAME: i8** null,
// CHECK-SAME: [[INT]] 515, {{.*}}"$sSo17CCRefrigeratorRefaMn", [[TYPE]]* null, i8* null }>

// CHECK: [[MUTABLE_REFRIGERATOR_NAME:@.*]] = private constant [52 x i8] c"CCMutableRefrigerator\00NCCMutableRefrigeratorRef\00St\00\00"

Expand All @@ -21,7 +28,10 @@
// CHECK-64-SAME: @"$sSo24CCMutableRefrigeratorRefaMr"

// CHECK-64: @"$sSo24CCMutableRefrigeratorRefaN" = linkonce_odr hidden global <{ {{.*}} }> <{
// CHECK-64-SAME: i8** @"$sBOWV", i64 515, {{.*}}"$sSo24CCMutableRefrigeratorRefaMn", %swift.type* null, i8* null }>
// -- value witness table
// CHECK-DIRECT-64-SAME: i8** @"$sBOWV",
// CHECK-INDIRECT-64-SAME: i8** null,
// CHECK-64-SAME: i64 515, {{.*}}"$sSo24CCMutableRefrigeratorRefaMn", %swift.type* null, i8* null }>

sil_stage canonical

Expand Down