Skip to content

Use llvm.used for __objc_protorefs and __objc_protolist #27346

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
Oct 3, 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
10 changes: 10 additions & 0 deletions lib/IRGen/GenObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ IRGenModule::getObjCProtocolGlobalVars(ProtocolDecl *proto) {
protocolLabel->setSection(GetObjCSectionName("__objc_protolist",
"coalesced,no_dead_strip"));

// Mark used to prevent DCE of public unreferenced protocols to ensure
// that they are available for external use when a used module is used
// as a library.
addUsedGlobal(protocolLabel);

// Introduce a variable to reference the protocol.
auto *protocolRef =
new llvm::GlobalVariable(Module, Int8PtrTy, /*constant*/ false,
Expand All @@ -374,6 +379,11 @@ IRGenModule::getObjCProtocolGlobalVars(ProtocolDecl *proto) {
protocolRef->setSection(GetObjCSectionName("__objc_protorefs",
"coalesced,no_dead_strip"));

// Mark used to prevent DCE of public unreferenced protocols to ensure
// that they are available for external use when a used module is used
// as a library.
addUsedGlobal(protocolRef);

ObjCProtocolPair pair{protocolRecord, protocolRef};
ObjCProtocols.insert({proto, pair});

Expand Down
29 changes: 29 additions & 0 deletions test/IRGen/objc_protocol_vars.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -parse-as-library -emit-ir %s | %FileCheck %s

// It tests whether the vars @"\01l_OBJC_LABEL_PROTOCOL_$__TtP18objc_protocol_vars1T_"
// and @"\01l_OBJC_PROTOCOL_REFERENCE_$__TtP18objc_protocol_vars1T_" are in llvm.used.
//
// import Foundation
// @objc
// public protocol T: AnyObject {
//
// var current: Int32 { get }
//
// func clone() -> T
//
//}
sil_stage canonical

import Builtin
import Swift
import SwiftShims

import Foundation

@objc public protocol T : AnyObject {
@objc var current: Int32 { get }
@objc func clone() -> T
}

// 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*), {{.*}}], {{.*}}