Skip to content

Commit ee73f80

Browse files
committed
Auto merge of #53673 - michaelwoerister:incr-thinlto-2000, r=alexcrichton
Enable ThinLTO with incremental compilation. This is an updated version of #52309. This PR allows `rustc` to use (local) ThinLTO and incremental compilation at the same time. In theory this should allow for getting compile-time improvements for small changes while keeping the runtime performance of the generated code roughly the same as when compiling non-incrementally. The difference to #52309 is that this version also caches the pre-LTO version of LLVM bitcode. This allows for another layer of caching: 1. if the module itself has changed, we have to re-codegen and re-optimize. 2. if the module itself has not changed, but a module it imported from during ThinLTO has, we don't need to re-codegen and don't need to re-run the first optimization phase. Only the second (i.e. ThinLTO-) optimization phase is re-run. 3. if neither the module itself nor any of its imports have changed then we can re-use the final, post-ThinLTO version of the module. (We might have to load its pre-ThinLTO version though so it's available for other modules to import from)
2 parents 591a17d + 21d05f6 commit ee73f80

File tree

17 files changed

+647
-297
lines changed

17 files changed

+647
-297
lines changed

src/Cargo.lock

+11
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,15 @@ dependencies = [
11981198
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
11991199
]
12001200

1201+
[[package]]
1202+
name = "memmap"
1203+
version = "0.6.2"
1204+
source = "registry+https://github.com/rust-lang/crates.io-index"
1205+
dependencies = [
1206+
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
1207+
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
1208+
]
1209+
12011210
[[package]]
12021211
name = "memoffset"
12031212
version = "0.2.1"
@@ -2029,6 +2038,7 @@ name = "rustc_codegen_llvm"
20292038
version = "0.0.0"
20302039
dependencies = [
20312040
"cc 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)",
2041+
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
20322042
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
20332043
"rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
20342044
"rustc_llvm 0.0.0",
@@ -3151,6 +3161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
31513161
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
31523162
"checksum mdbook 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "90b5a8d7e341ceee5db3882a06078d42661ddcfa2b3687319cc5da76ec4e782f"
31533163
"checksum memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a3b4142ab8738a78c51896f704f83c11df047ff1bda9a92a661aa6361552d93d"
3164+
"checksum memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2ffa2c986de11a9df78620c01eeaaf27d94d3ff02bf81bfcca953102dd0c6ff"
31543165
"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"
31553166
"checksum minifier 0.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9908ed7c62f990c21ab41fdca53a864a3ada0da69d8729c4de727b397e27bc11"
31563167
"checksum miniz-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "609ce024854aeb19a0ef7567d348aaa5a746b32fb72e336df7fcc16869d7e2b4"

src/librustc/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ pub struct WorkProduct {
878878
pub saved_files: Vec<(WorkProductFileKind, String)>,
879879
}
880880

881-
#[derive(Clone, Copy, Debug, RustcEncodable, RustcDecodable)]
881+
#[derive(Clone, Copy, Debug, RustcEncodable, RustcDecodable, PartialEq)]
882882
pub enum WorkProductFileKind {
883883
Object,
884884
Bytecode,

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum OptLevel {
6868
SizeMin, // -Oz
6969
}
7070

71-
#[derive(Clone, Copy, PartialEq, Hash)]
71+
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
7272
pub enum Lto {
7373
/// Don't do any LTO whatsoever
7474
No,

src/librustc/session/mod.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,6 @@ impl Session {
580580
return config::Lto::No;
581581
}
582582

583-
// Right now ThinLTO isn't compatible with incremental compilation.
584-
if self.opts.incremental.is_some() {
585-
return config::Lto::No;
586-
}
587-
588583
// Now we're in "defaults" territory. By default we enable ThinLTO for
589584
// optimized compiles (anything greater than O0).
590585
match self.opts.optimize {
@@ -1177,8 +1172,18 @@ pub fn build_session_(
11771172
// commandline argument, you can do so here.
11781173
fn validate_commandline_args_with_session_available(sess: &Session) {
11791174

1180-
if sess.lto() != Lto::No && sess.opts.incremental.is_some() {
1181-
sess.err("can't perform LTO when compiling incrementally");
1175+
if sess.opts.incremental.is_some() {
1176+
match sess.lto() {
1177+
Lto::Yes |
1178+
Lto::Thin |
1179+
Lto::Fat => {
1180+
sess.err("can't perform LTO when compiling incrementally");
1181+
}
1182+
Lto::ThinLocal |
1183+
Lto::No => {
1184+
// This is fine
1185+
}
1186+
}
11821187
}
11831188

11841189
// Since we don't know if code in an rlib will be linked to statically or

src/librustc/ty/query/config.rs

-6
Original file line numberDiff line numberDiff line change
@@ -716,12 +716,6 @@ impl<'tcx> QueryDescription<'tcx> for queries::codegen_unit<'tcx> {
716716
}
717717
}
718718

719-
impl<'tcx> QueryDescription<'tcx> for queries::compile_codegen_unit<'tcx> {
720-
fn describe(_tcx: TyCtxt, _: InternedString) -> String {
721-
"compile_codegen_unit".to_string()
722-
}
723-
}
724-
725719
impl<'tcx> QueryDescription<'tcx> for queries::output_filenames<'tcx> {
726720
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
727721
"output_filenames".to_string()

src/librustc/ty/query/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use middle::lib_features::LibFeatures;
2828
use middle::lang_items::{LanguageItems, LangItem};
2929
use middle::exported_symbols::{SymbolExportLevel, ExportedSymbol};
3030
use mir::interpret::ConstEvalResult;
31-
use mir::mono::{CodegenUnit, Stats};
31+
use mir::mono::CodegenUnit;
3232
use mir;
3333
use mir::interpret::GlobalId;
3434
use session::{CompileResult, CrateDisambiguator};
@@ -525,7 +525,6 @@ define_queries! { <'tcx>
525525
-> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>),
526526
[] fn is_codegened_item: IsCodegenedItem(DefId) -> bool,
527527
[] fn codegen_unit: CodegenUnit(InternedString) -> Arc<CodegenUnit<'tcx>>,
528-
[] fn compile_codegen_unit: CompileCodegenUnit(InternedString) -> Stats,
529528
},
530529

531530
Other {

src/librustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cc = "1.0.1"
1414
num_cpus = "1.0"
1515
rustc-demangle = "0.1.4"
1616
rustc_llvm = { path = "../librustc_llvm" }
17+
memmap = "0.6"
1718

1819
[features]
1920
# This is used to convince Cargo to separately cache builds of `rustc_codegen_llvm`

0 commit comments

Comments
 (0)