Skip to content

Commit b14f2b9

Browse files
authored
Merge pull request #38264 from jslegendre/driver-lto-library
[LTO] Driver support for -lto_library flag
2 parents b2ca238 + 1a38168 commit b14f2b9

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ 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, NoInteractiveOption]>,
555+
HelpText<"Perform LTO with <lto-library>">, MetaVarName<"<lto-library>">;
552556

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

lib/Driver/DarwinToolChains.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,29 @@ 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::remove_filename(LibLTOPath); // Remove '/bin'
334+
llvm::sys::path::append(LibLTOPath, "lib");
335+
llvm::sys::path::append(LibLTOPath, "libLTO.dylib");
336+
if (llvm::sys::fs::exists(LibLTOPath)) {
337+
Arguments.push_back("-lto_library");
338+
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
339+
} else {
340+
// Use libLTO.dylib from the default toolchain if a relative one does not exist.
341+
llvm::SmallString<128> LibLTOPath;
342+
if (findXcodeClangLibPath("libLTO.dylib", LibLTOPath)) {
343+
Arguments.push_back("-lto_library");
344+
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
345+
}
346+
}
328347
}
329348
}
330349

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)