@@ -7,27 +7,24 @@ use std::sync::Arc;
77use std:: { io, iter, slice} ;
88
99use object:: read:: archive:: ArchiveFile ;
10- use rustc_codegen_ssa:: back:: lto:: { SerializedModule , ThinModule , ThinShared } ;
11- use rustc_codegen_ssa:: back:: symbol_export;
10+ use rustc_codegen_ssa:: back:: lto:: {
11+ SerializedModule , ThinModule , ThinShared , exported_symbols_for_lto,
12+ } ;
1213use rustc_codegen_ssa:: back:: write:: { CodegenContext , FatLtoInput } ;
1314use rustc_codegen_ssa:: traits:: * ;
1415use rustc_codegen_ssa:: { ModuleCodegen , ModuleKind , looks_like_rust_object_file} ;
1516use rustc_data_structures:: fx:: FxHashMap ;
1617use rustc_data_structures:: memmap:: Mmap ;
1718use rustc_errors:: { DiagCtxtHandle , FatalError } ;
18- use rustc_hir:: def_id:: LOCAL_CRATE ;
1919use rustc_middle:: bug;
2020use rustc_middle:: dep_graph:: WorkProduct ;
21- use rustc_middle:: middle:: exported_symbols:: { SymbolExportInfo , SymbolExportLevel } ;
22- use rustc_session:: config:: { self , CrateType , Lto } ;
21+ use rustc_session:: config:: { self , Lto } ;
2322use tracing:: { debug, info} ;
2423
2524use crate :: back:: write:: {
2625 self , CodegenDiagnosticsStage , DiagnosticHandlers , bitcode_section_name, save_temp_bitcode,
2726} ;
28- use crate :: errors:: {
29- DynamicLinkingWithLTO , LlvmError , LtoBitcodeFromRlib , LtoDisallowed , LtoDylib , LtoProcMacro ,
30- } ;
27+ use crate :: errors:: { LlvmError , LtoBitcodeFromRlib } ;
3128use crate :: llvm:: AttributePlace :: Function ;
3229use crate :: llvm:: { self , build_string} ;
3330use crate :: { LlvmCodegenBackend , ModuleLlvm , SimpleCx , attributes} ;
@@ -36,45 +33,19 @@ use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, attributes};
3633/// session to determine which CGUs we can reuse.
3734const THIN_LTO_KEYS_INCR_COMP_FILE_NAME : & str = "thin-lto-past-keys.bin" ;
3835
39- fn crate_type_allows_lto ( crate_type : CrateType ) -> bool {
40- match crate_type {
41- CrateType :: Executable
42- | CrateType :: Dylib
43- | CrateType :: Staticlib
44- | CrateType :: Cdylib
45- | CrateType :: ProcMacro
46- | CrateType :: Sdylib => true ,
47- CrateType :: Rlib => false ,
48- }
49- }
50-
5136fn prepare_lto (
5237 cgcx : & CodegenContext < LlvmCodegenBackend > ,
5338 dcx : DiagCtxtHandle < ' _ > ,
5439) -> Result < ( Vec < CString > , Vec < ( SerializedModule < ModuleBuffer > , CString ) > ) , FatalError > {
55- let export_threshold = match cgcx. lto {
56- // We're just doing LTO for our one crate
57- Lto :: ThinLocal => SymbolExportLevel :: Rust ,
58-
59- // We're doing LTO for the entire crate graph
60- Lto :: Fat | Lto :: Thin => symbol_export:: crates_export_threshold ( & cgcx. crate_types ) ,
61-
62- Lto :: No => panic ! ( "didn't request LTO but we're doing LTO" ) ,
63- } ;
40+ let mut symbols_below_threshold = exported_symbols_for_lto ( cgcx, dcx) ?
41+ . into_iter ( )
42+ . map ( |symbol| CString :: new ( symbol) . unwrap ( ) )
43+ . collect :: < Vec < CString > > ( ) ;
6444
65- let symbol_filter = & |& ( ref name, info) : & ( String , SymbolExportInfo ) | {
66- if info. level . is_below_threshold ( export_threshold) || info. used {
67- Some ( CString :: new ( name. as_str ( ) ) . unwrap ( ) )
68- } else {
69- None
70- }
71- } ;
72- let exported_symbols = cgcx. exported_symbols . as_ref ( ) . expect ( "needs exported symbols for LTO" ) ;
73- let mut symbols_below_threshold = {
74- let _timer = cgcx. prof . generic_activity ( "LLVM_lto_generate_symbols_below_threshold" ) ;
75- exported_symbols[ & LOCAL_CRATE ] . iter ( ) . filter_map ( symbol_filter) . collect :: < Vec < CString > > ( )
76- } ;
77- info ! ( "{} symbols to preserve in this crate" , symbols_below_threshold. len( ) ) ;
45+ // __llvm_profile_counter_bias is pulled in at link time by an undefined reference to
46+ // __llvm_profile_runtime, therefore we won't know until link time if this symbol
47+ // should have default visibility.
48+ symbols_below_threshold. push ( c"__llvm_profile_counter_bias" . to_owned ( ) ) ;
7849
7950 // If we're performing LTO for the entire crate graph, then for each of our
8051 // upstream dependencies, find the corresponding rlib and load the bitcode
@@ -84,37 +55,7 @@ fn prepare_lto(
8455 // with either fat or thin LTO
8556 let mut upstream_modules = Vec :: new ( ) ;
8657 if cgcx. lto != Lto :: ThinLocal {
87- // Make sure we actually can run LTO
88- for crate_type in cgcx. crate_types . iter ( ) {
89- if !crate_type_allows_lto ( * crate_type) {
90- dcx. emit_err ( LtoDisallowed ) ;
91- return Err ( FatalError ) ;
92- } else if * crate_type == CrateType :: Dylib {
93- if !cgcx. opts . unstable_opts . dylib_lto {
94- dcx. emit_err ( LtoDylib ) ;
95- return Err ( FatalError ) ;
96- }
97- } else if * crate_type == CrateType :: ProcMacro && !cgcx. opts . unstable_opts . dylib_lto {
98- dcx. emit_err ( LtoProcMacro ) ;
99- return Err ( FatalError ) ;
100- }
101- }
102-
103- if cgcx. opts . cg . prefer_dynamic && !cgcx. opts . unstable_opts . dylib_lto {
104- dcx. emit_err ( DynamicLinkingWithLTO ) ;
105- return Err ( FatalError ) ;
106- }
107-
108- for & ( cnum, ref path) in cgcx. each_linked_rlib_for_lto . iter ( ) {
109- let exported_symbols =
110- cgcx. exported_symbols . as_ref ( ) . expect ( "needs exported symbols for LTO" ) ;
111- {
112- let _timer =
113- cgcx. prof . generic_activity ( "LLVM_lto_generate_symbols_below_threshold" ) ;
114- symbols_below_threshold
115- . extend ( exported_symbols[ & cnum] . iter ( ) . filter_map ( symbol_filter) ) ;
116- }
117-
58+ for & ( _cnum, ref path) in cgcx. each_linked_rlib_for_lto . iter ( ) {
11859 let archive_data = unsafe {
11960 Mmap :: map ( std:: fs:: File :: open ( & path) . expect ( "couldn't open rlib" ) )
12061 . expect ( "couldn't map rlib" )
@@ -147,10 +88,6 @@ fn prepare_lto(
14788 }
14889 }
14990
150- // __llvm_profile_counter_bias is pulled in at link time by an undefined reference to
151- // __llvm_profile_runtime, therefore we won't know until link time if this symbol
152- // should have default visibility.
153- symbols_below_threshold. push ( c"__llvm_profile_counter_bias" . to_owned ( ) ) ;
15491 Ok ( ( symbols_below_threshold, upstream_modules) )
15592}
15693
0 commit comments