Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 0e776a3

Browse files
committed
CodeGen: mark protocols as common data
This allows for the coalescing of the protocol declarations. When the protocols are declared in headers, multiple definitions of the protocol would be emitted. Marking them as common data indicates that any one can be selected. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285073 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d255fae commit 0e776a3

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

lib/CodeGen/CGObjCMac.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6562,15 +6562,20 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
65626562
const ObjCProtocolDecl *PD) {
65636563
llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
65646564

6565-
if (!Entry)
6565+
if (!Entry) {
65666566
// We use the initializer as a marker of whether this is a forward
65676567
// reference or not. At module finalization we add the empty
65686568
// contents for protocols which were referenced but never defined.
6569-
Entry =
6570-
new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6571-
false, llvm::GlobalValue::ExternalLinkage,
6572-
nullptr,
6573-
"\01l_OBJC_PROTOCOL_$_" + PD->getObjCRuntimeNameAsString());
6569+
llvm::SmallString<64> Protocol;
6570+
llvm::raw_svector_ostream(Protocol) << "\01l_OBJC_PROTOCOL_$_"
6571+
<< PD->getObjCRuntimeNameAsString();
6572+
6573+
Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6574+
false, llvm::GlobalValue::ExternalLinkage,
6575+
nullptr, Protocol);
6576+
if (!CGM.getTriple().isOSBinFormatMachO())
6577+
Entry->setComdat(CGM.getModule().getOrInsertComdat(Protocol));
6578+
}
65746579

65756580
return Entry;
65766581
}
@@ -6688,10 +6693,16 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
66886693
Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
66896694
Entry->setInitializer(Init);
66906695
} else {
6691-
Entry =
6692-
new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6693-
false, llvm::GlobalValue::WeakAnyLinkage, Init,
6694-
"\01l_OBJC_PROTOCOL_$_" + PD->getObjCRuntimeNameAsString());
6696+
llvm::SmallString<64> Protocol;
6697+
llvm::raw_svector_ostream(Protocol) << "\01l_OBJC_PROTOCOL_$_"
6698+
<< PD->getObjCRuntimeNameAsString();
6699+
6700+
Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6701+
false, llvm::GlobalValue::WeakAnyLinkage,
6702+
Init, Protocol);
6703+
if (!CGM.getTriple().isOSBinFormatMachO())
6704+
Entry->setComdat(CGM.getModule().getOrInsertComdat(Protocol));
6705+
66956706
Entry->setAlignment(
66966707
CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
66976708

@@ -6702,13 +6713,20 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
67026713

67036714
// Use this protocol meta-data to build protocol list table in section
67046715
// __DATA, __objc_protolist
6716+
llvm::SmallString<64> ProtocolRef;
6717+
llvm::raw_svector_ostream(ProtocolRef) << "\01l_OBJC_LABEL_PROTOCOL_$_"
6718+
<< PD->getObjCRuntimeNameAsString();
6719+
67056720
llvm::GlobalVariable *PTGV =
67066721
new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
67076722
false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6708-
"\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getObjCRuntimeNameAsString());
6723+
ProtocolRef);
6724+
if (!CGM.getTriple().isOSBinFormatMachO())
6725+
PTGV->setComdat(CGM.getModule().getOrInsertComdat(ProtocolRef));
67096726
PTGV->setAlignment(
67106727
CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
6711-
PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
6728+
if (CGM.getTriple().isOSBinFormatMachO())
6729+
PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
67126730
PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
67136731
CGM.addCompilerUsedGlobal(PTGV);
67146732
return Entry;

test/CodeGenObjC/protocol-comdat.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang -cc1 -triple thumbv7--windows-itanium -fobjc-runtime=ios -emit-llvm -o - %s -Wno-objc-root-class | FileCheck %s
2+
3+
@protocol P
4+
- (void) method;
5+
@end
6+
7+
@interface I<P>
8+
@end
9+
10+
@implementation I
11+
- (void) method { }
12+
@end
13+
14+
15+
// CHECK: $"\01l_OBJC_PROTOCOL_$_P" = comdat any
16+
// CHECK: $"\01l_OBJC_LABEL_PROTOCOL_$_P" = comdat any
17+
18+
// CHECK: @"\01l_OBJC_PROTOCOL_$_P" = {{.*}}, comdat
19+
// CHECK: @"\01l_OBJC_LABEL_PROTOCOL_$_P" = {{.*}}, comdat
20+

0 commit comments

Comments
 (0)