Skip to content

Commit 39bc32b

Browse files
committed
avoid processing mentioned items that are also still used
1 parent a6124d7 commit 39bc32b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<'tcx> CoroutineInfo<'tcx> {
314314
}
315315

316316
/// Some item that needs to monomorphize successfully for a MIR body to be considered well-formed.
317-
#[derive(Copy, Clone, PartialEq, Debug, HashStable, TyEncodable, TyDecodable)]
317+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, HashStable, TyEncodable, TyDecodable)]
318318
#[derive(TypeFoldable, TypeVisitable)]
319319
pub enum MentionedItem<'tcx> {
320320
Fn(DefId, GenericArgsRef<'tcx>),

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ struct MirUsedCollector<'a, 'tcx> {
719719
tcx: TyCtxt<'tcx>,
720720
body: &'a mir::Body<'tcx>,
721721
used_items: &'a mut MonoItems<'tcx>,
722+
/// See the comment in `collect_items_of_instance` for the purpose of this set.
723+
used_mentioned_items: &'a mut FxHashSet<MentionedItem<'tcx>>,
722724
instance: Instance<'tcx>,
723725
/// Spans for move size lints already emitted. Helps avoid duplicate lints.
724726
move_size_spans: Vec<Span>,
@@ -989,6 +991,10 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
989991
match terminator.kind {
990992
mir::TerminatorKind::Call { ref func, ref args, ref fn_span, .. } => {
991993
let callee_ty = func.ty(self.body, tcx);
994+
// *Before* monomorphizing, record that we already handled this mention.
995+
if let ty::FnDef(def_id, args) = callee_ty.kind() {
996+
self.used_mentioned_items.insert(MentionedItem::Fn(*def_id, args));
997+
}
992998
let callee_ty = self.monomorphize(callee_ty);
993999
self.check_fn_args_move_size(callee_ty, args, *fn_span, location);
9941000
visit_fn_use(self.tcx, callee_ty, true, source, &mut self.used_items)
@@ -1634,10 +1640,22 @@ fn collect_items_of_instance<'tcx>(
16341640
mode: CollectionMode,
16351641
) {
16361642
let body = tcx.instance_mir(instance.def);
1643+
// Naively, in "used" collection mode, all functions get added to *both* `used_items` and
1644+
// `mentioned_items`. Mentioned items processing will then notice that they have already been
1645+
// visited, but at that point each mentioned item has been monomorphized, added to the
1646+
// `mentioned_items` worklist, and checked in the global set of visited items. To removes that
1647+
// overhead, we have a special optimization that avoids adding items to `mentioned_items` when
1648+
// they are already added in `used_items`. We could just scan `used_items`, but that's a linear
1649+
// scan and not very efficient. Furthermore we can only do that *after* monomorphizing the
1650+
// mentioned item. So instead we collect all pre-monomorphized `MentionedItem` that were already
1651+
// added to `used_items` in a hash set, which can efficiently query in the
1652+
// `body.mentioned_items` loop below.
1653+
let mut used_mentioned_items = FxHashSet::<MentionedItem<'tcx>>::default();
16371654
let mut collector = MirUsedCollector {
16381655
tcx,
16391656
body,
16401657
used_items,
1658+
used_mentioned_items: &mut used_mentioned_items,
16411659
instance,
16421660
move_size_spans: vec![],
16431661
visiting_call_terminator: false,
@@ -1657,10 +1675,13 @@ fn collect_items_of_instance<'tcx>(
16571675
}
16581676
}
16591677

1660-
// Always gather mentioned items.
1678+
// Always gather mentioned items. We try to avoid processing items that we have already added to
1679+
// `used_items` above.
16611680
for item in &body.mentioned_items {
1662-
let item_mono = collector.monomorphize(item.node);
1663-
visit_mentioned_item(tcx, &item_mono, item.span, mentioned_items);
1681+
if !collector.used_mentioned_items.contains(&item.node) {
1682+
let item_mono = collector.monomorphize(item.node);
1683+
visit_mentioned_item(tcx, &item_mono, item.span, mentioned_items);
1684+
}
16641685
}
16651686
}
16661687

0 commit comments

Comments
 (0)