Skip to content

Commit b5b696c

Browse files
committed
Encode only MIR that can be used by other crates
Only reachable items might participate in the code generation in the downstream crates. Omit redundant optimized MIR of unreachable items from a crate metadata. Additionally, include reachable closures in reachable set, so that unreachable closures can be omitted on the same basis.
1 parent 26089ba commit b5b696c

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
1414
use rustc_hir as hir;
1515
use rustc_hir::def::DefKind;
1616
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,
1819
};
1920
use rustc_hir::definitions::DefPathData;
2021
use rustc_hir::intravisit;
@@ -50,7 +51,6 @@ pub(super) struct EncodeContext<'a, 'tcx> {
5051
opaque: opaque::FileEncoder,
5152
tcx: TyCtxt<'tcx>,
5253
feat: &'tcx rustc_feature::Features,
53-
5454
tables: TableBuilders,
5555

5656
lazy_state: LazyState,
@@ -1010,7 +1010,11 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
10101010
/// - we skip `optimized_mir` for check runs.
10111011
///
10121012
/// 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) {
10141018
match tcx.def_kind(def_id) {
10151019
// Constructors
10161020
DefKind::Ctor(_, _) => {
@@ -1027,14 +1031,15 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
10271031
// Full-fledged functions + closures
10281032
DefKind::AssocFn | DefKind::Fn | DefKind::Closure => {
10291033
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()));
10331039
// The function has a `const` modifier or is in a `#[const_trait]`.
10341040
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id())
10351041
|| 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)
10381043
}
10391044
// Generators require optimized MIR to compute layout.
10401045
DefKind::Generator => (false, true),
@@ -1580,9 +1585,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15801585
}
15811586

15821587
let tcx = self.tcx;
1588+
let reachable_set = tcx.reachable_set(());
15831589

15841590
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);
15861592
if encode_const || encode_opt { Some((def_id, encode_const, encode_opt)) } else { None }
15871593
});
15881594
for (def_id, encode_const, encode_opt) in keys_and_jobs {
@@ -2075,8 +2081,9 @@ fn prefetch_mir(tcx: TyCtxt<'_>) {
20752081
return;
20762082
}
20772083

2084+
let reachable_set = tcx.reachable_set(());
20782085
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);
20802087

20812088
if encode_const {
20822089
tcx.ensure_with_value().mir_for_ctfe(def_id);

compiler/rustc_passes/src/reachable.rs

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
9090
.typeck_results()
9191
.type_dependent_def(expr.hir_id)
9292
.map(|(kind, def_id)| Res::Def(kind, def_id)),
93+
hir::ExprKind::Closure(&hir::Closure { def_id, .. }) => {
94+
self.reachable_symbols.insert(def_id);
95+
None
96+
}
9397
_ => None,
9498
};
9599

tests/mir-opt/funky_arms.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::num::flt2dec;
99
use std::fmt::{Formatter, Result};
1010

1111
// EMIT_MIR funky_arms.float_to_exponential_common.ConstProp.diff
12-
fn float_to_exponential_common<T>(fmt: &mut Formatter<'_>, num: &T, upper: bool) -> Result
12+
pub fn float_to_exponential_common<T>(fmt: &mut Formatter<'_>, num: &T, upper: bool) -> Result
1313
where
1414
T: flt2dec::DecodableFloat,
1515
{

0 commit comments

Comments
 (0)