Skip to content

Commit 333f0db

Browse files
authored
Merge pull request #27346 from jinlin-bayarea/llvm_used
Use llvm.used for __objc_protorefs and __objc_protolist
2 parents bb4f46f + 9e414da commit 333f0db

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/IRGen/GenObjC.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ IRGenModule::getObjCProtocolGlobalVars(ProtocolDecl *proto) {
364364
protocolLabel->setSection(GetObjCSectionName("__objc_protolist",
365365
"coalesced,no_dead_strip"));
366366

367+
// Mark used to prevent DCE of public unreferenced protocols to ensure
368+
// that they are available for external use when a used module is used
369+
// as a library.
370+
addUsedGlobal(protocolLabel);
371+
367372
// Introduce a variable to reference the protocol.
368373
auto *protocolRef =
369374
new llvm::GlobalVariable(Module, Int8PtrTy, /*constant*/ false,
@@ -375,6 +380,11 @@ IRGenModule::getObjCProtocolGlobalVars(ProtocolDecl *proto) {
375380
protocolRef->setSection(GetObjCSectionName("__objc_protorefs",
376381
"coalesced,no_dead_strip"));
377382

383+
// Mark used to prevent DCE of public unreferenced protocols to ensure
384+
// that they are available for external use when a used module is used
385+
// as a library.
386+
addUsedGlobal(protocolRef);
387+
378388
ObjCProtocolPair pair{protocolRecord, protocolRef};
379389
ObjCProtocols.insert({proto, pair});
380390

test/IRGen/objc_protocol_vars.sil

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -parse-as-library -emit-ir %s | %FileCheck %s
2+
3+
// It tests whether the vars @"\01l_OBJC_LABEL_PROTOCOL_$__TtP18objc_protocol_vars1T_"
4+
// and @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP18objc_protocol_vars1T_" are in llvm.used.
5+
//
6+
// import Foundation
7+
// @objc
8+
// public protocol T: AnyObject {
9+
//
10+
// var current: Int32 { get }
11+
//
12+
// func clone() -> T
13+
//
14+
//}
15+
sil_stage canonical
16+
17+
import Builtin
18+
import Swift
19+
import SwiftShims
20+
21+
import Foundation
22+
23+
@objc public protocol T : AnyObject {
24+
@objc var current: Int32 { get }
25+
@objc func clone() -> T
26+
}
27+
28+
// CHECK: @llvm.used = appending global [{{.*}}] [{{.*}}, i8* bitcast (i8** @"\01l_OBJC_LABEL_PROTOCOL_$__TtP18objc_protocol_vars1T_" to i8*), i8* bitcast (i8** @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP18objc_protocol_vars1T_" to i8*), {{.*}}], {{.*}}
29+

0 commit comments

Comments
 (0)