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        target_features: &[String],
36    ) -> TargetMachineFactoryFn<Self>;
37    /// Performs fat LTO by merging all modules into a single one, running autodiff
38    /// if necessary and running any further optimizations
39    fn optimize_and_codegen_fat_lto(
40        sess: &Session,
41        cgcx: &CodegenContext,
42        shared_emitter: &SharedEmitter,
43        tm_factory: TargetMachineFactoryFn<Self>,
44        exported_symbols_for_lto: &[String],
45        each_linked_rlib_for_lto: &[PathBuf],
46        modules: Vec<FatLtoInput<Self>>,
47    ) -> CompiledModule;
48    /// Performs thin LTO by performing necessary global analysis and returning two
49    /// lists, one of the modules that need optimization and another for modules that
50    /// can simply be copied over from the incr. comp. cache.
51    fn run_thin_lto(
52        cgcx: &CodegenContext,
53        prof: &SelfProfilerRef,
54        dcx: DiagCtxtHandle<'_>,
55        exported_symbols_for_lto: &[String],
56        each_linked_rlib_for_lto: &[PathBuf],
57        modules: Vec<ThinLtoInput<Self>>,
58    ) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>);
59    fn optimize(
60        cgcx: &CodegenContext,
61        prof: &SelfProfilerRef,
62        shared_emitter: &SharedEmitter,
63        module: &mut ModuleCodegen<Self::Module>,
64        config: &ModuleConfig,
65    );
66    fn optimize_and_codegen_thin(
67        cgcx: &CodegenContext,
68        prof: &SelfProfilerRef,
69        shared_emitter: &SharedEmitter,
70        tm_factory: TargetMachineFactoryFn<Self>,
71        thin: ThinModule<Self>,
72    ) -> CompiledModule;
73    fn codegen(
74        cgcx: &CodegenContext,
75        prof: &SelfProfilerRef,
76        shared_emitter: &SharedEmitter,
77        module: ModuleCodegen<Self::Module>,
78        config: &ModuleConfig,
79    ) -> CompiledModule;
80    fn serialize_module(module: Self::Module, is_thin: bool) -> Self::ModuleBuffer;
81}
82
83pub trait ModuleBufferMethods: Send + Sync {
84    fn data(&self) -> &[u8];
85}
86
87impl ModuleBufferMethods for Infallible {
88    fn data(&self) -> &[u8] {
89        match *self {}
90    }
91}