2020-09-23 15:13:49 +02:00
|
|
|
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
|
|
|
|
|
//! standalone executable.
|
|
|
|
|
|
2022-08-12 09:55:59 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2021-12-20 18:56:35 +01:00
|
|
|
use rustc_codegen_ssa::back::metadata::create_compressed_metadata_file;
|
2020-08-28 12:10:48 +02:00
|
|
|
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
|
|
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2021-09-24 18:15:36 +02:00
|
|
|
use rustc_metadata::EncodedMetadata;
|
2020-05-09 14:14:45 +02:00
|
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
2020-12-27 10:30:38 +01:00
|
|
|
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
|
2020-03-12 11:48:17 +01:00
|
|
|
use rustc_session::cgu_reuse_tracker::CguReuse;
|
2020-08-28 12:10:48 +02:00
|
|
|
use rustc_session::config::{DebugInfo, OutputType};
|
2021-09-19 13:56:58 +02:00
|
|
|
use rustc_session::Session;
|
2020-03-12 11:48:17 +01:00
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
use cranelift_codegen::isa::TargetIsa;
|
|
|
|
|
use cranelift_object::{ObjectBuilder, ObjectModule};
|
2020-10-01 10:38:23 +02:00
|
|
|
|
2022-08-12 09:55:59 +00:00
|
|
|
use crate::global_asm::GlobalAsmConfig;
|
2021-03-05 19:12:59 +01:00
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
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.
|
2022-07-06 07:44:47 -05:00
|
|
|
builder.per_function_section(sess.opts.unstable_opts.function_sections.unwrap_or(false));
|
2021-09-19 13:56:58 +02:00
|
|
|
ObjectModule::new(builder)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 10:38:23 +02:00
|
|
|
fn emit_module(
|
2020-03-12 11:48:17 +01:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-04-30 14:49:58 +02:00
|
|
|
backend_config: &BackendConfig,
|
2020-03-12 11:58:59 +01:00
|
|
|
name: String,
|
|
|
|
|
kind: ModuleKind,
|
2020-10-01 10:38:23 +02:00
|
|
|
module: ObjectModule,
|
2020-04-05 13:48:26 +02:00
|
|
|
debug: Option<DebugContext<'_>>,
|
2021-04-30 14:49:58 +02:00
|
|
|
unwind_context: UnwindContext,
|
2020-10-01 10:38:23 +02:00
|
|
|
) -> 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));
|
2020-10-01 10:38:23 +02:00
|
|
|
let obj = product.object.write().unwrap();
|
2022-02-23 11:49:34 +01:00
|
|
|
|
|
|
|
|
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) {
|
2021-03-05 19:12:59 +01:00
|
|
|
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
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
let work_product = if backend_config.disable_incr_cache {
|
2020-03-12 11:58:59 +01:00
|
|
|
None
|
|
|
|
|
} else {
|
2022-07-04 14:38:42 +01:00
|
|
|
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(
|
2021-03-05 19:12:59 +01:00
|
|
|
CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None },
|
2020-03-12 11:58:59 +01:00
|
|
|
work_product,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 13:38:07 +00:00
|
|
|
fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCodegenResult {
|
2022-05-13 10:32:03 +00:00
|
|
|
let work_product = cgu.previous_work_product(tcx);
|
2022-05-15 11:31:28 +00:00
|
|
|
let obj_out = tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
|
2022-07-04 14:38:42 +01:00
|
|
|
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"),
|
|
|
|
|
);
|
2022-05-15 11:31:28 +00:00
|
|
|
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
|
|
|
|
2022-08-11 13:38:07 +00: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
|
|
|
|
2021-03-05 19:12:59 +01:00
|
|
|
fn module_codegen(
|
|
|
|
|
tcx: TyCtxt<'_>,
|
2022-08-12 09:55:59 +00:00
|
|
|
(backend_config, global_asm_config, cgu_name): (
|
|
|
|
|
BackendConfig,
|
|
|
|
|
Arc<GlobalAsmConfig>,
|
|
|
|
|
rustc_span::Symbol,
|
|
|
|
|
),
|
2021-03-05 19:12:59 +01:00
|
|
|
) -> 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
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
let isa = crate::build_isa(tcx.sess, &backend_config);
|
2021-09-19 13:56:58 +02:00
|
|
|
let mut module = make_module(tcx.sess, isa, cgu_name.as_str().to_string());
|
2020-07-16 14:58:41 +02:00
|
|
|
|
2020-12-27 10:30:38 +01:00
|
|
|
let mut cx = crate::CodegenCx::new(
|
|
|
|
|
tcx,
|
2021-04-30 14:49:58 +02:00
|
|
|
backend_config.clone(),
|
|
|
|
|
module.isa(),
|
2020-12-27 10:30:38 +01:00
|
|
|
tcx.sess.opts.debuginfo != DebugInfo::None,
|
2021-12-20 18:56:35 +01:00
|
|
|
cgu_name,
|
2020-12-27 10:30:38 +01:00
|
|
|
);
|
2021-04-30 14:49:58 +02:00
|
|
|
super::predefine_mono_items(tcx, &mut module, &mono_items);
|
2022-08-10 18:47:05 +00:00
|
|
|
let mut cached_context = Context::new();
|
2021-03-29 10:45:09 +02:00
|
|
|
for (mono_item, _) in mono_items {
|
2020-12-27 10:30:38 +01:00
|
|
|
match mono_item {
|
|
|
|
|
MonoItem::Fn(inst) => {
|
2022-08-10 18:29:46 +00:00
|
|
|
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,
|
|
|
|
|
)
|
2022-08-10 18:29:46 +00:00
|
|
|
});
|
2020-12-27 10:30:38 +01:00
|
|
|
}
|
2021-04-30 14:49:58 +02:00
|
|
|
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);
|
2020-12-27 10:30:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-27 20:48:53 +01:00
|
|
|
}
|
2021-05-27 13:08:14 +02:00
|
|
|
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
|
|
|
|
2021-04-30 14:49:58 +02: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,
|
|
|
|
|
)
|
|
|
|
|
});
|
2020-07-09 16:55:38 +02:00
|
|
|
|
2022-08-12 09:55:59 +00:00
|
|
|
match crate::global_asm::compile_global_asm(
|
|
|
|
|
&global_asm_config,
|
|
|
|
|
cgu.name().as_str(),
|
|
|
|
|
&cx.global_asm,
|
|
|
|
|
) {
|
2022-08-12 09:28:41 +00:00
|
|
|
Ok(()) => {}
|
2022-08-12 09:55:59 +00:00
|
|
|
Err(err) => tcx.sess.fatal(&err),
|
2022-08-12 09:28:41 +00:00
|
|
|
}
|
2020-07-09 16:55:38 +02:00
|
|
|
|
|
|
|
|
codegen_result
|
2020-03-12 11:58:59 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
pub(crate) fn run_aot(
|
2020-03-12 11:58:59 +01:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-03-05 19:12:59 +01:00
|
|
|
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 {
|
2020-05-09 14:14:45 +02:00
|
|
|
tcx.ensure().codegen_unit(cgu.name());
|
2020-03-12 11:48:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 09:55:59 +00:00
|
|
|
let global_asm_config = Arc::new(crate::global_asm::GlobalAsmConfig::new(tcx));
|
|
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
|
2020-08-28 12:10:48 +02:00
|
|
|
cgus.iter()
|
|
|
|
|
.map(|cgu| {
|
2022-08-11 13:38:07 +00:00
|
|
|
let cgu_reuse = if backend_config.disable_incr_cache {
|
|
|
|
|
CguReuse::No
|
|
|
|
|
} else {
|
|
|
|
|
determine_cgu_reuse(tcx, cgu)
|
|
|
|
|
};
|
2021-12-15 14:39:23 +11:00
|
|
|
tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
|
2020-08-28 12:10:48 +02:00
|
|
|
|
2022-08-11 13:49:08 +00:00
|
|
|
match cgu_reuse {
|
2022-08-11 13:38:07 +00:00
|
|
|
CguReuse::No => {
|
|
|
|
|
let dep_node = cgu.codegen_dep_node(tcx);
|
|
|
|
|
tcx.dep_graph
|
|
|
|
|
.with_task(
|
|
|
|
|
dep_node,
|
|
|
|
|
tcx,
|
2022-08-12 09:55:59 +00:00
|
|
|
(backend_config.clone(), global_asm_config.clone(), cgu.name()),
|
2022-08-11 13:38:07 +00:00
|
|
|
module_codegen,
|
|
|
|
|
Some(rustc_middle::dep_graph::hash_result),
|
|
|
|
|
)
|
|
|
|
|
.0
|
2020-08-28 12:10:48 +02:00
|
|
|
}
|
2022-08-11 13:38:07 +00:00
|
|
|
CguReuse::PreLto => reuse_workproduct_for_cgu(tcx, &*cgu),
|
2020-08-28 12:10:48 +02:00
|
|
|
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();
|
|
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
let isa = crate::build_isa(tcx.sess, &backend_config);
|
2021-09-19 13:56:58 +02:00
|
|
|
let mut allocator_module = make_module(tcx.sess, isa, "allocator_shim".to_string());
|
2021-04-30 14:49:58 +02:00
|
|
|
assert_eq!(pointer_ty(tcx), allocator_module.target_config().pointer_type());
|
2021-12-30 14:53:41 +01:00
|
|
|
let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true);
|
2020-08-28 12:10:48 +02:00
|
|
|
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,
|
2021-04-30 14:49:58 +02:00
|
|
|
&backend_config,
|
2020-03-12 11:48:17 +01:00
|
|
|
"allocator_shim".to_string(),
|
|
|
|
|
ModuleKind::Allocator,
|
|
|
|
|
allocator_module,
|
|
|
|
|
None,
|
2020-06-12 19:31:35 +02:00
|
|
|
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", || {
|
2020-03-31 13:20:19 +02:00
|
|
|
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();
|
|
|
|
|
|
2021-05-27 13:08:14 +02:00
|
|
|
let tmp_file =
|
|
|
|
|
tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
|
2020-03-12 11:48:17 +01:00
|
|
|
|
2021-12-20 18:56:35 +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) {
|
2021-03-05 19:12:59 +01:00
|
|
|
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),
|
2020-11-08 14:01:23 +00:00
|
|
|
dwarf_object: None,
|
2020-03-12 11:48:17 +01:00
|
|
|
bytecode: None,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-07 11:14:20 +02:00
|
|
|
// FIXME handle `-Ctarget-cpu=native`
|
2022-04-03 18:42:39 +02:00
|
|
|
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),
|
2020-08-28 12:10:48 +02:00
|
|
|
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();
|
2021-03-05 19:12:59 +01:00
|
|
|
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
|
|
|
}
|