@@ -102,14 +102,14 @@ use std::path::{Path, PathBuf};
102102
103103use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
104104use rustc_data_structures:: sync;
105- use rustc_hir:: def_id:: DefIdSet ;
105+ use rustc_hir:: def_id:: { DefIdSet , LOCAL_CRATE } ;
106106use rustc_middle:: mir;
107107use rustc_middle:: mir:: mono:: MonoItem ;
108108use rustc_middle:: mir:: mono:: { CodegenUnit , Linkage } ;
109109use rustc_middle:: ty:: print:: with_no_trimmed_paths;
110110use rustc_middle:: ty:: query:: Providers ;
111111use rustc_middle:: ty:: TyCtxt ;
112- use rustc_session:: config:: SwitchWithOptPath ;
112+ use rustc_session:: config:: { DumpMonoStatsFormat , SwitchWithOptPath } ;
113113use rustc_span:: symbol:: Symbol ;
114114
115115use crate :: collector:: InliningMap ;
@@ -417,7 +417,7 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co
417417 // Output monomorphization stats per def_id
418418 if let SwitchWithOptPath :: Enabled ( ref path) = tcx. sess . opts . unstable_opts . dump_mono_stats {
419419 if let Err ( err) =
420- dump_mono_items_stats ( tcx, & codegen_units, path, tcx. sess . opts . crate_name . as_deref ( ) )
420+ dump_mono_items_stats ( tcx, & codegen_units, path, tcx. crate_name ( LOCAL_CRATE ) )
421421 {
422422 tcx. sess . emit_fatal ( CouldntDumpMonoStats { error : err. to_string ( ) } ) ;
423423 }
@@ -483,7 +483,7 @@ fn dump_mono_items_stats<'tcx>(
483483 tcx : TyCtxt < ' tcx > ,
484484 codegen_units : & [ CodegenUnit < ' tcx > ] ,
485485 output_directory : & Option < PathBuf > ,
486- crate_name : Option < & str > ,
486+ crate_name : Symbol ,
487487) -> Result < ( ) , Box < dyn std:: error:: Error > > {
488488 let output_directory = if let Some ( ref directory) = output_directory {
489489 fs:: create_dir_all ( directory) ?;
@@ -492,9 +492,11 @@ fn dump_mono_items_stats<'tcx>(
492492 Path :: new ( "." )
493493 } ;
494494
495- let filename = format ! ( "{}.mono_items.md" , crate_name. unwrap_or( "unknown-crate" ) ) ;
495+ let format = tcx. sess . opts . unstable_opts . dump_mono_stats_format ;
496+ let ext = format. extension ( ) ;
497+ let filename = format ! ( "{crate_name}.mono_items.{ext}" ) ;
496498 let output_path = output_directory. join ( & filename) ;
497- let file = File :: create ( output_path) ?;
499+ let file = File :: create ( & output_path) ?;
498500 let mut file = BufWriter :: new ( file) ;
499501
500502 // Gather instantiated mono items grouped by def_id
@@ -508,30 +510,44 @@ fn dump_mono_items_stats<'tcx>(
508510 }
509511 }
510512
513+ #[ derive( serde:: Serialize ) ]
514+ struct MonoItem {
515+ name : String ,
516+ instantiation_count : usize ,
517+ size_estimate : usize ,
518+ total_estimate : usize ,
519+ }
520+
511521 // Output stats sorted by total instantiated size, from heaviest to lightest
512522 let mut stats: Vec < _ > = items_per_def_id
513523 . into_iter ( )
514524 . map ( |( def_id, items) | {
525+ let name = with_no_trimmed_paths ! ( tcx. def_path_str( def_id) ) ;
515526 let instantiation_count = items. len ( ) ;
516527 let size_estimate = items[ 0 ] . size_estimate ( tcx) ;
517528 let total_estimate = instantiation_count * size_estimate;
518- ( def_id , instantiation_count, size_estimate, total_estimate)
529+ MonoItem { name , instantiation_count, size_estimate, total_estimate }
519530 } )
520531 . collect ( ) ;
521- stats. sort_unstable_by_key ( |( _ , _ , _ , total_estimate ) | cmp:: Reverse ( * total_estimate) ) ;
532+ stats. sort_unstable_by_key ( |item | cmp:: Reverse ( item . total_estimate ) ) ;
522533
523534 if !stats. is_empty ( ) {
524- writeln ! (
525- file,
526- "| Item | Instantiation count | Estimated Cost Per Instantiation | Total Estimated Cost |"
527- ) ?;
528- writeln ! ( file, "| --- | ---: | ---: | ---: |" ) ?;
529- for ( def_id, instantiation_count, size_estimate, total_estimate) in stats {
530- let item = with_no_trimmed_paths ! ( tcx. def_path_str( def_id) ) ;
531- writeln ! (
532- file,
533- "| {item} | {instantiation_count} | {size_estimate} | {total_estimate} |"
534- ) ?;
535+ match format {
536+ DumpMonoStatsFormat :: Json => serde_json:: to_writer ( file, & stats) ?,
537+ DumpMonoStatsFormat :: Markdown => {
538+ writeln ! (
539+ file,
540+ "| Item | Instantiation count | Estimated Cost Per Instantiation | Total Estimated Cost |"
541+ ) ?;
542+ writeln ! ( file, "| --- | ---: | ---: | ---: |" ) ?;
543+
544+ for MonoItem { name, instantiation_count, size_estimate, total_estimate } in stats {
545+ writeln ! (
546+ file,
547+ "| `{name}` | {instantiation_count} | {size_estimate} | {total_estimate} |"
548+ ) ?;
549+ }
550+ }
535551 }
536552 }
537553
0 commit comments