1 //! Drivers are responsible for calling [`codegen_mono_item`] and performing any further actions
2 //! like JIT executing or writing object files.
6 use rustc_middle
::middle
::cstore
::EncodedMetadata
;
7 use rustc_middle
::mir
::mono
::{Linkage as RLinkage, MonoItem, Visibility}
;
10 use crate::CodegenMode
;
13 #[cfg(feature = "jit")]
16 pub(crate) fn codegen_crate(
18 metadata
: EncodedMetadata
,
19 need_metadata_module
: bool
,
20 backend_config
: crate::BackendConfig
,
22 tcx
.sess
.abort_if_errors();
24 match backend_config
.codegen_mode
{
25 CodegenMode
::Aot
=> aot
::run_aot(tcx
, backend_config
, metadata
, need_metadata_module
),
26 CodegenMode
::Jit
| CodegenMode
::JitLazy
=> {
28 tcx
.sess
.crate_types().contains(&rustc_session
::config
::CrateType
::Executable
);
30 tcx
.sess
.fatal("can't jit non-executable crate");
33 #[cfg(feature = "jit")]
34 let _
: ! = jit
::run_jit(tcx
, backend_config
);
36 #[cfg(not(feature = "jit"))]
37 tcx
.sess
.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
42 fn predefine_mono_items
<'tcx
>(
43 cx
: &mut crate::CodegenCx
<'_
, 'tcx
>,
44 mono_items
: &[(MonoItem
<'tcx
>, (RLinkage
, Visibility
))],
46 cx
.tcx
.sess
.time("predefine functions", || {
47 let is_compiler_builtins
= cx
.tcx
.is_compiler_builtins(LOCAL_CRATE
);
48 for &(mono_item
, (linkage
, visibility
)) in mono_items
{
50 MonoItem
::Fn(instance
) => {
51 let name
= cx
.tcx
.symbol_name(instance
).name
.to_string();
52 let _inst_guard
= crate::PrintOnPanic(|| format
!("{:?} {}", instance
, name
));
53 let sig
= get_function_sig(cx
.tcx
, cx
.module
.isa().triple(), instance
);
54 let linkage
= crate::linkage
::get_clif_linkage(
60 cx
.module
.declare_function(&name
, linkage
, &sig
).unwrap();
62 MonoItem
::Static(_
) | MonoItem
::GlobalAsm(_
) => {}
68 fn time
<R
>(tcx
: TyCtxt
<'_
>, name
: &'
static str, f
: impl FnOnce() -> R
) -> R
{
69 if std
::env
::var("CG_CLIF_DISPLAY_CG_TIME").as_ref().map(|val
| &**val
) == Ok("1") {
70 println
!("[{:<30}: {}] start", tcx
.crate_name(LOCAL_CRATE
), name
);
71 let before
= std
::time
::Instant
::now();
72 let res
= tcx
.sess
.time(name
, f
);
73 let after
= std
::time
::Instant
::now();
74 println
!("[{:<30}: {}] end time: {:?}", tcx
.crate_name(LOCAL_CRATE
), name
, after
- before
);
77 tcx
.sess
.time(name
, f
)