forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rustc: Persist LLVM's
Linker
in Fat LTO
This commit updates our Fat LTO logic to tweak our custom wrapper around LLVM's "link modules" functionality. Previously whenever the `LLVMRustLinkInExternalBitcode` function was called it would call LLVM's `Linker::linkModules` wrapper. Internally this would crate an instance of a `Linker` which internally creates an instance of an `IRMover`. Unfortunately for us the creation of `IRMover` is somewhat O(n) with the input module. This means that every time we linked a module it was O(n) with respect to the entire module we had built up! Now the modules we build up during LTO are quite large, so this quickly started creating an O(n^2) problem for us! Discovered in rust-lang#48025 it turns out this has always been a problem and we just haven't noticed it. It became particularly worse recently though due to most libraries having 16x more object files than they previously did (1 -> 16). This commit fixes this performance issue by preserving the `Linker` instance across all links into the main LLVM module. This means we only create one `IRMover` and allows LTO to progress much speedier. From the `cargo-cache` project in rust-lang#48025 a **full build** locally when from 5m15s to 2m24s. Looking at the timing logs each object file was linked in in single-digit millisecond rather than hundreds, clearly being a nice improvement! Closes rust-lang#48025
- Loading branch information
1 parent
16362c7
commit 43e8ac2
Showing
5 changed files
with
114 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#include "llvm/Linker/Linker.h" | ||
|
||
#include "rustllvm.h" | ||
|
||
using namespace llvm; | ||
|
||
struct RustLinker { | ||
Linker L; | ||
LLVMContext &Ctx; | ||
|
||
RustLinker(Module &M) : | ||
L(M), | ||
Ctx(M.getContext()) | ||
{} | ||
}; | ||
|
||
extern "C" RustLinker* | ||
LLVMRustLinkerNew(LLVMModuleRef DstRef) { | ||
Module *Dst = unwrap(DstRef); | ||
|
||
auto Ret = llvm::make_unique<RustLinker>(*Dst); | ||
return Ret.release(); | ||
} | ||
|
||
extern "C" void | ||
LLVMRustLinkerFree(RustLinker *L) { | ||
delete L; | ||
} | ||
|
||
extern "C" bool | ||
LLVMRustLinkerAdd(RustLinker *L, char *BC, size_t Len) { | ||
std::unique_ptr<MemoryBuffer> Buf = | ||
MemoryBuffer::getMemBufferCopy(StringRef(BC, Len)); | ||
|
||
#if LLVM_VERSION_GE(4, 0) | ||
Expected<std::unique_ptr<Module>> SrcOrError = | ||
llvm::getLazyBitcodeModule(Buf->getMemBufferRef(), L->Ctx); | ||
if (!SrcOrError) { | ||
LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str()); | ||
return false; | ||
} | ||
|
||
auto Src = std::move(*SrcOrError); | ||
#else | ||
ErrorOr<std::unique_ptr<Module>> Src = | ||
llvm::getLazyBitcodeModule(std::move(Buf), L->Ctx); | ||
if (!Src) { | ||
LLVMRustSetLastError(Src.getError().message().c_str()); | ||
return false; | ||
} | ||
#endif | ||
|
||
#if LLVM_VERSION_GE(4, 0) | ||
if (L->L.linkInModule(std::move(Src))) { | ||
#else | ||
if (L->L.linkInModule(std::move(Src.get()))) { | ||
#endif | ||
LLVMRustSetLastError(""); | ||
return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters