1 use super::write::CodegenContext;
3 use crate::ModuleCodegen;
5 use rustc_errors::FatalError;
10 pub struct ThinModule<B: WriteBackendMethods> {
11 pub shared: Arc<ThinShared<B>>,
15 impl<B: WriteBackendMethods> ThinModule<B> {
16 pub fn name(&self) -> &str {
17 self.shared.module_names[self.idx].to_str().unwrap()
20 pub fn cost(&self) -> u64 {
21 // Yes, that's correct, we're using the size of the bytecode as an
22 // indicator for how costly this codegen unit is.
23 self.data().len() as u64
26 pub fn data(&self) -> &[u8] {
27 let a = self.shared.thin_buffers.get(self.idx).map(|b| b.data());
29 let len = self.shared.thin_buffers.len();
30 self.shared.serialized_modules[self.idx - len].data()
35 pub struct ThinShared<B: WriteBackendMethods> {
36 pub data: B::ThinData,
37 pub thin_buffers: Vec<B::ThinBuffer>,
38 pub serialized_modules: Vec<SerializedModule<B::ModuleBuffer>>,
39 pub module_names: Vec<CString>,
43 pub enum LtoModuleCodegen<B: WriteBackendMethods> {
45 module: Option<ModuleCodegen<B::Module>>,
46 _serialized_bitcode: Vec<SerializedModule<B::ModuleBuffer>>,
52 impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
53 pub fn name(&self) -> &str {
55 LtoModuleCodegen::Fat { .. } => "everything",
56 LtoModuleCodegen::Thin(ref m) => m.name(),
60 /// Optimize this module within the given codegen context.
62 /// This function is unsafe as it'll return a `ModuleCodegen` still
63 /// points to LLVM data structures owned by this `LtoModuleCodegen`.
64 /// It's intended that the module returned is immediately code generated and
65 /// dropped, and then this LTO module is dropped.
66 pub unsafe fn optimize(
68 cgcx: &CodegenContext<B>,
69 ) -> Result<ModuleCodegen<B::Module>, FatalError> {
71 LtoModuleCodegen::Fat { ref mut module, .. } => {
72 let module = module.take().unwrap();
74 let config = cgcx.config(module.kind);
75 B::run_lto_pass_manager(cgcx, &module, config, false);
79 LtoModuleCodegen::Thin(ref mut thin) => B::optimize_thin(cgcx, thin),
83 /// A "gauge" of how costly it is to optimize this module, used to sort
84 /// biggest modules first.
85 pub fn cost(&self) -> u64 {
87 // Only one module with fat LTO, so the cost doesn't matter.
88 LtoModuleCodegen::Fat { .. } => 0,
89 LtoModuleCodegen::Thin(ref m) => m.cost(),
95 pub enum SerializedModule<M: ModuleBufferMethods> {
98 FromUncompressedFile(memmap::Mmap),
101 impl<M: ModuleBufferMethods> SerializedModule<M> {
102 pub fn data(&self) -> &[u8] {
104 SerializedModule::Local(ref m) => m.data(),
105 SerializedModule::FromRlib(ref m) => m,
106 SerializedModule::FromUncompressedFile(ref m) => m,