Skip to content
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
2 changes: 2 additions & 0 deletions include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class OutputInfo {

LTOKind LTOVariant = LTOKind::None;

std::string LibLTOPath;

/// Describes if and how the output of compile actions should be
/// linked together.
LinkKind LinkAction = LinkKind::None;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ def disable_bridging_pch : Flag<["-"], "disable-bridging-pch">,
def lto : Joined<["-"], "lto=">,
Flags<[FrontendOption, NoInteractiveOption]>,
HelpText<"Specify the LTO type to either 'llvm-thin' or 'llvm-full'">;

def lto_library : Separate<["-"], "lto-library">,
Flags<[FrontendOption, ArgumentIsPath, NoInteractiveOption]>,
HelpText<"Perform LTO with <lto-library>">, MetaVarName<"<lto-library>">;

def access_notes_path : Separate<["-"], "access-notes-path">,
Flags<[FrontendOption, ArgumentIsPath]>,
Expand Down
25 changes: 22 additions & 3 deletions lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,29 @@ toolchains::Darwin::addArgsToLinkARCLite(ArgStringList &Arguments,

void toolchains::Darwin::addLTOLibArgs(ArgStringList &Arguments,
const JobContext &context) const {
llvm::SmallString<128> LTOLibPath;
if (findXcodeClangLibPath("libLTO.dylib", LTOLibPath)) {
if (!context.OI.LibLTOPath.empty()) {
// Check for user-specified LTO library.
Arguments.push_back("-lto_library");
Arguments.push_back(context.Args.MakeArgString(LTOLibPath));
Arguments.push_back(context.Args.MakeArgString(context.OI.LibLTOPath));
} else {
// Check for relative libLTO.dylib. This would be the expected behavior in an
// Xcode toolchain.
StringRef P = llvm::sys::path::parent_path(getDriver().getSwiftProgramPath());
Copy link
Contributor

Choose a reason for hiding this comment

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

I think P is going to be <toolchain root>/usr/bin/ here, so we'll end up checking for libLTO in <toolchain root>/usr/bin/lib instead of <toolchain root>/usr/lib. I can't think of a good way to write a lit test for this unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. Fixed in latest commit.

llvm::SmallString<128> LibLTOPath(P);
llvm::sys::path::remove_filename(LibLTOPath); // Remove '/bin'
llvm::sys::path::append(LibLTOPath, "lib");
llvm::sys::path::append(LibLTOPath, "libLTO.dylib");
if (llvm::sys::fs::exists(LibLTOPath)) {
Arguments.push_back("-lto_library");
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
} else {
// Use libLTO.dylib from the default toolchain if a relative one does not exist.
llvm::SmallString<128> LibLTOPath;
if (findXcodeClangLibPath("libLTO.dylib", LibLTOPath)) {
Arguments.push_back("-lto_library");
Arguments.push_back(context.Args.MakeArgString(LibLTOPath));
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,12 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
}

if (const Arg *A = Args.getLastArg(options::OPT_lto_library)) {
OI.LibLTOPath = A->getValue();
} else {
OI.LibLTOPath = "";
}

auto CompilerOutputType = OI.LTOVariant != OutputInfo::LTOKind::None
? file_types::TY_LLVM_BC
Expand Down
15 changes: 15 additions & 0 deletions test/Driver/link-time-opt-darwin-ld-lib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@
// CHECK-SIMPLE-FULL-macosx: ld
// CHECK-SIMPLE-FULL-macosx-DAG: -lto_library {{.+}}/lib/libLTO.dylib
// CHECK-SIMPLE-FULL-macosx-DAG: [[OBJECTFILE]]


// Check that driver does not see libLTO.dylib as an input

// 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

// CHECK-SIMPLE-LTO-LIB: swift
// CHECK-SIMPLE-LTO-LIB-DAG: -emit-bc
// CHECK-SIMPLE-LTO-LIB-DAG: -lto=llvm-full
// CHECK-SIMPLE-LTO-LIB-NOT: -lto-library /foo/libLTO.dylib
// CHECK-SIMPLE-LTO-LIB-DAG: -o [[OBJECTFILE:.*\.bc]]

// CHECK-SIMPLE-LTO-LIB-macosx: ld
// CHECK-SIMPLE-LTO-LIB-macosx-DAG: -lto_library /foo/libLTO.dylib
// CHECK-SIMPLE-LTO-LIB-macosx-DAG: [[OBJECTFILE]]