Skip to content

[IRGen] Be smarter about adding the FORCE_LINK symbols for overlays #15647

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
Mar 31, 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
22 changes: 20 additions & 2 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,23 @@ static bool replaceModuleFlagsEntry(llvm::LLVMContext &Ctx,
llvm_unreachable("Could not replace old linker options entry?");
}

/// Returns true if the object file generated by \p IGM will be the "first"
/// object file in the module. This lets us determine where to put a symbol
/// that must be unique.
static bool isFirstObjectFileInModule(IRGenModule &IGM) {
if (IGM.getSILModule().isWholeModule())
return IGM.IRGen.getPrimaryIGM() == &IGM;

const DeclContext *DC = IGM.getSILModule().getAssociatedContext();
if (!DC)
return false;

assert(!isa<ModuleDecl>(DC) && "that would be a whole module build");
assert(isa<FileUnit>(DC) && "compiling something smaller than a file?");
ModuleDecl *containingModule = cast<FileUnit>(DC)->getParentModule();
return containingModule->getFiles().front() == DC;
}

void IRGenModule::emitAutolinkInfo() {
// Collect the linker options already in the module (from ClangCodeGen).
// FIXME: This constant should be vended by LLVM somewhere.
Expand Down Expand Up @@ -964,12 +981,13 @@ void IRGenModule::emitAutolinkInfo() {
addUsedGlobal(var);
}

if (!IRGen.Opts.ForceLoadSymbolName.empty()) {
if (!IRGen.Opts.ForceLoadSymbolName.empty() &&
isFirstObjectFileInModule(*this)) {
llvm::SmallString<64> buf;
encodeForceLoadSymbolName(buf, IRGen.Opts.ForceLoadSymbolName);
auto ForceImportThunk =
llvm::Function::Create(llvm::FunctionType::get(VoidTy, false),
llvm::GlobalValue::WeakODRLinkage, buf,
llvm::GlobalValue::ExternalLinkage, buf,
&Module);
if (useDllStorage())
ForceImportThunk
Expand Down
29 changes: 29 additions & 0 deletions test/IRGen/autolink-force-link.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %empty-directory(%t)

// RUN: %swift -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir %s %S/../Inputs/empty.swift | %FileCheck -check-prefix=CHECK-WMO %s

// CHECK-WMO: source_filename = "-"
// CHECK-WMO: define void @"_swift_FORCE_LOAD_$_TEST"()
// CHECK-WMO-NOT: source_filename


// RUN: %swift -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir -num-threads 1 %s %S/../Inputs/empty.swift | %FileCheck -check-prefix=CHECK-WMO-THREADED %s

// CHECK-WMO-THREADED: source_filename = "-"
// CHECK-WMO-THREADED: define void @"_swift_FORCE_LOAD_$_TEST"()
// CHECK-WMO-THREADED: source_filename = "-"
// CHECK-WMO-THREADED-NOT: _swift_FORCE_LOAD_$_TEST
// CHECK-WMO-THREADED-NOT: source_filename


// RUN: %swift -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir -primary-file %s %S/../Inputs/empty.swift | %FileCheck -check-prefix=CHECK-SINGLE-FILE-FIRST %s
// RUN: %swift -target x86_64-apple-macosx10.9 -parse-stdlib -autolink-force-load -module-name TEST -module-link-name TEST -emit-ir %S/../Inputs/empty.swift -primary-file %s | %FileCheck -check-prefix=CHECK-SINGLE-FILE-SECOND %s

// CHECK-SINGLE-FILE-FIRST: source_filename = "-"
// CHECK-SINGLE-FILE-FIRST: define void @"_swift_FORCE_LOAD_$_TEST"()
// CHECK-SINGLE-FILE-FIRST-NOT: source_filename

// CHECK-SINGLE-FILE-SECOND: source_filename = "-"
// CHECK-SINGLE-FILE-SECOND-NOT: _swift_FORCE_LOAD_$_TEST
// CHECK-SINGLE-FILE-SECOND-NOT: source_filename

4 changes: 2 additions & 2 deletions test/Serialization/autolinking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import someModule
// FRAMEWORK-DAG: !{{[0-9]+}} = !{!"-framework", !"someModule"}

// NO-FORCE-LOAD-NOT: FORCE_LOAD
// FORCE-LOAD: define weak_odr void @"_swift_FORCE_LOAD_$_module"() {
// FORCE-LOAD: define void @"_swift_FORCE_LOAD_$_module"() {
// FORCE-LOAD: ret void
// FORCE-LOAD: }
// FORCE-LOAD-HEX: define weak_odr void @"_swift_FORCE_LOAD_$306d6f64756c65"() {
// FORCE-LOAD-HEX: define void @"_swift_FORCE_LOAD_$306d6f64756c65"() {
// FORCE-LOAD-HEX: ret void
// FORCE-LOAD-HEX: }

Expand Down