Skip to content

[IRGen] Don't emit relative references to Objective-C class references. #21107

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 3 commits into from
Dec 7, 2018
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
37 changes: 13 additions & 24 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2565,28 +2565,12 @@ getProtocolDescriptorEntityReference(IRGenModule &IGM, ProtocolDecl *protocol) {
}

static TypeEntityReference
getObjCClassRefEntityReference(IRGenModule &IGM, ClassDecl *cls) {
// A reference to an Objective-C class object.
assert(cls->isObjC() && "Must have an Objective-C class here");

auto kind = TypeReferenceKind::IndirectObjCClass;
auto entity = LinkEntity::forObjCClassRef(cls);

auto ref = IGM.getAddrOfLLVMVariableOrGOTEquivalent(entity);
assert(!ref.isIndirect());

return TypeEntityReference(kind, ref.getValue());
}

static TypeEntityReference
getRuntimeOnlyClassEntityReference(IRGenModule &IGM, ClassDecl *cls) {
assert(cls->getForeignClassKind() == ClassDecl::ForeignKind::RuntimeOnly);

auto namedClangDecl = Mangle::ASTMangler::getClangDeclForMangling(cls);
assert(namedClangDecl);

getObjCClassByNameReference(IRGenModule &IGM, ClassDecl *cls) {
auto kind = TypeReferenceKind::DirectObjCClassName;
auto ref = IGM.getAddrOfGlobalString(namedClangDecl->getName());
SmallString<64> objcRuntimeNameBuffer;
auto ref = IGM.getAddrOfGlobalString(
cls->getObjCRuntimeName(objcRuntimeNameBuffer),
/*willByRelativelyAddressed=*/true);

return TypeEntityReference(kind, ref);
}
Expand All @@ -2605,17 +2589,22 @@ IRGenModule::getTypeEntityReference(NominalTypeDecl *decl) {

switch (clas->getForeignClassKind()) {
case ClassDecl::ForeignKind::RuntimeOnly:
return getRuntimeOnlyClassEntityReference(*this, clas);
return getObjCClassByNameReference(*this, clas);

case ClassDecl::ForeignKind::CFType:
return getTypeContextDescriptorEntityReference(*this, clas);

case ClassDecl::ForeignKind::Normal:
if (hasKnownSwiftMetadata(*this, clas)) {
return getTypeContextDescriptorEntityReference(*this, clas);
} else {
return getObjCClassRefEntityReference(*this, clas);
}

// Note: we would like to use an Objective-C class reference, but the
// Darwin linker currently has a bug where it will coalesce these symbols
// *after* computing a relative offset, causing incorrect relative
// offsets in the metadata. Therefore, reference Objective-C classes by
// their runtime names.
return getObjCClassByNameReference(*this, clas);
}
llvm_unreachable("bad foreign type kind");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/Linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ std::string LinkEntity::mangleAsString() const {
case Kind::ObjCClassRef: {
llvm::SmallString<64> tempBuffer;
StringRef name = cast<ClassDecl>(getDecl())->getObjCRuntimeName(tempBuffer);
std::string Result("\01l_OBJC_CLASS_REF_$_");
std::string Result("OBJC_CLASS_REF_$_");
Result.append(name.data(), name.size());
return Result;
}
Expand Down
2 changes: 1 addition & 1 deletion test/ClangImporter/attr-swift_private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public func testTopLevel() {
}

// CHECK-LABEL: define linkonce_odr hidden swiftcc %swift.metadata_response @"$sSo10PrivFooSubCMa{{.*}} {
// CHECK: %objc_class** @"\01l_OBJC_CLASS_REF_$_PrivFooSub"
// CHECK: %objc_class** @"OBJC_CLASS_REF_$_PrivFooSub"
// CHECK: }

// CHECK-LABEL: define linkonce_odr hidden {{.+}} @"$sSo3BarC8__noArgsABSgyt_tcfcTO"
Expand Down
4 changes: 2 additions & 2 deletions test/ClangImporter/objc_ir.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ func propertyAccess(b b: B) {
// CHECK: load i8*, i8** @"\01L_selector(setCounter:)"
b.counter = b.counter + 1

// CHECK: load %objc_class*, %objc_class** @"\01l_OBJC_CLASS_REF_$_B"
// CHECK: load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_B"
// CHECK: load i8*, i8** @"\01L_selector(sharedCounter)"
// CHECK: load i8*, i8** @"\01L_selector(setSharedCounter:)"
B.sharedCounter = B.sharedCounter + 1
}

// CHECK-LABEL: define hidden swiftcc %TSo1BC* @"$s7objc_ir8downcast1aSo1BCSo1AC_tF"(
func downcast(a a: A) -> B {
// CHECK: [[CLASS:%.*]] = load %objc_class*, %objc_class** @"\01l_OBJC_CLASS_REF_$_B"
// CHECK: [[CLASS:%.*]] = load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_B"
// CHECK: [[T0:%.*]] = call %objc_class* @swift_getInitializedObjCClass(%objc_class* [[CLASS]])
// CHECK: [[T1:%.*]] = bitcast %objc_class* [[T0]] to i8*
// CHECK: call i8* @swift_dynamicCastObjCClassUnconditional(i8* [[A:%.*]], i8* [[T1]]) [[NOUNWIND:#[0-9]+]]
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/COFF-objc-sections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class D {
// CHECK-COFF-NOT: @"$s4main1CCMf" = {{.*}}, section "__DATA,__objc_data, regular"
// CHECK-COFF: @"\01l_OBJC_LABEL_PROTOCOL_$_P" = {{.*}}, section ".objc_protolist$B"
// CHECK-COFF: @"\01l_OBJC_PROTOCOL_REFERENCE_$_P" = {{.*}}, section ".objc_protorefs$B"
// CHECK-COFF: @"\01l_OBJC_CLASS_REF_$_I" = {{.*}}, section ".objc_classrefs$B"
// CHECK-COFF: @"OBJC_CLASS_REF_$_I" = {{.*}}, section ".objc_classrefs$B"
// CHECK-COFF: @"\01L_selector(init)" = {{.*}}, section ".objc_selrefs$B"
// CHECK-COFF: @objc_classes = {{.*}}, section ".objc_classlist$B"
// CHECK-COFF: @objc_categories = {{.*}}, section ".objc_catlist$B"
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/ELF-objc-sections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class D {
// CHECK-ELF-NOT: @"$s4main1CCMf" = {{.*}}, section "__DATA,__objc_data, regular"
// CHECK-ELF: @"\01l_OBJC_LABEL_PROTOCOL_$_P" = {{.*}}, section "objc_protolist"
// CHECK-ELF: @"\01l_OBJC_PROTOCOL_REFERENCE_$_P" = {{.*}}, section "objc_protorefs", align 8
// CHECK-ELF: @"\01l_OBJC_CLASS_REF_$_I" = {{.*}}, section "objc_classrefs", align 8
// CHECK-ELF: @"OBJC_CLASS_REF_$_I" = {{.*}}, section "objc_classrefs", align 8
// CHECK-ELF: @"\01L_selector(init)" = {{.*}}, section "objc_selrefs"
// CHECK-ELF: @objc_classes = {{.*}}, section "objc_classlist"
// CHECK-ELF: @objc_categories = {{.*}}, section "objc_catlist"
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/MachO-objc-sections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class D {
// CHECK-MACHO: @"$s4main1CCMf" = {{.*}}, section "__DATA,__objc_data, regular"
// CHECK-MACHO: @"\01l_OBJC_LABEL_PROTOCOL_$_P" = {{.*}}, section "__DATA,__objc_protolist,coalesced,no_dead_strip"
// CHECK-MACHO: @"\01l_OBJC_PROTOCOL_REFERENCE_$_P" = {{.*}}, section "__DATA,__objc_protorefs,coalesced,no_dead_strip"
// CHECK-MACHO: @"\01l_OBJC_CLASS_REF_$_I" = {{.*}}, section "__DATA,__objc_classrefs,regular,no_dead_strip"
// CHECK-MACHO: @"OBJC_CLASS_REF_$_I" = {{.*}}, section "__DATA,__objc_classrefs,regular,no_dead_strip"
// CHECK-MACHO: @"\01L_selector(init)" = {{.*}}, section "__DATA,__objc_selrefs,literal_pointers,no_dead_strip"
// CHECK-MACHO: @objc_classes = {{.*}}, section "__DATA,__objc_classlist,regular,no_dead_strip"
// CHECK-MACHO: @objc_categories = {{.*}}, section "__DATA,__objc_catlist,regular,no_dead_strip"
Expand Down
4 changes: 3 additions & 1 deletion test/IRGen/objc_bridged_generic_conformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

// CHECK-NOT: _TMnCSo

// CHECK: @"$sSo6ThingyCyxG32objc_bridged_generic_conformance1PADMc" = hidden constant %swift.protocol_conformance_descriptor {{.*}} @"\01l_OBJC_CLASS_REF_$_Thingy"
// CHECK: @"$sSo6ThingyCyxG32objc_bridged_generic_conformance1PADMc" = hidden constant %swift.protocol_conformance_descriptor {{.*}} @[[THINGY_NAME:[0-9]]]

// CHECK: @[[THINGY_NAME]] = private constant [7 x i8] c"Thingy\00"

// CHECK-NOT: _TMnCSo

Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/objc_casts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bb0(%unused : $@thick T.Type, %obj : $NSObject):
// CHECK: [[T0:%.*]] = call %objc_object* @swift_dynamicCastMetatypeToObjectUnconditional(%swift.type* %0)
// TODO: is this really necessary? also, this really shouldn't use a direct reference
// CHECK-NEXT: [[T1:%.*]] = bitcast %objc_object* [[T0]] to i8*
// CHECK-NEXT: [[T2a:%.*]] = load %objc_class*, %objc_class** @"\01l_OBJC_CLASS_REF_$_Foo"
// CHECK-NEXT: [[T2a:%.*]] = load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_Foo"
// CHECK-NEXT: [[T2:%.*]] = call %objc_class* @swift_getInitializedObjCClass(%objc_class* [[T2a]])
// CHECK-NEXT: [[T3:%.*]] = bitcast %objc_class* [[T2]] to i8*
// CHECK-NEXT: call i8* @swift_dynamicCastObjCClassUnconditional(i8* [[T1]], i8* [[T3]])
Expand All @@ -49,7 +49,7 @@ bb0(%metatype : $@thick T.Type):
// CHECK: [[T0:%.*]] = call %objc_object* @swift_dynamicCastMetatypeToObjectUnconditional(%swift.type* [[ARG]])
// TODO: is this really necessary? also, this really shouldn't use a direct reference
// CHECK-NEXT: [[T1:%.*]] = bitcast %objc_object* [[T0]] to i8*
// CHECK-NEXT: [[T2a:%.*]] = load %objc_class*, %objc_class** @"\01l_OBJC_CLASS_REF_$_Foo"
// CHECK-NEXT: [[T2a:%.*]] = load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_Foo"
// CHECK-NEXT: [[T2:%.*]] = call %objc_class* @swift_getInitializedObjCClass(%objc_class* [[T2a]])
// CHECK-NEXT: [[T3:%.*]] = bitcast %objc_class* [[T2]] to i8*
// CHECK-NEXT: call i8* @swift_dynamicCastObjCClassUnconditional(i8* [[T1]], i8* [[T3]])
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/objc_generic_class_metadata.sil
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ entry:
%b = metatype $@thick GenericClass<NSObject>.Type
apply %z<GenericClass<NSObject>>(%b) : $@convention(thin) <T> (@thick T.Type) -> ()

// CHECK: [[T0:%.*]] = load %objc_class*, %objc_class** @"\01l_OBJC_CLASS_REF_$_GenericClass",
// CHECK: [[T0:%.*]] = load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_GenericClass",
// CHECK: [[OBJC_CLASS:%.*]] = call %objc_class* @swift_getInitializedObjCClass(%objc_class* [[T0]])
// CHECK: call swiftcc void @objc_class_sink(%objc_class* [[OBJC_CLASS]], %swift.type* [[METADATA]])
%c = metatype $@objc_metatype GenericClass<NSString>.Type
Expand Down Expand Up @@ -79,7 +79,7 @@ entry(%0: @owned $Subclass, %1: @owned $NSDictionary):
}

// CHECK-LABEL: define linkonce_odr hidden swiftcc %swift.metadata_response @"$sSo12GenericClassCMa"(
// CHECK: [[T0:%.*]] = load %objc_class*, %objc_class** @"\01l_OBJC_CLASS_REF_$_GenericClass",
// CHECK: [[T0:%.*]] = load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_GenericClass",
// CHECK: call %objc_class* @swift_getInitializedObjCClass(%objc_class* [[T0]])

// CHECK-LABEL: define linkonce_odr hidden swiftcc %swift.metadata_response @"$sSaySo12GenericClassC_SitGMa"
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/objc_properties_jit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension NSString {
}

// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} private void @runtime_registration
// CHECK: [[NSOBJECT_UNINIT:%.*]] = load %objc_class*, %objc_class** @"\01l_OBJC_CLASS_REF_$_NSString"
// CHECK: [[NSOBJECT_UNINIT:%.*]] = load %objc_class*, %objc_class** @"OBJC_CLASS_REF_$_NSString"
// CHECK: [[NSOBJECT:%.*]] = call %objc_class* @swift_getInitializedObjCClass(%objc_class* [[NSOBJECT_UNINIT]])
// CHECK: [[GET_CLASS_PROP:%.*]] = call i8* @sel_registerName({{.*}}(classProp)
// CHECK: call i8* @class_replaceMethod(%objc_class* @"OBJC_METACLASS_$_NSString", i8* [[GET_CLASS_PROP]]
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/objc_runtime_visible_conformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extension A : YourProtocol {}

// CHECK-LABEL: @"$sSo1AC32objc_runtime_visible_conformance10MyProtocolACMc"
// CHECK-SAME: @"$s32objc_runtime_visible_conformance10MyProtocolMp"
// CHECK-SAME: [2 x i8]* [[STRING_A:@[0-9]+]]
// CHECK-SAME: [[STRING_A:@[0-9]+]]
// CHECK-SAME: @"$sSo1AC32objc_runtime_visible_conformance10MyProtocolACWP"
// DirectObjCClassName
// CHECK-SAME: i32 16
// CHECK: [[STRING_A]] = private unnamed_addr constant [2 x i8] c"A\00"
// CHECK: [[STRING_A]] = private constant [22 x i8] c"MyRuntimeVisibleClass\00"
8 changes: 5 additions & 3 deletions test/IRGen/protocol_conformance_records_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ extension NSRect: Runcible {
// CHECK-LABEL: @"$sSo5GizmoC33protocol_conformance_records_objc8RuncibleACMc" = constant %swift.protocol_conformance_descriptor {
// -- protocol descriptor
// CHECK-SAME: [[RUNCIBLE]]
// -- class object reference
// CHECK-SAME: @"\01l_OBJC_CLASS_REF_$_Gizmo"
// -- class name reference
// CHECK-SAME: @[[GIZMO_NAME:[0-9]+]]
// -- witness table
// CHECK-SAME: @"$sSo5GizmoC33protocol_conformance_records_objc8RuncibleACWP"
// -- flags
// CHECK-SAME: i32 24
// CHECK-SAME: i32 16
// CHECK-SAME: }
extension Gizmo: Runcible {
public func runce() {}
}

// CHECK: @[[GIZMO_NAME]] = private constant [6 x i8] c"Gizmo\00"

// CHECK-LABEL: @"\01l_protocol_conformances" = private constant [
// CHECK-SAME: @"$sSo6NSRectV33protocol_conformance_records_objc8RuncibleACMc"
// CHECK-SAME: @"$sSo5GizmoC33protocol_conformance_records_objc8RuncibleACMc"