Skip to content

Commit 5c9a8b5

Browse files
committed
Add instance_def_size_estimate query
1 parent c8e9da4 commit 5c9a8b5

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ define_dep_nodes!( <'tcx>
638638
[input] TargetFeaturesWhitelist,
639639
[] TargetFeaturesEnabled(DefId),
640640

641+
[] InstanceDefSizeEstimate { instance_def: InstanceDef<'tcx> },
641642
);
642643

643644
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/mir/mono.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ impl<'tcx> MonoItem<'tcx> {
3131
MonoItem::Fn(instance) => {
3232
// Estimate the size of a function based on how many statements
3333
// it contains.
34-
let mir = tcx.instance_mir(instance.def);
35-
mir.basic_blocks().iter().map(|bb| bb.statements.len()).sum()
34+
tcx.instance_def_size_estimate(instance.def)
3635
},
3736
// Conservatively estimate the size of a static declaration
3837
// or assembly to be 1.

src/librustc/ty/maps/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::target_features_whitelist<'tcx> {
637637
}
638638
}
639639

640+
impl<'tcx> QueryDescription<'tcx> for queries::instance_def_size_estimate<'tcx> {
641+
fn describe(tcx: TyCtxt, def: ty::InstanceDef<'tcx>) -> String {
642+
format!("estimating size for `{}`", tcx.item_path_str(def.def_id()))
643+
}
644+
}
645+
640646
macro_rules! impl_disk_cacheable_query(
641647
($query_name:ident, |$key:tt| $cond:expr) => {
642648
impl<'tcx> QueryDescription<'tcx> for queries::$query_name<'tcx> {

src/librustc/ty/maps/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ define_maps! { <'tcx>
365365
target_features_whitelist_node(CrateNum) -> Rc<FxHashSet<String>>,
366366
[] fn target_features_enabled: TargetFeaturesEnabled(DefId) -> Rc<Vec<String>>,
367367

368+
// Get an estimate of the size of an InstanceDef based on its MIR for CGU partitioning.
369+
[] fn instance_def_size_estimate: instance_def_size_estimate_dep_node(ty::InstanceDef<'tcx>)
370+
-> usize,
368371
}
369372

370373
//////////////////////////////////////////////////////////////////////
@@ -514,3 +517,10 @@ fn substitute_normalize_and_test_predicates_node<'tcx>(key: (DefId, &'tcx Substs
514517
fn target_features_whitelist_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
515518
DepConstructor::TargetFeaturesWhitelist
516519
}
520+
521+
fn instance_def_size_estimate_dep_node<'tcx>(instance_def: ty::InstanceDef<'tcx>)
522+
-> DepConstructor<'tcx> {
523+
DepConstructor::InstanceDefSizeEstimate {
524+
instance_def
525+
}
526+
}

src/librustc/ty/maps/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
761761
DepKind::EraseRegionsTy |
762762
DepKind::NormalizeTy |
763763
DepKind::SubstituteNormalizeAndTestPredicates |
764+
DepKind::InstanceDefSizeEstimate |
764765

765766
// This one should never occur in this context
766767
DepKind::Null => {

src/librustc/ty/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,19 @@ fn crate_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26692669
tcx.hir.crate_hash
26702670
}
26712671

2672+
fn instance_def_size_estimate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
2673+
instance_def: InstanceDef<'tcx>)
2674+
-> usize {
2675+
match instance_def {
2676+
InstanceDef::Item(def_id) => {
2677+
let mir = tcx.optimized_mir(def_id);
2678+
mir.basic_blocks().iter().map(|bb| bb.statements.len()).sum()
2679+
},
2680+
// Estimate the size of compiler-generated shims to be 1.
2681+
_ => 1
2682+
}
2683+
}
2684+
26722685
pub fn provide(providers: &mut ty::maps::Providers) {
26732686
context::provide(providers);
26742687
erase_regions::provide(providers);
@@ -2686,6 +2699,7 @@ pub fn provide(providers: &mut ty::maps::Providers) {
26862699
original_crate_name,
26872700
crate_hash,
26882701
trait_impls_of: trait_def::trait_impls_of_provider,
2702+
instance_def_size_estimate,
26892703
..*providers
26902704
};
26912705
}

0 commit comments

Comments
 (0)