Skip to content

Commit a6dd7b5

Browse files
committed
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.
1 parent 1bf3949 commit a6dd7b5

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,12 @@ impl EncodeContext<'a, 'tcx> {
792792
self.encode_generics(def_id);
793793
self.encode_explicit_predicates(def_id);
794794
self.encode_inferred_outlives(def_id);
795-
self.encode_mir_for_ctfe(def_id.expect_local());
796-
self.encode_optimized_mir(def_id.expect_local());
795+
let opt_mir = tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir;
796+
if opt_mir {
797+
self.encode_optimized_mir(def_id.expect_local());
798+
}
797799
self.encode_promoted_mir(def_id.expect_local());
800+
self.encode_mir_for_ctfe(def_id.expect_local());
798801
}
799802

800803
fn encode_info_for_mod(&mut self, id: hir::HirId, md: &hir::Mod<'_>, attrs: &[ast::Attribute]) {
@@ -900,7 +903,10 @@ impl EncodeContext<'a, 'tcx> {
900903
self.encode_generics(def_id);
901904
self.encode_explicit_predicates(def_id);
902905
self.encode_inferred_outlives(def_id);
903-
self.encode_optimized_mir(def_id.expect_local());
906+
let opt_mir = tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir;
907+
if opt_mir {
908+
self.encode_optimized_mir(def_id.expect_local());
909+
}
904910
self.encode_mir_for_ctfe(def_id.expect_local());
905911
self.encode_promoted_mir(def_id.expect_local());
906912
}
@@ -1029,14 +1035,25 @@ impl EncodeContext<'a, 'tcx> {
10291035
}
10301036
}
10311037
ty::AssocKind::Fn => {
1032-
if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id.expect_local()) {
1033-
self.encode_optimized_mir(def_id.expect_local());
1034-
self.encode_promoted_mir(def_id.expect_local());
1038+
let opt_mir =
1039+
tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir;
1040+
if opt_mir {
1041+
if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id.expect_local()) {
1042+
self.encode_optimized_mir(def_id.expect_local());
1043+
self.encode_promoted_mir(def_id.expect_local());
1044+
}
10351045
}
10361046
}
10371047
}
10381048
}
10391049

1050+
fn should_encode_fn_opt_mir(&self, def_id: DefId) -> bool {
1051+
self.tcx.sess.opts.debugging_opts.always_encode_mir
1052+
|| (self.emit_codegen_mir
1053+
&& (self.tcx.generics_of(def_id).requires_monomorphization(self.tcx)
1054+
|| self.tcx.codegen_fn_attrs(def_id).requests_inline()))
1055+
}
1056+
10401057
fn encode_info_for_impl_item(&mut self, def_id: DefId) {
10411058
debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
10421059
let tcx = self.tcx;
@@ -1105,10 +1122,7 @@ impl EncodeContext<'a, 'tcx> {
11051122
let (mir, mir_const) = match ast_item.kind {
11061123
hir::ImplItemKind::Const(..) => (false, true),
11071124
hir::ImplItemKind::Fn(ref sig, _) => {
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()));
1125+
let opt_mir = self.should_encode_fn_opt_mir(def_id);
11121126
let is_const_fn = sig.header.constness == hir::Constness::Const;
11131127
(opt_mir, is_const_fn)
11141128
}
@@ -1432,10 +1446,7 @@ impl EncodeContext<'a, 'tcx> {
14321446
let (mir, const_mir) = match item.kind {
14331447
hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => (false, true),
14341448
hir::ItemKind::Fn(ref sig, ..) => {
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()));
1449+
let opt_mir = self.should_encode_fn_opt_mir(def_id);
14391450
let is_const_fn = sig.header.constness == hir::Constness::Const;
14401451
// We don't need the optimized MIR for const fns.
14411452
(opt_mir, is_const_fn)
@@ -1498,8 +1509,11 @@ impl EncodeContext<'a, 'tcx> {
14981509
record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig());
14991510
}
15001511
self.encode_generics(def_id.to_def_id());
1501-
self.encode_optimized_mir(def_id);
1502-
self.encode_promoted_mir(def_id);
1512+
let opt_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir || self.emit_codegen_mir;
1513+
if opt_mir {
1514+
self.encode_optimized_mir(def_id);
1515+
self.encode_promoted_mir(def_id);
1516+
}
15031517
}
15041518

15051519
fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) {

0 commit comments

Comments
 (0)