Skip to content

Commit dd9cda6

Browse files
committed
avoid processing mentioned items that are also still used
1 parent 1230ba3 commit dd9cda6

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

compiler/rustc_middle/src/mir/mod.rs

+1-1
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_mir_transform/src/mentioned_items.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ impl<'tcx> MirPass<'tcx> for MentionedItems {
3333
impl<'tcx> Visitor<'tcx> for MentionedItemsVisitor<'_, 'tcx> {
3434
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
3535
let const_ = constant.const_;
36-
// This is how function items get referenced: via constants of `FnDef` type.
36+
// This is how function items get referenced: via constants of `FnDef` type. This handles
37+
// both functions that are called anf those that are just turned to function pointers.
3738
if let ty::FnDef(def_id, args) = const_.ty().kind() {
3839
debug!("adding to required_items: {def_id:?}");
3940
self.mentioned_items

compiler/rustc_monomorphize/src/collector.rs

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

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

0 commit comments

Comments
 (0)