Skip to content

Commit 130ff8c

Browse files
committed
Auto merge of #115964 - bjorn3:cgu_reuse_tracker_global_state, r=cjgillot
Remove cgu_reuse_tracker from Session This removes a bit of global mutable state. It will now miss post-lto cgu reuse when ThinLTO determines that a cgu doesn't get changed, but there weren't any tests for this anyway and a test for it would be fragile to the exact implementation of ThinLTO in LLVM.
2 parents e20cb77 + f0b5820 commit 130ff8c

File tree

19 files changed

+274
-322
lines changed

19 files changed

+274
-322
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3650,6 +3650,7 @@ dependencies = [
36503650
"serde_json",
36513651
"smallvec",
36523652
"tempfile",
3653+
"thin-vec",
36533654
"thorin-dwp",
36543655
"tracing",
36553656
"windows",

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+37-61
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ use std::sync::Arc;
77
use std::thread::JoinHandle;
88

99
use cranelift_object::{ObjectBuilder, ObjectModule};
10+
use rustc_codegen_ssa::assert_module_sources::CguReuse;
1011
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
12+
use rustc_codegen_ssa::base::determine_cgu_reuse;
1113
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
1214
use rustc_data_structures::profiling::SelfProfilerRef;
1315
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1416
use rustc_metadata::EncodedMetadata;
1517
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
1618
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
17-
use rustc_session::cgu_reuse_tracker::CguReuse;
1819
use rustc_session::config::{DebugInfo, OutputFilenames, OutputType};
1920
use rustc_session::Session;
2021

@@ -374,43 +375,47 @@ pub(crate) fn run_aot(
374375
}
375376
}
376377

378+
// Calculate the CGU reuse
379+
let cgu_reuse = tcx.sess.time("find_cgu_reuse", || {
380+
cgus.iter().map(|cgu| determine_cgu_reuse(tcx, &cgu)).collect::<Vec<_>>()
381+
});
382+
383+
rustc_codegen_ssa::assert_module_sources::assert_module_sources(tcx, &|cgu_reuse_tracker| {
384+
for (i, cgu) in cgus.iter().enumerate() {
385+
let cgu_reuse = cgu_reuse[i];
386+
cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
387+
}
388+
});
389+
377390
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
378391

379392
let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len());
380393

381394
let modules = tcx.sess.time("codegen mono items", || {
382395
cgus.iter()
383-
.map(|cgu| {
384-
let cgu_reuse = if backend_config.disable_incr_cache {
385-
CguReuse::No
386-
} else {
387-
determine_cgu_reuse(tcx, cgu)
388-
};
389-
tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
390-
391-
match cgu_reuse {
392-
CguReuse::No => {
393-
let dep_node = cgu.codegen_dep_node(tcx);
394-
tcx.dep_graph
395-
.with_task(
396-
dep_node,
397-
tcx,
398-
(
399-
backend_config.clone(),
400-
global_asm_config.clone(),
401-
cgu.name(),
402-
concurrency_limiter.acquire(tcx.sess.diagnostic()),
403-
),
404-
module_codegen,
405-
Some(rustc_middle::dep_graph::hash_result),
406-
)
407-
.0
408-
}
409-
CguReuse::PreLto => unreachable!(),
410-
CguReuse::PostLto => {
411-
concurrency_limiter.job_already_done();
412-
OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
413-
}
396+
.enumerate()
397+
.map(|(i, cgu)| match cgu_reuse[i] {
398+
CguReuse::No => {
399+
let dep_node = cgu.codegen_dep_node(tcx);
400+
tcx.dep_graph
401+
.with_task(
402+
dep_node,
403+
tcx,
404+
(
405+
backend_config.clone(),
406+
global_asm_config.clone(),
407+
cgu.name(),
408+
concurrency_limiter.acquire(tcx.sess.diagnostic()),
409+
),
410+
module_codegen,
411+
Some(rustc_middle::dep_graph::hash_result),
412+
)
413+
.0
414+
}
415+
CguReuse::PreLto => unreachable!("LTO not yet supported"),
416+
CguReuse::PostLto => {
417+
concurrency_limiter.job_already_done();
418+
OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
414419
}
415420
})
416421
.collect::<Vec<_>>()
@@ -489,32 +494,3 @@ pub(crate) fn run_aot(
489494
concurrency_limiter,
490495
})
491496
}
492-
493-
// Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
494-
fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
495-
if !tcx.dep_graph.is_fully_enabled() {
496-
return CguReuse::No;
497-
}
498-
499-
let work_product_id = &cgu.work_product_id();
500-
if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
501-
// We don't have anything cached for this CGU. This can happen
502-
// if the CGU did not exist in the previous session.
503-
return CguReuse::No;
504-
}
505-
506-
// Try to mark the CGU as green. If it we can do so, it means that nothing
507-
// affecting the LLVM module has changed and we can re-use a cached version.
508-
// If we compile with any kind of LTO, this means we can re-use the bitcode
509-
// of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
510-
// know that later). If we are not doing LTO, there is only one optimized
511-
// version of each module, so we re-use that.
512-
let dep_node = cgu.codegen_dep_node(tcx);
513-
assert!(
514-
!tcx.dep_graph.dep_node_exists(&dep_node),
515-
"CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
516-
cgu.name()
517-
);
518-
519-
if tcx.try_mark_green(&dep_node) { CguReuse::PostLto } else { CguReuse::No }
520-
}

compiler/rustc_codegen_llvm/src/back/lto.rs

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_hir::def_id::LOCAL_CRATE;
1919
use rustc_middle::bug;
2020
use rustc_middle::dep_graph::WorkProduct;
2121
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
22-
use rustc_session::cgu_reuse_tracker::CguReuse;
2322
use rustc_session::config::{self, CrateType, Lto};
2423

2524
use std::ffi::{CStr, CString};
@@ -584,7 +583,6 @@ fn thin_lto(
584583
copy_jobs.push(work_product);
585584
info!(" - {}: re-used", module_name);
586585
assert!(cgcx.incr_comp_session_dir.is_some());
587-
cgcx.cgu_reuse_tracker.set_actual_reuse(module_name, CguReuse::PostLto);
588586
continue;
589587
}
590588
}

compiler/rustc_codegen_ssa/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pathdiff = "0.2.0"
1616
serde_json = "1.0.59"
1717
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
1818
regex = "1.4"
19+
thin-vec = "0.2.12"
1920

2021
rustc_serialize = { path = "../rustc_serialize" }
2122
rustc_arena = { path = "../rustc_arena" }

compiler/rustc_codegen_ssa/messages.ftl

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ codegen_ssa_atomic_compare_exchange = Atomic compare-exchange intrinsic missing
1111
1212
codegen_ssa_binary_output_to_tty = option `-o` or `--emit` is used to write binary output type `{$shorthand}` to stdout, but stdout is a tty
1313
14+
codegen_ssa_cgu_not_recorded =
15+
CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded
16+
1417
codegen_ssa_check_installed_visual_studio = please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.
1518
1619
codegen_ssa_copy_path = could not copy {$from} to {$to}: {$error}
@@ -39,13 +42,21 @@ codegen_ssa_failed_to_get_layout = failed to get layout for {$ty}: {$err}
3942
4043
codegen_ssa_failed_to_write = failed to write {$path}: {$error}
4144
45+
codegen_ssa_field_associated_value_expected = associated value expected for `{$name}`
46+
4247
codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced
4348
4449
codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files were produced
4550
4651
codegen_ssa_illegal_link_ordinal_format = illegal ordinal format in `link_ordinal`
4752
.note = an unsuffixed integer value, e.g., `1`, is expected
4853
54+
codegen_ssa_incorrect_cgu_reuse_type =
55+
CGU-reuse for `{$cgu_user_name}` is `{$actual_reuse}` but should be {$at_least ->
56+
[one] {"at least "}
57+
*[other] {""}
58+
}`{$expected_reuse}`
59+
4960
codegen_ssa_insufficient_vs_code_product = VS Code is a different product, and is not sufficient.
5061
5162
codegen_ssa_invalid_link_ordinal_nargs = incorrect number of arguments to `#[link_ordinal]`
@@ -153,19 +164,30 @@ codegen_ssa_linker_unsupported_modifier = `as-needed` modifier not supported for
153164
154165
codegen_ssa_linking_failed = linking with `{$linker_path}` failed: {$exit_status}
155166
167+
codegen_ssa_malformed_cgu_name =
168+
found malformed codegen unit name `{$user_path}`. codegen units names must always start with the name of the crate (`{$crate_name}` in this case).
169+
156170
codegen_ssa_metadata_object_file_write = error writing metadata object file: {$error}
157171
158172
codegen_ssa_missing_cpp_build_tool_component = or a necessary component may be missing from the "C++ build tools" workload
159173
160174
codegen_ssa_missing_memory_ordering = Atomic intrinsic missing memory ordering
161175
176+
codegen_ssa_missing_query_depgraph =
177+
found CGU-reuse attribute but `-Zquery-dep-graph` was not specified
178+
162179
codegen_ssa_msvc_missing_linker = the msvc targets depend on the msvc linker but `link.exe` was not found
163180
164181
codegen_ssa_multiple_external_func_decl = multiple declarations of external function `{$function}` from library `{$library_name}` have different calling conventions
165182
166183
codegen_ssa_multiple_main_functions = entry symbol `main` declared multiple times
167184
.help = did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead
168185
186+
codegen_ssa_no_field = no field `{$name}`
187+
188+
codegen_ssa_no_module_named =
189+
no module named `{$user_path}` (mangled: {$cgu_name}). available modules: {$cgu_names}
190+
169191
codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error}
170192
171193
codegen_ssa_processing_dymutil_failed = processing debug info with `dsymutil` failed: {$status}
@@ -297,6 +319,8 @@ codegen_ssa_unknown_atomic_operation = unknown atomic operation
297319
298320
codegen_ssa_unknown_atomic_ordering = unknown ordering in atomic intrinsic
299321
322+
codegen_ssa_unknown_reuse_kind = unknown cgu-reuse-kind `{$kind}` specified
323+
300324
codegen_ssa_unsupported_arch = unsupported arch `{$arch}` for os `{$os}`
301325
302326
codegen_ssa_unsupported_link_self_contained = option `-C link-self-contained` is not supported on this target

0 commit comments

Comments
 (0)