@@ -66,6 +66,11 @@ pub(super) struct EncodeContext<'a, 'tcx> {
6666 required_source_files : Option < GrowableBitSet < usize > > ,
6767 is_proc_macro : bool ,
6868 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 ,
6974}
7075
7176/// If the current crate is a proc-macro, returns early with `Lazy:empty()`.
@@ -1032,11 +1037,6 @@ impl EncodeContext<'a, 'tcx> {
10321037 }
10331038 }
10341039
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-
10401040 fn encode_info_for_impl_item ( & mut self , def_id : DefId ) {
10411041 debug ! ( "EncodeContext::encode_info_for_impl_item({:?})" , def_id) ;
10421042 let tcx = self . tcx ;
@@ -1105,13 +1105,12 @@ impl EncodeContext<'a, 'tcx> {
11051105 let ( mir, mir_const) = match ast_item. kind {
11061106 hir:: ImplItemKind :: Const ( ..) => ( false , true ) ,
11071107 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 ( ) ) ) ;
11121112 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)
11151114 }
11161115 hir:: ImplItemKind :: TyAlias ( ..) => ( false , false ) ,
11171116 } ;
@@ -1433,16 +1432,13 @@ impl EncodeContext<'a, 'tcx> {
14331432 let ( mir, const_mir) = match item. kind {
14341433 hir:: ItemKind :: Static ( ..) | hir:: ItemKind :: Const ( ..) => ( false , true ) ,
14351434 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 ( ) ) ) ;
14411439 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;
14441440 // We don't need the optimized MIR for const fns.
1445- ( mir , is_const_fn)
1441+ ( opt_mir , is_const_fn)
14461442 }
14471443 _ => ( false , false ) ,
14481444 } ;
@@ -2008,10 +2004,9 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> {
20082004 }
20092005 hir:: ItemKind :: Fn ( ref sig, ..) => {
20102006 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)
20132008 || tcx. codegen_fn_attrs ( def_id. to_def_id ( ) ) . requests_inline ( ) ;
2014- if needs_inline {
2009+ if opt_mir {
20152010 self . prefetch_mir ( def_id)
20162011 }
20172012 if sig. header . constness == hir:: Constness :: Const {
@@ -2045,11 +2040,10 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> {
20452040 }
20462041 hir:: ImplItemKind :: Fn ( ref sig, _) => {
20472042 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)
20502044 || tcx. codegen_fn_attrs ( def_id. to_def_id ( ) ) . requests_inline ( ) ;
20512045 let is_const_fn = sig. header . constness == hir:: Constness :: Const ;
2052- if needs_inline {
2046+ if opt_mir {
20532047 self . prefetch_mir ( def_id)
20542048 }
20552049 if is_const_fn {
@@ -2148,6 +2142,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
21482142 required_source_files,
21492143 is_proc_macro : tcx. sess . crate_types ( ) . contains ( & CrateType :: ProcMacro ) ,
21502144 hygiene_ctxt : & hygiene_ctxt,
2145+ emit_codegen_mir : tcx. sess . opts . output_types . should_codegen ( ) ,
21512146 } ;
21522147
21532148 // Encode the rustc version string in a predictable location.
0 commit comments