Skip to main content

rustc_codegen_ssa/back/
lto.rs

1use std::ffi::CString;
2use std::sync::Arc;
3
4use rustc_data_structures::memmap::Mmap;
5use rustc_errors::DiagCtxtHandle;
6use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
7use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportLevel};
8use rustc_middle::ty::TyCtxt;
9use rustc_session::config::{CrateType, Lto};
10use tracing::info;
11
12use crate::back::symbol_export::{self, allocator_shim_symbols, symbol_name_for_instance_in_crate};
13use crate::back::write::CodegenContext;
14use crate::base::allocator_kind_for_codegen;
15use crate::errors::{DynamicLinkingWithLTO, LtoDisallowed, LtoDylib, LtoProcMacro};
16use crate::traits::*;
17
18pub struct ThinModule<B: WriteBackendMethods> {
19    pub shared: Arc<ThinShared<B>>,
20    pub idx: usize,
21}
22
23impl<B: WriteBackendMethods> ThinModule<B> {
24    pub fn name(&self) -> &str {
25        self.shared.module_names[self.idx].to_str().unwrap()
26    }
27
28    pub fn cost(&self) -> u64 {
29        // Yes, that's correct, we're using the size of the bytecode as an
30        // indicator for how costly this codegen unit is.
31        self.data().len() as u64
32    }
33
34    pub fn data(&self) -> &[u8] {
35        let a = self.shared.thin_buffers.get(self.idx).map(|b| b.data());
36        a.unwrap_or_else(|| {
37            let len = self.shared.thin_buffers.len();
38            self.shared.serialized_modules[self.idx - len].data()
39        })
40    }
41}
42
43pub struct ThinShared<B: WriteBackendMethods> {
44    pub data: B::ThinData,
45    pub thin_buffers: Vec<B::ThinBuffer>,
46    pub serialized_modules: Vec<SerializedModule<B::ModuleBuffer>>,
47    pub module_names: Vec<CString>,
48}
49
50pub enum SerializedModule<M: ModuleBufferMethods> {
51    Local(M),
52    FromRlib(Vec<u8>),
53    FromUncompressedFile(Mmap),
54}
55
56impl<M: ModuleBufferMethods> SerializedModule<M> {
57    pub fn data(&self) -> &[u8] {
58        match *self {
59            SerializedModule::Local(ref m) => m.data(),
60            SerializedModule::FromRlib(ref m) => m,
61            SerializedModule::FromUncompressedFile(ref m) => m,
62        }
63    }
64}
65
66fn crate_type_allows_lto(crate_type: CrateType) -> bool {
67    match crate_type {
68        CrateType::Executable
69        | CrateType::Dylib
70        | CrateType::StaticLib
71        | CrateType::Cdylib
72        | CrateType::ProcMacro
73        | CrateType::Sdylib => true,
74        CrateType::Rlib => false,
75    }
76}
77
78pub(super) fn exported_symbols_for_lto(
79    tcx: TyCtxt<'_>,
80    each_linked_rlib_for_lto: &[CrateNum],
81) -> Vec<String> {
82    let export_threshold = match tcx.sess.lto() {
83        // We're just doing LTO for our one crate
84        Lto::ThinLocal => SymbolExportLevel::Rust,
85
86        // We're doing LTO for the entire crate graph
87        Lto::Fat | Lto::Thin => symbol_export::crates_export_threshold(&tcx.crate_types()),
88
89        Lto::No => return ::alloc::vec::Vec::new()vec![],
90    };
91
92    let copy_symbols = |cnum| {
93        tcx.exported_non_generic_symbols(cnum)
94            .iter()
95            .chain(tcx.exported_generic_symbols(cnum))
96            .filter_map(|&(s, info): &(ExportedSymbol<'_>, SymbolExportInfo)| {
97                if info.level.is_below_threshold(export_threshold) || info.used {
98                    Some(symbol_name_for_instance_in_crate(tcx, s, cnum))
99                } else {
100                    None
101                }
102            })
103            .collect::<Vec<_>>()
104    };
105    let mut symbols_below_threshold = {
106        let _timer = tcx.prof.generic_activity("lto_generate_symbols_below_threshold");
107        copy_symbols(LOCAL_CRATE)
108    };
109    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/lto.rs:109",
                        "rustc_codegen_ssa::back::lto", ::tracing::Level::INFO,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/lto.rs"),
                        ::tracing_core::__macro_support::Option::Some(109u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::lto"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("{0} symbols to preserve in this crate",
                                                    symbols_below_threshold.len()) as &dyn Value))])
            });
    } else { ; }
};info!("{} symbols to preserve in this crate", symbols_below_threshold.len());
110
111    // If we're performing LTO for the entire crate graph, then for each of our
112    // upstream dependencies, include their exported symbols.
113    if tcx.sess.lto() != Lto::ThinLocal {
114        for &cnum in each_linked_rlib_for_lto {
115            let _timer = tcx.prof.generic_activity("lto_generate_symbols_below_threshold");
116            symbols_below_threshold.extend(copy_symbols(cnum));
117        }
118    }
119
120    // Mark allocator shim symbols as exported only if they were generated.
121    if export_threshold == SymbolExportLevel::Rust
122        && let Some(kind) = allocator_kind_for_codegen(tcx)
123    {
124        symbols_below_threshold.extend(allocator_shim_symbols(tcx, kind).map(|(name, _kind)| name));
125    }
126
127    symbols_below_threshold
128}
129
130pub(super) fn check_lto_allowed<B: WriteBackendMethods>(
131    cgcx: &CodegenContext<B>,
132    dcx: DiagCtxtHandle<'_>,
133) {
134    if cgcx.lto == Lto::ThinLocal {
135        // Crate local LTO is always allowed
136        return;
137    }
138
139    // Make sure we actually can run LTO
140    for crate_type in cgcx.crate_types.iter() {
141        if !crate_type_allows_lto(*crate_type) {
142            dcx.handle().emit_fatal(LtoDisallowed);
143        } else if *crate_type == CrateType::Dylib {
144            if !cgcx.dylib_lto {
145                dcx.handle().emit_fatal(LtoDylib);
146            }
147        } else if *crate_type == CrateType::ProcMacro && !cgcx.dylib_lto {
148            dcx.handle().emit_fatal(LtoProcMacro);
149        }
150    }
151
152    if cgcx.prefer_dynamic && !cgcx.dylib_lto {
153        dcx.handle().emit_fatal(DynamicLinkingWithLTO);
154    }
155}