Skip to main content

rustc_codegen_ssa/traits/
write.rs

1use std::any::Any;
2use std::convert::Infallible;
3use std::path::PathBuf;
4
5use rustc_data_structures::profiling::SelfProfilerRef;
6use rustc_errors::DiagCtxtHandle;
7use rustc_middle::dep_graph::WorkProduct;
8use rustc_session::{Session, config};
9
10use crate::back::lto::ThinModule;
11use crate::back::write::{
12    CodegenContext, FatLtoInput, ModuleConfig, SharedEmitter, TargetMachineFactoryFn, ThinLtoInput,
13};
14use crate::{CompiledModule, ModuleCodegen};
15
16pub trait WriteBackendMethods: Clone + 'static {
17    type Module: Send + Sync;
18    type TargetMachine;
19    type ModuleBuffer: ModuleBufferMethods;
20    type ThinData: Send + Sync;
21
22    /// Returns `true` if this backend can be safely called from multiple threads.
23    ///
24    /// Defaults to `true`.
25    fn supports_parallel(&self) -> bool {
26        true
27    }
28    fn thread_profiler() -> Box<dyn Any> {
29        Box::new(())
30    }
31    fn target_machine_factory(
32        &self,
33        sess: &Session,
34        opt_level: config::OptLevel,
35    ) -> TargetMachineFactoryFn<Self>;
36    /// Performs fat LTO by merging all modules into a single one, running autodiff
37    /// if necessary and running any further optimizations
38    fn optimize_and_codegen_fat_lto(
39        sess: &Session,
40        cgcx: &CodegenContext,
41        shared_emitter: &SharedEmitter,
42        tm_factory: TargetMachineFactoryFn<Self>,
43        exported_symbols_for_lto: &[String],
44        each_linked_rlib_for_lto: &[PathBuf],
45        modules: Vec<FatLtoInput<Self>>,
46    ) -> CompiledModule;
47    /// Performs thin LTO by performing necessary global analysis and returning two
48    /// lists, one of the modules that need optimization and another for modules that
49    /// can simply be copied over from the incr. comp. cache.
50    fn run_thin_lto(
51        cgcx: &CodegenContext,
52        prof: &SelfProfilerRef,
53        dcx: DiagCtxtHandle<'_>,
54        exported_symbols_for_lto: &[String],
55        each_linked_rlib_for_lto: &[PathBuf],
56        modules: Vec<ThinLtoInput<Self>>,
57    ) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>);
58    fn optimize(
59        cgcx: &CodegenContext,
60        prof: &SelfProfilerRef,
61        shared_emitter: &SharedEmitter,
62        module: &mut ModuleCodegen<Self::Module>,
63        config: &ModuleConfig,
64    );
65    fn optimize_and_codegen_thin(
66        cgcx: &CodegenContext,
67        prof: &SelfProfilerRef,
68        shared_emitter: &SharedEmitter,
69        tm_factory: TargetMachineFactoryFn<Self>,
70        thin: ThinModule<Self>,
71    ) -> CompiledModule;
72    fn codegen(
73        cgcx: &CodegenContext,
74        prof: &SelfProfilerRef,
75        shared_emitter: &SharedEmitter,
76        module: ModuleCodegen<Self::Module>,
77        config: &ModuleConfig,
78    ) -> CompiledModule;
79    fn serialize_module(module: Self::Module, is_thin: bool) -> Self::ModuleBuffer;
80}
81
82pub trait ModuleBufferMethods: Send + Sync {
83    fn data(&self) -> &[u8];
84}
85
86impl ModuleBufferMethods for Infallible {
87    fn data(&self) -> &[u8] {
88        match *self {}
89    }
90}