@@ -66,6 +66,11 @@ pub(super) struct EncodeContext<'a, 'tcx> {
66
66
required_source_files : Option < GrowableBitSet < usize > > ,
67
67
is_proc_macro : bool ,
68
68
hygiene_ctxt : & ' a HygieneEncodeContext ,
69
+
70
+ // Determines if MIR used for code generation will be included in the crate
71
+ // metadata. When emitting only metadata (e.g., cargo check), we can avoid
72
+ // generating optimized MIR altogether.
73
+ emit_codegen_mir : bool ,
69
74
}
70
75
71
76
/// If the current crate is a proc-macro, returns early with `Lazy:empty()`.
@@ -1032,11 +1037,6 @@ impl EncodeContext<'a, 'tcx> {
1032
1037
}
1033
1038
}
1034
1039
1035
- fn metadata_output_only ( & self ) -> bool {
1036
- // MIR optimisation can be skipped when we're just interested in the metadata.
1037
- !self . tcx . sess . opts . output_types . should_codegen ( )
1038
- }
1039
-
1040
1040
fn encode_info_for_impl_item ( & mut self , def_id : DefId ) {
1041
1041
debug ! ( "EncodeContext::encode_info_for_impl_item({:?})" , def_id) ;
1042
1042
let tcx = self . tcx ;
@@ -1105,13 +1105,12 @@ impl EncodeContext<'a, 'tcx> {
1105
1105
let ( mir, mir_const) = match ast_item. kind {
1106
1106
hir:: ImplItemKind :: Const ( ..) => ( false , true ) ,
1107
1107
hir:: ImplItemKind :: Fn ( ref sig, _) => {
1108
- let generics = self . tcx . generics_of ( def_id ) ;
1109
- let needs_inline = ( generics . requires_monomorphization ( self . tcx )
1110
- || tcx. codegen_fn_attrs ( def_id) . requests_inline ( ) )
1111
- && ! self . metadata_output_only ( ) ;
1108
+ let opt_mir = tcx. sess . opts . debugging_opts . always_encode_mir
1109
+ || ( self . emit_codegen_mir
1110
+ && ( tcx. generics_of ( def_id) . requires_monomorphization ( tcx )
1111
+ || tcx . codegen_fn_attrs ( def_id ) . requests_inline ( ) ) ) ;
1112
1112
let is_const_fn = sig. header . constness == hir:: Constness :: Const ;
1113
- let always_encode_mir = self . tcx . sess . opts . debugging_opts . always_encode_mir ;
1114
- ( needs_inline || always_encode_mir, is_const_fn)
1113
+ ( opt_mir, is_const_fn)
1115
1114
}
1116
1115
hir:: ImplItemKind :: TyAlias ( ..) => ( false , false ) ,
1117
1116
} ;
@@ -1433,16 +1432,13 @@ impl EncodeContext<'a, 'tcx> {
1433
1432
let ( mir, const_mir) = match item. kind {
1434
1433
hir:: ItemKind :: Static ( ..) | hir:: ItemKind :: Const ( ..) => ( false , true ) ,
1435
1434
hir:: ItemKind :: Fn ( ref sig, ..) => {
1436
- let generics = tcx. generics_of ( def_id) ;
1437
- let needs_inline = ( generics. requires_monomorphization ( tcx)
1438
- || tcx. codegen_fn_attrs ( def_id) . requests_inline ( ) )
1439
- && !self . metadata_output_only ( ) ;
1440
-
1435
+ let opt_mir = self . tcx . sess . opts . debugging_opts . always_encode_mir
1436
+ || ( self . emit_codegen_mir
1437
+ && ( tcx. generics_of ( def_id) . requires_monomorphization ( tcx)
1438
+ || tcx. codegen_fn_attrs ( def_id) . requests_inline ( ) ) ) ;
1441
1439
let is_const_fn = sig. header . constness == hir:: Constness :: Const ;
1442
- let always_encode_mir = self . tcx . sess . opts . debugging_opts . always_encode_mir ;
1443
- let mir = needs_inline || always_encode_mir;
1444
1440
// We don't need the optimized MIR for const fns.
1445
- ( mir , is_const_fn)
1441
+ ( opt_mir , is_const_fn)
1446
1442
}
1447
1443
_ => ( false , false ) ,
1448
1444
} ;
@@ -2008,10 +2004,9 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> {
2008
2004
}
2009
2005
hir:: ItemKind :: Fn ( ref sig, ..) => {
2010
2006
let def_id = tcx. hir ( ) . local_def_id ( item. hir_id ) ;
2011
- let generics = tcx. generics_of ( def_id. to_def_id ( ) ) ;
2012
- let needs_inline = generics. requires_monomorphization ( tcx)
2007
+ let opt_mir = tcx. generics_of ( def_id. to_def_id ( ) ) . requires_monomorphization ( tcx)
2013
2008
|| tcx. codegen_fn_attrs ( def_id. to_def_id ( ) ) . requests_inline ( ) ;
2014
- if needs_inline {
2009
+ if opt_mir {
2015
2010
self . prefetch_mir ( def_id)
2016
2011
}
2017
2012
if sig. header . constness == hir:: Constness :: Const {
@@ -2045,11 +2040,10 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> {
2045
2040
}
2046
2041
hir:: ImplItemKind :: Fn ( ref sig, _) => {
2047
2042
let def_id = tcx. hir ( ) . local_def_id ( impl_item. hir_id ) ;
2048
- let generics = tcx. generics_of ( def_id. to_def_id ( ) ) ;
2049
- let needs_inline = generics. requires_monomorphization ( tcx)
2043
+ let opt_mir = tcx. generics_of ( def_id. to_def_id ( ) ) . requires_monomorphization ( tcx)
2050
2044
|| tcx. codegen_fn_attrs ( def_id. to_def_id ( ) ) . requests_inline ( ) ;
2051
2045
let is_const_fn = sig. header . constness == hir:: Constness :: Const ;
2052
- if needs_inline {
2046
+ if opt_mir {
2053
2047
self . prefetch_mir ( def_id)
2054
2048
}
2055
2049
if is_const_fn {
@@ -2148,6 +2142,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
2148
2142
required_source_files,
2149
2143
is_proc_macro : tcx. sess . crate_types ( ) . contains ( & CrateType :: ProcMacro ) ,
2150
2144
hygiene_ctxt : & hygiene_ctxt,
2145
+ emit_codegen_mir : tcx. sess . opts . output_types . should_codegen ( ) ,
2151
2146
} ;
2152
2147
2153
2148
// Encode the rustc version string in a predictable location.
0 commit comments