Skip to content

Commit 35735e7

Browse files
committed
[LTO] Driver support for -lto_library flag
This commit adds support for the -lto_library flag, allowing users to specify a custom LTO library on Darwin. This also fixes an issue where the default LTO library is used even if Driver is run from inside an alternate toolchain.
1 parent 910d918 commit 35735e7

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

include/swift/Driver/Driver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class OutputInfo {
109109

110110
LTOKind LTOVariant = LTOKind::None;
111111

112+
std::string LibLTOPath;
113+
112114
/// Describes if and how the output of compile actions should be
113115
/// linked together.
114116
LinkKind LinkAction = LinkKind::None;

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,11 @@ def disable_bridging_pch : Flag<["-"], "disable-bridging-pch">,
549549
def lto : Joined<["-"], "lto=">,
550550
Flags<[FrontendOption, NoInteractiveOption]>,
551551
HelpText<"Specify the LTO type to either 'llvm-thin' or 'llvm-full'">;
552+
553+
def lto_library : Separate<["-"], "lto_library">,
554+
Flags<[FrontendOption, ArgumentIsPath, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption,
555+
SwiftAPIDigesterOption]>,
556+
HelpText<"Perform LTO with <lto_library>">, MetaVarName<"<lto_library>">;
552557

553558
def access_notes_path : Separate<["-"], "access-notes-path">,
554559
Flags<[FrontendOption, ArgumentIsPath]>,

lib/Driver/DarwinToolChains.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,28 @@ toolchains::Darwin::addArgsToLinkARCLite(ArgStringList &Arguments,
321321

322322
void toolchains::Darwin::addLTOLibArgs(ArgStringList &Arguments,
323323
const JobContext &context) const {
324-
llvm::SmallString<128> LTOLibPath;
325-
if (findXcodeClangLibPath("libLTO.dylib", LTOLibPath)) {
324+
if (!context.OI.LibLTOPath.empty()) {
325+
// Check for user-specified LTO library.
326326
Arguments.push_back("-lto_library");
327-
Arguments.push_back(context.Args.MakeArgString(LTOLibPath));
327+
Arguments.push_back(context.Args.MakeArgString(context.OI.LibLTOPath));
328+
} else {
329+
// Check for relative libLTO.dylib. This would be the expected behavior in an
330+
// Xcode toolchain.
331+
StringRef P = llvm::sys::path::parent_path(getDriver().getSwiftProgramPath());
332+
llvm::SmallString<128> LibLTOPath(P);
333+
llvm::sys::path::append(LibLTOPath, "lib");
334+
llvm::sys::path::append(LibLTOPath, "libLTO.dylib");
335+
if (llvm::sys::fs::exists(LibLTOPath)) {
336+
Arguments.push_back("-lto_library");
337+
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
338+
} else {
339+
// Use libLTO.dylib from the default toolchain if a relative one does not exist.
340+
llvm::SmallString<128> LibLTOPath;
341+
if (findXcodeClangLibPath("libLTO.dylib", LibLTOPath)) {
342+
Arguments.push_back("-lto_library");
343+
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
344+
}
345+
}
328346
}
329347
}
330348

lib/Driver/Driver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,12 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
14481448
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
14491449
A->getAsString(Args), A->getValue());
14501450
}
1451+
1452+
if (const Arg *A = Args.getLastArg(options::OPT_lto_library)) {
1453+
OI.LibLTOPath = A->getValue();
1454+
} else {
1455+
OI.LibLTOPath = "";
1456+
}
14511457

14521458
auto CompilerOutputType = OI.LTOVariant != OutputInfo::LTOKind::None
14531459
? file_types::TY_LLVM_BC

test/Driver/link-time-opt-darwin-ld-lib.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,18 @@
2424
// CHECK-SIMPLE-FULL-macosx: ld
2525
// CHECK-SIMPLE-FULL-macosx-DAG: -lto_library {{.+}}/lib/libLTO.dylib
2626
// CHECK-SIMPLE-FULL-macosx-DAG: [[OBJECTFILE]]
27+
28+
29+
// Check that driver does not see libLTO.dylib as an input
30+
31+
// RUN: %swiftc_driver -driver-print-jobs %S/../Inputs/empty.swift -lto=llvm-full -lto_library /foo/libLTO.dylib -target x86_64-apple-macosx10.9 | %FileCheck %s --check-prefix=CHECK-SIMPLE-LTO-LIB --check-prefix=CHECK-SIMPLE-LTO-LIB-macosx
32+
33+
// CHECK-SIMPLE-LTO-LIB: swift
34+
// CHECK-SIMPLE-LTO-LIB-DAG: -emit-bc
35+
// CHECK-SIMPLE-LTO-LIB-DAG: -lto=llvm-full
36+
// CHECK-SIMPLE-LTO-LIB-NOT: -lto_library /foo/libLTO.dylib
37+
// CHECK-SIMPLE-LTO-LIB-DAG: -o [[OBJECTFILE:.*\.bc]]
38+
39+
// CHECK-SIMPLE-LTO-LIB-macosx: ld
40+
// CHECK-SIMPLE-LTO-LIB-macosx-DAG: -lto_library /foo/libLTO.dylib
41+
// CHECK-SIMPLE-LTO-LIB-macosx-DAG: [[OBJECTFILE]]

0 commit comments

Comments
 (0)