Skip to content

[5.6] IRGen: Fix using weak linkage to avoid duplicate metadata by only applying this to PROTOCOL related data #40546

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
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
144 changes: 80 additions & 64 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,22 +1317,25 @@ namespace {
assert(TheExtension && "can't emit category data for a class");
ConstantInitBuilder builder(IGM);
auto fields = builder.beginStruct();
auto internalLinkage = llvm::GlobalVariable::InternalLinkage;

// struct category_t {
// char const *name;
fields.add(IGM.getAddrOfGlobalString(CategoryName));
// const class_t *theClass;
fields.add(getClassMetadataRef());
// const method_list_t *instanceMethods;
emitAndAddMethodList(fields, MethodListKind::InstanceMethods);
emitAndAddMethodList(fields, MethodListKind::InstanceMethods,
internalLinkage);
// const method_list_t *classMethods;
emitAndAddMethodList(fields, MethodListKind::ClassMethods);
emitAndAddMethodList(fields, MethodListKind::ClassMethods,
internalLinkage);
// const protocol_list_t *baseProtocols;
fields.add(buildProtocolList());
fields.add(buildProtocolList(internalLinkage));
// const property_list_t *properties;
fields.add(buildPropertyList(ForClass));
fields.add(buildPropertyList(ForClass, internalLinkage));
// const property_list_t *classProperties;
fields.add(buildPropertyList(ForMetaClass));
fields.add(buildPropertyList(ForMetaClass, internalLinkage));
// uint32_t size;
// FIXME: Clang does this by using non-ad-hoc types for ObjC runtime
// structures.
Expand All @@ -1342,13 +1345,14 @@ namespace {

assert(fields.getNextOffsetFromGlobal() == size);
return buildGlobalVariable(fields, "_CATEGORY_", /*const*/ true,
llvm::GlobalVariable::InternalLinkage);
internalLinkage);
}

llvm::Constant *emitProtocol() {
ConstantInitBuilder builder(IGM);
auto fields = builder.beginStruct();
llvm::SmallString<64> nameBuffer;
auto weakLinkage = llvm::GlobalVariable::WeakAnyLinkage;

assert(isBuildingProtocol() && "not emitting a protocol");

Expand All @@ -1358,17 +1362,20 @@ namespace {
// char const *name;
fields.add(IGM.getAddrOfGlobalString(getEntityName(nameBuffer)));
// const protocol_list_t *baseProtocols;
fields.add(buildProtocolList());
fields.add(buildProtocolList(weakLinkage));
// const method_list_t *requiredInstanceMethods;
emitAndAddMethodList(fields, MethodListKind::InstanceMethods);
emitAndAddMethodList(fields, MethodListKind::InstanceMethods,
weakLinkage);
// const method_list_t *requiredClassMethods;
emitAndAddMethodList(fields, MethodListKind::ClassMethods);
emitAndAddMethodList(fields, MethodListKind::ClassMethods, weakLinkage);
// const method_list_t *optionalInstanceMethods;
emitAndAddMethodList(fields, MethodListKind::OptionalInstanceMethods);
emitAndAddMethodList(fields, MethodListKind::OptionalInstanceMethods,
weakLinkage);
// const method_list_t *optionalClassMethods;
emitAndAddMethodList(fields, MethodListKind::OptionalClassMethods);
emitAndAddMethodList(fields, MethodListKind::OptionalClassMethods,
weakLinkage);
// const property_list_t *properties;
fields.add(buildPropertyList(ForClass));
fields.add(buildPropertyList(ForClass, weakLinkage));

// uint32_t size;
// FIXME: Clang does this by using non-ad-hoc types for ObjC runtime
Expand All @@ -1390,11 +1397,12 @@ namespace {
// const char *demangledName;
fields.addNullPointer(IGM.Int8PtrTy);
// const property_list_t *classProperties;
fields.add(buildPropertyList(ForMetaClass));
fields.add(buildPropertyList(ForMetaClass, weakLinkage));
// };

assert(fields.getNextOffsetFromGlobal() == size);
return buildGlobalVariable(fields, "_PROTOCOL_", /*const*/ true);
return buildGlobalVariable(fields, "_PROTOCOL_", /*const*/ true,
weakLinkage);
}

void emitRODataFields(ConstantStructBuilder &b,
Expand Down Expand Up @@ -1455,12 +1463,14 @@ namespace {
b.add(buildName());

// const method_list_t *baseMethods;
emitAndAddMethodList(b, forMeta ? MethodListKind::ClassMethods
: MethodListKind::InstanceMethods);
emitAndAddMethodList(b,
forMeta ? MethodListKind::ClassMethods
: MethodListKind::InstanceMethods,
llvm::GlobalVariable::InternalLinkage);

// const protocol_list_t *baseProtocols;
// Apparently, this list is the same in the class and the metaclass.
b.add(buildProtocolList());
b.add(buildProtocolList(llvm::GlobalVariable::InternalLinkage));

// const ivar_list_t *ivars;
if (forMeta) {
Expand All @@ -1474,7 +1484,7 @@ namespace {
b.addNullPointer(IGM.Int8PtrTy);

// const property_list_t *baseProperties;
b.add(buildPropertyList(forMeta));
b.add(buildPropertyList(forMeta, llvm::GlobalVariable::InternalLinkage));

// If hasUpdater is true, the metadata update callback goes here.
if (hasUpdater) {
Expand Down Expand Up @@ -1723,7 +1733,8 @@ namespace {

/// Emit the method list and add the pointer to the `builder`.
void emitAndAddMethodList(ConstantInitBuilder::StructBuilder &builder,
MethodListKind kind) {
MethodListKind kind,
llvm::GlobalValue::LinkageTypes linkage) {
ArrayRef<MethodDescriptor> methods;
StringRef namePrefix;
switch (kind) {
Expand All @@ -1748,7 +1759,8 @@ namespace {
namePrefix = "_PROTOCOL_INSTANCE_METHODS_OPT_";
break;
}
llvm::Constant *methodListPtr = buildMethodList(methods, namePrefix);
llvm::Constant *methodListPtr =
buildMethodList(methods, namePrefix, linkage);
builder.add(methodListPtr);
}

Expand All @@ -1769,7 +1781,8 @@ namespace {
}

return buildGlobalVariable(array, "_PROTOCOL_METHOD_TYPES_",
/*const*/ true);
/*const*/ true,
llvm::GlobalVariable::WeakAnyLinkage);
}

void buildExtMethodTypes(ConstantArrayBuilder &array,
Expand All @@ -1792,13 +1805,14 @@ namespace {
///
/// This method does not return a value of a predictable type.
llvm::Constant *buildMethodList(ArrayRef<MethodDescriptor> methods,
StringRef name) {
return buildOptionalList(methods, 3 * IGM.getPointerSize(), name,
/*isConst*/ false,
[&](ConstantArrayBuilder &descriptors,
MethodDescriptor descriptor) {
buildMethod(descriptors, descriptor);
});
StringRef name,
llvm::GlobalValue::LinkageTypes linkage) {
return buildOptionalList(
methods, 3 * IGM.getPointerSize(), name,
/*isConst*/ false, linkage,
[&](ConstantArrayBuilder &descriptors, MethodDescriptor descriptor) {
buildMethod(descriptors, descriptor);
});
}

/*** Protocols *********************************************************/
Expand All @@ -1815,16 +1829,15 @@ namespace {
/// };
///
/// This method does not return a value of a predictable type.
llvm::Constant *buildProtocolList() {
return buildOptionalList(Protocols, Size(0),
chooseNamePrefix("_PROTOCOLS_",
"_CATEGORY_PROTOCOLS_",
"_PROTOCOL_PROTOCOLS_"),
/*isConst*/ true,
[&](ConstantArrayBuilder &descriptors,
ProtocolDecl *protocol) {
buildProtocol(descriptors, protocol);
});
llvm::Constant *buildProtocolList(llvm::GlobalValue::LinkageTypes linkage) {
return buildOptionalList(
Protocols, Size(0),
chooseNamePrefix("_PROTOCOLS_", "_CATEGORY_PROTOCOLS_",
"_PROTOCOL_PROTOCOLS_"),
/*isConst*/ true, linkage,
[&](ConstantArrayBuilder &descriptors, ProtocolDecl *protocol) {
buildProtocol(descriptors, protocol);
});
}

void buildProtocol(ConstantArrayBuilder &array, ProtocolDecl *protocol) {
Expand Down Expand Up @@ -1945,12 +1958,12 @@ namespace {
/// This method does not return a value of a predictable type.
llvm::Constant *buildIvarList() {
Size eltSize = 3 * IGM.getPointerSize() + Size(8);
return buildOptionalList(Ivars, eltSize, "_IVARS_",
/*constant*/ true,
[&](ConstantArrayBuilder &descriptors,
Field field) {
buildIvar(descriptors, field);
});
return buildOptionalList(
Ivars, eltSize, "_IVARS_",
/*constant*/ true, llvm::GlobalVariable::InternalLinkage,
[&](ConstantArrayBuilder &descriptors, Field field) {
buildIvar(descriptors, field);
});
}

/*** Properties ********************************************************/
Expand Down Expand Up @@ -2058,12 +2071,14 @@ namespace {
/// };
///
/// This method does not return a value of a predictable type.
llvm::Constant *buildPropertyList(ForMetaClass_t classOrMeta) {
llvm::Constant *buildPropertyList(ForMetaClass_t classOrMeta,
llvm::GlobalValue::LinkageTypes linkage) {
if (classOrMeta == ForClass) {
return buildPropertyList(InstanceProperties,
chooseNamePrefix("_PROPERTIES_",
"_CATEGORY_PROPERTIES_",
"_PROTOCOL_PROPERTIES_"));
"_PROTOCOL_PROPERTIES_"),
linkage);
}

// Older OSs' libobjcs can't handle class property data.
Expand All @@ -2075,18 +2090,20 @@ namespace {
return buildPropertyList(ClassProperties,
chooseNamePrefix("_CLASS_PROPERTIES_",
"_CATEGORY_CLASS_PROPERTIES_",
"_PROTOCOL_CLASS_PROPERTIES_"));
"_PROTOCOL_CLASS_PROPERTIES_"),
linkage);
}

llvm::Constant *buildPropertyList(ArrayRef<VarDecl*> properties,
StringRef namePrefix) {
llvm::Constant *buildPropertyList(ArrayRef<VarDecl *> properties,
StringRef namePrefix,
llvm::GlobalValue::LinkageTypes linkage) {
Size eltSize = 2 * IGM.getPointerSize();
return buildOptionalList(properties, eltSize, namePrefix,
/*constant*/ true,
[&](ConstantArrayBuilder &descriptors,
VarDecl *property) {
buildProperty(descriptors, property);
});
return buildOptionalList(
properties, eltSize, namePrefix,
/*constant*/ true, linkage,
[&](ConstantArrayBuilder &descriptors, VarDecl *property) {
buildProperty(descriptors, property);
});
}

/*** General ***********************************************************/
Expand All @@ -2098,10 +2115,9 @@ namespace {
/// \param optionalEltSize - if non-zero, a size which needs
/// to be placed in the list header
template <class C, class Fn>
llvm::Constant *buildOptionalList(const C &objects,
Size optionalEltSize,
StringRef nameBase,
bool isConst,
llvm::Constant *buildOptionalList(const C &objects, Size optionalEltSize,
StringRef nameBase, bool isConst,
llvm::GlobalValue::LinkageTypes linkage,
Fn &&buildElement) {
if (objects.empty())
return null();
Expand Down Expand Up @@ -2140,9 +2156,9 @@ namespace {

fields.fillPlaceholderWithInt(countPosition, countType, count);

return buildGlobalVariable(fields, nameBase, isConst);
return buildGlobalVariable(fields, nameBase, isConst, linkage);
}

/// Get the name of the class or protocol to mangle into the ObjC symbol
/// name.
StringRef getEntityName(llvm::SmallVectorImpl<char> &buffer) const {
Expand All @@ -2169,9 +2185,9 @@ namespace {
/// Build a private global variable as a structure containing the
/// given fields.
template <class B>
llvm::Constant *buildGlobalVariable(B &fields, StringRef nameBase, bool isConst,
llvm::GlobalValue::LinkageTypes linkage =
llvm::GlobalVariable::WeakAnyLinkage) {
llvm::Constant *
buildGlobalVariable(B &fields, StringRef nameBase, bool isConst,
llvm::GlobalValue::LinkageTypes linkage) {
llvm::SmallString<64> nameBuffer;
auto var =
fields.finishAndCreateGlobal(Twine(nameBase)
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/generic_casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import gizmo
// CHECK: @"\01l_OBJC_LABEL_PROTOCOL_$_NSRuncing" = weak hidden global i8* bitcast ({{.*}} @_PROTOCOL_NSRuncing to i8*), section {{"__DATA,__objc_protolist,coalesced,no_dead_strip"|"objc_protolist"|".objc_protolist\$B"}}
// CHECK: @"\01l_OBJC_PROTOCOL_REFERENCE_$_NSRuncing" = weak hidden global i8* bitcast ({{.*}} @_PROTOCOL_NSRuncing to i8*), section {{"__DATA,__objc_protorefs,coalesced,no_dead_strip"|"objc_protorefs"|".objc_protorefs\$B"}}

// CHECK: @_PROTOCOLS__TtC13generic_casts10ObjCClass2 = weak hidden constant { i64, [1 x i8*] } {
// CHECK: @_PROTOCOLS__TtC13generic_casts10ObjCClass2 = internal constant { i64, [1 x i8*] } {
// CHECK: i64 1,
// CHECK: @_PROTOCOL__TtP13generic_casts10ObjCProto2_
// CHECK: }
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/objc_async_metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
// CHECK: [[ENCODE_ASYNC_STRING:@.*]] = private unnamed_addr constant [28 x i8] c"v24@0:8@?<v@?@\22NSString\22>16\00"
// CHECK: [[ENCODE_ASYNC_THROWS_STRING:@.*]] = private unnamed_addr constant [38 x i8] c"v24@0:8@?<v@?@\22NSString\22@\22NSError\22>16\00"

// CHECK: @_INSTANCE_METHODS__TtC19objc_async_metadata7MyClass = weak hidden constant
// CHECK: @_INSTANCE_METHODS__TtC19objc_async_metadata7MyClass = internal constant
// CHECK-SAME: methodWithCompletionHandler:
// CHECK-SAME: [[ENCODE_ASYNC_STRING]]
// CHECK-SAME: throwingMethodWithCompletionHandler:
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/objc_attr_NSManaged.sil
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import Foundation
sil_vtable X {}

// The getter/setter should not show up in the Objective-C metadata.
// CHECK: @_INSTANCE_METHODS__TtC19objc_attr_NSManaged10SwiftGizmo = weak hidden constant { i32, i32, [2 x { i8*, i8*, i8* }] } { i32 24, i32 2, {{.*}} @"\01L_selector_data(initWithBellsOn:)", {{.*}} @"\01L_selector_data(init)",
// CHECK: @_INSTANCE_METHODS__TtC19objc_attr_NSManaged10SwiftGizmo = internal constant { i32, i32, [2 x { i8*, i8*, i8* }] } { i32 24, i32 2, {{.*}} @"\01L_selector_data(initWithBellsOn:)", {{.*}} @"\01L_selector_data(init)",

// CHECK: [[X:@[0-9]+]] = private unnamed_addr constant [2 x i8] c"x\00"

// The property 'x' should show up in the Objective-C metadata.
// CHECK: @_PROPERTIES__TtC19objc_attr_NSManaged10SwiftGizmo = weak hidden constant { {{.*}}i32, i32, [1 x { i8*, i8* }] } { i32 16, i32 1, [1 x { i8*, i8* }] [{ i8*, i8* } { i8* getelementptr inbounds ([2 x i8], [2 x i8]* [[X]], i64 0, i64 0),
// CHECK: @_PROPERTIES__TtC19objc_attr_NSManaged10SwiftGizmo = internal constant { {{.*}}i32, i32, [1 x { i8*, i8* }] } { i32 16, i32 1, [1 x { i8*, i8* }] [{ i8*, i8* } { i8* getelementptr inbounds ([2 x i8], [2 x i8]* [[X]], i64 0, i64 0),

// The getter/setter should not show up in the Swift metadata.
// CHECK: @"$s19objc_attr_NSManaged10SwiftGizmoCMf" = internal global <{ {{.*}} }> <{ void (%T19objc_attr_NSManaged10SwiftGizmoC*)* {{.*}}@"$s19objc_attr_NSManaged10SwiftGizmoCfD{{(\.ptrauth)?}}"{{.*}}, i8**{{.*}} @"$sBOWV{{(.ptrauth)?(.)?[0-9]?}}"{{.*}}, {{.*}} @"OBJC_METACLASS_$__TtC19objc_attr_NSManaged10SwiftGizmo{{(.ptrauth)?}}"{{.*}}, %objc_class*{{.*}} @"OBJC_CLASS_$_Gizmo{{(.ptrauth)?}}"{{.*}}, %swift.opaque* @_objc_empty_cache, %swift.opaque* null,{{.*}}@_DATA__TtC19objc_attr_NSManaged10SwiftGizmo{{.*}}i64 {{1|2}}){{.*}}, i32 {{1|0}}, i32 0, i32 8, i16 7, i16 0, i32 96, i32 16, {{.*}}* @"$s19objc_attr_NSManaged10SwiftGizmoCMn{{(\.ptrauth)?}}"{{.*}}, i8* null }>
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/objc_bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
// CHECK: [[SETTER_SIGNATURE:@.*]] = private unnamed_addr constant [11 x i8] c"v24@0:8@16\00"
// CHECK: [[DEALLOC_SIGNATURE:@.*]] = private unnamed_addr constant [8 x i8] c"v16@0:8\00"

// CHECK: @_INSTANCE_METHODS__TtC11objc_bridge3Bas = weak hidden constant { i32, i32, [17 x { i8*, i8*, i8* }] } {
// CHECK: @_INSTANCE_METHODS__TtC11objc_bridge3Bas = internal constant { i32, i32, [17 x { i8*, i8*, i8* }] } {
// CHECK: i32 24,
// CHECK: i32 17,
// CHECK: [17 x { i8*, i8*, i8* }] [
Expand Down Expand Up @@ -114,10 +114,10 @@ import Foundation
// CHECK: ]
// CHECK: }, section "__DATA, {{.*}}", align 8

// CHECK: @_PROPERTIES__TtC11objc_bridge3Bas = weak hidden constant { i32, i32, [5 x { i8*, i8* }] } {
// CHECK: @_PROPERTIES__TtC11objc_bridge3Bas = internal constant { i32, i32, [5 x { i8*, i8* }] } {

// CHECK: [[OBJC_BLOCK_PROPERTY:@.*]] = private unnamed_addr constant [8 x i8] c"T@?,N,C\00"
// CHECK: @_PROPERTIES__TtC11objc_bridge21OptionalBlockProperty = weak hidden constant {{.*}} [[OBJC_BLOCK_PROPERTY]]
// CHECK: @_PROPERTIES__TtC11objc_bridge21OptionalBlockProperty = internal constant {{.*}} [[OBJC_BLOCK_PROPERTY]]

func getDescription(_ o: NSObject) -> String {
return o.description
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/objc_class_property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// class properties, so no ObjC property is reflected.

// CHECK-NOT: @_PROPERTIES__TtC19objc_class_property7Smashed
// CHECK: @_CLASS_METHODS__TtC19objc_class_property7Smashed = weak hidden constant { i32, i32, [1 x { i8*, i8*, i8* }] } {
// CHECK: @_CLASS_METHODS__TtC19objc_class_property7Smashed = internal constant { i32, i32, [1 x { i8*, i8*, i8* }] } {
// CHECK: i8* getelementptr inbounds ([14 x i8], [14 x i8]* @"\01L_selector_data(sharedSmashed)"
// CHECK-NOT: @_PROPERTIES__TtC19objc_class_property7Smashed
// CHECK: @_INSTANCE_METHODS__TtC19objc_class_property7Smashed = weak hidden constant { i32, i32, [1 x { i8*, i8*, i8* }] } {
// CHECK: @_INSTANCE_METHODS__TtC19objc_class_property7Smashed = internal constant { i32, i32, [1 x { i8*, i8*, i8* }] } {
// CHECK: i8* getelementptr inbounds ([5 x i8], [5 x i8]* @"\01L_selector_data(init)"
// CHECK-NOT: @_PROPERTIES__TtC19objc_class_property7Smashed

Expand Down
Loading