1use std::cmp;
96use std::collections::hash_map::Entry;
97use std::fs::{self, File};
98use std::io::Write;
99use std::path::{Path, PathBuf};
100
101use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
102use rustc_data_structures::sync;
103use rustc_data_structures::unord::{UnordMap, UnordSet};
104use rustc_hir::LangItem;
105use rustc_hir::attrs::{InlineAttr, Linkage};
106use rustc_hir::def::DefKind;
107use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE};
108use rustc_hir::definitions::DefPathDataName;
109use rustc_middle::bug;
110use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
111use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
112use rustc_middle::mir::mono::{
113 CodegenUnit, CodegenUnitNameBuilder, InstantiationMode, MonoItem, MonoItemData,
114 MonoItemPartitions, Visibility,
115};
116use rustc_middle::ty::print::{characteristic_def_id_of_type, with_no_trimmed_paths};
117use rustc_middle::ty::{self, InstanceKind, TyCtxt};
118use rustc_middle::util::Providers;
119use rustc_session::CodegenUnits;
120use rustc_session::config::{DumpMonoStatsFormat, SwitchWithOptPath};
121use rustc_span::Symbol;
122use rustc_target::spec::SymbolVisibility;
123use tracing::debug;
124
125use crate::collector::{self, MonoItemCollectionStrategy, UsageMap};
126use crate::errors::{CouldntDumpMonoStats, SymbolAlreadyDefined};
127
128struct PartitioningCx<'a, 'tcx> {
129 tcx: TyCtxt<'tcx>,
130 usage_map: &'a UsageMap<'tcx>,
131}
132
133struct PlacedMonoItems<'tcx> {
134 codegen_units: Vec<CodegenUnit<'tcx>>,
136
137 internalization_candidates: UnordSet<MonoItem<'tcx>>,
138}
139
140fn partition<'tcx, I>(
142 tcx: TyCtxt<'tcx>,
143 mono_items: I,
144 usage_map: &UsageMap<'tcx>,
145) -> Vec<CodegenUnit<'tcx>>
146where
147 I: Iterator<Item = MonoItem<'tcx>>,
148{
149 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning");
150
151 let cx = &PartitioningCx { tcx, usage_map };
152
153 let PlacedMonoItems { mut codegen_units, internalization_candidates } = {
156 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_items");
157 let placed = place_mono_items(cx, mono_items);
158
159 debug_dump(tcx, "PLACE", &placed.codegen_units);
160
161 placed
162 };
163
164 {
168 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_merge_cgus");
169 merge_codegen_units(cx, &mut codegen_units);
170 debug_dump(tcx, "MERGE", &codegen_units);
171 }
172
173 if !tcx.sess.link_dead_code() {
176 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_internalize_symbols");
177 internalize_symbols(cx, &mut codegen_units, internalization_candidates);
178
179 debug_dump(tcx, "INTERNALIZE", &codegen_units);
180 }
181
182 if tcx.sess.instrument_coverage() {
184 mark_code_coverage_dead_code_cgu(&mut codegen_units);
185 }
186
187 if !codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()) {
189 let mut names = String::new();
190 for cgu in codegen_units.iter() {
191 names += &format!("- {}\n", cgu.name());
192 }
193 bug!("unsorted CGUs:\n{names}");
194 }
195
196 codegen_units
197}
198
199fn place_mono_items<'tcx, I>(cx: &PartitioningCx<'_, 'tcx>, mono_items: I) -> PlacedMonoItems<'tcx>
200where
201 I: Iterator<Item = MonoItem<'tcx>>,
202{
203 let mut codegen_units = UnordMap::default();
204 let is_incremental_build = cx.tcx.sess.opts.incremental.is_some();
205 let mut internalization_candidates = UnordSet::default();
206
207 let can_export_generics = cx.tcx.local_crate_exports_generics();
212 let always_export_generics = can_export_generics && cx.tcx.sess.opts.share_generics();
213
214 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
215 let cgu_name_cache = &mut UnordMap::default();
216
217 for mono_item in mono_items {
218 match mono_item.instantiation_mode(cx.tcx) {
223 InstantiationMode::GloballyShared { .. } => {}
224 InstantiationMode::LocalCopy => continue,
225 }
226
227 let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);
228 let is_volatile = is_incremental_build && mono_item.is_generic_fn();
229
230 let cgu_name = match characteristic_def_id {
231 Some(def_id) => compute_codegen_unit_name(
232 cx.tcx,
233 cgu_name_builder,
234 def_id,
235 is_volatile,
236 cgu_name_cache,
237 ),
238 None => fallback_cgu_name(cgu_name_builder),
239 };
240
241 let cgu = codegen_units.entry(cgu_name).or_insert_with(|| CodegenUnit::new(cgu_name));
242
243 let mut can_be_internalized = true;
244 let (linkage, visibility) = mono_item_linkage_and_visibility(
245 cx.tcx,
246 &mono_item,
247 &mut can_be_internalized,
248 can_export_generics,
249 always_export_generics,
250 );
251
252 if visibility == Visibility::Hidden && can_be_internalized {
253 internalization_candidates.insert(mono_item);
254 }
255 let size_estimate = mono_item.size_estimate(cx.tcx);
256
257 cgu.items_mut()
258 .insert(mono_item, MonoItemData { inlined: false, linkage, visibility, size_estimate });
259
260 let mut reachable_inlined_items = FxIndexSet::default();
265 get_reachable_inlined_items(cx.tcx, mono_item, cx.usage_map, &mut reachable_inlined_items);
266
267 for inlined_item in reachable_inlined_items {
271 cgu.items_mut().entry(inlined_item).or_insert_with(|| MonoItemData {
273 inlined: true,
274 linkage: Linkage::Internal,
275 visibility: Visibility::Default,
276 size_estimate: inlined_item.size_estimate(cx.tcx),
277 });
278 }
279 }
280
281 if codegen_units.is_empty() {
284 let cgu_name = fallback_cgu_name(cgu_name_builder);
285 codegen_units.insert(cgu_name, CodegenUnit::new(cgu_name));
286 }
287
288 let mut codegen_units: Vec<_> = cx.tcx.with_stable_hashing_context(|ref hcx| {
289 codegen_units.into_items().map(|(_, cgu)| cgu).collect_sorted(hcx, true)
290 });
291
292 for cgu in codegen_units.iter_mut() {
293 cgu.compute_size_estimate();
294 }
295
296 return PlacedMonoItems { codegen_units, internalization_candidates };
297
298 fn get_reachable_inlined_items<'tcx>(
299 tcx: TyCtxt<'tcx>,
300 item: MonoItem<'tcx>,
301 usage_map: &UsageMap<'tcx>,
302 visited: &mut FxIndexSet<MonoItem<'tcx>>,
303 ) {
304 usage_map.for_each_inlined_used_item(tcx, item, |inlined_item| {
305 let is_new = visited.insert(inlined_item);
306 if is_new {
307 get_reachable_inlined_items(tcx, inlined_item, usage_map, visited);
308 }
309 });
310 }
311}
312
313fn merge_codegen_units<'tcx>(
316 cx: &PartitioningCx<'_, 'tcx>,
317 codegen_units: &mut Vec<CodegenUnit<'tcx>>,
318) {
319 assert!(cx.tcx.sess.codegen_units().as_usize() >= 1);
320
321 assert!(codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()));
323
324 let mut cgu_contents: UnordMap<Symbol, Vec<Symbol>> =
326 codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
327
328 let max_codegen_units = cx.tcx.sess.codegen_units().as_usize();
343 while codegen_units.len() > max_codegen_units {
344 codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
346
347 let cgu_dst = &codegen_units[max_codegen_units - 1];
348
349 let mut max_overlap = 0;
352 let mut max_overlap_i = max_codegen_units;
353 for (i, cgu_src) in codegen_units.iter().enumerate().skip(max_codegen_units) {
354 if cgu_src.size_estimate() <= max_overlap {
355 break;
358 }
359
360 let overlap = compute_inlined_overlap(cgu_dst, cgu_src);
361 if overlap > max_overlap {
362 max_overlap = overlap;
363 max_overlap_i = i;
364 }
365 }
366
367 let mut cgu_src = codegen_units.swap_remove(max_overlap_i);
368 let cgu_dst = &mut codegen_units[max_codegen_units - 1];
369
370 cgu_dst.items_mut().append(cgu_src.items_mut());
374 cgu_dst.compute_size_estimate();
375
376 let mut consumed_cgu_names = cgu_contents.remove(&cgu_src.name()).unwrap();
379 cgu_contents.get_mut(&cgu_dst.name()).unwrap().append(&mut consumed_cgu_names);
380 }
381
382 const NON_INCR_MIN_CGU_SIZE: usize = 1800;
388
389 while cx.tcx.sess.opts.incremental.is_none()
399 && matches!(cx.tcx.sess.codegen_units(), CodegenUnits::Default(_))
400 && codegen_units.len() > 1
401 && codegen_units.iter().any(|cgu| cgu.size_estimate() < NON_INCR_MIN_CGU_SIZE)
402 {
403 codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
405
406 let mut smallest = codegen_units.pop().unwrap();
407 let second_smallest = codegen_units.last_mut().unwrap();
408
409 second_smallest.items_mut().append(smallest.items_mut());
413 second_smallest.compute_size_estimate();
414
415 }
417
418 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
419
420 if cx.tcx.sess.opts.incremental.is_some() {
422 let new_cgu_names = UnordMap::from(
428 cgu_contents
429 .items()
430 .filter(|(_, cgu_contents)| cgu_contents.len() > 1)
433 .map(|(current_cgu_name, cgu_contents)| {
434 let mut cgu_contents: Vec<&str> =
435 cgu_contents.iter().map(|s| s.as_str()).collect();
436
437 cgu_contents.sort_unstable();
441
442 (*current_cgu_name, cgu_contents.join("--"))
443 }),
444 );
445
446 for cgu in codegen_units.iter_mut() {
447 if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
448 let new_cgu_name = if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
449 Symbol::intern(&CodegenUnit::shorten_name(new_cgu_name))
450 } else {
451 Symbol::intern(&CodegenUnit::mangle_name(new_cgu_name))
455 };
456 cgu.set_name(new_cgu_name);
457 }
458 }
459
460 codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
462 } else {
463 codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
483 let num_digits = codegen_units.len().ilog10() as usize + 1;
484 for (index, cgu) in codegen_units.iter_mut().enumerate() {
485 let suffix = format!("{index:0num_digits$}");
489 let numbered_codegen_unit_name =
490 cgu_name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(suffix));
491 cgu.set_name(numbered_codegen_unit_name);
492 }
493 }
494}
495
496fn compute_inlined_overlap<'tcx>(cgu1: &CodegenUnit<'tcx>, cgu2: &CodegenUnit<'tcx>) -> usize {
499 let (src_cgu, dst_cgu) =
502 if cgu1.items().len() <= cgu2.items().len() { (cgu1, cgu2) } else { (cgu2, cgu1) };
503
504 let mut overlap = 0;
505 for (item, data) in src_cgu.items().iter() {
506 if data.inlined && dst_cgu.items().contains_key(item) {
507 overlap += data.size_estimate;
508 }
509 }
510 overlap
511}
512
513fn internalize_symbols<'tcx>(
514 cx: &PartitioningCx<'_, 'tcx>,
515 codegen_units: &mut [CodegenUnit<'tcx>],
516 internalization_candidates: UnordSet<MonoItem<'tcx>>,
517) {
518 #[derive(Clone, PartialEq, Eq, Debug)]
522 enum MonoItemPlacement {
523 SingleCgu(Symbol),
524 MultipleCgus,
525 }
526
527 let mut mono_item_placements = UnordMap::default();
528 let single_codegen_unit = codegen_units.len() == 1;
529
530 if !single_codegen_unit {
531 for cgu in codegen_units.iter() {
532 for item in cgu.items().keys() {
533 match mono_item_placements.entry(*item) {
536 Entry::Occupied(e) => {
537 let placement = e.into_mut();
538 debug_assert!(match *placement {
539 MonoItemPlacement::SingleCgu(cgu_name) => cgu_name != cgu.name(),
540 MonoItemPlacement::MultipleCgus => true,
541 });
542 *placement = MonoItemPlacement::MultipleCgus;
543 }
544 Entry::Vacant(e) => {
545 e.insert(MonoItemPlacement::SingleCgu(cgu.name()));
546 }
547 }
548 }
549 }
550 }
551
552 for cgu in codegen_units {
555 let home_cgu = MonoItemPlacement::SingleCgu(cgu.name());
556
557 for (item, data) in cgu.items_mut() {
558 if !internalization_candidates.contains(item) {
559 continue;
561 }
562
563 if !single_codegen_unit {
564 debug_assert_eq!(mono_item_placements[item], home_cgu);
565
566 if cx
567 .usage_map
568 .get_user_items(*item)
569 .iter()
570 .filter_map(|user_item| {
571 mono_item_placements.get(user_item)
574 })
575 .any(|placement| *placement != home_cgu)
576 {
577 continue;
580 }
581 }
582
583 data.linkage = Linkage::Internal;
586 data.visibility = Visibility::Default;
587 }
588 }
589}
590
591fn mark_code_coverage_dead_code_cgu<'tcx>(codegen_units: &mut [CodegenUnit<'tcx>]) {
592 assert!(!codegen_units.is_empty());
593
594 let dead_code_cgu = codegen_units
602 .iter_mut()
603 .filter(|cgu| cgu.items().iter().any(|(_, data)| data.linkage == Linkage::External))
604 .min_by_key(|cgu| cgu.size_estimate());
605
606 let dead_code_cgu = if let Some(cgu) = dead_code_cgu { cgu } else { &mut codegen_units[0] };
609
610 dead_code_cgu.make_code_coverage_dead_code_cgu();
611}
612
613fn characteristic_def_id_of_mono_item<'tcx>(
614 tcx: TyCtxt<'tcx>,
615 mono_item: MonoItem<'tcx>,
616) -> Option<DefId> {
617 match mono_item {
618 MonoItem::Fn(instance) => {
619 let def_id = match instance.def {
620 ty::InstanceKind::Item(def) => def,
621 ty::InstanceKind::VTableShim(..)
622 | ty::InstanceKind::ReifyShim(..)
623 | ty::InstanceKind::FnPtrShim(..)
624 | ty::InstanceKind::ClosureOnceShim { .. }
625 | ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
626 | ty::InstanceKind::Intrinsic(..)
627 | ty::InstanceKind::DropGlue(..)
628 | ty::InstanceKind::Virtual(..)
629 | ty::InstanceKind::CloneShim(..)
630 | ty::InstanceKind::ThreadLocalShim(..)
631 | ty::InstanceKind::FnPtrAddrShim(..)
632 | ty::InstanceKind::FutureDropPollShim(..)
633 | ty::InstanceKind::AsyncDropGlue(..)
634 | ty::InstanceKind::AsyncDropGlueCtorShim(..) => return None,
635 };
636
637 let assoc_parent = tcx.assoc_parent(def_id);
642
643 if let Some((_, DefKind::Trait)) = assoc_parent {
644 let self_ty = instance.args.type_at(0);
645 return characteristic_def_id_of_type(self_ty).or(Some(def_id));
647 }
648
649 if let Some((impl_def_id, DefKind::Impl { of_trait })) = assoc_parent {
650 if of_trait
651 && tcx.sess.opts.incremental.is_some()
652 && tcx.is_lang_item(tcx.trait_id_of_impl(impl_def_id).unwrap(), LangItem::Drop)
653 {
654 return None;
658 }
659
660 let impl_self_ty = tcx.instantiate_and_normalize_erasing_regions(
662 instance.args,
663 ty::TypingEnv::fully_monomorphized(),
664 tcx.type_of(impl_def_id),
665 );
666 if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
667 return Some(def_id);
668 }
669 }
670
671 Some(def_id)
672 }
673 MonoItem::Static(def_id) => Some(def_id),
674 MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.to_def_id()),
675 }
676}
677
678fn compute_codegen_unit_name(
679 tcx: TyCtxt<'_>,
680 name_builder: &mut CodegenUnitNameBuilder<'_>,
681 def_id: DefId,
682 volatile: bool,
683 cache: &mut CguNameCache,
684) -> Symbol {
685 let mut current_def_id = def_id;
687 let mut cgu_def_id = None;
688 loop {
690 if current_def_id.is_crate_root() {
691 if cgu_def_id.is_none() {
692 cgu_def_id = Some(def_id.krate.as_def_id());
694 }
695 break;
696 } else if tcx.def_kind(current_def_id) == DefKind::Mod {
697 if cgu_def_id.is_none() {
698 cgu_def_id = Some(current_def_id);
699 }
700 } else {
701 cgu_def_id = None;
705 }
706
707 current_def_id = tcx.parent(current_def_id);
708 }
709
710 let cgu_def_id = cgu_def_id.unwrap();
711
712 *cache.entry((cgu_def_id, volatile)).or_insert_with(|| {
713 let def_path = tcx.def_path(cgu_def_id);
714
715 let components = def_path.data.iter().map(|part| match part.data.name() {
716 DefPathDataName::Named(name) => name,
717 DefPathDataName::Anon { .. } => unreachable!(),
718 });
719
720 let volatile_suffix = volatile.then_some("volatile");
721
722 name_builder.build_cgu_name(def_path.krate, components, volatile_suffix)
723 })
724}
725
726fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder<'_>) -> Symbol {
728 name_builder.build_cgu_name(LOCAL_CRATE, &["fallback"], Some("cgu"))
729}
730
731fn mono_item_linkage_and_visibility<'tcx>(
732 tcx: TyCtxt<'tcx>,
733 mono_item: &MonoItem<'tcx>,
734 can_be_internalized: &mut bool,
735 can_export_generics: bool,
736 always_export_generics: bool,
737) -> (Linkage, Visibility) {
738 if let Some(explicit_linkage) = mono_item.explicit_linkage(tcx) {
739 return (explicit_linkage, Visibility::Default);
740 }
741 let vis = mono_item_visibility(
742 tcx,
743 mono_item,
744 can_be_internalized,
745 can_export_generics,
746 always_export_generics,
747 );
748 (Linkage::External, vis)
749}
750
751type CguNameCache = UnordMap<(DefId, bool), Symbol>;
752
753fn static_visibility<'tcx>(
754 tcx: TyCtxt<'tcx>,
755 can_be_internalized: &mut bool,
756 def_id: DefId,
757) -> Visibility {
758 if tcx.is_reachable_non_generic(def_id) {
759 *can_be_internalized = false;
760 default_visibility(tcx, def_id, false)
761 } else {
762 Visibility::Hidden
763 }
764}
765
766fn mono_item_visibility<'tcx>(
767 tcx: TyCtxt<'tcx>,
768 mono_item: &MonoItem<'tcx>,
769 can_be_internalized: &mut bool,
770 can_export_generics: bool,
771 always_export_generics: bool,
772) -> Visibility {
773 let instance = match mono_item {
774 MonoItem::Fn(instance) => instance,
776
777 MonoItem::Static(def_id) => return static_visibility(tcx, can_be_internalized, *def_id),
779 MonoItem::GlobalAsm(item_id) => {
780 return static_visibility(tcx, can_be_internalized, item_id.owner_id.to_def_id());
781 }
782 };
783
784 let def_id = match instance.def {
785 InstanceKind::Item(def_id)
786 | InstanceKind::DropGlue(def_id, Some(_))
787 | InstanceKind::FutureDropPollShim(def_id, _, _)
788 | InstanceKind::AsyncDropGlue(def_id, _)
789 | InstanceKind::AsyncDropGlueCtorShim(def_id, _) => def_id,
790
791 InstanceKind::ThreadLocalShim(def_id) => {
793 return static_visibility(tcx, can_be_internalized, def_id);
794 }
795
796 InstanceKind::VTableShim(..)
798 | InstanceKind::ReifyShim(..)
799 | InstanceKind::FnPtrShim(..)
800 | InstanceKind::Virtual(..)
801 | InstanceKind::Intrinsic(..)
802 | InstanceKind::ClosureOnceShim { .. }
803 | InstanceKind::ConstructCoroutineInClosureShim { .. }
804 | InstanceKind::DropGlue(..)
805 | InstanceKind::CloneShim(..)
806 | InstanceKind::FnPtrAddrShim(..) => return Visibility::Hidden,
807 };
808
809 if tcx.is_entrypoint(def_id) {
822 *can_be_internalized = false;
823 return Visibility::Hidden;
824 }
825
826 let is_generic = instance.args.non_erasable_generics().next().is_some();
827
828 let Some(def_id) = def_id.as_local() else {
830 return if is_generic
831 && (always_export_generics
832 || (can_export_generics
833 && tcx.codegen_fn_attrs(def_id).inline == InlineAttr::Never))
834 {
835 *can_be_internalized = false;
838 default_visibility(tcx, def_id, true)
839 } else {
840 Visibility::Hidden
841 };
842 };
843
844 if is_generic {
845 if always_export_generics
846 || (can_export_generics && tcx.codegen_fn_attrs(def_id).inline == InlineAttr::Never)
847 {
848 if tcx.is_unreachable_local_definition(def_id) {
849 Visibility::Hidden
851 } else {
852 *can_be_internalized = false;
854 default_visibility(tcx, def_id.to_def_id(), true)
855 }
856 } else {
857 Visibility::Hidden
860 }
861 } else {
862 if tcx.is_reachable_non_generic(def_id.to_def_id()) {
866 *can_be_internalized = false;
867 debug_assert!(!is_generic);
868 return default_visibility(tcx, def_id.to_def_id(), false);
869 }
870
871 let attrs = tcx.codegen_fn_attrs(def_id);
906 if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
907 *can_be_internalized = false;
908 }
909
910 Visibility::Hidden
911 }
912}
913
914fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibility {
915 if tcx.sess.default_visibility() == SymbolVisibility::Interposable {
917 return Visibility::Default;
918 }
919
920 let export_level = if is_generic {
921 SymbolExportLevel::Rust
923 } else {
924 match tcx.reachable_non_generics(id.krate).get(&id) {
925 Some(SymbolExportInfo { level: SymbolExportLevel::C, .. }) => SymbolExportLevel::C,
926 _ => SymbolExportLevel::Rust,
927 }
928 };
929
930 match export_level {
931 SymbolExportLevel::C => Visibility::Default,
934
935 SymbolExportLevel::Rust => tcx.sess.default_visibility().into(),
937 }
938}
939
940fn debug_dump<'a, 'tcx: 'a>(tcx: TyCtxt<'tcx>, label: &str, cgus: &[CodegenUnit<'tcx>]) {
941 let dump = move || {
942 use std::fmt::Write;
943
944 let mut num_cgus = 0;
945 let mut all_cgu_sizes = Vec::new();
946
947 let mut inlined_items = UnordSet::default();
953
954 let mut root_items = 0;
955 let mut unique_inlined_items = 0;
956 let mut placed_inlined_items = 0;
957
958 let mut root_size = 0;
959 let mut unique_inlined_size = 0;
960 let mut placed_inlined_size = 0;
961
962 for cgu in cgus.iter() {
963 num_cgus += 1;
964 all_cgu_sizes.push(cgu.size_estimate());
965
966 for (item, data) in cgu.items() {
967 if !data.inlined {
968 root_items += 1;
969 root_size += data.size_estimate;
970 } else {
971 if inlined_items.insert(item) {
972 unique_inlined_items += 1;
973 unique_inlined_size += data.size_estimate;
974 }
975 placed_inlined_items += 1;
976 placed_inlined_size += data.size_estimate;
977 }
978 }
979 }
980
981 all_cgu_sizes.sort_unstable_by_key(|&n| cmp::Reverse(n));
982
983 let unique_items = root_items + unique_inlined_items;
984 let placed_items = root_items + placed_inlined_items;
985 let items_ratio = placed_items as f64 / unique_items as f64;
986
987 let unique_size = root_size + unique_inlined_size;
988 let placed_size = root_size + placed_inlined_size;
989 let size_ratio = placed_size as f64 / unique_size as f64;
990
991 let mean_cgu_size = placed_size as f64 / num_cgus as f64;
992
993 assert_eq!(placed_size, all_cgu_sizes.iter().sum::<usize>());
994
995 let s = &mut String::new();
996 let _ = writeln!(s, "{label}");
997 let _ = writeln!(
998 s,
999 "- unique items: {unique_items} ({root_items} root + {unique_inlined_items} inlined), \
1000 unique size: {unique_size} ({root_size} root + {unique_inlined_size} inlined)\n\
1001 - placed items: {placed_items} ({root_items} root + {placed_inlined_items} inlined), \
1002 placed size: {placed_size} ({root_size} root + {placed_inlined_size} inlined)\n\
1003 - placed/unique items ratio: {items_ratio:.2}, \
1004 placed/unique size ratio: {size_ratio:.2}\n\
1005 - CGUs: {num_cgus}, mean size: {mean_cgu_size:.1}, sizes: {}",
1006 list(&all_cgu_sizes),
1007 );
1008 let _ = writeln!(s);
1009
1010 for (i, cgu) in cgus.iter().enumerate() {
1011 let name = cgu.name();
1012 let size = cgu.size_estimate();
1013 let num_items = cgu.items().len();
1014 let mean_size = size as f64 / num_items as f64;
1015
1016 let mut placed_item_sizes: Vec<_> =
1017 cgu.items().values().map(|data| data.size_estimate).collect();
1018 placed_item_sizes.sort_unstable_by_key(|&n| cmp::Reverse(n));
1019 let sizes = list(&placed_item_sizes);
1020
1021 let _ = writeln!(s, "- CGU[{i}]");
1022 let _ = writeln!(s, " - {name}, size: {size}");
1023 let _ =
1024 writeln!(s, " - items: {num_items}, mean size: {mean_size:.1}, sizes: {sizes}",);
1025
1026 for (item, data) in cgu.items_in_deterministic_order(tcx) {
1027 let linkage = data.linkage;
1028 let symbol_name = item.symbol_name(tcx).name;
1029 let symbol_hash_start = symbol_name.rfind('h');
1030 let symbol_hash = symbol_hash_start.map_or("<no hash>", |i| &symbol_name[i..]);
1031 let kind = if !data.inlined { "root" } else { "inlined" };
1032 let size = data.size_estimate;
1033 let _ = with_no_trimmed_paths!(writeln!(
1034 s,
1035 " - {item} [{linkage:?}] [{symbol_hash}] ({kind}, size: {size})"
1036 ));
1037 }
1038
1039 let _ = writeln!(s);
1040 }
1041
1042 return std::mem::take(s);
1043
1044 fn list(ns: &[usize]) -> String {
1047 let mut v = Vec::new();
1048 if ns.is_empty() {
1049 return "[]".to_string();
1050 }
1051
1052 let mut elem = |curr, curr_count| {
1053 if curr_count == 1 {
1054 v.push(format!("{curr}"));
1055 } else {
1056 v.push(format!("{curr} (x{curr_count})"));
1057 }
1058 };
1059
1060 let mut curr = ns[0];
1061 let mut curr_count = 1;
1062
1063 for &n in &ns[1..] {
1064 if n != curr {
1065 elem(curr, curr_count);
1066 curr = n;
1067 curr_count = 1;
1068 } else {
1069 curr_count += 1;
1070 }
1071 }
1072 elem(curr, curr_count);
1073
1074 format!("[{}]", v.join(", "))
1075 }
1076 };
1077
1078 debug!("{}", dump());
1079}
1080
1081#[inline(never)] fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'tcx>, mono_items: I)
1083where
1084 I: Iterator<Item = &'a MonoItem<'tcx>>,
1085 'tcx: 'a,
1086{
1087 let _prof_timer = tcx.prof.generic_activity("assert_symbols_are_distinct");
1088
1089 let mut symbols: Vec<_> =
1090 mono_items.map(|mono_item| (mono_item, mono_item.symbol_name(tcx))).collect();
1091
1092 symbols.sort_by_key(|sym| sym.1);
1093
1094 for &[(mono_item1, ref sym1), (mono_item2, ref sym2)] in symbols.array_windows() {
1095 if sym1 == sym2 {
1096 let span1 = mono_item1.local_span(tcx);
1097 let span2 = mono_item2.local_span(tcx);
1098
1099 let span = match (span1, span2) {
1101 (Some(span1), Some(span2)) => {
1102 Some(if span1.lo().0 > span2.lo().0 { span1 } else { span2 })
1103 }
1104 (span1, span2) => span1.or(span2),
1105 };
1106
1107 tcx.dcx().emit_fatal(SymbolAlreadyDefined { span, symbol: sym1.to_string() });
1108 }
1109 }
1110}
1111
1112fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> MonoItemPartitions<'_> {
1113 let collection_strategy = if tcx.sess.link_dead_code() {
1114 MonoItemCollectionStrategy::Eager
1115 } else {
1116 MonoItemCollectionStrategy::Lazy
1117 };
1118
1119 let (items, usage_map) = collector::collect_crate_mono_items(tcx, collection_strategy);
1120
1121 tcx.dcx().abort_if_errors();
1125
1126 let (codegen_units, _) = tcx.sess.time("partition_and_assert_distinct_symbols", || {
1127 sync::join(
1128 || {
1129 let mut codegen_units = partition(tcx, items.iter().copied(), &usage_map);
1130 codegen_units[0].make_primary();
1131 &*tcx.arena.alloc_from_iter(codegen_units)
1132 },
1133 || assert_symbols_are_distinct(tcx, items.iter()),
1134 )
1135 });
1136
1137 if tcx.prof.enabled() {
1138 for cgu in codegen_units {
1140 tcx.prof.artifact_size(
1141 "codegen_unit_size_estimate",
1142 cgu.name().as_str(),
1143 cgu.size_estimate() as u64,
1144 );
1145 }
1146 }
1147
1148 let mono_items: DefIdSet = items
1149 .iter()
1150 .filter_map(|mono_item| match *mono_item {
1151 MonoItem::Fn(ref instance) => Some(instance.def_id()),
1152 MonoItem::Static(def_id) => Some(def_id),
1153 _ => None,
1154 })
1155 .collect();
1156
1157 if let SwitchWithOptPath::Enabled(ref path) = tcx.sess.opts.unstable_opts.dump_mono_stats
1159 && let Err(err) =
1160 dump_mono_items_stats(tcx, codegen_units, path, tcx.crate_name(LOCAL_CRATE))
1161 {
1162 tcx.dcx().emit_fatal(CouldntDumpMonoStats { error: err.to_string() });
1163 }
1164
1165 if tcx.sess.opts.unstable_opts.print_mono_items {
1166 let mut item_to_cgus: UnordMap<_, Vec<_>> = Default::default();
1167
1168 for cgu in codegen_units {
1169 for (&mono_item, &data) in cgu.items() {
1170 item_to_cgus.entry(mono_item).or_default().push((cgu.name(), data.linkage));
1171 }
1172 }
1173
1174 let mut item_keys: Vec<_> = items
1175 .iter()
1176 .map(|i| {
1177 let mut output = with_no_trimmed_paths!(i.to_string());
1178 output.push_str(" @@");
1179 let mut empty = Vec::new();
1180 let cgus = item_to_cgus.get_mut(i).unwrap_or(&mut empty);
1181 cgus.sort_by_key(|(name, _)| *name);
1182 cgus.dedup();
1183 for &(ref cgu_name, linkage) in cgus.iter() {
1184 output.push(' ');
1185 output.push_str(cgu_name.as_str());
1186
1187 let linkage_abbrev = match linkage {
1188 Linkage::External => "External",
1189 Linkage::AvailableExternally => "Available",
1190 Linkage::LinkOnceAny => "OnceAny",
1191 Linkage::LinkOnceODR => "OnceODR",
1192 Linkage::WeakAny => "WeakAny",
1193 Linkage::WeakODR => "WeakODR",
1194 Linkage::Internal => "Internal",
1195 Linkage::ExternalWeak => "ExternalWeak",
1196 Linkage::Common => "Common",
1197 };
1198
1199 output.push('[');
1200 output.push_str(linkage_abbrev);
1201 output.push(']');
1202 }
1203 output
1204 })
1205 .collect();
1206
1207 item_keys.sort();
1208
1209 for item in item_keys {
1210 println!("MONO_ITEM {item}");
1211 }
1212 }
1213
1214 MonoItemPartitions { all_mono_items: tcx.arena.alloc(mono_items), codegen_units }
1215}
1216
1217fn dump_mono_items_stats<'tcx>(
1220 tcx: TyCtxt<'tcx>,
1221 codegen_units: &[CodegenUnit<'tcx>],
1222 output_directory: &Option<PathBuf>,
1223 crate_name: Symbol,
1224) -> Result<(), Box<dyn std::error::Error>> {
1225 let output_directory = if let Some(directory) = output_directory {
1226 fs::create_dir_all(directory)?;
1227 directory
1228 } else {
1229 Path::new(".")
1230 };
1231
1232 let format = tcx.sess.opts.unstable_opts.dump_mono_stats_format;
1233 let ext = format.extension();
1234 let filename = format!("{crate_name}.mono_items.{ext}");
1235 let output_path = output_directory.join(&filename);
1236 let mut file = File::create_buffered(&output_path)?;
1237
1238 let mut items_per_def_id: FxIndexMap<_, Vec<_>> = Default::default();
1240 for cgu in codegen_units {
1241 cgu.items()
1242 .keys()
1243 .filter(|mono_item| mono_item.is_user_defined())
1245 .for_each(|mono_item| {
1246 items_per_def_id.entry(mono_item.def_id()).or_default().push(mono_item);
1247 });
1248 }
1249
1250 #[derive(serde::Serialize)]
1251 struct MonoItem {
1252 name: String,
1253 instantiation_count: usize,
1254 size_estimate: usize,
1255 total_estimate: usize,
1256 }
1257
1258 let mut stats: Vec<_> = items_per_def_id
1260 .into_iter()
1261 .map(|(def_id, items)| {
1262 let name = with_no_trimmed_paths!(tcx.def_path_str(def_id));
1263 let instantiation_count = items.len();
1264 let size_estimate = items[0].size_estimate(tcx);
1265 let total_estimate = instantiation_count * size_estimate;
1266 MonoItem { name, instantiation_count, size_estimate, total_estimate }
1267 })
1268 .collect();
1269 stats.sort_unstable_by_key(|item| cmp::Reverse(item.total_estimate));
1270
1271 if !stats.is_empty() {
1272 match format {
1273 DumpMonoStatsFormat::Json => serde_json::to_writer(file, &stats)?,
1274 DumpMonoStatsFormat::Markdown => {
1275 writeln!(
1276 file,
1277 "| Item | Instantiation count | Estimated Cost Per Instantiation | Total Estimated Cost |"
1278 )?;
1279 writeln!(file, "| --- | ---: | ---: | ---: |")?;
1280
1281 for MonoItem { name, instantiation_count, size_estimate, total_estimate } in stats {
1282 writeln!(
1283 file,
1284 "| `{name}` | {instantiation_count} | {size_estimate} | {total_estimate} |"
1285 )?;
1286 }
1287 }
1288 }
1289 }
1290
1291 Ok(())
1292}
1293
1294pub(crate) fn provide(providers: &mut Providers) {
1295 providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
1296
1297 providers.is_codegened_item =
1298 |tcx, def_id| tcx.collect_and_partition_mono_items(()).all_mono_items.contains(&def_id);
1299
1300 providers.codegen_unit = |tcx, name| {
1301 tcx.collect_and_partition_mono_items(())
1302 .codegen_units
1303 .iter()
1304 .find(|cgu| cgu.name() == name)
1305 .unwrap_or_else(|| panic!("failed to find cgu with name {name:?}"))
1306 };
1307
1308 providers.size_estimate = |tcx, instance| {
1309 match instance.def {
1310 InstanceKind::Item(..)
1313 | InstanceKind::DropGlue(..)
1314 | InstanceKind::AsyncDropGlueCtorShim(..) => {
1315 let mir = tcx.instance_mir(instance.def);
1316 mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
1317 }
1318 _ => 1,
1320 }
1321 };
1322
1323 collector::provide(providers);
1324}