]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_cranelift/src/driver/mod.rs
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / compiler / rustc_codegen_cranelift / src / driver / mod.rs
1 //! Drivers are responsible for calling [`codegen_fn`] or [`codegen_static`] for each mono item and
2 //! performing any further actions like JIT executing or writing object files.
3 //!
4 //! [`codegen_fn`]: crate::base::codegen_fn
5 //! [`codegen_static`]: crate::constant::codegen_static
6
7 use rustc_data_structures::profiling::SelfProfilerRef;
8 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
9 use rustc_middle::mir::mono::{MonoItem, MonoItemData};
10
11 use crate::prelude::*;
12
13 pub(crate) mod aot;
14 #[cfg(feature = "jit")]
15 pub(crate) mod jit;
16
17 fn predefine_mono_items<'tcx>(
18 tcx: TyCtxt<'tcx>,
19 module: &mut dyn Module,
20 mono_items: &[(MonoItem<'tcx>, MonoItemData)],
21 ) {
22 tcx.prof.generic_activity("predefine functions").run(|| {
23 let is_compiler_builtins = tcx.is_compiler_builtins(LOCAL_CRATE);
24 for &(mono_item, data) in mono_items {
25 match mono_item {
26 MonoItem::Fn(instance) => {
27 let name = tcx.symbol_name(instance).name;
28 let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name));
29 let sig =
30 get_function_sig(tcx, module.target_config().default_call_conv, instance);
31 let linkage = crate::linkage::get_clif_linkage(
32 mono_item,
33 data.linkage,
34 data.visibility,
35 is_compiler_builtins,
36 );
37 let is_naked = tcx
38 .codegen_fn_attrs(instance.def_id())
39 .flags
40 .contains(CodegenFnAttrFlags::NAKED);
41 module
42 .declare_function(
43 name,
44 // Naked functions are defined in a separate object
45 // file from the codegen unit rustc expects them to
46 // be defined in.
47 if is_naked { Linkage::Import } else { linkage },
48 &sig,
49 )
50 .unwrap();
51 }
52 MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
53 }
54 }
55 });
56 }
57
58 struct MeasuremeProfiler(SelfProfilerRef);
59
60 struct TimingGuard {
61 profiler: std::mem::ManuallyDrop<SelfProfilerRef>,
62 inner: Option<rustc_data_structures::profiling::TimingGuard<'static>>,
63 }
64
65 impl Drop for TimingGuard {
66 fn drop(&mut self) {
67 self.inner.take();
68 unsafe {
69 std::mem::ManuallyDrop::drop(&mut self.profiler);
70 }
71 }
72 }
73
74 impl cranelift_codegen::timing::Profiler for MeasuremeProfiler {
75 fn start_pass(&self, pass: cranelift_codegen::timing::Pass) -> Box<dyn std::any::Any> {
76 let mut timing_guard =
77 TimingGuard { profiler: std::mem::ManuallyDrop::new(self.0.clone()), inner: None };
78 timing_guard.inner = Some(
79 unsafe { &*(&*timing_guard.profiler as &SelfProfilerRef as *const SelfProfilerRef) }
80 .generic_activity(pass.description()),
81 );
82 Box::new(timing_guard)
83 }
84 }