2020-09-23 08:13:49 -05:00
|
|
|
//! Drivers are responsible for calling [`codegen_mono_items`] and performing any further actions
|
|
|
|
//! like JIT executing or writing object files.
|
|
|
|
|
2019-05-04 09:54:25 -05:00
|
|
|
use std::any::Any;
|
|
|
|
|
2020-03-31 06:20:19 -05:00
|
|
|
use rustc_middle::middle::cstore::EncodedMetadata;
|
|
|
|
use rustc_middle::mir::mono::{Linkage as RLinkage, MonoItem, Visibility};
|
2019-05-04 09:54:25 -05:00
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-03-12 05:48:17 -05:00
|
|
|
mod aot;
|
2020-07-09 07:23:00 -05:00
|
|
|
#[cfg(feature = "jit")]
|
2020-03-12 05:44:27 -05:00
|
|
|
mod jit;
|
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn codegen_crate(
|
2019-06-16 04:13:49 -05:00
|
|
|
tcx: TyCtxt<'_>,
|
2019-05-04 09:54:25 -05:00
|
|
|
metadata: EncodedMetadata,
|
2019-05-11 05:23:40 -05:00
|
|
|
need_metadata_module: bool,
|
2020-09-29 11:41:59 -05:00
|
|
|
config: crate::BackendConfig,
|
2019-05-04 09:54:25 -05:00
|
|
|
) -> Box<dyn Any> {
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
2020-09-29 11:41:59 -05:00
|
|
|
if config.use_jit {
|
|
|
|
let is_executable = tcx
|
2020-08-28 05:10:48 -05:00
|
|
|
.sess
|
|
|
|
.crate_types()
|
2020-09-29 11:41:59 -05:00
|
|
|
.contains(&rustc_session::config::CrateType::Executable);
|
|
|
|
if !is_executable {
|
|
|
|
tcx.sess.fatal("can't jit non-executable crate");
|
|
|
|
}
|
|
|
|
|
2020-07-09 07:23:00 -05:00
|
|
|
#[cfg(feature = "jit")]
|
2020-03-12 05:44:27 -05:00
|
|
|
let _: ! = jit::run_jit(tcx);
|
2019-05-11 05:23:40 -05:00
|
|
|
|
2020-07-09 07:23:00 -05:00
|
|
|
#[cfg(not(feature = "jit"))]
|
2020-08-28 05:10:48 -05:00
|
|
|
tcx.sess
|
|
|
|
.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
|
2019-05-11 05:23:40 -05:00
|
|
|
}
|
|
|
|
|
2020-03-12 05:48:17 -05:00
|
|
|
aot::run_aot(tcx, metadata, need_metadata_module)
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn codegen_mono_items<'tcx>(
|
2020-10-01 03:38:23 -05:00
|
|
|
cx: &mut crate::CodegenCx<'tcx, impl Module>,
|
2020-03-09 05:21:40 -05:00
|
|
|
mono_items: Vec<(MonoItem<'tcx>, (RLinkage, Visibility))>,
|
2019-05-04 09:54:25 -05:00
|
|
|
) {
|
2020-08-22 08:49:16 -05:00
|
|
|
cx.tcx.sess.time("predefine functions", || {
|
2020-03-09 05:21:40 -05:00
|
|
|
for &(mono_item, (linkage, visibility)) in &mono_items {
|
2020-03-07 05:16:32 -06:00
|
|
|
match mono_item {
|
|
|
|
MonoItem::Fn(instance) => {
|
2020-08-28 05:10:48 -05:00
|
|
|
let (name, sig) = get_function_name_and_sig(
|
|
|
|
cx.tcx,
|
|
|
|
cx.module.isa().triple(),
|
|
|
|
instance,
|
|
|
|
false,
|
|
|
|
);
|
2020-03-07 05:16:32 -06:00
|
|
|
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
|
2020-08-22 08:49:16 -05:00
|
|
|
cx.module.declare_function(&name, linkage, &sig).unwrap();
|
2019-10-04 07:39:14 -05:00
|
|
|
}
|
2020-03-07 05:16:32 -06:00
|
|
|
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
|
2019-10-04 07:39:14 -05:00
|
|
|
}
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
});
|
2020-03-07 05:16:32 -06:00
|
|
|
|
|
|
|
for (mono_item, (linkage, visibility)) in mono_items {
|
2020-03-17 10:26:56 -05:00
|
|
|
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
|
2020-11-03 04:00:04 -06:00
|
|
|
codegen_mono_item(cx, mono_item, linkage);
|
2020-03-07 05:16:32 -06:00
|
|
|
}
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
|
2020-11-03 04:00:04 -06:00
|
|
|
fn codegen_mono_item<'tcx, M: Module>(
|
2020-10-01 03:38:23 -05:00
|
|
|
cx: &mut crate::CodegenCx<'tcx, M>,
|
2019-05-04 09:54:25 -05:00
|
|
|
mono_item: MonoItem<'tcx>,
|
|
|
|
linkage: Linkage,
|
|
|
|
) {
|
2020-08-22 08:49:16 -05:00
|
|
|
let tcx = cx.tcx;
|
2019-05-04 09:54:25 -05:00
|
|
|
match mono_item {
|
|
|
|
MonoItem::Fn(inst) => {
|
|
|
|
let _inst_guard =
|
2020-07-17 12:15:33 -05:00
|
|
|
crate::PrintOnPanic(|| format!("{:?} {}", inst, tcx.symbol_name(inst).name));
|
2019-05-04 09:54:25 -05:00
|
|
|
debug_assert!(!inst.substs.needs_infer());
|
2020-08-28 05:10:48 -05:00
|
|
|
tcx.sess
|
2020-11-03 04:00:04 -06:00
|
|
|
.time("codegen fn", || crate::base::codegen_fn(cx, inst, linkage));
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
MonoItem::Static(def_id) => {
|
2020-08-22 08:25:36 -05:00
|
|
|
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
2020-01-25 10:24:45 -06:00
|
|
|
MonoItem::GlobalAsm(hir_id) => {
|
2020-07-09 12:24:53 -05:00
|
|
|
let item = tcx.hir().expect_item(hir_id);
|
|
|
|
if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind {
|
|
|
|
cx.global_asm.push_str(&*asm.as_str());
|
|
|
|
cx.global_asm.push_str("\n\n");
|
|
|
|
} else {
|
|
|
|
bug!("Expected GlobalAsm found {:?}", item);
|
|
|
|
}
|
2020-01-25 10:24:45 -06:00
|
|
|
}
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:40:42 -05:00
|
|
|
fn time<R>(tcx: TyCtxt<'_>, name: &'static str, f: impl FnOnce() -> R) -> R {
|
2020-08-28 05:10:48 -05:00
|
|
|
if std::env::var("CG_CLIF_DISPLAY_CG_TIME")
|
|
|
|
.as_ref()
|
|
|
|
.map(|val| &**val)
|
|
|
|
== Ok("1")
|
|
|
|
{
|
2020-03-12 05:40:42 -05:00
|
|
|
println!("[{:<30}: {}] start", tcx.crate_name(LOCAL_CRATE), name);
|
2020-03-12 05:17:19 -05:00
|
|
|
let before = std::time::Instant::now();
|
2020-03-12 05:40:42 -05:00
|
|
|
let res = tcx.sess.time(name, f);
|
2020-03-12 05:17:19 -05:00
|
|
|
let after = std::time::Instant::now();
|
2020-08-28 05:10:48 -05:00
|
|
|
println!(
|
|
|
|
"[{:<30}: {}] end time: {:?}",
|
|
|
|
tcx.crate_name(LOCAL_CRATE),
|
|
|
|
name,
|
|
|
|
after - before
|
|
|
|
);
|
2020-03-12 05:17:19 -05:00
|
|
|
res
|
|
|
|
} else {
|
2020-03-12 05:40:42 -05:00
|
|
|
tcx.sess.time(name, f)
|
2020-03-12 05:17:19 -05:00
|
|
|
}
|
2019-05-04 09:54:25 -05:00
|
|
|
}
|