1use std::path::PathBuf;
209
210use rustc_attr_parsing::InlineAttr;
211use rustc_data_structures::fx::FxIndexMap;
212use rustc_data_structures::sync::{LRef, MTLock, par_for_each_in};
213use rustc_data_structures::unord::{UnordMap, UnordSet};
214use rustc_hir as hir;
215use rustc_hir::def::DefKind;
216use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
217use rustc_hir::lang_items::LangItem;
218use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
219use rustc_middle::mir::interpret::{AllocId, ErrorHandled, GlobalAlloc, Scalar};
220use rustc_middle::mir::mono::{CollectionMode, InstantiationMode, MonoItem};
221use rustc_middle::mir::visit::Visitor as MirVisitor;
222use rustc_middle::mir::{self, Location, MentionedItem, traversal};
223use rustc_middle::query::TyCtxtAt;
224use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCoercion};
225use rustc_middle::ty::layout::ValidityRequirement;
226use rustc_middle::ty::print::{shrunk_instance_name, with_no_trimmed_paths};
227use rustc_middle::ty::{
228 self, GenericArgs, GenericParamDefKind, Instance, InstanceKind, Interner, Ty, TyCtxt,
229 TypeFoldable, TypeVisitableExt, VtblEntry,
230};
231use rustc_middle::util::Providers;
232use rustc_middle::{bug, span_bug};
233use rustc_session::Limit;
234use rustc_session::config::EntryFnType;
235use rustc_span::source_map::{Spanned, dummy_spanned, respan};
236use rustc_span::{DUMMY_SP, Span};
237use tracing::{debug, instrument, trace};
238
239use crate::errors::{self, EncounteredErrorWhileInstantiating, NoOptimizedMir, RecursionLimit};
240
241#[derive(PartialEq)]
242pub(crate) enum MonoItemCollectionStrategy {
243 Eager,
244 Lazy,
245}
246
247struct SharedState<'tcx> {
249 visited: MTLock<UnordSet<MonoItem<'tcx>>>,
251 mentioned: MTLock<UnordSet<MonoItem<'tcx>>>,
254 usage_map: MTLock<UsageMap<'tcx>>,
256}
257
258pub(crate) struct UsageMap<'tcx> {
259 pub used_map: UnordMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>>,
261
262 user_map: UnordMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>>,
264}
265
266impl<'tcx> UsageMap<'tcx> {
267 fn new() -> UsageMap<'tcx> {
268 UsageMap { used_map: Default::default(), user_map: Default::default() }
269 }
270
271 fn record_used<'a>(&mut self, user_item: MonoItem<'tcx>, used_items: &'a MonoItems<'tcx>)
272 where
273 'tcx: 'a,
274 {
275 for used_item in used_items.items() {
276 self.user_map.entry(used_item).or_default().push(user_item);
277 }
278
279 assert!(self.used_map.insert(user_item, used_items.items().collect()).is_none());
280 }
281
282 pub(crate) fn get_user_items(&self, item: MonoItem<'tcx>) -> &[MonoItem<'tcx>] {
283 self.user_map.get(&item).map(|items| items.as_slice()).unwrap_or(&[])
284 }
285
286 pub(crate) fn for_each_inlined_used_item<F>(
288 &self,
289 tcx: TyCtxt<'tcx>,
290 item: MonoItem<'tcx>,
291 mut f: F,
292 ) where
293 F: FnMut(MonoItem<'tcx>),
294 {
295 let used_items = self.used_map.get(&item).unwrap();
296 for used_item in used_items.iter() {
297 let is_inlined = used_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy;
298 if is_inlined {
299 f(*used_item);
300 }
301 }
302 }
303}
304
305struct MonoItems<'tcx> {
306 items: FxIndexMap<MonoItem<'tcx>, Span>,
309}
310
311impl<'tcx> MonoItems<'tcx> {
312 fn new() -> Self {
313 Self { items: FxIndexMap::default() }
314 }
315
316 fn is_empty(&self) -> bool {
317 self.items.is_empty()
318 }
319
320 fn push(&mut self, item: Spanned<MonoItem<'tcx>>) {
321 self.items.entry(item.node).or_insert(item.span);
324 }
325
326 fn items(&self) -> impl Iterator<Item = MonoItem<'tcx>> + '_ {
327 self.items.keys().cloned()
328 }
329}
330
331impl<'tcx> IntoIterator for MonoItems<'tcx> {
332 type Item = Spanned<MonoItem<'tcx>>;
333 type IntoIter = impl Iterator<Item = Spanned<MonoItem<'tcx>>>;
334
335 fn into_iter(self) -> Self::IntoIter {
336 self.items.into_iter().map(|(item, span)| respan(span, item))
337 }
338}
339
340impl<'tcx> Extend<Spanned<MonoItem<'tcx>>> for MonoItems<'tcx> {
341 fn extend<I>(&mut self, iter: I)
342 where
343 I: IntoIterator<Item = Spanned<MonoItem<'tcx>>>,
344 {
345 for item in iter {
346 self.push(item)
347 }
348 }
349}
350
351#[instrument(skip(tcx, state, recursion_depths, recursion_limit), level = "debug")]
357fn collect_items_rec<'tcx>(
358 tcx: TyCtxt<'tcx>,
359 starting_item: Spanned<MonoItem<'tcx>>,
360 state: LRef<'_, SharedState<'tcx>>,
361 recursion_depths: &mut DefIdMap<usize>,
362 recursion_limit: Limit,
363 mode: CollectionMode,
364) {
365 if mode == CollectionMode::UsedItems {
366 if !state.visited.lock_mut().insert(starting_item.node) {
367 return;
369 }
370 } else {
371 if state.visited.lock().contains(&starting_item.node) {
372 return;
374 }
375 if !state.mentioned.lock_mut().insert(starting_item.node) {
376 return;
378 }
379 }
382
383 let mut used_items = MonoItems::new();
384 let mut mentioned_items = MonoItems::new();
385 let recursion_depth_reset;
386
387 let error_count = tcx.dcx().err_count();
411
412 match starting_item.node {
416 MonoItem::Static(def_id) => {
417 recursion_depth_reset = None;
418
419 if mode == CollectionMode::UsedItems {
422 let instance = Instance::mono(tcx, def_id);
423
424 debug_assert!(tcx.should_codegen_locally(instance));
426
427 let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else { bug!() };
428 if !nested {
430 let ty = instance.ty(tcx, ty::TypingEnv::fully_monomorphized());
431 visit_drop_use(tcx, ty, true, starting_item.span, &mut used_items);
432 }
433
434 if let Ok(alloc) = tcx.eval_static_initializer(def_id) {
435 for &prov in alloc.inner().provenance().ptrs().values() {
436 collect_alloc(tcx, prov.alloc_id(), &mut used_items);
437 }
438 }
439
440 if tcx.needs_thread_local_shim(def_id) {
441 used_items.push(respan(
442 starting_item.span,
443 MonoItem::Fn(Instance {
444 def: InstanceKind::ThreadLocalShim(def_id),
445 args: GenericArgs::empty(),
446 }),
447 ));
448 }
449 }
450
451 }
455 MonoItem::Fn(instance) => {
456 debug_assert!(tcx.should_codegen_locally(instance));
458
459 recursion_depth_reset = Some(check_recursion_limit(
461 tcx,
462 instance,
463 starting_item.span,
464 recursion_depths,
465 recursion_limit,
466 ));
467
468 rustc_data_structures::stack::ensure_sufficient_stack(|| {
469 let (used, mentioned) = tcx.items_of_instance((instance, mode));
470 used_items.extend(used.into_iter().copied());
471 mentioned_items.extend(mentioned.into_iter().copied());
472 });
473 }
474 MonoItem::GlobalAsm(item_id) => {
475 assert!(
476 mode == CollectionMode::UsedItems,
477 "should never encounter global_asm when collecting mentioned items"
478 );
479 recursion_depth_reset = None;
480
481 let item = tcx.hir().item(item_id);
482 if let hir::ItemKind::GlobalAsm(asm) = item.kind {
483 for (op, op_sp) in asm.operands {
484 match op {
485 hir::InlineAsmOperand::Const { .. } => {
486 }
490 hir::InlineAsmOperand::SymFn { anon_const } => {
491 let fn_ty =
492 tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
493 visit_fn_use(tcx, fn_ty, false, *op_sp, &mut used_items);
494 }
495 hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
496 let instance = Instance::mono(tcx, *def_id);
497 if tcx.should_codegen_locally(instance) {
498 trace!("collecting static {:?}", def_id);
499 used_items.push(dummy_spanned(MonoItem::Static(*def_id)));
500 }
501 }
502 hir::InlineAsmOperand::In { .. }
503 | hir::InlineAsmOperand::Out { .. }
504 | hir::InlineAsmOperand::InOut { .. }
505 | hir::InlineAsmOperand::SplitInOut { .. }
506 | hir::InlineAsmOperand::Label { .. } => {
507 span_bug!(*op_sp, "invalid operand type for global_asm!")
508 }
509 }
510 }
511 } else {
512 span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
513 }
514
515 }
517 };
518
519 if tcx.dcx().err_count() > error_count
522 && starting_item.node.is_generic_fn()
523 && starting_item.node.is_user_defined()
524 {
525 let formatted_item = with_no_trimmed_paths!(starting_item.node.to_string());
526 tcx.dcx().emit_note(EncounteredErrorWhileInstantiating {
527 span: starting_item.span,
528 formatted_item,
529 });
530 }
531 if mode == CollectionMode::UsedItems {
537 state.usage_map.lock_mut().record_used(starting_item.node, &used_items);
538 }
539
540 if mode == CollectionMode::MentionedItems {
541 assert!(used_items.is_empty(), "'mentioned' collection should never encounter used items");
542 } else {
543 for used_item in used_items {
544 collect_items_rec(
545 tcx,
546 used_item,
547 state,
548 recursion_depths,
549 recursion_limit,
550 CollectionMode::UsedItems,
551 );
552 }
553 }
554
555 for mentioned_item in mentioned_items {
558 collect_items_rec(
559 tcx,
560 mentioned_item,
561 state,
562 recursion_depths,
563 recursion_limit,
564 CollectionMode::MentionedItems,
565 );
566 }
567
568 if let Some((def_id, depth)) = recursion_depth_reset {
569 recursion_depths.insert(def_id, depth);
570 }
571}
572
573fn check_recursion_limit<'tcx>(
574 tcx: TyCtxt<'tcx>,
575 instance: Instance<'tcx>,
576 span: Span,
577 recursion_depths: &mut DefIdMap<usize>,
578 recursion_limit: Limit,
579) -> (DefId, usize) {
580 let def_id = instance.def_id();
581 let recursion_depth = recursion_depths.get(&def_id).cloned().unwrap_or(0);
582 debug!(" => recursion depth={}", recursion_depth);
583
584 let adjusted_recursion_depth = if tcx.is_lang_item(def_id, LangItem::DropInPlace) {
585 recursion_depth / 4
588 } else {
589 recursion_depth
590 };
591
592 if !recursion_limit.value_within_limit(adjusted_recursion_depth) {
596 let def_span = tcx.def_span(def_id);
597 let def_path_str = tcx.def_path_str(def_id);
598 let (shrunk, written_to_path) = shrunk_instance_name(tcx, instance);
599 let mut path = PathBuf::new();
600 let was_written = if let Some(written_to_path) = written_to_path {
601 path = written_to_path;
602 true
603 } else {
604 false
605 };
606 tcx.dcx().emit_fatal(RecursionLimit {
607 span,
608 shrunk,
609 def_span,
610 def_path_str,
611 was_written,
612 path,
613 });
614 }
615
616 recursion_depths.insert(def_id, recursion_depth + 1);
617
618 (def_id, recursion_depth)
619}
620
621struct MirUsedCollector<'a, 'tcx> {
622 tcx: TyCtxt<'tcx>,
623 body: &'a mir::Body<'tcx>,
624 used_items: &'a mut MonoItems<'tcx>,
625 used_mentioned_items: &'a mut UnordSet<MentionedItem<'tcx>>,
628 instance: Instance<'tcx>,
629}
630
631impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
632 fn monomorphize<T>(&self, value: T) -> T
633 where
634 T: TypeFoldable<TyCtxt<'tcx>>,
635 {
636 trace!("monomorphize: self.instance={:?}", self.instance);
637 self.instance.instantiate_mir_and_normalize_erasing_regions(
638 self.tcx,
639 ty::TypingEnv::fully_monomorphized(),
640 ty::EarlyBinder::bind(value),
641 )
642 }
643
644 fn eval_constant(
646 &mut self,
647 constant: &mir::ConstOperand<'tcx>,
648 ) -> Option<mir::ConstValue<'tcx>> {
649 let const_ = self.monomorphize(constant.const_);
650 match const_.eval(self.tcx, ty::TypingEnv::fully_monomorphized(), constant.span) {
655 Ok(v) => Some(v),
656 Err(ErrorHandled::TooGeneric(..)) => span_bug!(
657 constant.span,
658 "collection encountered polymorphic constant: {:?}",
659 const_
660 ),
661 Err(err @ ErrorHandled::Reported(..)) => {
662 err.emit_note(self.tcx);
663 return None;
664 }
665 }
666 }
667}
668
669impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
670 fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
671 debug!("visiting rvalue {:?}", *rvalue);
672
673 let span = self.body.source_info(location).span;
674
675 match *rvalue {
676 mir::Rvalue::Cast(
680 mir::CastKind::PointerCoercion(PointerCoercion::Unsize, _)
681 | mir::CastKind::PointerCoercion(PointerCoercion::DynStar, _),
682 ref operand,
683 target_ty,
684 ) => {
685 let source_ty = operand.ty(self.body, self.tcx);
686 self.used_mentioned_items
688 .insert(MentionedItem::UnsizeCast { source_ty, target_ty });
689 let target_ty = self.monomorphize(target_ty);
690 let source_ty = self.monomorphize(source_ty);
691 let (source_ty, target_ty) =
692 find_vtable_types_for_unsizing(self.tcx.at(span), source_ty, target_ty);
693 if (target_ty.is_trait() && !source_ty.is_trait())
697 || (target_ty.is_dyn_star() && !source_ty.is_dyn_star())
698 {
699 create_mono_items_for_vtable_methods(
700 self.tcx,
701 target_ty,
702 source_ty,
703 span,
704 self.used_items,
705 );
706 }
707 }
708 mir::Rvalue::Cast(
709 mir::CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, _),
710 ref operand,
711 _,
712 ) => {
713 let fn_ty = operand.ty(self.body, self.tcx);
714 self.used_mentioned_items.insert(MentionedItem::Fn(fn_ty));
716 let fn_ty = self.monomorphize(fn_ty);
717 visit_fn_use(self.tcx, fn_ty, false, span, self.used_items);
718 }
719 mir::Rvalue::Cast(
720 mir::CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_), _),
721 ref operand,
722 _,
723 ) => {
724 let source_ty = operand.ty(self.body, self.tcx);
725 self.used_mentioned_items.insert(MentionedItem::Closure(source_ty));
727 let source_ty = self.monomorphize(source_ty);
728 if let ty::Closure(def_id, args) = *source_ty.kind() {
729 let instance =
730 Instance::resolve_closure(self.tcx, def_id, args, ty::ClosureKind::FnOnce);
731 if self.tcx.should_codegen_locally(instance) {
732 self.used_items.push(create_fn_mono_item(self.tcx, instance, span));
733 }
734 } else {
735 bug!()
736 }
737 }
738 mir::Rvalue::ThreadLocalRef(def_id) => {
739 assert!(self.tcx.is_thread_local_static(def_id));
740 let instance = Instance::mono(self.tcx, def_id);
741 if self.tcx.should_codegen_locally(instance) {
742 trace!("collecting thread-local static {:?}", def_id);
743 self.used_items.push(respan(span, MonoItem::Static(def_id)));
744 }
745 }
746 _ => { }
747 }
748
749 self.super_rvalue(rvalue, location);
750 }
751
752 #[instrument(skip(self), level = "debug")]
755 fn visit_const_operand(&mut self, constant: &mir::ConstOperand<'tcx>, location: Location) {
756 let Some(val) = self.eval_constant(constant) else { return };
758 collect_const_value(self.tcx, val, self.used_items);
759 }
760
761 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
762 debug!("visiting terminator {:?} @ {:?}", terminator, location);
763 let source = self.body.source_info(location).span;
764
765 let tcx = self.tcx;
766 let push_mono_lang_item = |this: &mut Self, lang_item: LangItem| {
767 let instance = Instance::mono(tcx, tcx.require_lang_item(lang_item, Some(source)));
768 if tcx.should_codegen_locally(instance) {
769 this.used_items.push(create_fn_mono_item(tcx, instance, source));
770 }
771 };
772
773 match terminator.kind {
774 mir::TerminatorKind::Call { ref func, .. }
775 | mir::TerminatorKind::TailCall { ref func, .. } => {
776 let callee_ty = func.ty(self.body, tcx);
777 self.used_mentioned_items.insert(MentionedItem::Fn(callee_ty));
779 let callee_ty = self.monomorphize(callee_ty);
780 visit_fn_use(self.tcx, callee_ty, true, source, &mut self.used_items)
781 }
782 mir::TerminatorKind::Drop { ref place, .. } => {
783 let ty = place.ty(self.body, self.tcx).ty;
784 self.used_mentioned_items.insert(MentionedItem::Drop(ty));
786 let ty = self.monomorphize(ty);
787 visit_drop_use(self.tcx, ty, true, source, self.used_items);
788 }
789 mir::TerminatorKind::InlineAsm { ref operands, .. } => {
790 for op in operands {
791 match *op {
792 mir::InlineAsmOperand::SymFn { ref value } => {
793 let fn_ty = value.const_.ty();
794 self.used_mentioned_items.insert(MentionedItem::Fn(fn_ty));
796 let fn_ty = self.monomorphize(fn_ty);
797 visit_fn_use(self.tcx, fn_ty, false, source, self.used_items);
798 }
799 mir::InlineAsmOperand::SymStatic { def_id } => {
800 let instance = Instance::mono(self.tcx, def_id);
801 if self.tcx.should_codegen_locally(instance) {
802 trace!("collecting asm sym static {:?}", def_id);
803 self.used_items.push(respan(source, MonoItem::Static(def_id)));
804 }
805 }
806 _ => {}
807 }
808 }
809 }
810 mir::TerminatorKind::Assert { ref msg, .. } => match &**msg {
811 mir::AssertKind::BoundsCheck { .. } => {
812 push_mono_lang_item(self, LangItem::PanicBoundsCheck);
813 }
814 mir::AssertKind::MisalignedPointerDereference { .. } => {
815 push_mono_lang_item(self, LangItem::PanicMisalignedPointerDereference);
816 }
817 mir::AssertKind::NullPointerDereference => {
818 push_mono_lang_item(self, LangItem::PanicNullPointerDereference);
819 }
820 _ => {
821 push_mono_lang_item(self, msg.panic_function());
822 }
823 },
824 mir::TerminatorKind::UnwindTerminate(reason) => {
825 push_mono_lang_item(self, reason.lang_item());
826 }
827 mir::TerminatorKind::Goto { .. }
828 | mir::TerminatorKind::SwitchInt { .. }
829 | mir::TerminatorKind::UnwindResume
830 | mir::TerminatorKind::Return
831 | mir::TerminatorKind::Unreachable => {}
832 mir::TerminatorKind::CoroutineDrop
833 | mir::TerminatorKind::Yield { .. }
834 | mir::TerminatorKind::FalseEdge { .. }
835 | mir::TerminatorKind::FalseUnwind { .. } => bug!(),
836 }
837
838 if let Some(mir::UnwindAction::Terminate(reason)) = terminator.unwind() {
839 push_mono_lang_item(self, reason.lang_item());
840 }
841
842 self.super_terminator(terminator, location);
843 }
844}
845
846fn visit_drop_use<'tcx>(
847 tcx: TyCtxt<'tcx>,
848 ty: Ty<'tcx>,
849 is_direct_call: bool,
850 source: Span,
851 output: &mut MonoItems<'tcx>,
852) {
853 let instance = Instance::resolve_drop_in_place(tcx, ty);
854 visit_instance_use(tcx, instance, is_direct_call, source, output);
855}
856
857fn visit_fn_use<'tcx>(
860 tcx: TyCtxt<'tcx>,
861 ty: Ty<'tcx>,
862 is_direct_call: bool,
863 source: Span,
864 output: &mut MonoItems<'tcx>,
865) {
866 if let ty::FnDef(def_id, args) = *ty.kind() {
867 let instance = if is_direct_call {
868 ty::Instance::expect_resolve(
869 tcx,
870 ty::TypingEnv::fully_monomorphized(),
871 def_id,
872 args,
873 source,
874 )
875 } else {
876 match ty::Instance::resolve_for_fn_ptr(
877 tcx,
878 ty::TypingEnv::fully_monomorphized(),
879 def_id,
880 args,
881 ) {
882 Some(instance) => instance,
883 _ => bug!("failed to resolve instance for {ty}"),
884 }
885 };
886 visit_instance_use(tcx, instance, is_direct_call, source, output);
887 }
888}
889
890fn visit_instance_use<'tcx>(
891 tcx: TyCtxt<'tcx>,
892 instance: ty::Instance<'tcx>,
893 is_direct_call: bool,
894 source: Span,
895 output: &mut MonoItems<'tcx>,
896) {
897 debug!("visit_item_use({:?}, is_direct_call={:?})", instance, is_direct_call);
898 if !tcx.should_codegen_locally(instance) {
899 return;
900 }
901 if let Some(intrinsic) = tcx.intrinsic(instance.def_id()) {
902 if let Some(_requirement) = ValidityRequirement::from_intrinsic(intrinsic.name) {
903 let def_id = tcx.require_lang_item(LangItem::PanicNounwind, None);
908 let panic_instance = Instance::mono(tcx, def_id);
909 if tcx.should_codegen_locally(panic_instance) {
910 output.push(create_fn_mono_item(tcx, panic_instance, source));
911 }
912 } else if !intrinsic.must_be_overridden {
913 let instance = ty::Instance::new(instance.def_id(), instance.args);
918 if tcx.should_codegen_locally(instance) {
919 output.push(create_fn_mono_item(tcx, instance, source));
920 }
921 }
922 }
923
924 match instance.def {
925 ty::InstanceKind::Virtual(..) | ty::InstanceKind::Intrinsic(_) => {
926 if !is_direct_call {
927 bug!("{:?} being reified", instance);
928 }
929 }
930 ty::InstanceKind::ThreadLocalShim(..) => {
931 bug!("{:?} being reified", instance);
932 }
933 ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) => {
934 if !is_direct_call {
936 output.push(create_fn_mono_item(tcx, instance, source));
937 }
938 }
939 ty::InstanceKind::DropGlue(_, Some(_))
940 | ty::InstanceKind::AsyncDropGlueCtorShim(_, Some(_))
941 | ty::InstanceKind::VTableShim(..)
942 | ty::InstanceKind::ReifyShim(..)
943 | ty::InstanceKind::ClosureOnceShim { .. }
944 | ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
945 | ty::InstanceKind::Item(..)
946 | ty::InstanceKind::FnPtrShim(..)
947 | ty::InstanceKind::CloneShim(..)
948 | ty::InstanceKind::FnPtrAddrShim(..) => {
949 output.push(create_fn_mono_item(tcx, instance, source));
950 }
951 }
952}
953
954fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
957 let Some(def_id) = instance.def.def_id_if_not_guaranteed_local_codegen() else {
958 return true;
959 };
960
961 if tcx.is_foreign_item(def_id) {
962 return false;
964 }
965
966 if tcx.def_kind(def_id).has_codegen_attrs()
967 && matches!(tcx.codegen_fn_attrs(def_id).inline, InlineAttr::Force { .. })
968 {
969 tcx.delay_bug("attempt to codegen `#[rustc_force_inline]` item");
972 }
973
974 if def_id.is_local() {
975 return true;
977 }
978
979 if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(tcx).is_some() {
980 return false;
982 }
983
984 if let DefKind::Static { .. } = tcx.def_kind(def_id) {
985 return false;
987 }
988
989 if !tcx.is_mir_available(def_id) {
990 tcx.dcx().emit_fatal(NoOptimizedMir {
991 span: tcx.def_span(def_id),
992 crate_name: tcx.crate_name(def_id.krate),
993 });
994 }
995
996 true
997}
998
999fn find_vtable_types_for_unsizing<'tcx>(
1041 tcx: TyCtxtAt<'tcx>,
1042 source_ty: Ty<'tcx>,
1043 target_ty: Ty<'tcx>,
1044) -> (Ty<'tcx>, Ty<'tcx>) {
1045 let ptr_vtable = |inner_source: Ty<'tcx>, inner_target: Ty<'tcx>| {
1046 let typing_env = ty::TypingEnv::fully_monomorphized();
1047 let type_has_metadata = |ty: Ty<'tcx>| -> bool {
1048 if ty.is_sized(tcx.tcx, typing_env) {
1049 return false;
1050 }
1051 let tail = tcx.struct_tail_for_codegen(ty, typing_env);
1052 match tail.kind() {
1053 ty::Foreign(..) => false,
1054 ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
1055 _ => bug!("unexpected unsized tail: {:?}", tail),
1056 }
1057 };
1058 if type_has_metadata(inner_source) {
1059 (inner_source, inner_target)
1060 } else {
1061 tcx.struct_lockstep_tails_for_codegen(inner_source, inner_target, typing_env)
1062 }
1063 };
1064
1065 match (source_ty.kind(), target_ty.kind()) {
1066 (&ty::Ref(_, a, _), &ty::Ref(_, b, _) | &ty::RawPtr(b, _))
1067 | (&ty::RawPtr(a, _), &ty::RawPtr(b, _)) => ptr_vtable(a, b),
1068 (_, _)
1069 if let Some(source_boxed) = source_ty.boxed_ty()
1070 && let Some(target_boxed) = target_ty.boxed_ty() =>
1071 {
1072 ptr_vtable(source_boxed, target_boxed)
1073 }
1074
1075 (_, &ty::Dynamic(_, _, ty::DynStar)) => ptr_vtable(source_ty, target_ty),
1077
1078 (&ty::Adt(source_adt_def, source_args), &ty::Adt(target_adt_def, target_args)) => {
1079 assert_eq!(source_adt_def, target_adt_def);
1080
1081 let CustomCoerceUnsized::Struct(coerce_index) =
1082 match crate::custom_coerce_unsize_info(tcx, source_ty, target_ty) {
1083 Ok(ccu) => ccu,
1084 Err(e) => {
1085 let e = Ty::new_error(tcx.tcx, e);
1086 return (e, e);
1087 }
1088 };
1089
1090 let source_fields = &source_adt_def.non_enum_variant().fields;
1091 let target_fields = &target_adt_def.non_enum_variant().fields;
1092
1093 assert!(
1094 coerce_index.index() < source_fields.len()
1095 && source_fields.len() == target_fields.len()
1096 );
1097
1098 find_vtable_types_for_unsizing(
1099 tcx,
1100 source_fields[coerce_index].ty(*tcx, source_args),
1101 target_fields[coerce_index].ty(*tcx, target_args),
1102 )
1103 }
1104 _ => bug!(
1105 "find_vtable_types_for_unsizing: invalid coercion {:?} -> {:?}",
1106 source_ty,
1107 target_ty
1108 ),
1109 }
1110}
1111
1112#[instrument(skip(tcx), level = "debug", ret)]
1113fn create_fn_mono_item<'tcx>(
1114 tcx: TyCtxt<'tcx>,
1115 instance: Instance<'tcx>,
1116 source: Span,
1117) -> Spanned<MonoItem<'tcx>> {
1118 let def_id = instance.def_id();
1119 if tcx.sess.opts.unstable_opts.profile_closures
1120 && def_id.is_local()
1121 && tcx.is_closure_like(def_id)
1122 {
1123 crate::util::dump_closure_profile(tcx, instance);
1124 }
1125
1126 respan(source, MonoItem::Fn(instance))
1127}
1128
1129fn create_mono_items_for_vtable_methods<'tcx>(
1132 tcx: TyCtxt<'tcx>,
1133 trait_ty: Ty<'tcx>,
1134 impl_ty: Ty<'tcx>,
1135 source: Span,
1136 output: &mut MonoItems<'tcx>,
1137) {
1138 assert!(!trait_ty.has_escaping_bound_vars() && !impl_ty.has_escaping_bound_vars());
1139
1140 let ty::Dynamic(trait_ty, ..) = trait_ty.kind() else {
1141 bug!("create_mono_items_for_vtable_methods: {trait_ty:?} not a trait type");
1142 };
1143 if let Some(principal) = trait_ty.principal() {
1144 let trait_ref =
1145 tcx.instantiate_bound_regions_with_erased(principal.with_self_ty(tcx, impl_ty));
1146 assert!(!trait_ref.has_escaping_bound_vars());
1147
1148 let entries = tcx.vtable_entries(trait_ref);
1150 debug!(?entries);
1151 let methods = entries
1152 .iter()
1153 .filter_map(|entry| match entry {
1154 VtblEntry::MetadataDropInPlace
1155 | VtblEntry::MetadataSize
1156 | VtblEntry::MetadataAlign
1157 | VtblEntry::Vacant => None,
1158 VtblEntry::TraitVPtr(_) => {
1159 None
1161 }
1162 VtblEntry::Method(instance) => {
1163 Some(*instance).filter(|instance| tcx.should_codegen_locally(*instance))
1164 }
1165 })
1166 .map(|item| create_fn_mono_item(tcx, item, source));
1167 output.extend(methods);
1168 }
1169
1170 visit_drop_use(tcx, impl_ty, false, source, output);
1172}
1173
1174fn collect_alloc<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut MonoItems<'tcx>) {
1176 match tcx.global_alloc(alloc_id) {
1177 GlobalAlloc::Static(def_id) => {
1178 assert!(!tcx.is_thread_local_static(def_id));
1179 let instance = Instance::mono(tcx, def_id);
1180 if tcx.should_codegen_locally(instance) {
1181 trace!("collecting static {:?}", def_id);
1182 output.push(dummy_spanned(MonoItem::Static(def_id)));
1183 }
1184 }
1185 GlobalAlloc::Memory(alloc) => {
1186 trace!("collecting {:?} with {:#?}", alloc_id, alloc);
1187 let ptrs = alloc.inner().provenance().ptrs();
1188 if !ptrs.is_empty() {
1190 rustc_data_structures::stack::ensure_sufficient_stack(move || {
1191 for &prov in ptrs.values() {
1192 collect_alloc(tcx, prov.alloc_id(), output);
1193 }
1194 });
1195 }
1196 }
1197 GlobalAlloc::Function { instance, .. } => {
1198 if tcx.should_codegen_locally(instance) {
1199 trace!("collecting {:?} with {:#?}", alloc_id, instance);
1200 output.push(create_fn_mono_item(tcx, instance, DUMMY_SP));
1201 }
1202 }
1203 GlobalAlloc::VTable(ty, dyn_ty) => {
1204 let alloc_id = tcx.vtable_allocation((
1205 ty,
1206 dyn_ty
1207 .principal()
1208 .map(|principal| tcx.instantiate_bound_regions_with_erased(principal)),
1209 ));
1210 collect_alloc(tcx, alloc_id, output)
1211 }
1212 }
1213}
1214
1215#[instrument(skip(tcx), level = "debug")]
1219fn collect_items_of_instance<'tcx>(
1220 tcx: TyCtxt<'tcx>,
1221 instance: Instance<'tcx>,
1222 mode: CollectionMode,
1223) -> (MonoItems<'tcx>, MonoItems<'tcx>) {
1224 tcx.ensure_ok().check_mono_item(instance);
1226
1227 let body = tcx.instance_mir(instance.def);
1228 let mut used_items = MonoItems::new();
1239 let mut mentioned_items = MonoItems::new();
1240 let mut used_mentioned_items = Default::default();
1241 let mut collector = MirUsedCollector {
1242 tcx,
1243 body,
1244 used_items: &mut used_items,
1245 used_mentioned_items: &mut used_mentioned_items,
1246 instance,
1247 };
1248
1249 if mode == CollectionMode::UsedItems {
1250 for (bb, data) in traversal::mono_reachable(body, tcx, instance) {
1251 collector.visit_basic_block_data(bb, data)
1252 }
1253 }
1254
1255 for const_op in body.required_consts() {
1258 if let Some(val) = collector.eval_constant(const_op) {
1259 collect_const_value(tcx, val, &mut mentioned_items);
1260 }
1261 }
1262
1263 for item in body.mentioned_items() {
1266 if !collector.used_mentioned_items.contains(&item.node) {
1267 let item_mono = collector.monomorphize(item.node);
1268 visit_mentioned_item(tcx, &item_mono, item.span, &mut mentioned_items);
1269 }
1270 }
1271
1272 (used_items, mentioned_items)
1273}
1274
1275fn items_of_instance<'tcx>(
1276 tcx: TyCtxt<'tcx>,
1277 (instance, mode): (Instance<'tcx>, CollectionMode),
1278) -> (&'tcx [Spanned<MonoItem<'tcx>>], &'tcx [Spanned<MonoItem<'tcx>>]) {
1279 let (used_items, mentioned_items) = collect_items_of_instance(tcx, instance, mode);
1280
1281 let used_items = tcx.arena.alloc_from_iter(used_items);
1282 let mentioned_items = tcx.arena.alloc_from_iter(mentioned_items);
1283
1284 (used_items, mentioned_items)
1285}
1286
1287#[instrument(skip(tcx, span, output), level = "debug")]
1289fn visit_mentioned_item<'tcx>(
1290 tcx: TyCtxt<'tcx>,
1291 item: &MentionedItem<'tcx>,
1292 span: Span,
1293 output: &mut MonoItems<'tcx>,
1294) {
1295 match *item {
1296 MentionedItem::Fn(ty) => {
1297 if let ty::FnDef(def_id, args) = *ty.kind() {
1298 let instance = Instance::expect_resolve(
1299 tcx,
1300 ty::TypingEnv::fully_monomorphized(),
1301 def_id,
1302 args,
1303 span,
1304 );
1305 visit_instance_use(tcx, instance, true, span, output);
1310 }
1311 }
1312 MentionedItem::Drop(ty) => {
1313 visit_drop_use(tcx, ty, true, span, output);
1314 }
1315 MentionedItem::UnsizeCast { source_ty, target_ty } => {
1316 let (source_ty, target_ty) =
1317 find_vtable_types_for_unsizing(tcx.at(span), source_ty, target_ty);
1318 if (target_ty.is_trait() && !source_ty.is_trait())
1322 || (target_ty.is_dyn_star() && !source_ty.is_dyn_star())
1323 {
1324 create_mono_items_for_vtable_methods(tcx, target_ty, source_ty, span, output);
1325 }
1326 }
1327 MentionedItem::Closure(source_ty) => {
1328 if let ty::Closure(def_id, args) = *source_ty.kind() {
1329 let instance =
1330 Instance::resolve_closure(tcx, def_id, args, ty::ClosureKind::FnOnce);
1331 if tcx.should_codegen_locally(instance) {
1332 output.push(create_fn_mono_item(tcx, instance, span));
1333 }
1334 } else {
1335 bug!()
1336 }
1337 }
1338 }
1339}
1340
1341#[instrument(skip(tcx, output), level = "debug")]
1342fn collect_const_value<'tcx>(
1343 tcx: TyCtxt<'tcx>,
1344 value: mir::ConstValue<'tcx>,
1345 output: &mut MonoItems<'tcx>,
1346) {
1347 match value {
1348 mir::ConstValue::Scalar(Scalar::Ptr(ptr, _size)) => {
1349 collect_alloc(tcx, ptr.provenance.alloc_id(), output)
1350 }
1351 mir::ConstValue::Indirect { alloc_id, .. } => collect_alloc(tcx, alloc_id, output),
1352 mir::ConstValue::Slice { data, meta: _ } => {
1353 for &prov in data.inner().provenance().ptrs().values() {
1354 collect_alloc(tcx, prov.alloc_id(), output);
1355 }
1356 }
1357 _ => {}
1358 }
1359}
1360
1361#[instrument(skip(tcx, mode), level = "debug")]
1368fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionStrategy) -> Vec<MonoItem<'_>> {
1369 debug!("collecting roots");
1370 let mut roots = MonoItems::new();
1371
1372 {
1373 let entry_fn = tcx.entry_fn(());
1374
1375 debug!("collect_roots: entry_fn = {:?}", entry_fn);
1376
1377 let mut collector = RootCollector { tcx, strategy: mode, entry_fn, output: &mut roots };
1378
1379 let crate_items = tcx.hir_crate_items(());
1380
1381 for id in crate_items.free_items() {
1382 collector.process_item(id);
1383 }
1384
1385 for id in crate_items.impl_items() {
1386 collector.process_impl_item(id);
1387 }
1388
1389 for id in crate_items.nested_bodies() {
1390 collector.process_nested_body(id);
1391 }
1392
1393 collector.push_extra_entry_roots();
1394 }
1395
1396 roots
1400 .into_iter()
1401 .filter_map(|Spanned { node: mono_item, .. }| {
1402 mono_item.is_instantiable(tcx).then_some(mono_item)
1403 })
1404 .collect()
1405}
1406
1407struct RootCollector<'a, 'tcx> {
1408 tcx: TyCtxt<'tcx>,
1409 strategy: MonoItemCollectionStrategy,
1410 output: &'a mut MonoItems<'tcx>,
1411 entry_fn: Option<(DefId, EntryFnType)>,
1412}
1413
1414impl<'v> RootCollector<'_, 'v> {
1415 fn process_item(&mut self, id: hir::ItemId) {
1416 match self.tcx.def_kind(id.owner_id) {
1417 DefKind::Enum | DefKind::Struct | DefKind::Union => {
1418 if self.strategy == MonoItemCollectionStrategy::Eager
1419 && !self.tcx.generics_of(id.owner_id).requires_monomorphization(self.tcx)
1420 {
1421 debug!("RootCollector: ADT drop-glue for `{id:?}`",);
1422 let id_args =
1423 ty::GenericArgs::for_item(self.tcx, id.owner_id.to_def_id(), |param, _| {
1424 match param.kind {
1425 GenericParamDefKind::Lifetime => {
1426 self.tcx.lifetimes.re_erased.into()
1427 }
1428 GenericParamDefKind::Type { .. }
1429 | GenericParamDefKind::Const { .. } => {
1430 unreachable!(
1431 "`own_requires_monomorphization` check means that \
1432 we should have no type/const params"
1433 )
1434 }
1435 }
1436 });
1437
1438 if self.tcx.instantiate_and_check_impossible_predicates((
1441 id.owner_id.to_def_id(),
1442 id_args,
1443 )) {
1444 return;
1445 }
1446
1447 let ty =
1448 self.tcx.type_of(id.owner_id.to_def_id()).instantiate(self.tcx, id_args);
1449 assert!(!ty.has_non_region_param());
1450 visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output);
1451 }
1452 }
1453 DefKind::GlobalAsm => {
1454 debug!(
1455 "RootCollector: ItemKind::GlobalAsm({})",
1456 self.tcx.def_path_str(id.owner_id)
1457 );
1458 self.output.push(dummy_spanned(MonoItem::GlobalAsm(id)));
1459 }
1460 DefKind::Static { .. } => {
1461 let def_id = id.owner_id.to_def_id();
1462 debug!("RootCollector: ItemKind::Static({})", self.tcx.def_path_str(def_id));
1463 self.output.push(dummy_spanned(MonoItem::Static(def_id)));
1464 }
1465 DefKind::Const => {
1466 if !self.tcx.generics_of(id.owner_id).requires_monomorphization(self.tcx)
1472 && let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id())
1473 {
1474 collect_const_value(self.tcx, val, self.output);
1475 }
1476 }
1477 DefKind::Impl { .. } => {
1478 if self.strategy == MonoItemCollectionStrategy::Eager {
1479 create_mono_items_for_default_impls(self.tcx, id, self.output);
1480 }
1481 }
1482 DefKind::Fn => {
1483 self.push_if_root(id.owner_id.def_id);
1484 }
1485 _ => {}
1486 }
1487 }
1488
1489 fn process_impl_item(&mut self, id: hir::ImplItemId) {
1490 if matches!(self.tcx.def_kind(id.owner_id), DefKind::AssocFn) {
1491 self.push_if_root(id.owner_id.def_id);
1492 }
1493 }
1494
1495 fn process_nested_body(&mut self, def_id: LocalDefId) {
1496 match self.tcx.def_kind(def_id) {
1497 DefKind::Closure => {
1498 if self.strategy == MonoItemCollectionStrategy::Eager
1499 && !self
1500 .tcx
1501 .generics_of(self.tcx.typeck_root_def_id(def_id.to_def_id()))
1502 .requires_monomorphization(self.tcx)
1503 {
1504 let instance = match *self.tcx.type_of(def_id).instantiate_identity().kind() {
1505 ty::Closure(def_id, args)
1506 | ty::Coroutine(def_id, args)
1507 | ty::CoroutineClosure(def_id, args) => {
1508 Instance::new(def_id, self.tcx.erase_regions(args))
1509 }
1510 _ => unreachable!(),
1511 };
1512 let Ok(instance) = self.tcx.try_normalize_erasing_regions(
1513 ty::TypingEnv::fully_monomorphized(),
1514 instance,
1515 ) else {
1516 return;
1518 };
1519 let mono_item = create_fn_mono_item(self.tcx, instance, DUMMY_SP);
1520 if mono_item.node.is_instantiable(self.tcx) {
1521 self.output.push(mono_item);
1522 }
1523 }
1524 }
1525 _ => {}
1526 }
1527 }
1528
1529 fn is_root(&self, def_id: LocalDefId) -> bool {
1530 !self.tcx.generics_of(def_id).requires_monomorphization(self.tcx)
1531 && match self.strategy {
1532 MonoItemCollectionStrategy::Eager => {
1533 !matches!(self.tcx.codegen_fn_attrs(def_id).inline, InlineAttr::Force { .. })
1534 }
1535 MonoItemCollectionStrategy::Lazy => {
1536 self.entry_fn.and_then(|(id, _)| id.as_local()) == Some(def_id)
1537 || self.tcx.is_reachable_non_generic(def_id)
1538 || self
1539 .tcx
1540 .codegen_fn_attrs(def_id)
1541 .flags
1542 .contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
1543 }
1544 }
1545 }
1546
1547 #[instrument(skip(self), level = "debug")]
1550 fn push_if_root(&mut self, def_id: LocalDefId) {
1551 if self.is_root(def_id) {
1552 debug!("found root");
1553
1554 let instance = Instance::mono(self.tcx, def_id.to_def_id());
1555 self.output.push(create_fn_mono_item(self.tcx, instance, DUMMY_SP));
1556 }
1557 }
1558
1559 fn push_extra_entry_roots(&mut self) {
1565 let Some((main_def_id, EntryFnType::Main { .. })) = self.entry_fn else {
1566 return;
1567 };
1568
1569 let Some(start_def_id) = self.tcx.lang_items().start_fn() else {
1570 self.tcx.dcx().emit_fatal(errors::StartNotFound);
1571 };
1572 let main_ret_ty = self.tcx.fn_sig(main_def_id).no_bound_vars().unwrap().output();
1573
1574 let main_ret_ty = self.tcx.normalize_erasing_regions(
1580 ty::TypingEnv::fully_monomorphized(),
1581 main_ret_ty.no_bound_vars().unwrap(),
1582 );
1583
1584 let start_instance = Instance::expect_resolve(
1585 self.tcx,
1586 ty::TypingEnv::fully_monomorphized(),
1587 start_def_id,
1588 self.tcx.mk_args(&[main_ret_ty.into()]),
1589 DUMMY_SP,
1590 );
1591
1592 self.output.push(create_fn_mono_item(self.tcx, start_instance, DUMMY_SP));
1593 }
1594}
1595
1596#[instrument(level = "debug", skip(tcx, output))]
1597fn create_mono_items_for_default_impls<'tcx>(
1598 tcx: TyCtxt<'tcx>,
1599 item: hir::ItemId,
1600 output: &mut MonoItems<'tcx>,
1601) {
1602 let Some(impl_) = tcx.impl_trait_header(item.owner_id) else {
1603 return;
1604 };
1605
1606 if matches!(impl_.polarity, ty::ImplPolarity::Negative) {
1607 return;
1608 }
1609
1610 if tcx.generics_of(item.owner_id).own_requires_monomorphization() {
1611 return;
1612 }
1613
1614 let only_region_params = |param: &ty::GenericParamDef, _: &_| match param.kind {
1620 GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
1621 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
1622 unreachable!(
1623 "`own_requires_monomorphization` check means that \
1624 we should have no type/const params"
1625 )
1626 }
1627 };
1628 let impl_args = GenericArgs::for_item(tcx, item.owner_id.to_def_id(), only_region_params);
1629 let trait_ref = impl_.trait_ref.instantiate(tcx, impl_args);
1630
1631 if tcx.instantiate_and_check_impossible_predicates((item.owner_id.to_def_id(), impl_args)) {
1641 return;
1642 }
1643
1644 let typing_env = ty::TypingEnv::fully_monomorphized();
1645 let trait_ref = tcx.normalize_erasing_regions(typing_env, trait_ref);
1646 let overridden_methods = tcx.impl_item_implementor_ids(item.owner_id);
1647 for method in tcx.provided_trait_methods(trait_ref.def_id) {
1648 if overridden_methods.contains_key(&method.def_id) {
1649 continue;
1650 }
1651
1652 if tcx.generics_of(method.def_id).own_requires_monomorphization() {
1653 continue;
1654 }
1655
1656 let args = trait_ref.args.extend_to(tcx, method.def_id, only_region_params);
1660 let instance = ty::Instance::expect_resolve(tcx, typing_env, method.def_id, args, DUMMY_SP);
1661
1662 let mono_item = create_fn_mono_item(tcx, instance, DUMMY_SP);
1663 if mono_item.node.is_instantiable(tcx) && tcx.should_codegen_locally(instance) {
1664 output.push(mono_item);
1665 }
1666 }
1667}
1668
1669#[instrument(skip(tcx, strategy), level = "debug")]
1674pub(crate) fn collect_crate_mono_items<'tcx>(
1675 tcx: TyCtxt<'tcx>,
1676 strategy: MonoItemCollectionStrategy,
1677) -> (Vec<MonoItem<'tcx>>, UsageMap<'tcx>) {
1678 let _prof_timer = tcx.prof.generic_activity("monomorphization_collector");
1679
1680 let roots = tcx
1681 .sess
1682 .time("monomorphization_collector_root_collections", || collect_roots(tcx, strategy));
1683
1684 debug!("building mono item graph, beginning at roots");
1685
1686 let mut state = SharedState {
1687 visited: MTLock::new(UnordSet::default()),
1688 mentioned: MTLock::new(UnordSet::default()),
1689 usage_map: MTLock::new(UsageMap::new()),
1690 };
1691 let recursion_limit = tcx.recursion_limit();
1692
1693 {
1694 let state: LRef<'_, _> = &mut state;
1695
1696 tcx.sess.time("monomorphization_collector_graph_walk", || {
1697 par_for_each_in(roots, |root| {
1698 let mut recursion_depths = DefIdMap::default();
1699 collect_items_rec(
1700 tcx,
1701 dummy_spanned(root),
1702 state,
1703 &mut recursion_depths,
1704 recursion_limit,
1705 CollectionMode::UsedItems,
1706 );
1707 });
1708 });
1709 }
1710
1711 let mono_items = tcx.with_stable_hashing_context(move |ref hcx| {
1714 state.visited.into_inner().into_sorted(hcx, true)
1715 });
1716
1717 (mono_items, state.usage_map.into_inner())
1718}
1719
1720pub(crate) fn provide(providers: &mut Providers) {
1721 providers.hooks.should_codegen_locally = should_codegen_locally;
1722 providers.items_of_instance = items_of_instance;
1723}