]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/driver/mod.rs
Rollup merge of #82686 - CDirkx:unix-platform, r=m-ou-se
[rust.git] / compiler / rustc_codegen_cranelift / src / driver / mod.rs
1 //! Drivers are responsible for calling [`codegen_mono_item`] and performing any further actions
2 //! like JIT executing or writing object files.
3
4 use std::any::Any;
5
6 use rustc_middle::middle::cstore::EncodedMetadata;
7 use rustc_middle::mir::mono::{Linkage as RLinkage, MonoItem, Visibility};
8
9 use crate::prelude::*;
10 use crate::CodegenMode;
11
12 mod aot;
13 #[cfg(feature = "jit")]
14 mod jit;
15
16 pub(crate) fn codegen_crate(
17     tcx: TyCtxt<'_>,
18     metadata: EncodedMetadata,
19     need_metadata_module: bool,
20     backend_config: crate::BackendConfig,
21 ) -> Box<dyn Any> {
22     tcx.sess.abort_if_errors();
23
24     match backend_config.codegen_mode {
25         CodegenMode::Aot => aot::run_aot(tcx, backend_config, metadata, need_metadata_module),
26         CodegenMode::Jit | CodegenMode::JitLazy => {
27             let is_executable =
28                 tcx.sess.crate_types().contains(&rustc_session::config::CrateType::Executable);
29             if !is_executable {
30                 tcx.sess.fatal("can't jit non-executable crate");
31             }
32
33             #[cfg(feature = "jit")]
34             let _: ! = jit::run_jit(tcx, backend_config);
35
36             #[cfg(not(feature = "jit"))]
37             tcx.sess.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
38         }
39     }
40 }
41
42 fn predefine_mono_items<'tcx>(
43     cx: &mut crate::CodegenCx<'_, 'tcx>,
44     mono_items: &[(MonoItem<'tcx>, (RLinkage, Visibility))],
45 ) {
46     cx.tcx.sess.time("predefine functions", || {
47         for &(mono_item, (linkage, visibility)) in mono_items {
48             match mono_item {
49                 MonoItem::Fn(instance) => {
50                     let name = cx.tcx.symbol_name(instance).name.to_string();
51                     let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name));
52                     let sig = get_function_sig(cx.tcx, cx.module.isa().triple(), instance);
53                     let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
54                     cx.module.declare_function(&name, linkage, &sig).unwrap();
55                 }
56                 MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
57             }
58         }
59     });
60 }
61
62 fn time<R>(tcx: TyCtxt<'_>, name: &'static str, f: impl FnOnce() -> R) -> R {
63     if std::env::var("CG_CLIF_DISPLAY_CG_TIME").as_ref().map(|val| &**val) == Ok("1") {
64         println!("[{:<30}: {}] start", tcx.crate_name(LOCAL_CRATE), name);
65         let before = std::time::Instant::now();
66         let res = tcx.sess.time(name, f);
67         let after = std::time::Instant::now();
68         println!("[{:<30}: {}] end time: {:?}", tcx.crate_name(LOCAL_CRATE), name, after - before);
69         res
70     } else {
71         tcx.sess.time(name, f)
72     }
73 }