Skip to content

Add a -emit-dead-strippable-symbols flag that emits functions/variable/metadata in a dead_strip-friendly way. #34281

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

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ class IRGenOptions {
/// Emit names of struct stored properties and enum cases.
unsigned EnableReflectionNames : 1;

/// Emit metadata, variables and functions in a way that a linker can remove
/// them if they're unused, even across modules (when linking statically).
unsigned EmitDeadStrippableSymbols : 1;

/// Emit mangled names of anonymous context descriptors.
unsigned EnableAnonymousContextMangledNames : 1;

Expand Down Expand Up @@ -348,7 +352,8 @@ class IRGenOptions {
PrintInlineTree(false), EmbedMode(IRGenEmbedMode::None),
LLVMLTOKind(IRGenLLVMLTOKind::None), HasValueNamesSetting(false),
ValueNames(false), EnableReflectionMetadata(true),
EnableReflectionNames(true), EnableAnonymousContextMangledNames(false),
EnableReflectionNames(true), EmitDeadStrippableSymbols(false),
EnableAnonymousContextMangledNames(false),
ForcePublicLinkage(false), LazyInitializeClassMetadata(false),
LazyInitializeProtocolConformances(false), DisableLegacyTypeInfo(false),
PrespecializeGenericMetadata(false), UseIncrementalLLVMCodeGen(true),
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,11 @@ def emit_sorted_sil : Flag<["-"], "emit-sorted-sil">,
HelpText<"When printing SIL, print out all sil entities sorted by name to "
"ease diffing">;

def emit_dead_strippable_symbols : Flag<["-"], "emit-dead-strippable-symbols">,
HelpText<"Emit metadata, variables and functions in a way that a linker can "
"remove them if they're unused, even across modules (when linking "
"statically).">;

def emit_syntax : Flag<["-"], "emit-syntax">,
HelpText<"Parse input file(s) and emit the Syntax tree(s) as JSON">, ModeOpt;

Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,12 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
Opts.EnableReflectionNames = false;
}

if (Args.hasArg(OPT_emit_dead_strippable_symbols)) {
if (!Opts.UseJIT) {
Opts.EmitDeadStrippableSymbols = true;
}
}

if (Args.hasArg(OPT_force_public_linkage)) {
Opts.ForcePublicLinkage = true;
}
Expand Down
194 changes: 139 additions & 55 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1991,8 +1991,9 @@ void irgen::updateLinkageForDefinition(IRGenModule &IGM,

// Everything externally visible is considered used in Swift.
// That mostly means we need to be good at not marking things external.
if (LinkInfo::isUsed(IRL))
IGM.addUsedGlobal(global);
if (LinkInfo::isUsed(IRL) && !IGM.IRGen.Opts.EmitDeadStrippableSymbols) {
IGM.addUsedGlobal(global);
}
}

LinkInfo LinkInfo::get(IRGenModule &IGM, const LinkEntity &entity,
Expand Down Expand Up @@ -2085,7 +2086,11 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM,
// Everything externally visible is considered used in Swift.
// That mostly means we need to be good at not marking things external.
if (linkInfo.isUsed()) {
IGM.addUsedGlobal(fn);
// Don't allow dead_strip'ing for "main".
if (name == SWIFT_ENTRY_POINT_FUNCTION ||
!IGM.IRGen.Opts.EmitDeadStrippableSymbols) {
IGM.addUsedGlobal(fn);
}
}

return fn;
Expand Down Expand Up @@ -2132,7 +2137,7 @@ llvm::GlobalVariable *swift::irgen::createVariable(

// Everything externally visible is considered used in Swift.
// That mostly means we need to be good at not marking things external.
if (linkInfo.isUsed()) {
if (linkInfo.isUsed() && !IGM.IRGen.Opts.EmitDeadStrippableSymbols) {
IGM.addUsedGlobal(var);
}

Expand Down Expand Up @@ -3350,6 +3355,47 @@ llvm::Constant *IRGenModule::emitSwiftProtocols() {
if (SwiftProtocols.empty())
return nullptr;

StringRef sectionName;
switch (TargetInfo.OutputObjectFormat) {
case llvm::Triple::UnknownObjectFormat:
llvm_unreachable("Don't know how to emit protocols for "
"the selected object format.");
case llvm::Triple::MachO:
sectionName = IRGen.Opts.EmitDeadStrippableSymbols
? "__TEXT, __swift5_protos, regular, live_support"
: "__TEXT, __swift5_protos, regular, no_dead_strip";
break;
case llvm::Triple::ELF:
case llvm::Triple::Wasm:
sectionName = "swift5_protocols";
break;
case llvm::Triple::XCOFF:
case llvm::Triple::COFF:
sectionName = ".sw5prt$B";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question I have is if this information around sectionName is something that is also somewhere else. Seems like we should have that centralized so it isn't out of sync, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it was just below? Nm.

break;
}

if (IRGen.Opts.EmitDeadStrippableSymbols) {
for (auto *protocol : SwiftProtocols) {
auto ref = getTypeEntityReference(protocol);
auto name = "\x01l_protocol_" + std::string(ref.getValue()->getName());
auto var = new llvm::GlobalVariable(
Module, ProtocolRecordTy, /*isConstant*/ true,
llvm::GlobalValue::PrivateLinkage, /*initializer*/ nullptr, name);

llvm::Constant *relativeAddr =
emitDirectRelativeReference(ref.getValue(), var, {0});
llvm::Constant *recordFields[] = {relativeAddr};
auto record = llvm::ConstantStruct::get(ProtocolRecordTy, recordFields);
var->setInitializer(record);
var->setSection(sectionName);
var->setAlignment(llvm::MaybeAlign(4));

disableAddressSanitizer(*this, var);
}
return nullptr;
}

// Define the global variable for the protocol list.
ConstantInitBuilder builder(*this);
auto recordsArray = builder.beginArray(ProtocolRecordTy);
Expand All @@ -3374,24 +3420,6 @@ llvm::Constant *IRGenModule::emitSwiftProtocols() {
/*isConstant*/ true,
llvm::GlobalValue::PrivateLinkage);

StringRef sectionName;
switch (TargetInfo.OutputObjectFormat) {
case llvm::Triple::UnknownObjectFormat:
llvm_unreachable("Don't know how to emit protocols for "
"the selected object format.");
case llvm::Triple::MachO:
sectionName = "__TEXT, __swift5_protos, regular, no_dead_strip";
break;
case llvm::Triple::ELF:
case llvm::Triple::Wasm:
sectionName = "swift5_protocols";
break;
case llvm::Triple::XCOFF:
case llvm::Triple::COFF:
sectionName = ".sw5prt$B";
break;
}

var->setSection(sectionName);

disableAddressSanitizer(*this, var);
Expand All @@ -3413,6 +3441,49 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
if (ProtocolConformances.empty())
return nullptr;

StringRef sectionName;
switch (TargetInfo.OutputObjectFormat) {
case llvm::Triple::UnknownObjectFormat:
llvm_unreachable("Don't know how to emit protocol conformances for "
"the selected object format.");
case llvm::Triple::MachO:
sectionName = IRGen.Opts.EmitDeadStrippableSymbols
? "__TEXT, __swift5_proto, regular, live_support"
: "__TEXT, __swift5_proto, regular, no_dead_strip";
break;
case llvm::Triple::ELF:
case llvm::Triple::Wasm:
sectionName = "swift5_protocol_conformances";
break;
case llvm::Triple::XCOFF:
case llvm::Triple::COFF:
sectionName = ".sw5prtc$B";
break;
}

if (IRGen.Opts.EmitDeadStrippableSymbols) {
for (const auto &record : ProtocolConformances) {
auto conformance = record.conformance;
auto entity = LinkEntity::forProtocolConformanceDescriptor(conformance);
auto name =
"\x01l_protocol_conformance_" + std::string(entity.mangleAsString());
auto var = new llvm::GlobalVariable(
Module, RelativeAddressTy, /*isConstant*/ true,
llvm::GlobalValue::PrivateLinkage, /*initializer*/ nullptr, name);

auto descriptor =
getAddrOfLLVMVariable(entity, ConstantInit(), DebugTypeInfo());
llvm::Constant *relativeAddr =
emitDirectRelativeReference(descriptor, var, {});
var->setInitializer(relativeAddr);
var->setSection(sectionName);
var->setAlignment(llvm::MaybeAlign(4));

disableAddressSanitizer(*this, var);
}
return nullptr;
}

// Define the global variable for the conformance list.
ConstantInitBuilder builder(*this);
auto descriptorArray = builder.beginArray(RelativeAddressTy);
Expand All @@ -3434,24 +3505,6 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
/*isConstant*/ true,
llvm::GlobalValue::PrivateLinkage);

StringRef sectionName;
switch (TargetInfo.OutputObjectFormat) {
case llvm::Triple::UnknownObjectFormat:
llvm_unreachable("Don't know how to emit protocol conformances for "
"the selected object format.");
case llvm::Triple::MachO:
sectionName = "__TEXT, __swift5_proto, regular, no_dead_strip";
break;
case llvm::Triple::ELF:
case llvm::Triple::Wasm:
sectionName = "swift5_protocol_conformances";
break;
case llvm::Triple::XCOFF:
case llvm::Triple::COFF:
sectionName = ".sw5prtc$B";
break;
}

var->setSection(sectionName);

disableAddressSanitizer(*this, var);
Expand All @@ -3466,7 +3519,9 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
std::string sectionName;
switch (TargetInfo.OutputObjectFormat) {
case llvm::Triple::MachO:
sectionName = "__TEXT, __swift5_types, regular, no_dead_strip";
sectionName = IRGen.Opts.EmitDeadStrippableSymbols
? "__TEXT, __swift5_types, regular, live_support"
: "__TEXT, __swift5_types, regular, no_dead_strip";
break;
case llvm::Triple::ELF:
case llvm::Triple::Wasm:
Expand All @@ -3485,6 +3540,45 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
if (RuntimeResolvableTypes.empty())
return nullptr;

auto generateRecord = [this](TypeEntityReference ref,
llvm::GlobalVariable *var,
ArrayRef<unsigned> baseIndices) {
// Form the relative address, with the type reference kind in the low bits.
llvm::Constant *relativeAddr =
emitDirectRelativeReference(ref.getValue(), var, baseIndices);
unsigned lowBits = static_cast<unsigned>(ref.getKind());
if (lowBits != 0) {
relativeAddr = llvm::ConstantExpr::getAdd(
relativeAddr, llvm::ConstantInt::get(RelativeAddressTy, lowBits));
}

llvm::Constant *recordFields[] = {relativeAddr};
auto record = llvm::ConstantStruct::get(TypeMetadataRecordTy, recordFields);
return record;
};

if (IRGen.Opts.EmitDeadStrippableSymbols) {
for (auto type : RuntimeResolvableTypes) {
auto ref = getTypeEntityReference(type);
auto name =
"\x01l_type_metadata_" + std::string(ref.getValue()->getName());
auto var = new llvm::GlobalVariable(
Module, TypeMetadataRecordTy, /*isConstant*/ true,
llvm::GlobalValue::PrivateLinkage, /*initializer*/ nullptr, name);

auto record = generateRecord(ref, var, {0});
var->setInitializer(record);
var->setSection(sectionName);
var->setAlignment(llvm::MaybeAlign(4));

disableAddressSanitizer(*this, var);

addCompilerUsedGlobal(var);
}

return nullptr;
}

// Define the global variable for the conformance list.
// We have to do this before defining the initializer since the entries will
// contain offsets relative to themselves.
Expand All @@ -3502,20 +3596,8 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
SmallVector<llvm::Constant *, 8> elts;
for (auto type : RuntimeResolvableTypes) {
auto ref = getTypeEntityReference(type);

// Form the relative address, with the type reference kind in the low bits.
unsigned arrayIdx = elts.size();
llvm::Constant *relativeAddr =
emitDirectRelativeReference(ref.getValue(), var, { arrayIdx, 0 });
unsigned lowBits = static_cast<unsigned>(ref.getKind());
if (lowBits != 0) {
relativeAddr = llvm::ConstantExpr::getAdd(relativeAddr,
llvm::ConstantInt::get(RelativeAddressTy, lowBits));
}

llvm::Constant *recordFields[] = { relativeAddr };
auto record = llvm::ConstantStruct::get(TypeMetadataRecordTy,
recordFields);
auto record = generateRecord(ref, var, { arrayIdx, 0 });
elts.push_back(record);
}

Expand Down Expand Up @@ -3890,7 +3972,9 @@ llvm::GlobalValue *IRGenModule::defineAlias(LinkEntity entity,
ApplyIRLinkage({link.getLinkage(), link.getVisibility(), link.getDLLStorage()})
.to(alias);

if (link.isUsed()) {
// Everything externally visible is considered used in Swift.
// That mostly means we need to be good at not marking things external.
if (link.isUsed() && !IRGen.Opts.EmitDeadStrippableSymbols) {
addUsedGlobal(alias);
}

Expand Down
71 changes: 71 additions & 0 deletions test/IRGen/dead_strip.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Checks that applying -dead_strip linker flag actually removes unused classes and functions from static libraries.

// REQUIRES: OS=macosx

// RUN: echo "-target x86_64-apple-macos11.0 -swift-version 5 -parse-as-library -working-directory %t" >> %t-commonflags
// RUN: echo "-Xfrontend -disable-objc-interop" >> %t-commonflags
// RUN: echo "-Xfrontend -disable-reflection-metadata -Xfrontend -disable-reflection-names" >> %t-commonflags

// RUN: %empty-directory(%t)
// RUN: %target-swiftc_driver @%t-commonflags -emit-library -static -DMODULE -emit-module %s -module-name MyModule
// RUN: %target-swiftc_driver @%t-commonflags %s -I%t -L%t -lMyModule -Xlinker -dead_strip -module-name main -o %t/main
// RUN: %llvm-nm --defined-only %t/main | %FileCheck %s

// RUN: %empty-directory(%t)
// RUN: %target-swiftc_driver @%t-commonflags -emit-library -static -DMODULE -emit-module %s -module-name MyModule -Xfrontend -emit-dead-strippable-symbols
// RUN: %target-swiftc_driver @%t-commonflags %s -I%t -L%t -lMyModule -Xlinker -dead_strip -module-name main -o %t/main
// RUN: %llvm-nm --defined-only %t/main | %FileCheck %s -check-prefix CHECK-DEADSTRIPPABLE

#if MODULE

public protocol MyProtocol {
}
public class MyClass: MyProtocol {
}

public func fooFromModule() { print("fooFromModule") }
public func barFromModule() { print("barFromModule") }

#else

import MyModule

@_cdecl("main")
func main() {
print("Hello!")
fooFromModule()
}

#endif

// CHECK: _$s8MyModule07barFromB0yyF
// CHECK: _$s8MyModule07fooFromB0yyF
// CHECK: _$s8MyModule0A5ClassCAA0A8ProtocolAAMc
// CHECK: _$s8MyModule0A5ClassCAA0A8ProtocolAAWP
// CHECK: _$s8MyModule0A5ClassCACycfC
// CHECK: _$s8MyModule0A5ClassCACycfCTq
// CHECK: _$s8MyModule0A5ClassCACycfc
// CHECK: _$s8MyModule0A5ClassCMa
// CHECK: _$s8MyModule0A5ClassCMf
// CHECK: _$s8MyModule0A5ClassCMn
// CHECK: _$s8MyModule0A5ClassCN
// CHECK: _$s8MyModule0A5ClassCfD
// CHECK: _$s8MyModule0A5ClassCfd
// CHECK: _$s8MyModule0A8ProtocolMp
// CHECK: _$s8MyModuleMXM

// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule07barFromB0yyF
// CHECK-DEADSTRIPPABLE: _$s8MyModule07fooFromB0yyF
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCAA0A8ProtocolAAMc
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCAA0A8ProtocolAAWP
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCACycfC
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCACycfCTq
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCACycfc
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCMa
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCMf
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCMn
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCN
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCfD
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A5ClassCfd
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModule0A8ProtocolMp
// CHECK-DEADSTRIPPABLE-NOT: _$s8MyModuleMXM
Loading