@@ -170,6 +170,7 @@ use rustc_hir as hir;
170170use rustc_hir:: def:: DefKind ;
171171use rustc_hir:: def_id:: { DefId , DefIdMap , LocalDefId } ;
172172use rustc_hir:: lang_items:: LangItem ;
173+ use rustc_middle:: middle:: exported_symbols:: ExportedSymbol ;
173174use rustc_middle:: mir:: interpret:: { AllocId , ErrorHandled , GlobalAlloc , Scalar } ;
174175use rustc_middle:: mir:: mono:: { InstantiationMode , MonoItem } ;
175176use rustc_middle:: mir:: visit:: Visitor as MirVisitor ;
@@ -184,6 +185,7 @@ use rustc_middle::ty::{
184185} ;
185186use rustc_middle:: ty:: { GenericArgKind , GenericArgs } ;
186187use rustc_middle:: { middle:: codegen_fn_attrs:: CodegenFnAttrFlags , mir:: visit:: TyContext } ;
188+ use rustc_session:: config:: CrateType ;
187189use rustc_session:: config:: EntryFnType ;
188190use rustc_session:: lint:: builtin:: LARGE_ASSIGNMENTS ;
189191use rustc_session:: Limit ;
@@ -316,6 +318,7 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<
316318 }
317319
318320 collector. push_extra_entry_roots ( ) ;
321+ collector. push_extra_roots_from_mir_only_rlibs ( ) ;
319322 }
320323
321324 // We can only codegen items that are instantiable - items all of
@@ -1025,9 +1028,24 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
10251028 return true ;
10261029 } ;
10271030
1031+ let def_is_for_mir_only_rlib = if def_id. krate == rustc_hir:: def_id:: LOCAL_CRATE {
1032+ tcx. building_mir_only_rlib ( )
1033+ } else {
1034+ tcx. mir_only_crates ( ( ) ) . iter ( ) . any ( |c| * c == def_id. krate )
1035+ } ;
1036+
10281037 if tcx. is_foreign_item ( def_id) {
1029- // Foreign items are always linked against, there's no way of instantiating them.
1030- return false ;
1038+ if def_is_for_mir_only_rlib {
1039+ return tcx. is_mir_available ( instance. def_id ( ) ) ;
1040+ } else {
1041+ // Foreign items are always linked against, there's no way of instantiating them.
1042+ return false ;
1043+ }
1044+ }
1045+
1046+ if def_is_for_mir_only_rlib {
1047+ let has_mir = tcx. is_mir_available ( instance. def_id ( ) ) ;
1048+ return has_mir || matches ! ( tcx. def_kind( instance. def_id( ) ) , DefKind :: Static { .. } ) ;
10311049 }
10321050
10331051 if tcx. intrinsic ( def_id) . is_some_and ( |i| i. must_be_overridden ) {
@@ -1040,18 +1058,20 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
10401058 return true ;
10411059 }
10421060
1061+ if !def_is_for_mir_only_rlib {
1062+ if let DefKind :: Static { .. } = tcx. def_kind ( def_id) {
1063+ // We cannot monomorphize statics from upstream crates.
1064+ return false ;
1065+ }
1066+ }
1067+
10431068 if tcx. is_reachable_non_generic ( def_id)
10441069 || instance. polymorphize ( tcx) . upstream_monomorphization ( tcx) . is_some ( )
10451070 {
10461071 // We can link to the item in question, no instance needed in this crate.
10471072 return false ;
10481073 }
10491074
1050- if let DefKind :: Static { .. } = tcx. def_kind ( def_id) {
1051- // We cannot monomorphize statics from upstream crates.
1052- return false ;
1053- }
1054-
10551075 if !tcx. is_mir_available ( def_id) {
10561076 tcx. dcx ( ) . emit_fatal ( NoOptimizedMir {
10571077 span : tcx. def_span ( def_id) ,
@@ -1358,6 +1378,76 @@ impl<'v> RootCollector<'_, 'v> {
13581378
13591379 self . output . push ( create_fn_mono_item ( self . tcx , start_instance, DUMMY_SP ) ) ;
13601380 }
1381+
1382+ fn push_extra_roots_from_mir_only_rlibs ( & mut self ) {
1383+ // An upstream extern function may be used anywhere in the dependency tree, so we
1384+ // cannot do any reachability analysis on them. We blindly monomorphize every
1385+ // extern function declared anywhere in our dependency tree. We must give them
1386+ // GloballyShared codegen because we don't know if the only call to an upstream
1387+ // extern function is also upstream: We don't have reachability information. All we
1388+ // can do is codegen all extern functions and pray for the linker to delete the
1389+ // ones that are reachable.
1390+ if !self . tcx . crate_types ( ) . iter ( ) . any ( |c| !matches ! ( c, CrateType :: Rlib ) ) {
1391+ return ;
1392+ }
1393+
1394+ /*
1395+ eprintln!(
1396+ "Monomorphizing upstream crates for {:?}, {:?}",
1397+ self.tcx.crate_name(rustc_span::def_id::LOCAL_CRATE),
1398+ self.tcx.crate_types()
1399+ );
1400+ for krate in self.tcx.mir_only_crates(()) {
1401+ eprintln!("{:?}", self.tcx.crate_name(*krate));
1402+ }
1403+ */
1404+
1405+ for ( symbol, _info) in self
1406+ . tcx
1407+ . mir_only_crates ( ( ) )
1408+ . into_iter ( )
1409+ . filter ( |krate| {
1410+ if [ "alloc" , "core" , "std" ] . contains ( & self . tcx . crate_name ( * * krate) . as_str ( ) )
1411+ && self . tcx . crate_types ( ) == & [ CrateType :: ProcMacro ]
1412+ {
1413+ false
1414+ } else {
1415+ if self . tcx . crate_types ( ) == & [ CrateType :: ProcMacro ] {
1416+ eprintln ! ( "{:?}" , self . tcx. crate_name( * * krate) . as_str( ) ) ;
1417+ }
1418+ true
1419+ }
1420+ } )
1421+ . flat_map ( |krate| self . tcx . exported_symbols ( * krate) )
1422+ {
1423+ let def_id = match symbol {
1424+ ExportedSymbol :: NonGeneric ( def_id) => def_id,
1425+ ExportedSymbol :: ThreadLocalShim ( def_id) => {
1426+ //eprintln!("{:?}", def_id);
1427+ let item = MonoItem :: Fn ( Instance {
1428+ def : InstanceDef :: ThreadLocalShim ( * def_id) ,
1429+ args : GenericArgs :: empty ( ) ,
1430+ } ) ;
1431+ self . output . push ( dummy_spanned ( item) ) ;
1432+ continue ;
1433+ }
1434+ _ => continue ,
1435+ } ;
1436+ match self . tcx . def_kind ( def_id) {
1437+ DefKind :: Fn | DefKind :: AssocFn => {
1438+ //eprintln!("{:?}", def_id);
1439+ let instance = Instance :: mono ( self . tcx , * def_id) ;
1440+ let item = create_fn_mono_item ( self . tcx , instance, DUMMY_SP ) ;
1441+ self . output . push ( item) ;
1442+ }
1443+ DefKind :: Static { .. } => {
1444+ //eprintln!("{:?}", def_id);
1445+ self . output . push ( dummy_spanned ( MonoItem :: Static ( * def_id) ) ) ;
1446+ }
1447+ _ => { }
1448+ }
1449+ }
1450+ }
13611451}
13621452
13631453#[ instrument( level = "debug" , skip( tcx, output) ) ]
0 commit comments