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