Files
rust/src/driver/aot.rs

381 lines
13 KiB
Rust
Raw Normal View History

//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
//! standalone executable.
use std::sync::Arc;
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
2020-03-12 11:48:17 +01:00
use rustc_session::cgu_reuse_tracker::CguReuse;
use rustc_session::config::{DebugInfo, OutputType};
use rustc_session::Session;
2020-03-12 11:48:17 +01:00
use cranelift_codegen::isa::TargetIsa;
use cranelift_object::{ObjectBuilder, ObjectModule};
use crate::global_asm::GlobalAsmConfig;
use crate::{prelude::*, BackendConfig};
2020-03-12 11:48:17 +01:00
2020-03-12 11:58:59 +01:00
struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
impl<HCX> HashStable<HCX> for ModuleCodegenResult {
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
// do nothing
}
}
2022-08-11 13:49:08 +00:00
pub(crate) struct OngoingCodegen {
modules: Vec<ModuleCodegenResult>,
allocator_module: Option<CompiledModule>,
metadata_module: Option<CompiledModule>,
metadata: EncodedMetadata,
crate_info: CrateInfo,
work_products: FxHashMap<WorkProductId, WorkProduct>,
}
impl OngoingCodegen {
pub(crate) fn join(self) -> (CodegenResults, FxHashMap<WorkProductId, WorkProduct>) {
let mut work_products = self.work_products;
let mut modules = vec![];
for module_codegen_result in self.modules {
let ModuleCodegenResult(module, work_product) = module_codegen_result;
if let Some((work_product_id, work_product)) = work_product {
work_products.insert(work_product_id, work_product);
}
modules.push(module);
}
(
CodegenResults {
modules,
allocator_module: self.allocator_module,
metadata_module: self.metadata_module,
metadata: self.metadata,
crate_info: self.crate_info,
},
work_products,
)
}
}
fn make_module(sess: &Session, isa: Box<dyn TargetIsa>, name: String) -> ObjectModule {
let mut builder =
ObjectBuilder::new(isa, name + ".o", cranelift_module::default_libcall_names()).unwrap();
// Unlike cg_llvm, cg_clif defaults to disabling -Zfunction-sections. For cg_llvm binary size
// is important, while cg_clif cares more about compilation times. Enabling -Zfunction-sections
// can easily double the amount of time necessary to perform linking.
builder.per_function_section(sess.opts.unstable_opts.function_sections.unwrap_or(false));
ObjectModule::new(builder)
}
fn emit_module(
2020-03-12 11:48:17 +01:00
tcx: TyCtxt<'_>,
backend_config: &BackendConfig,
2020-03-12 11:58:59 +01:00
name: String,
kind: ModuleKind,
module: ObjectModule,
debug: Option<DebugContext<'_>>,
unwind_context: UnwindContext,
) -> ModuleCodegenResult {
2020-03-12 11:58:59 +01:00
let mut product = module.finish();
if let Some(mut debug) = debug {
debug.emit(&mut product);
}
2020-05-01 19:21:29 +02:00
unwind_context.emit(&mut product);
2021-05-11 14:39:04 +02:00
let tmp_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(&name));
let obj = product.object.write().unwrap();
tcx.sess.prof.artifact_size("object_file", name.clone(), obj.len().try_into().unwrap());
2020-08-08 16:14:11 +02:00
if let Err(err) = std::fs::write(&tmp_file, obj) {
tcx.sess.fatal(&format!("error writing object file: {}", err));
2020-08-08 16:14:11 +02:00
}
2020-03-12 11:48:17 +01:00
let work_product = if backend_config.disable_incr_cache {
2020-03-12 11:58:59 +01:00
None
} else {
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
tcx.sess,
&name,
&[("o", &tmp_file)],
)
2020-03-12 11:48:17 +01:00
};
2020-03-12 11:58:59 +01:00
ModuleCodegenResult(
CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None },
2020-03-12 11:58:59 +01:00
work_product,
)
}
fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCodegenResult {
let work_product = cgu.previous_work_product(tcx);
let obj_out = tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
let source_file = rustc_incremental::in_incr_comp_dir_sess(
&tcx.sess,
&work_product.saved_files.get("o").expect("no saved object file in work product"),
);
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 11:58:59 +01:00
}
2020-03-12 11:48:17 +01:00
ModuleCodegenResult(
CompiledModule {
name: cgu.name().to_string(),
kind: ModuleKind::Regular,
object: Some(obj_out),
dwarf_object: None,
bytecode: None,
},
Some((cgu.work_product_id(), work_product)),
)
2020-03-12 11:58:59 +01:00
}
2020-03-12 11:48:17 +01:00
fn module_codegen(
tcx: TyCtxt<'_>,
(backend_config, global_asm_config, cgu_name): (
BackendConfig,
Arc<GlobalAsmConfig>,
rustc_span::Symbol,
),
) -> ModuleCodegenResult {
2020-03-12 11:58:59 +01:00
let cgu = tcx.codegen_unit(cgu_name);
let mono_items = cgu.items_in_deterministic_order(tcx);
2020-03-12 11:48:17 +01:00
let isa = crate::build_isa(tcx.sess, &backend_config);
let mut module = make_module(tcx.sess, isa, cgu_name.as_str().to_string());
let mut cx = crate::CodegenCx::new(
tcx,
backend_config.clone(),
module.isa(),
tcx.sess.opts.debuginfo != DebugInfo::None,
cgu_name,
);
super::predefine_mono_items(tcx, &mut module, &mono_items);
2022-08-10 18:47:05 +00:00
let mut cached_context = Context::new();
for (mono_item, _) in mono_items {
match mono_item {
MonoItem::Fn(inst) => {
cx.tcx.sess.time("codegen fn", || {
2022-08-10 18:47:05 +00:00
crate::base::codegen_and_compile_fn(
&mut cx,
&mut cached_context,
&mut module,
inst,
)
});
}
MonoItem::Static(def_id) => crate::constant::codegen_static(tcx, &mut module, def_id),
2021-01-30 19:18:48 +01:00
MonoItem::GlobalAsm(item_id) => {
2022-08-12 09:11:47 +00:00
crate::global_asm::codegen_global_asm_item(tcx, &mut cx.global_asm, item_id);
}
}
}
crate::main_shim::maybe_create_entry_wrapper(
tcx,
&mut module,
&mut cx.unwind_context,
false,
cgu.is_primary(),
);
2020-03-12 11:58:59 +01:00
let debug_context = cx.debug_context;
let unwind_context = cx.unwind_context;
let codegen_result = tcx.sess.time("write object file", || {
emit_module(
tcx,
&backend_config,
cgu.name().as_str().to_string(),
ModuleKind::Regular,
module,
debug_context,
unwind_context,
)
});
match crate::global_asm::compile_global_asm(
&global_asm_config,
cgu.name().as_str(),
&cx.global_asm,
) {
Ok(()) => {}
Err(err) => tcx.sess.fatal(&err),
}
codegen_result
2020-03-12 11:58:59 +01:00
}
pub(crate) fn run_aot(
2020-03-12 11:58:59 +01:00
tcx: TyCtxt<'_>,
backend_config: BackendConfig,
2020-03-12 11:58:59 +01:00
metadata: EncodedMetadata,
need_metadata_module: bool,
2022-08-11 13:49:08 +00:00
) -> Box<OngoingCodegen> {
2020-04-18 21:13:09 +02:00
let cgus = if tcx.sess.opts.output_types.should_codegen() {
2021-05-11 14:39:04 +02:00
tcx.collect_and_partition_mono_items(()).1
2020-04-18 21:13:09 +02:00
} 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 11:48:17 +01:00
if tcx.dep_graph.is_fully_enabled() {
for cgu in &*cgus {
tcx.ensure().codegen_unit(cgu.name());
2020-03-12 11:48:17 +01:00
}
}
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
cgus.iter()
.map(|cgu| {
let cgu_reuse = if backend_config.disable_incr_cache {
CguReuse::No
} else {
determine_cgu_reuse(tcx, cgu)
};
tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
2022-08-11 13:49:08 +00:00
match cgu_reuse {
CguReuse::No => {
let dep_node = cgu.codegen_dep_node(tcx);
tcx.dep_graph
.with_task(
dep_node,
tcx,
(backend_config.clone(), global_asm_config.clone(), cgu.name()),
module_codegen,
Some(rustc_middle::dep_graph::hash_result),
)
.0
}
CguReuse::PreLto => reuse_workproduct_for_cgu(tcx, &*cgu),
CguReuse::PostLto => unreachable!(),
}
})
.collect::<Vec<_>>()
2020-03-12 11:48:17 +01:00
});
tcx.sess.abort_if_errors();
2022-08-11 13:49:08 +00:00
let mut work_products = FxHashMap::default();
let isa = crate::build_isa(tcx.sess, &backend_config);
let mut allocator_module = make_module(tcx.sess, isa, "allocator_shim".to_string());
assert_eq!(pointer_ty(tcx), allocator_module.target_config().pointer_type());
let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
let created_alloc_shim =
crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
2020-03-12 11:48:17 +01:00
let allocator_module = if created_alloc_shim {
let ModuleCodegenResult(module, work_product) = emit_module(
tcx,
&backend_config,
2020-03-12 11:48:17 +01:00
"allocator_shim".to_string(),
ModuleKind::Allocator,
allocator_module,
None,
allocator_unwind_context,
2020-03-12 11:48:17 +01:00
);
if let Some((id, product)) = work_product {
work_products.insert(id, product);
}
Some(module)
} else {
None
};
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 11:48:17 +01: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(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
2020-03-12 11:48:17 +01:00
let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
let obj = create_compressed_metadata_file(tcx.sess, &metadata, &symbol_name);
2020-03-12 11:48:17 +01:00
2020-08-08 16:14:11 +02:00
if let Err(err) = std::fs::write(&tmp_file, obj) {
tcx.sess.fatal(&format!("error writing metadata object file: {}", err));
2020-08-08 16:14:11 +02:00
}
2020-03-12 11:48:17 +01:00
(metadata_cgu_name, tmp_file)
});
Some(CompiledModule {
name: metadata_cgu_name,
kind: ModuleKind::Metadata,
object: Some(tmp_file),
dwarf_object: None,
2020-03-12 11:48:17 +01:00
bytecode: None,
})
} else {
None
};
// FIXME handle `-Ctarget-cpu=native`
let target_cpu = match tcx.sess.opts.cg.target_cpu {
Some(ref name) => name,
None => tcx.sess.target.cpu.as_ref(),
}
.to_owned();
2022-08-11 13:49:08 +00:00
Box::new(OngoingCodegen {
modules,
allocator_module,
metadata_module,
metadata,
crate_info: CrateInfo::new(tcx, target_cpu),
work_products,
2022-08-11 13:49:08 +00:00
})
2020-03-12 11:48:17 +01:00
}
// 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() {
2020-03-12 11:48:17 +01:00
// 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()
);
2021-01-18 23:53:42 +01:00
if tcx.try_mark_green(&dep_node) { CguReuse::PreLto } else { CguReuse::No }
2020-03-12 11:48:17 +01:00
}