@@ -27,6 +27,7 @@ use super::ModuleLlvm;
27
27
use super :: ModuleCodegen ;
28
28
use super :: ModuleKind ;
29
29
use super :: CachedModuleCodegen ;
30
+ use super :: LlvmCodegenBackend ;
30
31
31
32
use abi;
32
33
use back:: write;
@@ -54,8 +55,6 @@ use callee;
54
55
use rustc_mir:: monomorphize:: collector:: { self , MonoItemCollectionMode } ;
55
56
use rustc_mir:: monomorphize:: item:: DefPathBasedNames ;
56
57
use common:: { self , IntPredicate , RealPredicate , TypeKind } ;
57
- use context:: CodegenCx ;
58
- use debuginfo;
59
58
use meth;
60
59
use mir;
61
60
use monomorphize:: Instance ;
@@ -720,9 +719,9 @@ fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
720
719
}
721
720
}
722
721
723
- pub fn codegen_crate < ' a , ' tcx , B : BackendMethods > (
722
+ pub fn codegen_crate< ' a , ' ll : ' a , ' tcx : ' ll , B : BackendMethods < ' a , ' ll , ' tcx > > (
724
723
backend : B ,
725
- tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
724
+ tcx : TyCtxt < ' ll , ' tcx , ' tcx > ,
726
725
rx : mpsc:: Receiver < Box < dyn Any + Send > >
727
726
) -> B :: OngoingCodegen {
728
727
@@ -941,6 +940,7 @@ pub fn codegen_crate<'a, 'tcx, B : BackendMethods>(
941
940
ongoing_codegen. into_inner ( )
942
941
}
943
942
943
+ =======
944
944
/// A curious wrapper structure whose only purpose is to call `codegen_aborted`
945
945
/// when it's dropped abnormally.
946
946
///
@@ -968,6 +968,83 @@ impl AbortCodegenOnDrop {
968
968
969
969
impl Deref for AbortCodegenOnDrop {
970
970
type Target = OngoingCodegen ;
971
+ }
972
+
973
+ fn assert_and_save_dep_graph < ' ll , ' tcx > ( tcx : TyCtxt < ' ll , ' tcx , ' tcx > ) {
974
+ time ( tcx. sess ,
975
+ "assert dep graph" ,
976
+ || rustc_incremental:: assert_dep_graph ( tcx) ) ;
977
+
978
+ time ( tcx. sess ,
979
+ "serialize dep graph" ,
980
+ || rustc_incremental:: save_dep_graph ( tcx) ) ;
981
+ }
982
+
983
+ fn collect_and_partition_mono_items < ' ll , ' tcx > (
984
+ tcx : TyCtxt < ' ll , ' tcx , ' tcx > ,
985
+ cnum : CrateNum ,
986
+ ) -> ( Arc < DefIdSet > , Arc < Vec < Arc < CodegenUnit < ' tcx > > > > )
987
+ {
988
+ assert_eq ! ( cnum, LOCAL_CRATE ) ;
989
+
990
+ let collection_mode = match tcx. sess . opts . debugging_opts . print_mono_items {
991
+ Some ( ref s) => {
992
+ let mode_string = s. to_lowercase ( ) ;
993
+ let mode_string = mode_string. trim ( ) ;
994
+ if mode_string == "eager" {
995
+ MonoItemCollectionMode :: Eager
996
+ } else {
997
+ if mode_string != "lazy" {
998
+ let message = format ! ( "Unknown codegen-item collection mode '{}'. \
999
+ Falling back to 'lazy' mode.",
1000
+ mode_string) ;
1001
+ tcx. sess . warn ( & message) ;
1002
+ }
1003
+
1004
+ MonoItemCollectionMode :: Lazy
1005
+ }
1006
+ }
1007
+ None => {
1008
+ if tcx. sess . opts . cg . link_dead_code {
1009
+ MonoItemCollectionMode :: Eager
1010
+ } else {
1011
+ MonoItemCollectionMode :: Lazy
1012
+ }
1013
+ }
1014
+ } ;
1015
+
1016
+ let ( items, inlining_map) =
1017
+ time ( tcx. sess , "monomorphization collection" , || {
1018
+ collector:: collect_crate_mono_items ( tcx, collection_mode)
1019
+ } ) ;
1020
+
1021
+ tcx. sess . abort_if_errors ( ) ;
1022
+
1023
+ :: rustc_mir:: monomorphize:: assert_symbols_are_distinct ( tcx, items. iter ( ) ) ;
1024
+
1025
+ let strategy = if tcx. sess . opts . incremental . is_some ( ) {
1026
+ PartitioningStrategy :: PerModule
1027
+ } else {
1028
+ PartitioningStrategy :: FixedUnitCount ( tcx. sess . codegen_units ( ) )
1029
+ } ;
1030
+
1031
+ let codegen_units = time ( tcx. sess , "codegen unit partitioning" , || {
1032
+ partitioning:: partition ( tcx,
1033
+ items. iter ( ) . cloned ( ) ,
1034
+ strategy,
1035
+ & inlining_map)
1036
+ . into_iter ( )
1037
+ . map ( Arc :: new)
1038
+ . collect :: < Vec < _ > > ( )
1039
+ } ) ;
1040
+
1041
+ let mono_items: DefIdSet = items. iter ( ) . filter_map ( |mono_item| {
1042
+ match * mono_item {
1043
+ MonoItem :: Fn ( ref instance) => Some ( instance. def_id ( ) ) ,
1044
+ MonoItem :: Static ( def_id) => Some ( def_id) ,
1045
+ _ => None ,
1046
+ }
1047
+ } ) . collect ( ) ;
971
1048
972
1049
fn deref ( & self ) -> & OngoingCodegen {
973
1050
self . 0 . as_ref ( ) . unwrap ( )
@@ -1087,7 +1164,13 @@ impl CrateInfo {
1087
1164
}
1088
1165
}
1089
1166
1090
- fn compile_codegen_unit < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
1167
+ fn is_codegened_item ( tcx : TyCtxt , id : DefId ) -> bool {
1168
+ let ( all_mono_items, _) =
1169
+ tcx. collect_and_partition_mono_items ( LOCAL_CRATE ) ;
1170
+ all_mono_items. contains ( & id)
1171
+ }
1172
+
1173
+ fn compile_codegen_unit < ' ll , ' tcx > ( tcx : TyCtxt < ' ll , ' tcx , ' tcx > ,
1091
1174
cgu_name : InternedString )
1092
1175
-> Stats {
1093
1176
let start_time = Instant :: now ( ) ;
@@ -1109,67 +1192,49 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1109
1192
cost) ;
1110
1193
return stats;
1111
1194
1112
- fn module_codegen < ' a , ' tcx > (
1113
- tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
1195
+ fn module_codegen < ' ll , ' tcx > (
1196
+ tcx : TyCtxt < ' ll , ' tcx , ' tcx > ,
1114
1197
cgu_name : InternedString )
1115
1198
-> ( Stats , ModuleCodegen < ModuleLlvm > )
1116
1199
{
1200
+ let backend = LlvmCodegenBackend ( ( ) ) ;
1117
1201
let cgu = tcx. codegen_unit ( cgu_name) ;
1118
-
1119
1202
// Instantiate monomorphizations without filling out definitions yet...
1120
- let llvm_module = ModuleLlvm :: new ( tcx. sess , & cgu_name. as_str ( ) ) ;
1203
+ let llvm_module = backend . new_metadata ( tcx. sess , & cgu_name. as_str ( ) ) ;
1121
1204
let stats = {
1122
- let cx = CodegenCx :: new ( tcx, cgu, & llvm_module) ;
1205
+ let cx = backend . new_codegen_context ( tcx, cgu, & llvm_module) ;
1123
1206
let mono_items = cx. codegen_unit
1124
1207
. items_in_deterministic_order ( cx. tcx ) ;
1125
1208
for & ( mono_item, ( linkage, visibility) ) in & mono_items {
1126
- mono_item. predefine ( & cx, linkage, visibility) ;
1209
+ mono_item. predefine :: < Builder < & Value > > ( & cx, linkage, visibility) ;
1127
1210
}
1128
1211
1129
1212
// ... and now that we have everything pre-defined, fill out those definitions.
1130
1213
for & ( mono_item, _) in & mono_items {
1131
- mono_item. define ( & cx) ;
1214
+ mono_item. define :: < Builder < & Value > > ( & cx) ;
1132
1215
}
1133
1216
1134
1217
// If this codegen unit contains the main function, also create the
1135
1218
// wrapper here
1136
1219
maybe_create_entry_wrapper :: < Builder < & Value > > ( & cx) ;
1137
1220
1138
1221
// Run replace-all-uses-with for statics that need it
1139
- for & ( old_g, new_g) in cx. statics_to_rauw . borrow ( ) . iter ( ) {
1140
- unsafe {
1141
- let bitcast = llvm:: LLVMConstPointerCast ( new_g, cx. val_ty ( old_g) ) ;
1142
- llvm:: LLVMReplaceAllUsesWith ( old_g, bitcast) ;
1143
- llvm:: LLVMDeleteGlobal ( old_g) ;
1144
- }
1222
+ for & ( old_g, new_g) in cx. statics_to_rauw ( ) . borrow ( ) . iter ( ) {
1223
+ cx. static_replace_all_uses ( old_g, new_g)
1145
1224
}
1146
1225
1147
1226
// Create the llvm.used variable
1148
1227
// This variable has type [N x i8*] and is stored in the llvm.metadata section
1149
- if !cx. used_statics . borrow ( ) . is_empty ( ) {
1150
- let name = const_cstr ! ( "llvm.used" ) ;
1151
- let section = const_cstr ! ( "llvm.metadata" ) ;
1152
- let array = cx. const_array (
1153
- & cx. type_ptr_to ( cx. type_i8 ( ) ) ,
1154
- & * cx. used_statics . borrow ( )
1155
- ) ;
1156
-
1157
- unsafe {
1158
- let g = llvm:: LLVMAddGlobal ( cx. llmod ,
1159
- cx. val_ty ( array) ,
1160
- name. as_ptr ( ) ) ;
1161
- llvm:: LLVMSetInitializer ( g, array) ;
1162
- llvm:: LLVMRustSetLinkage ( g, llvm:: Linkage :: AppendingLinkage ) ;
1163
- llvm:: LLVMSetSection ( g, section. as_ptr ( ) ) ;
1164
- }
1228
+ if !cx. used_statics ( ) . borrow ( ) . is_empty ( ) {
1229
+ cx. create_used_variable ( )
1165
1230
}
1166
1231
1167
1232
// Finalize debuginfo
1168
1233
if cx. sess ( ) . opts . debuginfo != DebugInfo :: None {
1169
- debuginfo :: finalize ( & cx ) ;
1234
+ cx . debuginfo_finalize ( ) ;
1170
1235
}
1171
1236
1172
- cx. stats . into_inner ( )
1237
+ cx. consume_stats ( ) . into_inner ( )
1173
1238
} ;
1174
1239
1175
1240
( stats, ModuleCodegen {
0 commit comments