Skip to content

Commit 43e8ac2

Browse files
committed
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 #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 #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 #48025
1 parent 16362c7 commit 43e8ac2

File tree

5 files changed

+114
-49
lines changed

5 files changed

+114
-49
lines changed

src/librustc_llvm/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ fn main() {
155155
cfg.file("../rustllvm/PassWrapper.cpp")
156156
.file("../rustllvm/RustWrapper.cpp")
157157
.file("../rustllvm/ArchiveWrapper.cpp")
158+
.file("../rustllvm/Linker.cpp")
158159
.cpp(true)
159160
.cpp_link_stdlib(None) // we handle this below
160161
.compile("rustllvm");

src/librustc_llvm/ffi.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ pub type RustArchiveMemberRef = *mut RustArchiveMember_opaque;
444444
#[allow(missing_copy_implementations)]
445445
pub enum OperandBundleDef_opaque {}
446446
pub type OperandBundleDefRef = *mut OperandBundleDef_opaque;
447+
#[allow(missing_copy_implementations)]
448+
pub enum Linker_opaque {}
449+
pub type LinkerRef = *mut Linker_opaque;
447450

448451
pub type DiagnosticHandler = unsafe extern "C" fn(DiagnosticInfoRef, *mut c_void);
449452
pub type InlineAsmDiagHandler = unsafe extern "C" fn(SMDiagnosticRef, *const c_void, c_uint);
@@ -1608,7 +1611,6 @@ extern "C" {
16081611
pub fn LLVMRustPrintPasses();
16091612
pub fn LLVMRustSetNormalizedTarget(M: ModuleRef, triple: *const c_char);
16101613
pub fn LLVMRustAddAlwaysInlinePass(P: PassManagerBuilderRef, AddLifetimes: bool);
1611-
pub fn LLVMRustLinkInExternalBitcode(M: ModuleRef, bc: *const c_char, len: size_t) -> bool;
16121614
pub fn LLVMRustRunRestrictionPass(M: ModuleRef, syms: *const *const c_char, len: size_t);
16131615
pub fn LLVMRustMarkAllFunctionsNounwind(M: ModuleRef);
16141616

@@ -1724,4 +1726,10 @@ extern "C" {
17241726
CU2: *mut *mut c_void);
17251727
pub fn LLVMRustThinLTOPatchDICompileUnit(M: ModuleRef, CU: *mut c_void);
17261728
pub fn LLVMRustThinLTORemoveAvailableExternally(M: ModuleRef);
1729+
1730+
pub fn LLVMRustLinkerNew(M: ModuleRef) -> LinkerRef;
1731+
pub fn LLVMRustLinkerAdd(linker: LinkerRef,
1732+
bytecode: *const c_char,
1733+
bytecode_len: usize) -> bool;
1734+
pub fn LLVMRustLinkerFree(linker: LinkerRef);
17271735
}

src/librustc_trans/back/lto.rs

+32-8
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,20 @@ fn fat_lto(cgcx: &CodegenContext,
247247
// know much about the memory management here so we err on the side of being
248248
// save and persist everything with the original module.
249249
let mut serialized_bitcode = Vec::new();
250+
let mut linker = Linker::new(llmod);
250251
for (bc_decoded, name) in serialized_modules {
251252
info!("linking {:?}", name);
252-
time(cgcx.time_passes, &format!("ll link {:?}", name), || unsafe {
253+
time(cgcx.time_passes, &format!("ll link {:?}", name), || {
253254
let data = bc_decoded.data();
254-
if llvm::LLVMRustLinkInExternalBitcode(llmod,
255-
data.as_ptr() as *const libc::c_char,
256-
data.len() as libc::size_t) {
257-
Ok(())
258-
} else {
255+
linker.add(&data).map_err(|()| {
259256
let msg = format!("failed to load bc of {:?}", name);
260-
Err(write::llvm_err(&diag_handler, msg))
261-
}
257+
write::llvm_err(&diag_handler, msg)
258+
})
262259
})?;
263260
timeline.record(&format!("link {:?}", name));
264261
serialized_bitcode.push(bc_decoded);
265262
}
263+
drop(linker);
266264
cgcx.save_temp_bitcode(&module, "lto.input");
267265

268266
// Internalize everything that *isn't* in our whitelist to help strip out
@@ -289,6 +287,32 @@ fn fat_lto(cgcx: &CodegenContext,
289287
}])
290288
}
291289

290+
struct Linker(llvm::LinkerRef);
291+
292+
impl Linker {
293+
fn new(llmod: ModuleRef) -> Linker {
294+
unsafe { Linker(llvm::LLVMRustLinkerNew(llmod)) }
295+
}
296+
297+
fn add(&mut self, bytecode: &[u8]) -> Result<(), ()> {
298+
unsafe {
299+
if llvm::LLVMRustLinkerAdd(self.0,
300+
bytecode.as_ptr() as *const libc::c_char,
301+
bytecode.len()) {
302+
Ok(())
303+
} else {
304+
Err(())
305+
}
306+
}
307+
}
308+
}
309+
310+
impl Drop for Linker {
311+
fn drop(&mut self) {
312+
unsafe { llvm::LLVMRustLinkerFree(self.0); }
313+
}
314+
}
315+
292316
/// Prepare "thin" LTO to get run on these modules.
293317
///
294318
/// The general structure of ThinLTO is quite different from the structure of

src/rustllvm/Linker.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#include "llvm/Linker/Linker.h"
12+
13+
#include "rustllvm.h"
14+
15+
using namespace llvm;
16+
17+
struct RustLinker {
18+
Linker L;
19+
LLVMContext &Ctx;
20+
21+
RustLinker(Module &M) :
22+
L(M),
23+
Ctx(M.getContext())
24+
{}
25+
};
26+
27+
extern "C" RustLinker*
28+
LLVMRustLinkerNew(LLVMModuleRef DstRef) {
29+
Module *Dst = unwrap(DstRef);
30+
31+
auto Ret = llvm::make_unique<RustLinker>(*Dst);
32+
return Ret.release();
33+
}
34+
35+
extern "C" void
36+
LLVMRustLinkerFree(RustLinker *L) {
37+
delete L;
38+
}
39+
40+
extern "C" bool
41+
LLVMRustLinkerAdd(RustLinker *L, char *BC, size_t Len) {
42+
std::unique_ptr<MemoryBuffer> Buf =
43+
MemoryBuffer::getMemBufferCopy(StringRef(BC, Len));
44+
45+
#if LLVM_VERSION_GE(4, 0)
46+
Expected<std::unique_ptr<Module>> SrcOrError =
47+
llvm::getLazyBitcodeModule(Buf->getMemBufferRef(), L->Ctx);
48+
if (!SrcOrError) {
49+
LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
50+
return false;
51+
}
52+
53+
auto Src = std::move(*SrcOrError);
54+
#else
55+
ErrorOr<std::unique_ptr<Module>> Src =
56+
llvm::getLazyBitcodeModule(std::move(Buf), L->Ctx);
57+
if (!Src) {
58+
LLVMRustSetLastError(Src.getError().message().c_str());
59+
return false;
60+
}
61+
#endif
62+
63+
#if LLVM_VERSION_GE(4, 0)
64+
if (L->L.linkInModule(std::move(Src))) {
65+
#else
66+
if (L->L.linkInModule(std::move(Src.get()))) {
67+
#endif
68+
LLVMRustSetLastError("");
69+
return false;
70+
}
71+
return true;
72+
}

src/rustllvm/RustWrapper.cpp

-40
Original file line numberDiff line numberDiff line change
@@ -916,46 +916,6 @@ extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
916916
}
917917
}
918918

919-
extern "C" bool LLVMRustLinkInExternalBitcode(LLVMModuleRef DstRef, char *BC,
920-
size_t Len) {
921-
Module *Dst = unwrap(DstRef);
922-
923-
std::unique_ptr<MemoryBuffer> Buf =
924-
MemoryBuffer::getMemBufferCopy(StringRef(BC, Len));
925-
926-
#if LLVM_VERSION_GE(4, 0)
927-
Expected<std::unique_ptr<Module>> SrcOrError =
928-
llvm::getLazyBitcodeModule(Buf->getMemBufferRef(), Dst->getContext());
929-
if (!SrcOrError) {
930-
LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
931-
return false;
932-
}
933-
934-
auto Src = std::move(*SrcOrError);
935-
#else
936-
ErrorOr<std::unique_ptr<Module>> Src =
937-
llvm::getLazyBitcodeModule(std::move(Buf), Dst->getContext());
938-
if (!Src) {
939-
LLVMRustSetLastError(Src.getError().message().c_str());
940-
return false;
941-
}
942-
#endif
943-
944-
std::string Err;
945-
946-
raw_string_ostream Stream(Err);
947-
DiagnosticPrinterRawOStream DP(Stream);
948-
#if LLVM_VERSION_GE(4, 0)
949-
if (Linker::linkModules(*Dst, std::move(Src))) {
950-
#else
951-
if (Linker::linkModules(*Dst, std::move(Src.get()))) {
952-
#endif
953-
LLVMRustSetLastError(Err.c_str());
954-
return false;
955-
}
956-
return true;
957-
}
958-
959919
// Note that the two following functions look quite similar to the
960920
// LLVMGetSectionName function. Sadly, it appears that this function only
961921
// returns a char* pointer, which isn't guaranteed to be null-terminated. The

0 commit comments

Comments
 (0)