@@ -7,27 +7,24 @@ use std::sync::Arc;
7
7
use std:: { io, iter, slice} ;
8
8
9
9
use 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
+ } ;
12
13
use rustc_codegen_ssa:: back:: write:: { CodegenContext , FatLtoInput } ;
13
14
use rustc_codegen_ssa:: traits:: * ;
14
15
use rustc_codegen_ssa:: { ModuleCodegen , ModuleKind , looks_like_rust_object_file} ;
15
16
use rustc_data_structures:: fx:: FxHashMap ;
16
17
use rustc_data_structures:: memmap:: Mmap ;
17
18
use rustc_errors:: { DiagCtxtHandle , FatalError } ;
18
- use rustc_hir:: def_id:: LOCAL_CRATE ;
19
19
use rustc_middle:: bug;
20
20
use 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 } ;
23
22
use tracing:: { debug, info} ;
24
23
25
24
use crate :: back:: write:: {
26
25
self , CodegenDiagnosticsStage , DiagnosticHandlers , bitcode_section_name, save_temp_bitcode,
27
26
} ;
28
- use crate :: errors:: {
29
- DynamicLinkingWithLTO , LlvmError , LtoBitcodeFromRlib , LtoDisallowed , LtoDylib , LtoProcMacro ,
30
- } ;
27
+ use crate :: errors:: { LlvmError , LtoBitcodeFromRlib } ;
31
28
use crate :: llvm:: AttributePlace :: Function ;
32
29
use crate :: llvm:: { self , build_string} ;
33
30
use crate :: { LlvmCodegenBackend , ModuleLlvm , SimpleCx , attributes} ;
@@ -36,45 +33,19 @@ use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, attributes};
36
33
/// session to determine which CGUs we can reuse.
37
34
const THIN_LTO_KEYS_INCR_COMP_FILE_NAME : & str = "thin-lto-past-keys.bin" ;
38
35
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
-
51
36
fn prepare_lto (
52
37
cgcx : & CodegenContext < LlvmCodegenBackend > ,
53
38
dcx : DiagCtxtHandle < ' _ > ,
54
39
) -> 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 > > ( ) ;
64
44
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 ( ) ) ;
78
49
79
50
// If we're performing LTO for the entire crate graph, then for each of our
80
51
// upstream dependencies, find the corresponding rlib and load the bitcode
@@ -84,37 +55,7 @@ fn prepare_lto(
84
55
// with either fat or thin LTO
85
56
let mut upstream_modules = Vec :: new ( ) ;
86
57
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 ( ) {
118
59
let archive_data = unsafe {
119
60
Mmap :: map ( std:: fs:: File :: open ( & path) . expect ( "couldn't open rlib" ) )
120
61
. expect ( "couldn't map rlib" )
@@ -147,10 +88,6 @@ fn prepare_lto(
147
88
}
148
89
}
149
90
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 ( ) ) ;
154
91
Ok ( ( symbols_below_threshold, upstream_modules) )
155
92
}
156
93
0 commit comments