From b2a57e6b428689d8b6a9881f8ccdfd30d76767bc Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 24 Jun 2025 12:45:56 +1000 Subject: [PATCH] Tweak `-Zinput-stats` and `-Zmeta-stats` output. To make it match `-Zmacro-stats`, and work better if you have enabled it for multiple crates. - Print each crate's name. - Print a `===` banner at the start and end for separation. --- compiler/rustc_interface/src/passes.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 5 +++-- compiler/rustc_passes/src/input_stats.rs | 13 +++++++------ tests/ui/stats/input-stats.stderr | 10 ++++++---- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 13c2a22172c7..ae7f0c4a8234 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -370,7 +370,7 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) { let mut lint_buffer = resolver.lint_buffer.steal(); if sess.opts.unstable_opts.input_stats { - input_stats::print_ast_stats(krate); + input_stats::print_ast_stats(tcx, krate); } // Needs to go *after* expansion to be able to check the results of macro expansion. diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 14643f92a5d9..ed3c18a02a69 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -794,7 +794,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // `RUSTFLAGS='-Zmeta-stats' cargo build`). It still doesn't guarantee // non-interleaving, though. let mut s = String::new(); - _ = writeln!(s, "{prefix} METADATA STATS"); + _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w)); + _ = writeln!(s, "{prefix} METADATA STATS: {}", tcx.crate_name(LOCAL_CRATE)); _ = writeln!(s, "{prefix} {:size_w$}", "Section", "Size"); _ = writeln!(s, "{prefix} {}", "-".repeat(banner_w)); for (label, size) in stats { @@ -814,7 +815,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { usize_with_underscores(total_bytes), perc(zero_bytes) ); - _ = writeln!(s, "{prefix}"); + _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w)); eprint!("{s}"); } diff --git a/compiler/rustc_passes/src/input_stats.rs b/compiler/rustc_passes/src/input_stats.rs index 00212b2a6db1..40bc18939d63 100644 --- a/compiler/rustc_passes/src/input_stats.rs +++ b/compiler/rustc_passes/src/input_stats.rs @@ -65,16 +65,16 @@ pub fn print_hir_stats(tcx: TyCtxt<'_>) { StatCollector { tcx: Some(tcx), nodes: FxHashMap::default(), seen: FxHashSet::default() }; tcx.hir_walk_toplevel_module(&mut collector); tcx.hir_walk_attributes(&mut collector); - collector.print("HIR STATS", "hir-stats"); + collector.print(tcx, "HIR STATS", "hir-stats"); } -pub fn print_ast_stats(krate: &ast::Crate) { +pub fn print_ast_stats(tcx: TyCtxt<'_>, krate: &ast::Crate) { use rustc_ast::visit::Visitor; let mut collector = StatCollector { tcx: None, nodes: FxHashMap::default(), seen: FxHashSet::default() }; collector.visit_crate(krate); - collector.print("POST EXPANSION AST STATS", "ast-stats"); + collector.print(tcx, "POST EXPANSION AST STATS", "ast-stats"); } impl<'k> StatCollector<'k> { @@ -116,7 +116,7 @@ impl<'k> StatCollector<'k> { } } - fn print(&self, title: &str, prefix: &str) { + fn print(&self, tcx: TyCtxt<'_>, title: &str, prefix: &str) { use std::fmt::Write; // We will soon sort, so the initial order does not matter. @@ -142,7 +142,8 @@ impl<'k> StatCollector<'k> { // `RUSTFLAGS='-Zinput-stats' cargo build`). It still doesn't guarantee // non-interleaving, though. let mut s = String::new(); - _ = writeln!(s, "{prefix} {title}"); + _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w)); + _ = writeln!(s, "{prefix} {title}: {}", tcx.crate_name(hir::def_id::LOCAL_CRATE)); _ = writeln!( s, "{prefix} {:acc_size_w$}{:>count_w$}{:>item_size_w$}", @@ -193,7 +194,7 @@ impl<'k> StatCollector<'k> { "", usize_with_underscores(total_count), ); - _ = writeln!(s, "{prefix}"); + _ = writeln!(s, "{prefix} {}", "=".repeat(banner_w)); eprint!("{s}"); } } diff --git a/tests/ui/stats/input-stats.stderr b/tests/ui/stats/input-stats.stderr index 77e39f687709..b3b8784fa270 100644 --- a/tests/ui/stats/input-stats.stderr +++ b/tests/ui/stats/input-stats.stderr @@ -1,4 +1,5 @@ -ast-stats POST EXPANSION AST STATS +ast-stats ================================================================ +ast-stats POST EXPANSION AST STATS: input_stats ast-stats Name Accumulated Size Count Item Size ast-stats ---------------------------------------------------------------- ast-stats Item 1_584 (NN.N%) 11 144 @@ -57,8 +58,9 @@ ast-stats - AngleBracketed 40 (NN.N%) 1 ast-stats Crate 40 (NN.N%) 1 40 ast-stats ---------------------------------------------------------------- ast-stats Total 7_416 127 -ast-stats -hir-stats HIR STATS +ast-stats ================================================================ +hir-stats ================================================================ +hir-stats HIR STATS: input_stats hir-stats Name Accumulated Size Count Item Size hir-stats ---------------------------------------------------------------- hir-stats PathSegment 1_776 (NN.N%) 37 48 @@ -118,4 +120,4 @@ hir-stats Lifetime 28 (NN.N%) 1 28 hir-stats ForeignItemRef 24 (NN.N%) 1 24 hir-stats ---------------------------------------------------------------- hir-stats Total 8_676 172 -hir-stats +hir-stats ================================================================