2020-09-23 08:13:49 -05:00
|
|
|
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
|
|
|
|
//! standalone executable.
|
|
|
|
|
2020-07-09 09:55:38 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-04-11 14:51:28 -05:00
|
|
|
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
2020-08-28 05:10:48 -05:00
|
|
|
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
|
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-05-09 07:14:45 -05:00
|
|
|
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
2020-03-31 06:20:19 -05:00
|
|
|
use rustc_middle::middle::cstore::EncodedMetadata;
|
2020-12-27 03:30:38 -06:00
|
|
|
use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
|
2020-03-12 05:48:17 -05:00
|
|
|
use rustc_session::cgu_reuse_tracker::CguReuse;
|
2020-08-28 05:10:48 -05:00
|
|
|
use rustc_session::config::{DebugInfo, OutputType};
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2021-03-05 12:12:59 -06:00
|
|
|
use cranelift_object::ObjectModule;
|
2020-10-01 03:38:23 -05:00
|
|
|
|
2021-03-05 12:12:59 -06:00
|
|
|
use crate::{prelude::*, BackendConfig};
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2020-03-12 05:58:59 -05:00
|
|
|
struct ModuleCodegenResult(CompiledModule, Option<(WorkProductId, WorkProduct)>);
|
|
|
|
|
|
|
|
impl<HCX> HashStable<HCX> for ModuleCodegenResult {
|
|
|
|
fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 03:38:23 -05:00
|
|
|
fn emit_module(
|
2020-03-12 05:48:17 -05:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-04-30 07:49:58 -05:00
|
|
|
backend_config: &BackendConfig,
|
2020-03-12 05:58:59 -05:00
|
|
|
name: String,
|
|
|
|
kind: ModuleKind,
|
2020-10-01 03:38:23 -05:00
|
|
|
module: ObjectModule,
|
2020-04-05 06:48:26 -05:00
|
|
|
debug: Option<DebugContext<'_>>,
|
2021-04-30 07:49:58 -05:00
|
|
|
unwind_context: UnwindContext,
|
2020-10-01 03:38:23 -05:00
|
|
|
) -> ModuleCodegenResult {
|
2020-03-12 05:58:59 -05:00
|
|
|
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);
|
|
|
|
|
2021-05-11 07:39:04 -05:00
|
|
|
let tmp_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(&name));
|
2020-10-01 03:38:23 -05:00
|
|
|
let obj = product.object.write().unwrap();
|
2020-08-08 09:14:11 -05:00
|
|
|
if let Err(err) = std::fs::write(&tmp_file, obj) {
|
2021-03-05 12:12:59 -06:00
|
|
|
tcx.sess.fatal(&format!("error writing object file: {}", err));
|
2020-08-08 09:14:11 -05:00
|
|
|
}
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
let work_product = if backend_config.disable_incr_cache {
|
2020-03-12 05:58:59 -05:00
|
|
|
None
|
|
|
|
} else {
|
2020-05-18 04:35:23 -05:00
|
|
|
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
|
2020-03-12 05:58:59 -05:00
|
|
|
tcx.sess,
|
|
|
|
&name,
|
2020-05-18 04:35:23 -05:00
|
|
|
&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(
|
2021-03-05 12:12:59 -06:00
|
|
|
CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None },
|
2020-03-12 05:58:59 -05:00
|
|
|
work_product,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reuse_workproduct_for_cgu(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-04-05 06:48:26 -05:00
|
|
|
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);
|
2020-05-18 04:35:23 -05:00
|
|
|
if let Some(saved_file) = &work_product.saved_file {
|
2021-05-27 06:08:14 -05:00
|
|
|
let obj_out =
|
|
|
|
tcx.output_filenames(()).temp_path(OutputType::Object, Some(&cgu.name().as_str()));
|
2020-05-09 07:14:45 -05:00
|
|
|
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,
|
2020-11-08 08:01:23 -06:00
|
|
|
dwarf_object: None,
|
2020-03-12 05:58:59 -05:00
|
|
|
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
|
|
|
|
2021-03-05 12:12:59 -06:00
|
|
|
fn module_codegen(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
(backend_config, cgu_name): (BackendConfig, rustc_span::Symbol),
|
|
|
|
) -> ModuleCodegenResult {
|
2020-03-12 05:58:59 -05:00
|
|
|
let cgu = tcx.codegen_unit(cgu_name);
|
|
|
|
let mono_items = cgu.items_in_deterministic_order(tcx);
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
let isa = crate::build_isa(tcx.sess, &backend_config);
|
|
|
|
let mut module = crate::backend::make_module(tcx.sess, isa, cgu_name.as_str().to_string());
|
2020-07-16 07:58:41 -05:00
|
|
|
|
2020-12-27 03:30:38 -06:00
|
|
|
let mut cx = crate::CodegenCx::new(
|
|
|
|
tcx,
|
2021-04-30 07:49:58 -05:00
|
|
|
backend_config.clone(),
|
|
|
|
module.isa(),
|
2020-12-27 03:30:38 -06:00
|
|
|
tcx.sess.opts.debuginfo != DebugInfo::None,
|
|
|
|
);
|
2021-04-30 07:49:58 -05:00
|
|
|
super::predefine_mono_items(tcx, &mut module, &mono_items);
|
2021-03-29 03:45:09 -05:00
|
|
|
for (mono_item, _) in mono_items {
|
2020-12-27 03:30:38 -06:00
|
|
|
match mono_item {
|
|
|
|
MonoItem::Fn(inst) => {
|
2021-04-30 07:49:58 -05:00
|
|
|
cx.tcx
|
|
|
|
.sess
|
|
|
|
.time("codegen fn", || crate::base::codegen_fn(&mut cx, &mut module, inst));
|
2020-12-27 03:30:38 -06:00
|
|
|
}
|
2021-04-30 07:49:58 -05:00
|
|
|
MonoItem::Static(def_id) => crate::constant::codegen_static(tcx, &mut module, def_id),
|
2021-01-30 12:18:48 -06:00
|
|
|
MonoItem::GlobalAsm(item_id) => {
|
|
|
|
let item = cx.tcx.hir().item(item_id);
|
2021-04-11 14:51:28 -05:00
|
|
|
if let rustc_hir::ItemKind::GlobalAsm(asm) = item.kind {
|
|
|
|
if !asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
|
|
|
|
cx.global_asm.push_str("\n.intel_syntax noprefix\n");
|
|
|
|
} else {
|
|
|
|
cx.global_asm.push_str("\n.att_syntax\n");
|
|
|
|
}
|
|
|
|
for piece in asm.template {
|
|
|
|
match *piece {
|
|
|
|
InlineAsmTemplatePiece::String(ref s) => cx.global_asm.push_str(s),
|
|
|
|
InlineAsmTemplatePiece::Placeholder { .. } => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cx.global_asm.push_str("\n.att_syntax\n\n");
|
2020-12-27 03:30:38 -06:00
|
|
|
} else {
|
|
|
|
bug!("Expected GlobalAsm found {:?}", item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 13:48:53 -06:00
|
|
|
}
|
2021-05-27 06:08:14 -05:00
|
|
|
crate::main_shim::maybe_create_entry_wrapper(
|
|
|
|
tcx,
|
|
|
|
&mut module,
|
|
|
|
&mut cx.unwind_context,
|
|
|
|
false,
|
|
|
|
cgu.is_primary(),
|
|
|
|
);
|
2020-03-12 05:58:59 -05:00
|
|
|
|
2021-04-30 07:49:58 -05: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 09:55:38 -05:00
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
codegen_global_asm(tcx, &cgu.name().as_str(), &cx.global_asm);
|
2020-07-09 09:55:38 -05:00
|
|
|
|
|
|
|
codegen_result
|
2020-03-12 05:58:59 -05:00
|
|
|
}
|
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
pub(crate) fn run_aot(
|
2020-03-12 05:58:59 -05:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-03-05 12:12:59 -06:00
|
|
|
backend_config: BackendConfig,
|
2020-03-12 05:58:59 -05:00
|
|
|
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() {
|
2021-05-11 07:39:04 -05:00
|
|
|
tcx.collect_and_partition_mono_items(()).1
|
2020-04-18 14:13:09 -05: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 05:48:17 -05:00
|
|
|
|
|
|
|
if tcx.dep_graph.is_fully_enabled() {
|
|
|
|
for cgu in &*cgus {
|
2020-05-09 07:14:45 -05:00
|
|
|
tcx.ensure().codegen_unit(cgu.name());
|
2020-03-12 05:48:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
|
2020-08-28 05:10:48 -05:00
|
|
|
cgus.iter()
|
|
|
|
.map(|cgu| {
|
|
|
|
let cgu_reuse = determine_cgu_reuse(tcx, cgu);
|
2021-03-05 12:12:59 -06:00
|
|
|
tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
|
2020-08-28 05:10:48 -05:00
|
|
|
|
|
|
|
match cgu_reuse {
|
2021-04-30 07:49:58 -05:00
|
|
|
_ if backend_config.disable_incr_cache => {}
|
2020-08-28 05:10:48 -05:00
|
|
|
CguReuse::No => {}
|
|
|
|
CguReuse::PreLto => {
|
|
|
|
return reuse_workproduct_for_cgu(tcx, &*cgu, &mut work_products);
|
|
|
|
}
|
|
|
|
CguReuse::PostLto => unreachable!(),
|
2020-03-12 05:48:17 -05:00
|
|
|
}
|
|
|
|
|
2020-08-28 05:10:48 -05:00
|
|
|
let dep_node = cgu.codegen_dep_node(tcx);
|
|
|
|
let (ModuleCodegenResult(module, work_product), _) = tcx.dep_graph.with_task(
|
|
|
|
dep_node,
|
|
|
|
tcx,
|
2021-04-30 07:49:58 -05:00
|
|
|
(backend_config.clone(), cgu.name()),
|
2020-08-28 05:10:48 -05:00
|
|
|
module_codegen,
|
|
|
|
rustc_middle::dep_graph::hash_result,
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some((id, product)) = work_product {
|
|
|
|
work_products.insert(id, product);
|
|
|
|
}
|
2020-03-12 05:48:17 -05:00
|
|
|
|
2020-08-28 05:10:48 -05:00
|
|
|
module
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2020-03-12 05:48:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
let isa = crate::build_isa(tcx.sess, &backend_config);
|
|
|
|
let mut allocator_module =
|
|
|
|
crate::backend::make_module(tcx.sess, isa, "allocator_shim".to_string());
|
|
|
|
assert_eq!(pointer_ty(tcx), allocator_module.target_config().pointer_type());
|
2020-12-27 03:30:38 -06:00
|
|
|
let mut allocator_unwind_context = UnwindContext::new(tcx, allocator_module.isa(), true);
|
2020-08-28 05:10:48 -05:00
|
|
|
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,
|
2021-04-30 07:49:58 -05:00
|
|
|
&backend_config,
|
2020-03-12 05:48:17 -05:00
|
|
|
"allocator_shim".to_string(),
|
|
|
|
ModuleKind::Allocator,
|
|
|
|
allocator_module,
|
|
|
|
None,
|
2020-06-12 12:31:35 -05:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
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 06:20:19 -05:00
|
|
|
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();
|
|
|
|
|
2021-05-27 06:08:14 -05:00
|
|
|
let tmp_file =
|
|
|
|
tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
|
2020-03-12 05:48:17 -05:00
|
|
|
|
|
|
|
let obj = crate::backend::with_object(tcx.sess, &metadata_cgu_name, |object| {
|
|
|
|
crate::metadata::write_metadata(tcx, object);
|
|
|
|
});
|
|
|
|
|
2020-08-08 09:14:11 -05:00
|
|
|
if let Err(err) = std::fs::write(&tmp_file, obj) {
|
2021-03-05 12:12:59 -06:00
|
|
|
tcx.sess.fatal(&format!("error writing metadata object file: {}", err));
|
2020-08-08 09:14:11 -05:00
|
|
|
}
|
2020-03-12 05:48:17 -05:00
|
|
|
|
|
|
|
(metadata_cgu_name, tmp_file)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(CompiledModule {
|
|
|
|
name: metadata_cgu_name,
|
|
|
|
kind: ModuleKind::Metadata,
|
|
|
|
object: Some(tmp_file),
|
2020-11-08 08:01:23 -06:00
|
|
|
dwarf_object: None,
|
2020-03-12 05:48:17 -05:00
|
|
|
bytecode: None,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-07-07 04:14:20 -05:00
|
|
|
// FIXME handle `-Ctarget-cpu=native`
|
|
|
|
let target_cpu =
|
|
|
|
tcx.sess.opts.cg.target_cpu.as_ref().unwrap_or(&tcx.sess.target.cpu).to_owned();
|
2020-08-28 05:10:48 -05:00
|
|
|
Box::new((
|
|
|
|
CodegenResults {
|
|
|
|
modules,
|
|
|
|
allocator_module,
|
|
|
|
metadata_module,
|
|
|
|
metadata,
|
2021-07-07 04:14:20 -05:00
|
|
|
crate_info: CrateInfo::new(tcx, target_cpu),
|
2020-08-28 05:10:48 -05:00
|
|
|
},
|
|
|
|
work_products,
|
|
|
|
))
|
2020-03-12 05:48:17 -05:00
|
|
|
}
|
|
|
|
|
2020-07-09 09:55:38 -05:00
|
|
|
fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
|
if global_asm.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-15 07:19:23 -05:00
|
|
|
if cfg!(not(feature = "inline_asm"))
|
2020-11-08 05:27:51 -06:00
|
|
|
|| tcx.sess.target.is_like_osx
|
|
|
|
|| tcx.sess.target.is_like_windows
|
2020-08-15 07:19:23 -05:00
|
|
|
{
|
2020-07-09 11:56:17 -05:00
|
|
|
if global_asm.contains("__rust_probestack") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME fix linker error on macOS
|
2020-08-15 07:19:23 -05:00
|
|
|
if cfg!(not(feature = "inline_asm")) {
|
2020-08-28 05:10:48 -05:00
|
|
|
tcx.sess.fatal(
|
|
|
|
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift",
|
|
|
|
);
|
2020-08-15 07:19:23 -05:00
|
|
|
} else {
|
2021-03-05 12:12:59 -06:00
|
|
|
tcx.sess.fatal("asm! and global_asm! are not yet supported on macOS and Windows");
|
2020-08-15 07:19:23 -05:00
|
|
|
}
|
2020-07-09 11:56:17 -05:00
|
|
|
}
|
|
|
|
|
2020-07-09 11:55:46 -05:00
|
|
|
let assembler = crate::toolchain::get_toolchain_binary(tcx.sess, "as");
|
|
|
|
let linker = crate::toolchain::get_toolchain_binary(tcx.sess, "ld");
|
|
|
|
|
2020-07-09 09:55:38 -05:00
|
|
|
// Remove all LLVM style comments
|
2020-08-28 05:10:48 -05:00
|
|
|
let global_asm = global_asm
|
|
|
|
.lines()
|
2021-03-05 12:12:59 -06:00
|
|
|
.map(|line| if let Some(index) = line.find("//") { &line[0..index] } else { line })
|
2020-08-28 05:10:48 -05:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n");
|
2020-07-09 09:55:38 -05:00
|
|
|
|
2021-05-27 06:08:14 -05:00
|
|
|
let output_object_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu_name));
|
2020-07-09 09:55:38 -05:00
|
|
|
|
|
|
|
// Assemble `global_asm`
|
|
|
|
let global_asm_object_file = add_file_stem_postfix(output_object_file.clone(), ".asm");
|
2020-07-09 11:55:46 -05:00
|
|
|
let mut child = Command::new(assembler)
|
2020-08-28 05:10:48 -05:00
|
|
|
.arg("-o")
|
|
|
|
.arg(&global_asm_object_file)
|
2020-07-09 09:55:38 -05:00
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("Failed to spawn `as`.");
|
2021-03-05 12:12:59 -06:00
|
|
|
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
|
2020-07-09 09:55:38 -05:00
|
|
|
let status = child.wait().expect("Failed to wait for `as`.");
|
|
|
|
if !status.success() {
|
2021-03-05 12:12:59 -06:00
|
|
|
tcx.sess.fatal(&format!("Failed to assemble `{}`", global_asm));
|
2020-07-09 09:55:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link the global asm and main object file together
|
|
|
|
let main_object_file = add_file_stem_postfix(output_object_file.clone(), ".main");
|
|
|
|
std::fs::rename(&output_object_file, &main_object_file).unwrap();
|
2020-07-09 11:55:46 -05:00
|
|
|
let status = Command::new(linker)
|
2020-07-09 09:55:38 -05:00
|
|
|
.arg("-r") // Create a new object file
|
2020-08-28 05:10:48 -05:00
|
|
|
.arg("-o")
|
|
|
|
.arg(output_object_file)
|
2020-07-09 09:55:38 -05:00
|
|
|
.arg(&main_object_file)
|
|
|
|
.arg(&global_asm_object_file)
|
|
|
|
.status()
|
|
|
|
.unwrap();
|
|
|
|
if !status.success() {
|
|
|
|
tcx.sess.fatal(&format!(
|
|
|
|
"Failed to link `{}` and `{}` together",
|
|
|
|
main_object_file.display(),
|
|
|
|
global_asm_object_file.display(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fs::remove_file(global_asm_object_file).unwrap();
|
|
|
|
std::fs::remove_file(main_object_file).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_file_stem_postfix(mut path: PathBuf, postfix: &str) -> PathBuf {
|
|
|
|
let mut new_filename = path.file_stem().unwrap().to_owned();
|
|
|
|
new_filename.push(postfix);
|
|
|
|
if let Some(extension) = path.extension() {
|
|
|
|
new_filename.push(".");
|
|
|
|
new_filename.push(extension);
|
|
|
|
}
|
|
|
|
path.set_file_name(new_filename);
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:48:17 -05: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 12:12:59 -06:00
|
|
|
if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
|
2020-03-12 05:48:17 -05: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 16:53:42 -06:00
|
|
|
if tcx.try_mark_green(&dep_node) { CguReuse::PreLto } else { CguReuse::No }
|
2020-03-12 05:48:17 -05:00
|
|
|
}
|