1mod autodiff;
96
97use std::cmp;
98use std::collections::hash_map::Entry;
99use std::fs::{self, File};
100use std::io::Write;
101use std::path::{Path, PathBuf};
102
103use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
104use rustc_data_structures::sync;
105use rustc_data_structures::unord::{UnordMap, UnordSet};
106use rustc_hir::LangItem;
107use rustc_hir::def::DefKind;
108use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE};
109use rustc_hir::definitions::DefPathDataName;
110use rustc_middle::bug;
111use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
112use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
113use rustc_middle::mir::mono::{
114 CodegenUnit, CodegenUnitNameBuilder, InstantiationMode, Linkage, MonoItem, MonoItemData,
115 MonoItemPartitions, Visibility,
116};
117use rustc_middle::ty::print::{characteristic_def_id_of_type, with_no_trimmed_paths};
118use rustc_middle::ty::{self, InstanceKind, TyCtxt};
119use rustc_middle::util::Providers;
120use rustc_session::CodegenUnits;
121use rustc_session::config::{DumpMonoStatsFormat, SwitchWithOptPath};
122use rustc_span::Symbol;
123use rustc_target::spec::SymbolVisibility;
124use tracing::debug;
125
126use crate::collector::{self, MonoItemCollectionStrategy, UsageMap};
127use crate::errors::{CouldntDumpMonoStats, SymbolAlreadyDefined, UnknownCguCollectionMode};
128
129struct PartitioningCx<'a, 'tcx> {
130 tcx: TyCtxt<'tcx>,
131 usage_map: &'a UsageMap<'tcx>,
132}
133
134struct PlacedMonoItems<'tcx> {
135 codegen_units: Vec<CodegenUnit<'tcx>>,
137
138 internalization_candidates: UnordSet<MonoItem<'tcx>>,
139}
140
141fn partition<'tcx, I>(
143 tcx: TyCtxt<'tcx>,
144 mono_items: I,
145 usage_map: &UsageMap<'tcx>,
146) -> Vec<CodegenUnit<'tcx>>
147where
148 I: Iterator<Item = MonoItem<'tcx>>,
149{
150 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning");
151
152 let cx = &PartitioningCx { tcx, usage_map };
153
154 let PlacedMonoItems { mut codegen_units, internalization_candidates } = {
157 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_place_items");
158 let placed = place_mono_items(cx, mono_items);
159
160 debug_dump(tcx, "PLACE", &placed.codegen_units);
161
162 placed
163 };
164
165 {
169 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_merge_cgus");
170 merge_codegen_units(cx, &mut codegen_units);
171 debug_dump(tcx, "MERGE", &codegen_units);
172 }
173
174 if !tcx.sess.link_dead_code() {
177 let _prof_timer = tcx.prof.generic_activity("cgu_partitioning_internalize_symbols");
178 internalize_symbols(cx, &mut codegen_units, internalization_candidates);
179
180 debug_dump(tcx, "INTERNALIZE", &codegen_units);
181 }
182
183 if tcx.sess.instrument_coverage() {
185 mark_code_coverage_dead_code_cgu(&mut codegen_units);
186 }
187
188 if !codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()) {
190 let mut names = String::new();
191 for cgu in codegen_units.iter() {
192 names += &format!("- {}\n", cgu.name());
193 }
194 bug!("unsorted CGUs:\n{names}");
195 }
196
197 codegen_units
198}
199
200fn place_mono_items<'tcx, I>(cx: &PartitioningCx<'_, 'tcx>, mono_items: I) -> PlacedMonoItems<'tcx>
201where
202 I: Iterator<Item = MonoItem<'tcx>>,
203{
204 let mut codegen_units = UnordMap::default();
205 let is_incremental_build = cx.tcx.sess.opts.incremental.is_some();
206 let mut internalization_candidates = UnordSet::default();
207
208 let can_export_generics = cx.tcx.local_crate_exports_generics();
213 let always_export_generics = can_export_generics && cx.tcx.sess.opts.share_generics();
214
215 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
216 let cgu_name_cache = &mut UnordMap::default();
217
218 for mono_item in mono_items {
219 match mono_item.instantiation_mode(cx.tcx) {
224 InstantiationMode::GloballyShared { .. } => {}
225 InstantiationMode::LocalCopy => {
226 if Some(mono_item.def_id()) != cx.tcx.lang_items().start_fn() {
227 continue;
228 }
229 }
230 }
231
232 let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);
233 let is_volatile = is_incremental_build && mono_item.is_generic_fn();
234
235 let cgu_name = match characteristic_def_id {
236 Some(def_id) => compute_codegen_unit_name(
237 cx.tcx,
238 cgu_name_builder,
239 def_id,
240 is_volatile,
241 cgu_name_cache,
242 ),
243 None => fallback_cgu_name(cgu_name_builder),
244 };
245
246 let cgu = codegen_units.entry(cgu_name).or_insert_with(|| CodegenUnit::new(cgu_name));
247
248 let mut can_be_internalized = true;
249 let (linkage, visibility) = mono_item_linkage_and_visibility(
250 cx.tcx,
251 &mono_item,
252 &mut can_be_internalized,
253 can_export_generics,
254 always_export_generics,
255 );
256
257 let autodiff_active = cfg!(llvm_enzyme)
259 && cx
260 .tcx
261 .codegen_fn_attrs(mono_item.def_id())
262 .autodiff_item
263 .as_ref()
264 .is_some_and(|ad| ad.is_active());
265
266 if !autodiff_active && visibility == Visibility::Hidden && can_be_internalized {
267 internalization_candidates.insert(mono_item);
268 }
269 let size_estimate = mono_item.size_estimate(cx.tcx);
270
271 cgu.items_mut()
272 .insert(mono_item, MonoItemData { inlined: false, linkage, visibility, size_estimate });
273
274 let mut reachable_inlined_items = FxIndexSet::default();
279 get_reachable_inlined_items(cx.tcx, mono_item, cx.usage_map, &mut reachable_inlined_items);
280
281 for inlined_item in reachable_inlined_items {
285 cgu.items_mut().entry(inlined_item).or_insert_with(|| MonoItemData {
287 inlined: true,
288 linkage: Linkage::Internal,
289 visibility: Visibility::Default,
290 size_estimate: inlined_item.size_estimate(cx.tcx),
291 });
292 }
293 }
294
295 if codegen_units.is_empty() {
298 let cgu_name = fallback_cgu_name(cgu_name_builder);
299 codegen_units.insert(cgu_name, CodegenUnit::new(cgu_name));
300 }
301
302 let mut codegen_units: Vec<_> = cx.tcx.with_stable_hashing_context(|ref hcx| {
303 codegen_units.into_items().map(|(_, cgu)| cgu).collect_sorted(hcx, true)
304 });
305
306 for cgu in codegen_units.iter_mut() {
307 cgu.compute_size_estimate();
308 }
309
310 return PlacedMonoItems { codegen_units, internalization_candidates };
311
312 fn get_reachable_inlined_items<'tcx>(
313 tcx: TyCtxt<'tcx>,
314 item: MonoItem<'tcx>,
315 usage_map: &UsageMap<'tcx>,
316 visited: &mut FxIndexSet<MonoItem<'tcx>>,
317 ) {
318 usage_map.for_each_inlined_used_item(tcx, item, |inlined_item| {
319 let is_new = visited.insert(inlined_item);
320 if is_new {
321 get_reachable_inlined_items(tcx, inlined_item, usage_map, visited);
322 }
323 });
324 }
325}
326
327fn merge_codegen_units<'tcx>(
330 cx: &PartitioningCx<'_, 'tcx>,
331 codegen_units: &mut Vec<CodegenUnit<'tcx>>,
332) {
333 assert!(cx.tcx.sess.codegen_units().as_usize() >= 1);
334
335 assert!(codegen_units.is_sorted_by(|a, b| a.name().as_str() <= b.name().as_str()));
337
338 let mut cgu_contents: UnordMap<Symbol, Vec<Symbol>> =
340 codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
341
342 let max_codegen_units = cx.tcx.sess.codegen_units().as_usize();
357 while codegen_units.len() > max_codegen_units {
358 codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
360
361 let cgu_dst = &codegen_units[max_codegen_units - 1];
362
363 let mut max_overlap = 0;
366 let mut max_overlap_i = max_codegen_units;
367 for (i, cgu_src) in codegen_units.iter().enumerate().skip(max_codegen_units) {
368 if cgu_src.size_estimate() <= max_overlap {
369 break;
372 }
373
374 let overlap = compute_inlined_overlap(cgu_dst, cgu_src);
375 if overlap > max_overlap {
376 max_overlap = overlap;
377 max_overlap_i = i;
378 }
379 }
380
381 let mut cgu_src = codegen_units.swap_remove(max_overlap_i);
382 let cgu_dst = &mut codegen_units[max_codegen_units - 1];
383
384 cgu_dst.items_mut().append(cgu_src.items_mut());
388 cgu_dst.compute_size_estimate();
389
390 let mut consumed_cgu_names = cgu_contents.remove(&cgu_src.name()).unwrap();
393 cgu_contents.get_mut(&cgu_dst.name()).unwrap().append(&mut consumed_cgu_names);
394 }
395
396 const NON_INCR_MIN_CGU_SIZE: usize = 1800;
402
403 while cx.tcx.sess.opts.incremental.is_none()
413 && matches!(cx.tcx.sess.codegen_units(), CodegenUnits::Default(_))
414 && codegen_units.len() > 1
415 && codegen_units.iter().any(|cgu| cgu.size_estimate() < NON_INCR_MIN_CGU_SIZE)
416 {
417 codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
419
420 let mut smallest = codegen_units.pop().unwrap();
421 let second_smallest = codegen_units.last_mut().unwrap();
422
423 second_smallest.items_mut().append(smallest.items_mut());
427 second_smallest.compute_size_estimate();
428
429 }
431
432 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
433
434 if cx.tcx.sess.opts.incremental.is_some() {
436 let new_cgu_names = UnordMap::from(
442 cgu_contents
443 .items()
444 .filter(|(_, cgu_contents)| cgu_contents.len() > 1)
447 .map(|(current_cgu_name, cgu_contents)| {
448 let mut cgu_contents: Vec<&str> =
449 cgu_contents.iter().map(|s| s.as_str()).collect();
450
451 cgu_contents.sort_unstable();
455
456 (*current_cgu_name, cgu_contents.join("--"))
457 }),
458 );
459
460 for cgu in codegen_units.iter_mut() {
461 if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
462 if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
463 cgu.set_name(Symbol::intern(new_cgu_name));
464 } else {
465 let new_cgu_name = CodegenUnit::mangle_name(new_cgu_name);
469 cgu.set_name(Symbol::intern(&new_cgu_name));
470 }
471 }
472 }
473
474 codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
476 } else {
477 codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
497 let num_digits = codegen_units.len().ilog10() as usize + 1;
498 for (index, cgu) in codegen_units.iter_mut().enumerate() {
499 let suffix = format!("{index:0num_digits$}");
503 let numbered_codegen_unit_name =
504 cgu_name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(suffix));
505 cgu.set_name(numbered_codegen_unit_name);
506 }
507 }
508}
509
510fn compute_inlined_overlap<'tcx>(cgu1: &CodegenUnit<'tcx>, cgu2: &CodegenUnit<'tcx>) -> usize {
513 let (src_cgu, dst_cgu) =
516 if cgu1.items().len() <= cgu2.items().len() { (cgu1, cgu2) } else { (cgu2, cgu1) };
517
518 let mut overlap = 0;
519 for (item, data) in src_cgu.items().iter() {
520 if data.inlined && dst_cgu.items().contains_key(item) {
521 overlap += data.size_estimate;
522 }
523 }
524 overlap
525}
526
527fn internalize_symbols<'tcx>(
528 cx: &PartitioningCx<'_, 'tcx>,
529 codegen_units: &mut [CodegenUnit<'tcx>],
530 internalization_candidates: UnordSet<MonoItem<'tcx>>,
531) {
532 #[derive(Clone, PartialEq, Eq, Debug)]
536 enum MonoItemPlacement {
537 SingleCgu(Symbol),
538 MultipleCgus,
539 }
540
541 let mut mono_item_placements = UnordMap::default();
542 let single_codegen_unit = codegen_units.len() == 1;
543
544 if !single_codegen_unit {
545 for cgu in codegen_units.iter() {
546 for item in cgu.items().keys() {
547 match mono_item_placements.entry(*item) {
550 Entry::Occupied(e) => {
551 let placement = e.into_mut();
552 debug_assert!(match *placement {
553 MonoItemPlacement::SingleCgu(cgu_name) => cgu_name != cgu.name(),
554 MonoItemPlacement::MultipleCgus => true,
555 });
556 *placement = MonoItemPlacement::MultipleCgus;
557 }
558 Entry::Vacant(e) => {
559 e.insert(MonoItemPlacement::SingleCgu(cgu.name()));
560 }
561 }
562 }
563 }
564 }
565
566 for cgu in codegen_units {
569 let home_cgu = MonoItemPlacement::SingleCgu(cgu.name());
570
571 for (item, data) in cgu.items_mut() {
572 if !internalization_candidates.contains(item) {
573 continue;
575 }
576
577 if !single_codegen_unit {
578 debug_assert_eq!(mono_item_placements[item], home_cgu);
579
580 if cx
581 .usage_map
582 .get_user_items(*item)
583 .iter()
584 .filter_map(|user_item| {
585 mono_item_placements.get(user_item)
588 })
589 .any(|placement| *placement != home_cgu)
590 {
591 continue;
594 }
595 }
596
597 data.linkage = Linkage::Internal;
600 data.visibility = Visibility::Default;
601 }
602 }
603}
604
605fn mark_code_coverage_dead_code_cgu<'tcx>(codegen_units: &mut [CodegenUnit<'tcx>]) {
606 assert!(!codegen_units.is_empty());
607
608 let dead_code_cgu = codegen_units
616 .iter_mut()
617 .filter(|cgu| cgu.items().iter().any(|(_, data)| data.linkage == Linkage::External))
618 .min_by_key(|cgu| cgu.size_estimate());
619
620 let dead_code_cgu = if let Some(cgu) = dead_code_cgu { cgu } else { &mut codegen_units[0] };
623
624 dead_code_cgu.make_code_coverage_dead_code_cgu();
625}
626
627fn characteristic_def_id_of_mono_item<'tcx>(
628 tcx: TyCtxt<'tcx>,
629 mono_item: MonoItem<'tcx>,
630) -> Option<DefId> {
631 match mono_item {
632 MonoItem::Fn(instance) => {
633 let def_id = match instance.def {
634 ty::InstanceKind::Item(def) => def,
635 ty::InstanceKind::VTableShim(..)
636 | ty::InstanceKind::ReifyShim(..)
637 | ty::InstanceKind::FnPtrShim(..)
638 | ty::InstanceKind::ClosureOnceShim { .. }
639 | ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
640 | ty::InstanceKind::Intrinsic(..)
641 | ty::InstanceKind::DropGlue(..)
642 | ty::InstanceKind::Virtual(..)
643 | ty::InstanceKind::CloneShim(..)
644 | ty::InstanceKind::ThreadLocalShim(..)
645 | ty::InstanceKind::FnPtrAddrShim(..)
646 | ty::InstanceKind::AsyncDropGlueCtorShim(..) => return None,
647 };
648
649 if tcx.trait_of_item(def_id).is_some() {
654 let self_ty = instance.args.type_at(0);
655 return characteristic_def_id_of_type(self_ty).or(Some(def_id));
657 }
658
659 if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
660 if tcx.sess.opts.incremental.is_some()
661 && tcx
662 .trait_id_of_impl(impl_def_id)
663 .is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Drop))
664 {
665 return None;
669 }
670
671 let impl_self_ty = tcx.instantiate_and_normalize_erasing_regions(
673 instance.args,
674 ty::TypingEnv::fully_monomorphized(),
675 tcx.type_of(impl_def_id),
676 );
677 if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
678 return Some(def_id);
679 }
680 }
681
682 Some(def_id)
683 }
684 MonoItem::Static(def_id) => Some(def_id),
685 MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.to_def_id()),
686 }
687}
688
689fn compute_codegen_unit_name(
690 tcx: TyCtxt<'_>,
691 name_builder: &mut CodegenUnitNameBuilder<'_>,
692 def_id: DefId,
693 volatile: bool,
694 cache: &mut CguNameCache,
695) -> Symbol {
696 let mut current_def_id = def_id;
698 let mut cgu_def_id = None;
699 loop {
701 if current_def_id.is_crate_root() {
702 if cgu_def_id.is_none() {
703 cgu_def_id = Some(def_id.krate.as_def_id());
705 }
706 break;
707 } else if tcx.def_kind(current_def_id) == DefKind::Mod {
708 if cgu_def_id.is_none() {
709 cgu_def_id = Some(current_def_id);
710 }
711 } else {
712 cgu_def_id = None;
716 }
717
718 current_def_id = tcx.parent(current_def_id);
719 }
720
721 let cgu_def_id = cgu_def_id.unwrap();
722
723 *cache.entry((cgu_def_id, volatile)).or_insert_with(|| {
724 let def_path = tcx.def_path(cgu_def_id);
725
726 let components = def_path.data.iter().map(|part| match part.data.name() {
727 DefPathDataName::Named(name) => name,
728 DefPathDataName::Anon { .. } => unreachable!(),
729 });
730
731 let volatile_suffix = volatile.then_some("volatile");
732
733 name_builder.build_cgu_name(def_path.krate, components, volatile_suffix)
734 })
735}
736
737fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder<'_>) -> Symbol {
739 name_builder.build_cgu_name(LOCAL_CRATE, &["fallback"], Some("cgu"))
740}
741
742fn mono_item_linkage_and_visibility<'tcx>(
743 tcx: TyCtxt<'tcx>,
744 mono_item: &MonoItem<'tcx>,
745 can_be_internalized: &mut bool,
746 can_export_generics: bool,
747 always_export_generics: bool,
748) -> (Linkage, Visibility) {
749 if let Some(explicit_linkage) = mono_item.explicit_linkage(tcx) {
750 return (explicit_linkage, Visibility::Default);
751 }
752 let vis = mono_item_visibility(
753 tcx,
754 mono_item,
755 can_be_internalized,
756 can_export_generics,
757 always_export_generics,
758 );
759 (Linkage::External, vis)
760}
761
762type CguNameCache = UnordMap<(DefId, bool), Symbol>;
763
764fn static_visibility<'tcx>(
765 tcx: TyCtxt<'tcx>,
766 can_be_internalized: &mut bool,
767 def_id: DefId,
768) -> Visibility {
769 if tcx.is_reachable_non_generic(def_id) {
770 *can_be_internalized = false;
771 default_visibility(tcx, def_id, false)
772 } else {
773 Visibility::Hidden
774 }
775}
776
777fn mono_item_visibility<'tcx>(
778 tcx: TyCtxt<'tcx>,
779 mono_item: &MonoItem<'tcx>,
780 can_be_internalized: &mut bool,
781 can_export_generics: bool,
782 always_export_generics: bool,
783) -> Visibility {
784 let instance = match mono_item {
785 MonoItem::Fn(instance) => instance,
787
788 MonoItem::Static(def_id) => return static_visibility(tcx, can_be_internalized, *def_id),
790 MonoItem::GlobalAsm(item_id) => {
791 return static_visibility(tcx, can_be_internalized, item_id.owner_id.to_def_id());
792 }
793 };
794
795 let def_id = match instance.def {
796 InstanceKind::Item(def_id)
797 | InstanceKind::DropGlue(def_id, Some(_))
798 | InstanceKind::AsyncDropGlueCtorShim(def_id, Some(_)) => def_id,
799
800 InstanceKind::ThreadLocalShim(def_id) => {
802 return static_visibility(tcx, can_be_internalized, def_id);
803 }
804
805 InstanceKind::VTableShim(..)
807 | InstanceKind::ReifyShim(..)
808 | InstanceKind::FnPtrShim(..)
809 | InstanceKind::Virtual(..)
810 | InstanceKind::Intrinsic(..)
811 | InstanceKind::ClosureOnceShim { .. }
812 | InstanceKind::ConstructCoroutineInClosureShim { .. }
813 | InstanceKind::DropGlue(..)
814 | InstanceKind::AsyncDropGlueCtorShim(..)
815 | InstanceKind::CloneShim(..)
816 | InstanceKind::FnPtrAddrShim(..) => return Visibility::Hidden,
817 };
818
819 if tcx.is_lang_item(def_id, LangItem::Start) {
833 *can_be_internalized = false;
834 return Visibility::Hidden;
835 }
836
837 let is_generic = instance.args.non_erasable_generics().next().is_some();
838
839 let Some(def_id) = def_id.as_local() else {
841 return if is_generic
842 && (always_export_generics
843 || (can_export_generics
844 && tcx.codegen_fn_attrs(def_id).inline
845 == rustc_attr_parsing::InlineAttr::Never))
846 {
847 *can_be_internalized = false;
850 default_visibility(tcx, def_id, true)
851 } else {
852 Visibility::Hidden
853 };
854 };
855
856 if is_generic {
857 if always_export_generics
858 || (can_export_generics
859 && tcx.codegen_fn_attrs(def_id).inline == rustc_attr_parsing::InlineAttr::Never)
860 {
861 if tcx.is_unreachable_local_definition(def_id) {
862 Visibility::Hidden
864 } else {
865 *can_be_internalized = false;
867 default_visibility(tcx, def_id.to_def_id(), true)
868 }
869 } else {
870 Visibility::Hidden
873 }
874 } else {
875 if tcx.is_reachable_non_generic(def_id.to_def_id()) {
879 *can_be_internalized = false;
880 debug_assert!(!is_generic);
881 return default_visibility(tcx, def_id.to_def_id(), false);
882 }
883
884 let attrs = tcx.codegen_fn_attrs(def_id);
919 if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
920 *can_be_internalized = false;
921 }
922
923 Visibility::Hidden
924 }
925}
926
927fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibility {
928 if tcx.sess.default_visibility() == SymbolVisibility::Interposable {
930 return Visibility::Default;
931 }
932
933 let export_level = if is_generic {
934 SymbolExportLevel::Rust
936 } else {
937 match tcx.reachable_non_generics(id.krate).get(&id) {
938 Some(SymbolExportInfo { level: SymbolExportLevel::C, .. }) => SymbolExportLevel::C,
939 _ => SymbolExportLevel::Rust,
940 }
941 };
942
943 match export_level {
944 SymbolExportLevel::C => Visibility::Default,
947
948 SymbolExportLevel::Rust => tcx.sess.default_visibility().into(),
950 }
951}
952
953fn debug_dump<'a, 'tcx: 'a>(tcx: TyCtxt<'tcx>, label: &str, cgus: &[CodegenUnit<'tcx>]) {
954 let dump = move || {
955 use std::fmt::Write;
956
957 let mut num_cgus = 0;
958 let mut all_cgu_sizes = Vec::new();
959
960 let mut inlined_items = UnordSet::default();
966
967 let mut root_items = 0;
968 let mut unique_inlined_items = 0;
969 let mut placed_inlined_items = 0;
970
971 let mut root_size = 0;
972 let mut unique_inlined_size = 0;
973 let mut placed_inlined_size = 0;
974
975 for cgu in cgus.iter() {
976 num_cgus += 1;
977 all_cgu_sizes.push(cgu.size_estimate());
978
979 for (item, data) in cgu.items() {
980 if !data.inlined {
981 root_items += 1;
982 root_size += data.size_estimate;
983 } else {
984 if inlined_items.insert(item) {
985 unique_inlined_items += 1;
986 unique_inlined_size += data.size_estimate;
987 }
988 placed_inlined_items += 1;
989 placed_inlined_size += data.size_estimate;
990 }
991 }
992 }
993
994 all_cgu_sizes.sort_unstable_by_key(|&n| cmp::Reverse(n));
995
996 let unique_items = root_items + unique_inlined_items;
997 let placed_items = root_items + placed_inlined_items;
998 let items_ratio = placed_items as f64 / unique_items as f64;
999
1000 let unique_size = root_size + unique_inlined_size;
1001 let placed_size = root_size + placed_inlined_size;
1002 let size_ratio = placed_size as f64 / unique_size as f64;
1003
1004 let mean_cgu_size = placed_size as f64 / num_cgus as f64;
1005
1006 assert_eq!(placed_size, all_cgu_sizes.iter().sum::<usize>());
1007
1008 let s = &mut String::new();
1009 let _ = writeln!(s, "{label}");
1010 let _ = writeln!(
1011 s,
1012 "- unique items: {unique_items} ({root_items} root + {unique_inlined_items} inlined), \
1013 unique size: {unique_size} ({root_size} root + {unique_inlined_size} inlined)\n\
1014 - placed items: {placed_items} ({root_items} root + {placed_inlined_items} inlined), \
1015 placed size: {placed_size} ({root_size} root + {placed_inlined_size} inlined)\n\
1016 - placed/unique items ratio: {items_ratio:.2}, \
1017 placed/unique size ratio: {size_ratio:.2}\n\
1018 - CGUs: {num_cgus}, mean size: {mean_cgu_size:.1}, sizes: {}",
1019 list(&all_cgu_sizes),
1020 );
1021 let _ = writeln!(s);
1022
1023 for (i, cgu) in cgus.iter().enumerate() {
1024 let name = cgu.name();
1025 let size = cgu.size_estimate();
1026 let num_items = cgu.items().len();
1027 let mean_size = size as f64 / num_items as f64;
1028
1029 let mut placed_item_sizes: Vec<_> =
1030 cgu.items().values().map(|data| data.size_estimate).collect();
1031 placed_item_sizes.sort_unstable_by_key(|&n| cmp::Reverse(n));
1032 let sizes = list(&placed_item_sizes);
1033
1034 let _ = writeln!(s, "- CGU[{i}]");
1035 let _ = writeln!(s, " - {name}, size: {size}");
1036 let _ =
1037 writeln!(s, " - items: {num_items}, mean size: {mean_size:.1}, sizes: {sizes}",);
1038
1039 for (item, data) in cgu.items_in_deterministic_order(tcx) {
1040 let linkage = data.linkage;
1041 let symbol_name = item.symbol_name(tcx).name;
1042 let symbol_hash_start = symbol_name.rfind('h');
1043 let symbol_hash = symbol_hash_start.map_or("<no hash>", |i| &symbol_name[i..]);
1044 let kind = if !data.inlined { "root" } else { "inlined" };
1045 let size = data.size_estimate;
1046 let _ = with_no_trimmed_paths!(writeln!(
1047 s,
1048 " - {item} [{linkage:?}] [{symbol_hash}] ({kind}, size: {size})"
1049 ));
1050 }
1051
1052 let _ = writeln!(s);
1053 }
1054
1055 return std::mem::take(s);
1056
1057 fn list(ns: &[usize]) -> String {
1060 let mut v = Vec::new();
1061 if ns.is_empty() {
1062 return "[]".to_string();
1063 }
1064
1065 let mut elem = |curr, curr_count| {
1066 if curr_count == 1 {
1067 v.push(format!("{curr}"));
1068 } else {
1069 v.push(format!("{curr} (x{curr_count})"));
1070 }
1071 };
1072
1073 let mut curr = ns[0];
1074 let mut curr_count = 1;
1075
1076 for &n in &ns[1..] {
1077 if n != curr {
1078 elem(curr, curr_count);
1079 curr = n;
1080 curr_count = 1;
1081 } else {
1082 curr_count += 1;
1083 }
1084 }
1085 elem(curr, curr_count);
1086
1087 format!("[{}]", v.join(", "))
1088 }
1089 };
1090
1091 debug!("{}", dump());
1092}
1093
1094#[inline(never)] fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'tcx>, mono_items: I)
1096where
1097 I: Iterator<Item = &'a MonoItem<'tcx>>,
1098 'tcx: 'a,
1099{
1100 let _prof_timer = tcx.prof.generic_activity("assert_symbols_are_distinct");
1101
1102 let mut symbols: Vec<_> =
1103 mono_items.map(|mono_item| (mono_item, mono_item.symbol_name(tcx))).collect();
1104
1105 symbols.sort_by_key(|sym| sym.1);
1106
1107 for &[(mono_item1, ref sym1), (mono_item2, ref sym2)] in symbols.array_windows() {
1108 if sym1 == sym2 {
1109 let span1 = mono_item1.local_span(tcx);
1110 let span2 = mono_item2.local_span(tcx);
1111
1112 let span = match (span1, span2) {
1114 (Some(span1), Some(span2)) => {
1115 Some(if span1.lo().0 > span2.lo().0 { span1 } else { span2 })
1116 }
1117 (span1, span2) => span1.or(span2),
1118 };
1119
1120 tcx.dcx().emit_fatal(SymbolAlreadyDefined { span, symbol: sym1.to_string() });
1121 }
1122 }
1123}
1124
1125fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> MonoItemPartitions<'_> {
1126 let collection_strategy = match tcx.sess.opts.unstable_opts.print_mono_items {
1127 Some(ref s) => {
1128 let mode = s.to_lowercase();
1129 let mode = mode.trim();
1130 if mode == "eager" {
1131 MonoItemCollectionStrategy::Eager
1132 } else {
1133 if mode != "lazy" {
1134 tcx.dcx().emit_warn(UnknownCguCollectionMode { mode });
1135 }
1136
1137 MonoItemCollectionStrategy::Lazy
1138 }
1139 }
1140 None => {
1141 if tcx.sess.link_dead_code() {
1142 MonoItemCollectionStrategy::Eager
1143 } else {
1144 MonoItemCollectionStrategy::Lazy
1145 }
1146 }
1147 };
1148
1149 let (items, usage_map) = collector::collect_crate_mono_items(tcx, collection_strategy);
1150
1151 tcx.dcx().abort_if_errors();
1155
1156 let (codegen_units, _) = tcx.sess.time("partition_and_assert_distinct_symbols", || {
1157 sync::join(
1158 || {
1159 let mut codegen_units = partition(tcx, items.iter().copied(), &usage_map);
1160 codegen_units[0].make_primary();
1161 &*tcx.arena.alloc_from_iter(codegen_units)
1162 },
1163 || assert_symbols_are_distinct(tcx, items.iter()),
1164 )
1165 });
1166
1167 if tcx.prof.enabled() {
1168 for cgu in codegen_units {
1170 tcx.prof.artifact_size(
1171 "codegen_unit_size_estimate",
1172 cgu.name().as_str(),
1173 cgu.size_estimate() as u64,
1174 );
1175 }
1176 }
1177
1178 #[cfg(not(llvm_enzyme))]
1179 let autodiff_mono_items: Vec<_> = vec![];
1180 #[cfg(llvm_enzyme)]
1181 let mut autodiff_mono_items: Vec<_> = vec![];
1182 let mono_items: DefIdSet = items
1183 .iter()
1184 .filter_map(|mono_item| match *mono_item {
1185 MonoItem::Fn(ref instance) => {
1186 #[cfg(llvm_enzyme)]
1187 autodiff_mono_items.push((mono_item, instance));
1188 Some(instance.def_id())
1189 }
1190 MonoItem::Static(def_id) => Some(def_id),
1191 _ => None,
1192 })
1193 .collect();
1194
1195 let autodiff_items =
1196 autodiff::find_autodiff_source_functions(tcx, &usage_map, autodiff_mono_items);
1197 let autodiff_items = tcx.arena.alloc_from_iter(autodiff_items);
1198
1199 if let SwitchWithOptPath::Enabled(ref path) = tcx.sess.opts.unstable_opts.dump_mono_stats {
1201 if let Err(err) =
1202 dump_mono_items_stats(tcx, codegen_units, path, tcx.crate_name(LOCAL_CRATE))
1203 {
1204 tcx.dcx().emit_fatal(CouldntDumpMonoStats { error: err.to_string() });
1205 }
1206 }
1207
1208 if tcx.sess.opts.unstable_opts.print_mono_items.is_some() {
1209 let mut item_to_cgus: UnordMap<_, Vec<_>> = Default::default();
1210
1211 for cgu in codegen_units {
1212 for (&mono_item, &data) in cgu.items() {
1213 item_to_cgus.entry(mono_item).or_default().push((cgu.name(), data.linkage));
1214 }
1215 }
1216
1217 let mut item_keys: Vec<_> = items
1218 .iter()
1219 .map(|i| {
1220 let mut output = with_no_trimmed_paths!(i.to_string());
1221 output.push_str(" @@");
1222 let mut empty = Vec::new();
1223 let cgus = item_to_cgus.get_mut(i).unwrap_or(&mut empty);
1224 cgus.sort_by_key(|(name, _)| *name);
1225 cgus.dedup();
1226 for &(ref cgu_name, linkage) in cgus.iter() {
1227 output.push(' ');
1228 output.push_str(cgu_name.as_str());
1229
1230 let linkage_abbrev = match linkage {
1231 Linkage::External => "External",
1232 Linkage::AvailableExternally => "Available",
1233 Linkage::LinkOnceAny => "OnceAny",
1234 Linkage::LinkOnceODR => "OnceODR",
1235 Linkage::WeakAny => "WeakAny",
1236 Linkage::WeakODR => "WeakODR",
1237 Linkage::Internal => "Internal",
1238 Linkage::ExternalWeak => "ExternalWeak",
1239 Linkage::Common => "Common",
1240 };
1241
1242 output.push('[');
1243 output.push_str(linkage_abbrev);
1244 output.push(']');
1245 }
1246 output
1247 })
1248 .collect();
1249
1250 item_keys.sort();
1251
1252 for item in item_keys {
1253 println!("MONO_ITEM {item}");
1254 }
1255 }
1256
1257 MonoItemPartitions {
1258 all_mono_items: tcx.arena.alloc(mono_items),
1259 codegen_units,
1260 autodiff_items,
1261 }
1262}
1263
1264fn dump_mono_items_stats<'tcx>(
1267 tcx: TyCtxt<'tcx>,
1268 codegen_units: &[CodegenUnit<'tcx>],
1269 output_directory: &Option<PathBuf>,
1270 crate_name: Symbol,
1271) -> Result<(), Box<dyn std::error::Error>> {
1272 let output_directory = if let Some(ref directory) = output_directory {
1273 fs::create_dir_all(directory)?;
1274 directory
1275 } else {
1276 Path::new(".")
1277 };
1278
1279 let format = tcx.sess.opts.unstable_opts.dump_mono_stats_format;
1280 let ext = format.extension();
1281 let filename = format!("{crate_name}.mono_items.{ext}");
1282 let output_path = output_directory.join(&filename);
1283 let mut file = File::create_buffered(&output_path)?;
1284
1285 let mut items_per_def_id: FxIndexMap<_, Vec<_>> = Default::default();
1287 for cgu in codegen_units {
1288 cgu.items()
1289 .keys()
1290 .filter(|mono_item| mono_item.is_user_defined())
1292 .for_each(|mono_item| {
1293 items_per_def_id.entry(mono_item.def_id()).or_default().push(mono_item);
1294 });
1295 }
1296
1297 #[derive(serde::Serialize)]
1298 struct MonoItem {
1299 name: String,
1300 instantiation_count: usize,
1301 size_estimate: usize,
1302 total_estimate: usize,
1303 }
1304
1305 let mut stats: Vec<_> = items_per_def_id
1307 .into_iter()
1308 .map(|(def_id, items)| {
1309 let name = with_no_trimmed_paths!(tcx.def_path_str(def_id));
1310 let instantiation_count = items.len();
1311 let size_estimate = items[0].size_estimate(tcx);
1312 let total_estimate = instantiation_count * size_estimate;
1313 MonoItem { name, instantiation_count, size_estimate, total_estimate }
1314 })
1315 .collect();
1316 stats.sort_unstable_by_key(|item| cmp::Reverse(item.total_estimate));
1317
1318 if !stats.is_empty() {
1319 match format {
1320 DumpMonoStatsFormat::Json => serde_json::to_writer(file, &stats)?,
1321 DumpMonoStatsFormat::Markdown => {
1322 writeln!(
1323 file,
1324 "| Item | Instantiation count | Estimated Cost Per Instantiation | Total Estimated Cost |"
1325 )?;
1326 writeln!(file, "| --- | ---: | ---: | ---: |")?;
1327
1328 for MonoItem { name, instantiation_count, size_estimate, total_estimate } in stats {
1329 writeln!(
1330 file,
1331 "| `{name}` | {instantiation_count} | {size_estimate} | {total_estimate} |"
1332 )?;
1333 }
1334 }
1335 }
1336 }
1337
1338 Ok(())
1339}
1340
1341pub(crate) fn provide(providers: &mut Providers) {
1342 providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
1343
1344 providers.is_codegened_item =
1345 |tcx, def_id| tcx.collect_and_partition_mono_items(()).all_mono_items.contains(&def_id);
1346
1347 providers.codegen_unit = |tcx, name| {
1348 tcx.collect_and_partition_mono_items(())
1349 .codegen_units
1350 .iter()
1351 .find(|cgu| cgu.name() == name)
1352 .unwrap_or_else(|| panic!("failed to find cgu with name {name:?}"))
1353 };
1354
1355 providers.size_estimate = |tcx, instance| {
1356 match instance.def {
1357 InstanceKind::Item(..)
1360 | InstanceKind::DropGlue(..)
1361 | InstanceKind::AsyncDropGlueCtorShim(..) => {
1362 let mir = tcx.instance_mir(instance.def);
1363 mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
1364 }
1365 _ => 1,
1367 }
1368 };
1369
1370 collector::provide(providers);
1371}