From a6dd7b505615620a9181891db06d39b1b1043f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Tue, 12 Jan 2021 00:00:00 +0000 Subject: [PATCH] Consistently avoid constructing optimized MIR when not doing codegen The optimized MIR for closures is being encoded unconditionally, while being unnecessary for cargo check. This turns out to be especially costly with MIR inlining enabled, since it triggers computation of optimized MIR for all callees that are being examined for inlining purposes. Skip encoding of optimized MIR for closures, enum constructors, struct constructors, and trait fns when not doing codegen, like it is already done for other items since 49433. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 46 +++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index fd990877107..f71233c9472 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -792,9 +792,12 @@ impl EncodeContext<'a, 'tcx> { self.encode_generics(def_id); self.encode_explicit_predicates(def_id); self.encode_inferred_outlives(def_id); - self.encode_mir_for_ctfe(def_id.expect_local()); - self.encode_optimized_mir(def_id.expect_local()); + let opt_mir = tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir; + if opt_mir { + self.encode_optimized_mir(def_id.expect_local()); + } self.encode_promoted_mir(def_id.expect_local()); + self.encode_mir_for_ctfe(def_id.expect_local()); } fn encode_info_for_mod(&mut self, id: hir::HirId, md: &hir::Mod<'_>, attrs: &[ast::Attribute]) { @@ -900,7 +903,10 @@ impl EncodeContext<'a, 'tcx> { self.encode_generics(def_id); self.encode_explicit_predicates(def_id); self.encode_inferred_outlives(def_id); - self.encode_optimized_mir(def_id.expect_local()); + let opt_mir = tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir; + if opt_mir { + self.encode_optimized_mir(def_id.expect_local()); + } self.encode_mir_for_ctfe(def_id.expect_local()); self.encode_promoted_mir(def_id.expect_local()); } @@ -1029,14 +1035,25 @@ impl EncodeContext<'a, 'tcx> { } } ty::AssocKind::Fn => { - if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id.expect_local()) { - self.encode_optimized_mir(def_id.expect_local()); - self.encode_promoted_mir(def_id.expect_local()); + let opt_mir = + tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir; + if opt_mir { + if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id.expect_local()) { + self.encode_optimized_mir(def_id.expect_local()); + self.encode_promoted_mir(def_id.expect_local()); + } } } } } + fn should_encode_fn_opt_mir(&self, def_id: DefId) -> bool { + self.tcx.sess.opts.debugging_opts.always_encode_mir + || (self.emit_codegen_mir + && (self.tcx.generics_of(def_id).requires_monomorphization(self.tcx) + || self.tcx.codegen_fn_attrs(def_id).requests_inline())) + } + fn encode_info_for_impl_item(&mut self, def_id: DefId) { debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id); let tcx = self.tcx; @@ -1105,10 +1122,7 @@ impl EncodeContext<'a, 'tcx> { let (mir, mir_const) = match ast_item.kind { hir::ImplItemKind::Const(..) => (false, true), hir::ImplItemKind::Fn(ref sig, _) => { - let opt_mir = tcx.sess.opts.debugging_opts.always_encode_mir - || (self.emit_codegen_mir - && (tcx.generics_of(def_id).requires_monomorphization(tcx) - || tcx.codegen_fn_attrs(def_id).requests_inline())); + let opt_mir = self.should_encode_fn_opt_mir(def_id); let is_const_fn = sig.header.constness == hir::Constness::Const; (opt_mir, is_const_fn) } @@ -1432,10 +1446,7 @@ impl EncodeContext<'a, 'tcx> { let (mir, const_mir) = match item.kind { hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => (false, true), hir::ItemKind::Fn(ref sig, ..) => { - let opt_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir - || (self.emit_codegen_mir - && (tcx.generics_of(def_id).requires_monomorphization(tcx) - || tcx.codegen_fn_attrs(def_id).requests_inline())); + let opt_mir = self.should_encode_fn_opt_mir(def_id); let is_const_fn = sig.header.constness == hir::Constness::Const; // We don't need the optimized MIR for const fns. (opt_mir, is_const_fn) @@ -1498,8 +1509,11 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig()); } self.encode_generics(def_id.to_def_id()); - self.encode_optimized_mir(def_id); - self.encode_promoted_mir(def_id); + let opt_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir; + if opt_mir { + self.encode_optimized_mir(def_id); + self.encode_promoted_mir(def_id); + } } fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) {