@@ -14,7 +14,8 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
14
14
use rustc_hir as hir;
15
15
use rustc_hir:: def:: DefKind ;
16
16
use rustc_hir:: def_id:: {
17
- CrateNum , DefId , DefIndex , LocalDefId , CRATE_DEF_ID , CRATE_DEF_INDEX , LOCAL_CRATE ,
17
+ CrateNum , DefId , DefIndex , LocalDefId , LocalDefIdSet , CRATE_DEF_ID , CRATE_DEF_INDEX ,
18
+ LOCAL_CRATE ,
18
19
} ;
19
20
use rustc_hir:: definitions:: DefPathData ;
20
21
use rustc_hir:: intravisit;
@@ -50,7 +51,6 @@ pub(super) struct EncodeContext<'a, 'tcx> {
50
51
opaque : opaque:: FileEncoder ,
51
52
tcx : TyCtxt < ' tcx > ,
52
53
feat : & ' tcx rustc_feature:: Features ,
53
-
54
54
tables : TableBuilders ,
55
55
56
56
lazy_state : LazyState ,
@@ -1010,7 +1010,11 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
1010
1010
/// - we skip `optimized_mir` for check runs.
1011
1011
///
1012
1012
/// Return a pair, resp. for CTFE and for LLVM.
1013
- fn should_encode_mir ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> ( bool , bool ) {
1013
+ fn should_encode_mir (
1014
+ tcx : TyCtxt < ' _ > ,
1015
+ reachable_set : & LocalDefIdSet ,
1016
+ def_id : LocalDefId ,
1017
+ ) -> ( bool , bool ) {
1014
1018
match tcx. def_kind ( def_id) {
1015
1019
// Constructors
1016
1020
DefKind :: Ctor ( _, _) => {
@@ -1027,14 +1031,15 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
1027
1031
// Full-fledged functions + closures
1028
1032
DefKind :: AssocFn | DefKind :: Fn | DefKind :: Closure => {
1029
1033
let generics = tcx. generics_of ( def_id) ;
1030
- let needs_inline = ( generics. requires_monomorphization ( tcx)
1031
- || tcx. codegen_fn_attrs ( def_id) . requests_inline ( ) )
1032
- && tcx. sess . opts . output_types . should_codegen ( ) ;
1034
+ let opt = tcx. sess . opts . unstable_opts . always_encode_mir
1035
+ || ( tcx. sess . opts . output_types . should_codegen ( )
1036
+ && reachable_set. contains ( & def_id)
1037
+ && ( generics. requires_monomorphization ( tcx)
1038
+ || tcx. codegen_fn_attrs ( def_id) . requests_inline ( ) ) ) ;
1033
1039
// The function has a `const` modifier or is in a `#[const_trait]`.
1034
1040
let is_const_fn = tcx. is_const_fn_raw ( def_id. to_def_id ( ) )
1035
1041
|| tcx. is_const_default_method ( def_id. to_def_id ( ) ) ;
1036
- let always_encode_mir = tcx. sess . opts . unstable_opts . always_encode_mir ;
1037
- ( is_const_fn, needs_inline || always_encode_mir)
1042
+ ( is_const_fn, opt)
1038
1043
}
1039
1044
// Generators require optimized MIR to compute layout.
1040
1045
DefKind :: Generator => ( false , true ) ,
@@ -1580,9 +1585,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
1580
1585
}
1581
1586
1582
1587
let tcx = self . tcx ;
1588
+ let reachable_set = tcx. reachable_set ( ( ) ) ;
1583
1589
1584
1590
let keys_and_jobs = tcx. mir_keys ( ( ) ) . iter ( ) . filter_map ( |& def_id| {
1585
- let ( encode_const, encode_opt) = should_encode_mir ( tcx, def_id) ;
1591
+ let ( encode_const, encode_opt) = should_encode_mir ( tcx, reachable_set , def_id) ;
1586
1592
if encode_const || encode_opt { Some ( ( def_id, encode_const, encode_opt) ) } else { None }
1587
1593
} ) ;
1588
1594
for ( def_id, encode_const, encode_opt) in keys_and_jobs {
@@ -2075,8 +2081,9 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
2075
2081
return ;
2076
2082
}
2077
2083
2084
+ let reachable_set = tcx. reachable_set ( ( ) ) ;
2078
2085
par_for_each_in ( tcx. mir_keys ( ( ) ) , |& def_id| {
2079
- let ( encode_const, encode_opt) = should_encode_mir ( tcx, def_id) ;
2086
+ let ( encode_const, encode_opt) = should_encode_mir ( tcx, reachable_set , def_id) ;
2080
2087
2081
2088
if encode_const {
2082
2089
tcx. ensure_with_value ( ) . mir_for_ctfe ( def_id) ;
0 commit comments