rustc_codegen_ssa/back/
lto.rs1use 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 self.data().len() as u64
32 }
33
34 pub fn data(&self) -> &[u8] {
35 self.shared.modules[self.idx].data()
36 }
37}
38
39pub struct ThinShared<B: WriteBackendMethods> {
40 pub data: B::ThinData,
41 pub modules: Vec<SerializedModule<B::ModuleBuffer>>,
42 pub module_names: Vec<CString>,
43}
44
45pub enum SerializedModule<M: ModuleBufferMethods> {
46 Local(M),
47 FromRlib(Vec<u8>),
48 FromUncompressedFile(Mmap),
49}
50
51impl<M: ModuleBufferMethods> SerializedModule<M> {
52 pub fn data(&self) -> &[u8] {
53 match *self {
54 SerializedModule::Local(ref m) => m.data(),
55 SerializedModule::FromRlib(ref m) => m,
56 SerializedModule::FromUncompressedFile(ref m) => m,
57 }
58 }
59}
60
61fn crate_type_allows_lto(crate_type: CrateType) -> bool {
62 match crate_type {
63 CrateType::Executable
64 | CrateType::Dylib
65 | CrateType::StaticLib
66 | CrateType::Cdylib
67 | CrateType::ProcMacro
68 | CrateType::Sdylib => true,
69 CrateType::Rlib => false,
70 }
71}
72
73pub(super) fn exported_symbols_for_lto(
74 tcx: TyCtxt<'_>,
75 each_linked_rlib_for_lto: &[CrateNum],
76) -> Vec<String> {
77 let export_threshold = match tcx.sess.lto() {
78 Lto::ThinLocal => SymbolExportLevel::Rust,
80
81 Lto::Fat | Lto::Thin => symbol_export::crates_export_threshold(&tcx.crate_types()),
83
84 Lto::No => return ::alloc::vec::Vec::new()vec![],
85 };
86
87 let copy_symbols = |cnum| {
88 tcx.exported_non_generic_symbols(cnum)
89 .iter()
90 .chain(tcx.exported_generic_symbols(cnum))
91 .filter_map(|&(s, info): &(ExportedSymbol<'_>, SymbolExportInfo)| {
92 if info.level.is_below_threshold(export_threshold) || info.used {
93 Some(symbol_name_for_instance_in_crate(tcx, s, cnum))
94 } else {
95 None
96 }
97 })
98 .collect::<Vec<_>>()
99 };
100 let mut symbols_below_threshold = {
101 let _timer = tcx.prof.generic_activity("lto_generate_symbols_below_threshold");
102 copy_symbols(LOCAL_CRATE)
103 };
104 {
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:104",
"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(104u32),
::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());
105
106 for &cnum in each_linked_rlib_for_lto {
109 let _timer = tcx.prof.generic_activity("lto_generate_symbols_below_threshold");
110 symbols_below_threshold.extend(copy_symbols(cnum));
111 }
112
113 if export_threshold == SymbolExportLevel::Rust
115 && let Some(kind) = allocator_kind_for_codegen(tcx)
116 {
117 symbols_below_threshold.extend(allocator_shim_symbols(tcx, kind).map(|(name, _kind)| name));
118 }
119
120 symbols_below_threshold
121}
122
123pub(super) fn check_lto_allowed(cgcx: &CodegenContext, dcx: DiagCtxtHandle<'_>) {
124 if cgcx.lto == Lto::ThinLocal {
125 return;
127 }
128
129 for crate_type in cgcx.crate_types.iter() {
131 if !crate_type_allows_lto(*crate_type) {
132 dcx.handle().emit_fatal(LtoDisallowed);
133 } else if *crate_type == CrateType::Dylib {
134 if !cgcx.dylib_lto {
135 dcx.handle().emit_fatal(LtoDylib);
136 }
137 } else if *crate_type == CrateType::ProcMacro && !cgcx.dylib_lto {
138 dcx.handle().emit_fatal(LtoProcMacro);
139 }
140 }
141
142 if cgcx.prefer_dynamic && !cgcx.dylib_lto {
143 dcx.handle().emit_fatal(DynamicLinkingWithLTO);
144 }
145}