Skip to content

Commit ded38db

Browse files
committed
rustc: Enable LTO and multiple codegen units
This commit is a refactoring of the LTO backend in Rust to support compilations with multiple codegen units. The immediate result of this PR is to remove the artificial error emitted by rustc about `-C lto -C codegen-units-8`, but longer term this is intended to lay the groundwork for LTO with incremental compilation and ultimately be the underpinning of ThinLTO support. The problem here that needed solving is that when rustc is producing multiple codegen units in one compilation LTO needs to merge them all together. Previously only upstream dependencies were merged and it was inherently relied on that there was only one local codegen unit. Supporting this involved refactoring the optimization backend architecture for rustc, namely splitting the `optimize_and_codegen` function into `optimize` and `codegen`. After an LLVM module has been optimized it may be blocked and queued up for LTO, and only after LTO are modules code generated. Non-LTO compilations should look the same as they do today backend-wise, we'll spin up a thread for each codegen unit and optimize/codegen in that thread. LTO compilations will, however, send the LLVM module back to the coordinator thread once optimizations have finished. When all LLVM modules have finished optimizing the coordinator will invoke the LTO backend, producing a further list of LLVM modules. Currently this is always a list of one LLVM module. The coordinator then spawns further work to run LTO and code generation passes over each module. In the course of this refactoring a number of other pieces were refactored: * Management of the bytecode encoding in rlibs was centralized into one module instead of being scattered across LTO and linking. * Some internal refactorings on the link stage of the compiler was done to work directly from `CompiledModule` structures instead of lists of paths. * The trans time-graph output was tweaked a little to include a name on each bar and inflate the size of the bars a little
1 parent d514263 commit ded38db

File tree

13 files changed

+803
-430
lines changed

13 files changed

+803
-430
lines changed

src/librustc/session/config.rs

-18
Original file line numberDiff line numberDiff line change
@@ -1552,24 +1552,6 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
15521552
early_error(error_format, "Value for codegen units must be a positive nonzero integer");
15531553
}
15541554

1555-
// It's possible that we have `codegen_units > 1` but only one item in
1556-
// `trans.modules`. We could theoretically proceed and do LTO in that
1557-
// case, but it would be confusing to have the validity of
1558-
// `-Z lto -C codegen-units=2` depend on details of the crate being
1559-
// compiled, so we complain regardless.
1560-
if cg.lto {
1561-
if let Some(n) = codegen_units {
1562-
if n > 1 {
1563-
// This case is impossible to handle because LTO expects to be able
1564-
// to combine the entire crate and all its dependencies into a
1565-
// single compilation unit, but each codegen unit is in a separate
1566-
// LLVM context, so they can't easily be combined.
1567-
early_error(error_format, "can't perform LTO when using multiple codegen units");
1568-
}
1569-
}
1570-
codegen_units = Some(1);
1571-
}
1572-
15731555
if cg.lto && debugging_opts.incremental.is_some() {
15741556
early_error(error_format, "can't perform LTO when compiling incrementally");
15751557
}

src/librustc_llvm/ffi.rs

+7
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ pub mod debuginfo {
478478
}
479479
}
480480

481+
pub enum ModuleBuffer {}
481482

482483
// Link to our native llvm bindings (things that we need to use the C++ api
483484
// for) and because llvm is written in C++ we need to link against libstdc++
@@ -1609,6 +1610,7 @@ extern "C" {
16091610
pub fn LLVMRustSetNormalizedTarget(M: ModuleRef, triple: *const c_char);
16101611
pub fn LLVMRustAddAlwaysInlinePass(P: PassManagerBuilderRef, AddLifetimes: bool);
16111612
pub fn LLVMRustLinkInExternalBitcode(M: ModuleRef, bc: *const c_char, len: size_t) -> bool;
1613+
pub fn LLVMRustLinkInParsedExternalBitcode(M: ModuleRef, M: ModuleRef) -> bool;
16121614
pub fn LLVMRustRunRestrictionPass(M: ModuleRef, syms: *const *const c_char, len: size_t);
16131615
pub fn LLVMRustMarkAllFunctionsNounwind(M: ModuleRef);
16141616

@@ -1678,4 +1680,9 @@ extern "C" {
16781680
pub fn LLVMRustSetComdat(M: ModuleRef, V: ValueRef, Name: *const c_char);
16791681
pub fn LLVMRustUnsetComdat(V: ValueRef);
16801682
pub fn LLVMRustSetModulePIELevel(M: ModuleRef);
1683+
pub fn LLVMRustModuleBufferCreate(M: ModuleRef) -> *mut ModuleBuffer;
1684+
pub fn LLVMRustModuleBufferPtr(p: *const ModuleBuffer) -> *const u8;
1685+
pub fn LLVMRustModuleBufferLen(p: *const ModuleBuffer) -> usize;
1686+
pub fn LLVMRustModuleBufferFree(p: *mut ModuleBuffer);
1687+
pub fn LLVMRustModuleCost(M: ModuleRef) -> u64;
16811688
}

src/librustc_trans/back/archive.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::path::{Path, PathBuf};
1717
use std::ptr;
1818
use std::str;
1919

20+
use back::bytecode::RLIB_BYTECODE_EXTENSION;
2021
use libc;
2122
use llvm::archive_ro::{ArchiveRO, Child};
2223
use llvm::{self, ArchiveKind};
@@ -154,12 +155,9 @@ impl<'a> ArchiveBuilder<'a> {
154155
// might be also an extra name suffix
155156
let obj_start = format!("{}", name);
156157

157-
// Ignoring all bytecode files, no matter of
158-
// name
159-
let bc_ext = ".bytecode.deflate";
160-
161158
self.add_archive(rlib, move |fname: &str| {
162-
if fname.ends_with(bc_ext) || fname == METADATA_FILENAME {
159+
// Ignore bytecode/metadata files, no matter the name.
160+
if fname.ends_with(RLIB_BYTECODE_EXTENSION) || fname == METADATA_FILENAME {
163161
return true
164162
}
165163

src/librustc_trans/back/bytecode.rs

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright 2017 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+
//! Management of the encoding of LLVM bytecode into rlibs
12+
//!
13+
//! This module contains the management of encoding LLVM bytecode into rlibs,
14+
//! primarily for the usage in LTO situations. Currently the compiler will
15+
//! unconditionally encode LLVM-IR into rlibs regardless of what's happening
16+
//! elsewhere, so we currently compress the bytecode via deflate to avoid taking
17+
//! up too much space on disk.
18+
//!
19+
//! After compressing the bytecode we then have the rest of the format to
20+
//! basically deal with various bugs in various archive implementations. The
21+
//! format currently is:
22+
//!
23+
//! RLIB LLVM-BYTECODE OBJECT LAYOUT
24+
//! Version 2
25+
//! Bytes Data
26+
//! 0..10 "RUST_OBJECT" encoded in ASCII
27+
//! 11..14 format version as little-endian u32
28+
//! 15..19 the length of the module identifier string
29+
//! 20..n the module identifier string
30+
//! n..n+8 size in bytes of deflate compressed LLVM bitcode as
31+
//! little-endian u64
32+
//! n+9.. compressed LLVM bitcode
33+
//! ? maybe a byte to make this whole thing even length
34+
35+
use std::io::{Read, Write};
36+
use std::ptr;
37+
use std::str;
38+
39+
use flate2::Compression;
40+
use flate2::read::DeflateDecoder;
41+
use flate2::write::DeflateEncoder;
42+
43+
// This is the "magic number" expected at the beginning of a LLVM bytecode
44+
// object in an rlib.
45+
pub const RLIB_BYTECODE_OBJECT_MAGIC: &'static [u8] = b"RUST_OBJECT";
46+
47+
// The version number this compiler will write to bytecode objects in rlibs
48+
pub const RLIB_BYTECODE_OBJECT_VERSION: u8 = 2;
49+
50+
pub const RLIB_BYTECODE_EXTENSION: &str = "bytecode.encoded";
51+
52+
pub fn encode(identifier: &str, bytecode: &[u8]) -> Vec<u8> {
53+
let mut encoded = Vec::new();
54+
55+
// Start off with the magic string
56+
encoded.extend_from_slice(RLIB_BYTECODE_OBJECT_MAGIC);
57+
58+
// Next up is the version
59+
encoded.extend_from_slice(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]);
60+
61+
// Next is the LLVM module identifier length + contents
62+
let identifier_len = identifier.len();
63+
encoded.extend_from_slice(&[
64+
(identifier_len >> 0) as u8,
65+
(identifier_len >> 8) as u8,
66+
(identifier_len >> 16) as u8,
67+
(identifier_len >> 24) as u8,
68+
]);
69+
encoded.extend_from_slice(identifier.as_bytes());
70+
71+
// Next is the LLVM module deflate compressed, prefixed with its length. We
72+
// don't know its length yet, so fill in 0s
73+
let deflated_size_pos = encoded.len();
74+
encoded.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0]);
75+
76+
let before = encoded.len();
77+
DeflateEncoder::new(&mut encoded, Compression::Fast)
78+
.write_all(bytecode)
79+
.unwrap();
80+
let after = encoded.len();
81+
82+
// Fill in the length we reserved space for before
83+
let bytecode_len = (after - before) as u64;
84+
encoded[deflated_size_pos + 0] = (bytecode_len >> 0) as u8;
85+
encoded[deflated_size_pos + 1] = (bytecode_len >> 8) as u8;
86+
encoded[deflated_size_pos + 2] = (bytecode_len >> 16) as u8;
87+
encoded[deflated_size_pos + 3] = (bytecode_len >> 24) as u8;
88+
encoded[deflated_size_pos + 4] = (bytecode_len >> 32) as u8;
89+
encoded[deflated_size_pos + 5] = (bytecode_len >> 40) as u8;
90+
encoded[deflated_size_pos + 6] = (bytecode_len >> 48) as u8;
91+
encoded[deflated_size_pos + 7] = (bytecode_len >> 56) as u8;
92+
93+
// If the number of bytes written to the object so far is odd, add a
94+
// padding byte to make it even. This works around a crash bug in LLDB
95+
// (see issue #15950)
96+
if encoded.len() % 2 == 1 {
97+
encoded.push(0);
98+
}
99+
100+
return encoded
101+
}
102+
103+
pub struct DecodedBytecode<'a> {
104+
identifier: &'a str,
105+
encoded_bytecode: &'a [u8],
106+
}
107+
108+
impl<'a> DecodedBytecode<'a> {
109+
pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, String> {
110+
if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) {
111+
return Err(format!("magic bytecode prefix not found"))
112+
}
113+
let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..];
114+
if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) {
115+
return Err(format!("wrong version prefix found in bytecode"))
116+
}
117+
let data = &data[4..];
118+
if data.len() < 4 {
119+
return Err(format!("bytecode corrupted"))
120+
}
121+
let identifier_len = unsafe {
122+
u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize
123+
};
124+
let data = &data[4..];
125+
if data.len() < identifier_len {
126+
return Err(format!("bytecode corrupted"))
127+
}
128+
let identifier = match str::from_utf8(&data[..identifier_len]) {
129+
Ok(s) => s,
130+
Err(_) => return Err(format!("bytecode corrupted"))
131+
};
132+
let data = &data[identifier_len..];
133+
if data.len() < 8 {
134+
return Err(format!("bytecode corrupted"))
135+
}
136+
let bytecode_len = unsafe {
137+
u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize
138+
};
139+
let data = &data[8..];
140+
if data.len() < bytecode_len {
141+
return Err(format!("bytecode corrupted"))
142+
}
143+
let encoded_bytecode = &data[..bytecode_len];
144+
145+
Ok(DecodedBytecode {
146+
identifier,
147+
encoded_bytecode,
148+
})
149+
}
150+
151+
pub fn bytecode(&self) -> Vec<u8> {
152+
let mut data = Vec::new();
153+
DeflateDecoder::new(self.encoded_bytecode).read_to_end(&mut data).unwrap();
154+
return data
155+
}
156+
157+
pub fn identifier(&self) -> &'a str {
158+
self.identifier
159+
}
160+
}

0 commit comments

Comments
 (0)