Skip to content

Commit 785a96b

Browse files
committed
Querify MonoItem size estimation
1 parent 372e5b5 commit 785a96b

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

compiler/rustc_middle/src/mir/mono.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_target::spec::SymbolVisibility;
1919
use tracing::debug;
2020

2121
use crate::dep_graph::{DepNode, WorkProduct, WorkProductId};
22+
use crate::query::Providers;
2223
use crate::ty::{GenericArgs, Instance, InstanceKind, SymbolName, TyCtxt};
2324

2425
/// Describes how a monomorphization will be instantiated in object files.
@@ -62,30 +63,6 @@ impl<'tcx> MonoItem<'tcx> {
6263
}
6364
}
6465

65-
// Note: if you change how item size estimates work, you might need to
66-
// change NON_INCR_MIN_CGU_SIZE as well.
67-
pub fn size_estimate(&self, tcx: TyCtxt<'tcx>) -> usize {
68-
match *self {
69-
MonoItem::Fn(instance) => {
70-
match instance.def {
71-
// "Normal" functions size estimate: the number of
72-
// statements, plus one for the terminator.
73-
InstanceKind::Item(..)
74-
| InstanceKind::DropGlue(..)
75-
| InstanceKind::AsyncDropGlueCtorShim(..) => {
76-
let mir = tcx.instance_mir(instance.def);
77-
mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
78-
}
79-
// Other compiler-generated shims size estimate: 1
80-
_ => 1,
81-
}
82-
}
83-
// Conservatively estimate the size of a static declaration or
84-
// assembly item to be 1.
85-
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1,
86-
}
87-
}
88-
8966
pub fn is_generic_fn(&self) -> bool {
9067
match self {
9168
MonoItem::Fn(instance) => instance.args.non_erasable_generics().next().is_some(),
@@ -574,3 +551,31 @@ pub enum CollectionMode {
574551
/// that it is optimization-independent.
575552
MentionedItems,
576553
}
554+
555+
// Note: if you change how item size estimates work, you might need to
556+
// change NON_INCR_MIN_CGU_SIZE as well.
557+
fn size_estimate<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> usize {
558+
match item {
559+
MonoItem::Fn(instance) => {
560+
match instance.def {
561+
// "Normal" functions size estimate: the number of
562+
// statements, plus one for the terminator.
563+
InstanceKind::Item(..)
564+
| InstanceKind::DropGlue(..)
565+
| InstanceKind::AsyncDropGlueCtorShim(..) => {
566+
let mir = tcx.instance_mir(instance.def);
567+
mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
568+
}
569+
// Other compiler-generated shims size estimate: 1
570+
_ => 1,
571+
}
572+
}
573+
// Conservatively estimate the size of a static declaration or
574+
// assembly item to be 1.
575+
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1,
576+
}
577+
}
578+
579+
pub fn provide(providers: &mut Providers) {
580+
providers.size_estimate = size_estimate;
581+
}

compiler/rustc_middle/src/query/keys.rs

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_target::abi;
99

1010
use crate::infer::canonical::CanonicalQueryInput;
1111
use crate::mir::mono::CollectionMode;
12+
use crate::query::MonoItem;
1213
use crate::ty::fast_reject::SimplifiedType;
1314
use crate::ty::layout::{TyAndLayout, ValidityRequirement};
1415
use crate::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt};
@@ -87,6 +88,14 @@ impl<'tcx> Key for ty::Instance<'tcx> {
8788
}
8889
}
8990

91+
impl<'tcx> Key for MonoItem<'tcx> {
92+
type Cache<V> = DefaultCache<Self, V>;
93+
94+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
95+
tcx.def_span(self.def_id())
96+
}
97+
}
98+
9099
impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
91100
type Cache<V> = DefaultCache<Self, V>;
92101

compiler/rustc_middle/src/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2321,6 +2321,11 @@ rustc_queries! {
23212321
desc { "collecting used items" }
23222322
cache_on_disk_if { true }
23232323
}
2324+
2325+
query size_estimate(key: MonoItem<'tcx>) -> usize {
2326+
desc { "estimating codegen size" }
2327+
cache_on_disk_if { true }
2328+
}
23242329
}
23252330

23262331
rustc_query_append! { define_callbacks! }

compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,7 @@ pub fn provide(providers: &mut Providers) {
21552155
print::provide(providers);
21562156
super::util::bug::provide(providers);
21572157
super::middle::provide(providers);
2158+
super::mir::mono::provide(providers);
21582159
*providers = Providers {
21592160
trait_impls_of: trait_def::trait_impls_of_provider,
21602161
incoherent_impls: trait_def::incoherent_impls_provider,

compiler/rustc_monomorphize/src/partitioning.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ where
254254
if visibility == Visibility::Hidden && can_be_internalized {
255255
internalization_candidates.insert(mono_item);
256256
}
257-
let size_estimate = mono_item.size_estimate(cx.tcx);
257+
let size_estimate = cx.tcx.size_estimate(mono_item);
258258

259259
cgu.items_mut().insert(mono_item, MonoItemData {
260260
inlined: false,
@@ -279,7 +279,7 @@ where
279279
inlined: true,
280280
linkage: Linkage::Internal,
281281
visibility: Visibility::Default,
282-
size_estimate: inlined_item.size_estimate(cx.tcx),
282+
size_estimate: cx.tcx.size_estimate(inlined_item),
283283
});
284284
}
285285
}
@@ -1274,7 +1274,7 @@ fn dump_mono_items_stats<'tcx>(
12741274
.map(|(def_id, items)| {
12751275
let name = with_no_trimmed_paths!(tcx.def_path_str(def_id));
12761276
let instantiation_count = items.len();
1277-
let size_estimate = items[0].size_estimate(tcx);
1277+
let size_estimate = tcx.size_estimate(*items[0]);
12781278
let total_estimate = instantiation_count * size_estimate;
12791279
MonoItem { name, instantiation_count, size_estimate, total_estimate }
12801280
})

0 commit comments

Comments
 (0)