Sync from rust 0039d739d40a076334e111488946441378d11cd7

This commit is contained in:
bjorn3 2023-10-19 12:18:00 +00:00
commit 5f2d3ac041
2 changed files with 40 additions and 61 deletions

View File

@ -7,14 +7,15 @@ use std::sync::Arc;
use std::thread::JoinHandle; use std::thread::JoinHandle;
use cranelift_object::{ObjectBuilder, ObjectModule}; use cranelift_object::{ObjectBuilder, ObjectModule};
use rustc_codegen_ssa::assert_module_sources::CguReuse;
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file; use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
use rustc_codegen_ssa::base::determine_cgu_reuse;
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind}; use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::mir::mono::{CodegenUnit, MonoItem}; use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
use rustc_session::cgu_reuse_tracker::CguReuse;
use rustc_session::config::{DebugInfo, OutputFilenames, OutputType}; use rustc_session::config::{DebugInfo, OutputFilenames, OutputType};
use rustc_session::Session; use rustc_session::Session;
@ -374,43 +375,47 @@ pub(crate) fn run_aot(
} }
} }
// Calculate the CGU reuse
let cgu_reuse = tcx.sess.time("find_cgu_reuse", || {
cgus.iter().map(|cgu| determine_cgu_reuse(tcx, &cgu)).collect::<Vec<_>>()
});
rustc_codegen_ssa::assert_module_sources::assert_module_sources(tcx, &|cgu_reuse_tracker| {
for (i, cgu) in cgus.iter().enumerate() {
let cgu_reuse = cgu_reuse[i];
cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
}
});
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx)); let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len()); let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len());
let modules = tcx.sess.time("codegen mono items", || { let modules = tcx.sess.time("codegen mono items", || {
cgus.iter() cgus.iter()
.map(|cgu| { .enumerate()
let cgu_reuse = if backend_config.disable_incr_cache { .map(|(i, cgu)| match cgu_reuse[i] {
CguReuse::No CguReuse::No => {
} else { let dep_node = cgu.codegen_dep_node(tcx);
determine_cgu_reuse(tcx, cgu) tcx.dep_graph
}; .with_task(
tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse); dep_node,
tcx,
match cgu_reuse { (
CguReuse::No => { backend_config.clone(),
let dep_node = cgu.codegen_dep_node(tcx); global_asm_config.clone(),
tcx.dep_graph cgu.name(),
.with_task( concurrency_limiter.acquire(tcx.sess.diagnostic()),
dep_node, ),
tcx, module_codegen,
( Some(rustc_middle::dep_graph::hash_result),
backend_config.clone(), )
global_asm_config.clone(), .0
cgu.name(), }
concurrency_limiter.acquire(tcx.sess.diagnostic()), CguReuse::PreLto => unreachable!("LTO not yet supported"),
), CguReuse::PostLto => {
module_codegen, concurrency_limiter.job_already_done();
Some(rustc_middle::dep_graph::hash_result), OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
)
.0
}
CguReuse::PreLto => unreachable!(),
CguReuse::PostLto => {
concurrency_limiter.job_already_done();
OngoingModuleCodegen::Sync(reuse_workproduct_for_cgu(tcx, cgu))
}
} }
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
@ -489,32 +494,3 @@ pub(crate) fn run_aot(
concurrency_limiter, concurrency_limiter,
}) })
} }
// Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953
fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
if !tcx.dep_graph.is_fully_enabled() {
return CguReuse::No;
}
let work_product_id = &cgu.work_product_id();
if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
// We don't have anything cached for this CGU. This can happen
// if the CGU did not exist in the previous session.
return CguReuse::No;
}
// Try to mark the CGU as green. If it we can do so, it means that nothing
// affecting the LLVM module has changed and we can re-use a cached version.
// If we compile with any kind of LTO, this means we can re-use the bitcode
// of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
// know that later). If we are not doing LTO, there is only one optimized
// version of each module, so we re-use that.
let dep_node = cgu.codegen_dep_node(tcx);
assert!(
!tcx.dep_graph.dep_node_exists(&dep_node),
"CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
cgu.name()
);
if tcx.try_mark_green(&dep_node) { CguReuse::PostLto } else { CguReuse::No }
}

View File

@ -1,3 +1,6 @@
#![cfg_attr(not(bootstrap), allow(internal_features))]
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
#![cfg_attr(not(bootstrap), doc(rust_logo))]
#![feature(rustc_private)] #![feature(rustc_private)]
// Note: please avoid adding other feature gates where possible // Note: please avoid adding other feature gates where possible
#![warn(rust_2018_idioms)] #![warn(rust_2018_idioms)]