@@ -102,14 +102,14 @@ use std::path::{Path, PathBuf};
102
102
103
103
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
104
104
use rustc_data_structures:: sync;
105
- use rustc_hir:: def_id:: DefIdSet ;
105
+ use rustc_hir:: def_id:: { DefIdSet , LOCAL_CRATE } ;
106
106
use rustc_middle:: mir;
107
107
use rustc_middle:: mir:: mono:: MonoItem ;
108
108
use rustc_middle:: mir:: mono:: { CodegenUnit , Linkage } ;
109
109
use rustc_middle:: ty:: print:: with_no_trimmed_paths;
110
110
use rustc_middle:: ty:: query:: Providers ;
111
111
use rustc_middle:: ty:: TyCtxt ;
112
- use rustc_session:: config:: SwitchWithOptPath ;
112
+ use rustc_session:: config:: { DumpMonoStatsFormat , SwitchWithOptPath } ;
113
113
use rustc_span:: symbol:: Symbol ;
114
114
115
115
use crate :: collector:: InliningMap ;
@@ -417,7 +417,7 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co
417
417
// Output monomorphization stats per def_id
418
418
if let SwitchWithOptPath :: Enabled ( ref path) = tcx. sess . opts . unstable_opts . dump_mono_stats {
419
419
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 ) )
421
421
{
422
422
tcx. sess . emit_fatal ( CouldntDumpMonoStats { error : err. to_string ( ) } ) ;
423
423
}
@@ -483,7 +483,7 @@ fn dump_mono_items_stats<'tcx>(
483
483
tcx : TyCtxt < ' tcx > ,
484
484
codegen_units : & [ CodegenUnit < ' tcx > ] ,
485
485
output_directory : & Option < PathBuf > ,
486
- crate_name : Option < & str > ,
486
+ crate_name : Symbol ,
487
487
) -> Result < ( ) , Box < dyn std:: error:: Error > > {
488
488
let output_directory = if let Some ( ref directory) = output_directory {
489
489
fs:: create_dir_all ( directory) ?;
@@ -492,9 +492,11 @@ fn dump_mono_items_stats<'tcx>(
492
492
Path :: new ( "." )
493
493
} ;
494
494
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}" ) ;
496
498
let output_path = output_directory. join ( & filename) ;
497
- let file = File :: create ( output_path) ?;
499
+ let file = File :: create ( & output_path) ?;
498
500
let mut file = BufWriter :: new ( file) ;
499
501
500
502
// Gather instantiated mono items grouped by def_id
@@ -508,30 +510,44 @@ fn dump_mono_items_stats<'tcx>(
508
510
}
509
511
}
510
512
513
+ #[ derive( serde:: Serialize ) ]
514
+ struct MonoItem {
515
+ name : String ,
516
+ instantiation_count : usize ,
517
+ size_estimate : usize ,
518
+ total_estimate : usize ,
519
+ }
520
+
511
521
// Output stats sorted by total instantiated size, from heaviest to lightest
512
522
let mut stats: Vec < _ > = items_per_def_id
513
523
. into_iter ( )
514
524
. map ( |( def_id, items) | {
525
+ let name = with_no_trimmed_paths ! ( tcx. def_path_str( def_id) ) ;
515
526
let instantiation_count = items. len ( ) ;
516
527
let size_estimate = items[ 0 ] . size_estimate ( tcx) ;
517
528
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 }
519
530
} )
520
531
. 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 ) ) ;
522
533
523
534
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
+ }
535
551
}
536
552
}
537
553
0 commit comments