]>
Commit | Line | Data |
---|---|---|
fc512014 | 1 | //! Drivers are responsible for calling [`codegen_mono_item`] and performing any further actions |
29967ef6 XL |
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::*; | |
5869c6ff | 10 | use crate::CodegenMode; |
29967ef6 XL |
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 | config: crate::BackendConfig, | |
21 | ) -> Box<dyn Any> { | |
22 | tcx.sess.abort_if_errors(); | |
23 | ||
5869c6ff XL |
24 | match config.codegen_mode { |
25 | CodegenMode::Aot => aot::run_aot(tcx, metadata, need_metadata_module), | |
26 | CodegenMode::Jit | CodegenMode::JitLazy => { | |
27 | let is_executable = tcx | |
28 | .sess | |
29 | .crate_types() | |
30 | .contains(&rustc_session::config::CrateType::Executable); | |
31 | if !is_executable { | |
32 | tcx.sess.fatal("can't jit non-executable crate"); | |
33 | } | |
29967ef6 | 34 | |
5869c6ff XL |
35 | #[cfg(feature = "jit")] |
36 | let _: ! = jit::run_jit(tcx, config.codegen_mode); | |
29967ef6 | 37 | |
5869c6ff XL |
38 | #[cfg(not(feature = "jit"))] |
39 | tcx.sess | |
40 | .fatal("jit support was disabled when compiling rustc_codegen_cranelift"); | |
41 | } | |
29967ef6 | 42 | } |
29967ef6 XL |
43 | } |
44 | ||
fc512014 | 45 | fn predefine_mono_items<'tcx>( |
29967ef6 | 46 | cx: &mut crate::CodegenCx<'tcx, impl Module>, |
fc512014 | 47 | mono_items: &[(MonoItem<'tcx>, (RLinkage, Visibility))], |
29967ef6 XL |
48 | ) { |
49 | cx.tcx.sess.time("predefine functions", || { | |
fc512014 | 50 | for &(mono_item, (linkage, visibility)) in mono_items { |
29967ef6 XL |
51 | match mono_item { |
52 | MonoItem::Fn(instance) => { | |
5869c6ff XL |
53 | let name = cx.tcx.symbol_name(instance).name.to_string(); |
54 | let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name)); | |
55 | let sig = get_function_sig(cx.tcx, cx.module.isa().triple(), instance); | |
29967ef6 XL |
56 | let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); |
57 | cx.module.declare_function(&name, linkage, &sig).unwrap(); | |
58 | } | |
59 | MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {} | |
60 | } | |
61 | } | |
62 | }); | |
29967ef6 XL |
63 | } |
64 | ||
29967ef6 XL |
65 | fn time<R>(tcx: TyCtxt<'_>, name: &'static str, f: impl FnOnce() -> R) -> R { |
66 | if std::env::var("CG_CLIF_DISPLAY_CG_TIME") | |
67 | .as_ref() | |
68 | .map(|val| &**val) | |
69 | == Ok("1") | |
70 | { | |
71 | println!("[{:<30}: {}] start", tcx.crate_name(LOCAL_CRATE), name); | |
72 | let before = std::time::Instant::now(); | |
73 | let res = tcx.sess.time(name, f); | |
74 | let after = std::time::Instant::now(); | |
75 | println!( | |
76 | "[{:<30}: {}] end time: {:?}", | |
77 | tcx.crate_name(LOCAL_CRATE), | |
78 | name, | |
79 | after - before | |
80 | ); | |
81 | res | |
82 | } else { | |
83 | tcx.sess.time(name, f) | |
84 | } | |
85 | } |