rust/src/driver/aot.rs

288 lines
9.5 KiB
Rust
Raw Normal View History

use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::middle::cstore::EncodedMetadata;
use rustc_middle::mir::mono::CodegenUnit;
use rustc_session::config::{DebugInfo, OutputType};
2020-03-12 05:48:17 -05:00
use rustc_session::cgu_reuse_tracker::CguReuse;
use rustc_codegen_ssa::back::linker::LinkerInfo;
2020-06-20 11:44:49 -05:00
use rustc_codegen_ssa::{CrateInfo, CodegenResults, CompiledModule, ModuleKind};
2020-03-12 05:58:59 -05:00
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2020-03-12 05:48:17 -05:00
use crate::prelude::*;
use crate::backend::{Emit, WriteDebugInfo};
2020-03-12 05:58:59 -05:00
fn new_module(tcx: TyCtxt<'_>, name: String) -> Module<crate::backend::Backend> {
let module = crate::backend::make_module(tcx.sess, name);
assert_eq!(pointer_ty(tcx), module.target_config().pointer_type());
module
}
struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
impl<HCX> HashStable<HCX> for ModuleCodegenResult {
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
// do nothing
}
}
fn emit_module<B: Backend>(
2020-03-12 05:48:17 -05:00
tcx: TyCtxt<'_>,
2020-03-12 05:58:59 -05:00
name: String,
kind: ModuleKind,
mut module: Module<B>,
debug: Option<DebugContext<'_>>,
2020-05-01 12:21:29 -05:00
unwind_context: UnwindContext<'_>,
2020-03-12 05:58:59 -05:00
) -> ModuleCodegenResult
where B::Product: Emit + WriteDebugInfo,
{
module.finalize_definitions();
let mut product = module.finish();
if let Some(mut debug) = debug {
debug.emit(&mut product);
}
2020-05-01 12:21:29 -05:00
unwind_context.emit(&mut product);
2020-03-12 05:58:59 -05:00
let tmp_file = tcx
.output_filenames(LOCAL_CRATE)
.temp_path(OutputType::Object, Some(&name));
let obj = product.emit();
std::fs::write(&tmp_file, obj).unwrap();
2020-03-12 05:48:17 -05:00
2020-03-12 05:58:59 -05:00
let work_product = if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() {
None
} else {
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
2020-03-12 05:58:59 -05:00
tcx.sess,
&name,
&Some(tmp_file.clone()),
2020-03-12 05:58:59 -05:00
)
2020-03-12 05:48:17 -05:00
};
2020-03-12 05:58:59 -05:00
ModuleCodegenResult(
CompiledModule {
name,
kind,
object: Some(tmp_file),
bytecode: None,
},
work_product,
)
}
fn reuse_workproduct_for_cgu(
tcx: TyCtxt<'_>,
cgu: &CodegenUnit<'_>,
2020-03-12 05:58:59 -05:00
work_products: &mut FxHashMap<WorkProductId, WorkProduct>,
) -> CompiledModule {
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir();
let mut object = None;
let work_product = cgu.work_product(tcx);
if let Some(saved_file) = &work_product.saved_file {
let obj_out = tcx.output_filenames(LOCAL_CRATE).temp_path(OutputType::Object, Some(&cgu.name().as_str()));
object = Some(obj_out.clone());
2020-03-12 05:58:59 -05:00
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, &saved_file);
if let Err(err) = rustc_fs_util::link_or_copy(&source_file, &obj_out) {
tcx.sess.err(&format!(
"unable to copy {} to {}: {}",
source_file.display(),
obj_out.display(),
err
));
}
}
2020-03-12 05:48:17 -05:00
2020-03-12 05:58:59 -05:00
work_products.insert(cgu.work_product_id(), work_product);
2020-03-12 05:48:17 -05:00
2020-03-12 05:58:59 -05:00
CompiledModule {
name: cgu.name().to_string(),
kind: ModuleKind::Regular,
object,
bytecode: None,
2020-03-12 05:48:17 -05:00
}
2020-03-12 05:58:59 -05:00
}
2020-03-12 05:48:17 -05:00
2020-03-12 05:58:59 -05:00
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegenResult {
let cgu = tcx.codegen_unit(cgu_name);
let mono_items = cgu.items_in_deterministic_order(tcx);
2020-03-12 05:48:17 -05:00
let module = new_module(tcx, cgu_name.as_str().to_string());
2020-03-12 05:58:59 -05:00
2020-06-20 11:44:49 -05:00
let mut cx = crate::CodegenCx::new(tcx, module, tcx.sess.opts.debuginfo != DebugInfo::None);
super::codegen_mono_items(&mut cx, mono_items);
let (mut module, debug, mut unwind_context) = tcx.sess.time("finalize CodegenCx", || cx.finalize());
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context);
2020-03-12 05:58:59 -05:00
emit_module(
tcx,
cgu.name().as_str().to_string(),
ModuleKind::Regular,
module,
debug,
2020-05-01 12:21:29 -05:00
unwind_context,
2020-03-12 05:58:59 -05:00
)
}
pub(super) fn run_aot(
tcx: TyCtxt<'_>,
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> Box<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>)> {
let mut work_products = FxHashMap::default();
2020-03-12 05:48:17 -05:00
2020-04-18 14:13:09 -05:00
let cgus = if tcx.sess.opts.output_types.should_codegen() {
tcx.collect_and_partition_mono_items(LOCAL_CRATE).1
} else {
// If only `--emit metadata` is used, we shouldn't perform any codegen.
// Also `tcx.collect_and_partition_mono_items` may panic in that case.
&[]
};
2020-03-12 05:48:17 -05:00
if tcx.dep_graph.is_fully_enabled() {
for cgu in &*cgus {
tcx.ensure().codegen_unit(cgu.name());
2020-03-12 05:48:17 -05:00
}
}
let modules = super::time(tcx, "codegen mono items", || {
cgus.iter().map(|cgu| {
let cgu_reuse = determine_cgu_reuse(tcx, cgu);
tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
match cgu_reuse {
_ if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() => {}
CguReuse::No => {}
CguReuse::PreLto => {
2020-03-12 05:58:59 -05:00
return reuse_workproduct_for_cgu(tcx, &*cgu, &mut work_products);
2020-03-12 05:48:17 -05:00
}
CguReuse::PostLto => unreachable!(),
}
let dep_node = cgu.codegen_dep_node(tcx);
let (ModuleCodegenResult(module, work_product), _) =
tcx.dep_graph.with_task(dep_node, tcx, cgu.name(), module_codegen, rustc_middle::dep_graph::hash_result);
2020-03-12 05:48:17 -05:00
if let Some((id, product)) = work_product {
work_products.insert(id, product);
}
module
}).collect::<Vec<_>>()
});
tcx.sess.abort_if_errors();
let mut allocator_module = new_module(tcx, "allocator_shim".to_string());
let mut allocator_unwind_context = UnwindContext::new(tcx, allocator_module.isa());
let created_alloc_shim = crate::allocator::codegen(
tcx,
&mut allocator_module,
&mut allocator_unwind_context,
);
2020-03-12 05:48:17 -05:00
let allocator_module = if created_alloc_shim {
let ModuleCodegenResult(module, work_product) = emit_module(
tcx,
"allocator_shim".to_string(),
ModuleKind::Allocator,
allocator_module,
None,
allocator_unwind_context,
2020-03-12 05:48:17 -05:00
);
if let Some((id, product)) = work_product {
work_products.insert(id, product);
}
Some(module)
} else {
None
};
rustc_incremental::assert_dep_graph(tcx);
rustc_incremental::save_dep_graph(tcx);
let metadata_module = if need_metadata_module {
let _timer = tcx.prof.generic_activity("codegen crate metadata");
let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
2020-03-12 05:48:17 -05:00
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
let metadata_cgu_name = cgu_name_builder
.build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata"))
.as_str()
.to_string();
let tmp_file = tcx
.output_filenames(LOCAL_CRATE)
.temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
let obj = crate::backend::with_object(tcx.sess, &metadata_cgu_name, |object| {
crate::metadata::write_metadata(tcx, object);
});
std::fs::write(&tmp_file, obj).unwrap();
(metadata_cgu_name, tmp_file)
});
Some(CompiledModule {
name: metadata_cgu_name,
kind: ModuleKind::Metadata,
object: Some(tmp_file),
bytecode: None,
})
} else {
None
};
2020-04-18 14:13:09 -05:00
if tcx.sess.opts.output_types.should_codegen() {
rustc_incremental::assert_module_sources::assert_module_sources(tcx);
}
2020-03-12 05:48:17 -05:00
Box::new((CodegenResults {
crate_name: tcx.crate_name(LOCAL_CRATE),
modules,
allocator_module,
metadata_module,
crate_hash: tcx.crate_hash(LOCAL_CRATE),
metadata,
windows_subsystem: None, // Windows is not yet supported
linker_info: LinkerInfo::new(tcx),
crate_info: CrateInfo::new(tcx),
}, work_products))
}
// 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.dep_graph.try_mark_green(tcx, &dep_node).is_some() {
CguReuse::PreLto
} else {
CguReuse::No
}
}