Add prefix to every line of -Zhir-stats output.

This is based on `-Zprint-type-sizes` which does the same thing. It
makes the output provenance clearer, and helps with post-processing.
E.g. if you have `-Zhir-stats` output from numerous compiler invocations
you can now easily extract the pre-expansion stats separately from the
post-expansion stats.
This commit is contained in:
Nicholas Nethercote 2022-08-26 13:52:41 +10:00
parent 223d16ebbd
commit 9ee0192095
3 changed files with 164 additions and 165 deletions

View File

@ -69,7 +69,7 @@ pub fn parse<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
} }
if sess.opts.unstable_opts.hir_stats { if sess.opts.unstable_opts.hir_stats {
hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS"); hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS", "ast-stats-1");
} }
Ok(krate) Ok(krate)
@ -416,7 +416,7 @@ pub fn configure_and_expand(
} }
if sess.opts.unstable_opts.hir_stats { if sess.opts.unstable_opts.hir_stats {
hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS"); hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS", "ast-stats-2");
} }
resolver.resolve_crate(&krate); resolver.resolve_crate(&krate);

View File

@ -74,16 +74,16 @@ pub fn print_hir_stats(tcx: TyCtxt<'_>) {
}; };
tcx.hir().walk_toplevel_module(&mut collector); tcx.hir().walk_toplevel_module(&mut collector);
tcx.hir().walk_attributes(&mut collector); tcx.hir().walk_attributes(&mut collector);
collector.print("HIR STATS"); collector.print("HIR STATS", "hir-stats");
} }
pub fn print_ast_stats(krate: &ast::Crate, title: &str) { pub fn print_ast_stats(krate: &ast::Crate, title: &str, prefix: &str) {
use rustc_ast::visit::Visitor; use rustc_ast::visit::Visitor;
let mut collector = let mut collector =
StatCollector { krate: None, nodes: FxHashMap::default(), seen: FxHashSet::default() }; StatCollector { krate: None, nodes: FxHashMap::default(), seen: FxHashSet::default() };
collector.visit_crate(krate); collector.visit_crate(krate);
collector.print(title); collector.print(title, prefix);
} }
impl<'k> StatCollector<'k> { impl<'k> StatCollector<'k> {
@ -119,23 +119,26 @@ impl<'k> StatCollector<'k> {
} }
} }
fn print(&self, title: &str) { fn print(&self, title: &str, prefix: &str) {
let mut nodes: Vec<_> = self.nodes.iter().collect(); let mut nodes: Vec<_> = self.nodes.iter().collect();
nodes.sort_by_key(|&(_, ref node)| node.stats.count * node.stats.size); nodes.sort_by_key(|&(_, ref node)| node.stats.count * node.stats.size);
let total_size = nodes.iter().map(|(_, node)| node.stats.count * node.stats.size).sum(); let total_size = nodes.iter().map(|(_, node)| node.stats.count * node.stats.size).sum();
eprintln!("\n{}\n", title); eprintln!("{} {}", prefix, title);
eprintln!(
eprintln!("{:<18}{:>18}{:>14}{:>14}", "Name", "Accumulated Size", "Count", "Item Size"); "{} {:<18}{:>18}{:>14}{:>14}",
eprintln!("----------------------------------------------------------------"); prefix, "Name", "Accumulated Size", "Count", "Item Size"
);
eprintln!("{} ----------------------------------------------------------------", prefix);
let percent = |m, n| (m * 100) as f64 / n as f64; let percent = |m, n| (m * 100) as f64 / n as f64;
for (label, node) in nodes { for (label, node) in nodes {
let size = node.stats.count * node.stats.size; let size = node.stats.count * node.stats.size;
eprintln!( eprintln!(
"{:<18}{:>10} ({:4.1}%){:>14}{:>14}", "{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
prefix,
label, label,
to_readable_str(size), to_readable_str(size),
percent(size, total_size), percent(size, total_size),
@ -149,7 +152,8 @@ impl<'k> StatCollector<'k> {
for (label, subnode) in subnodes { for (label, subnode) in subnodes {
let size = subnode.count * subnode.size; let size = subnode.count * subnode.size;
eprintln!( eprintln!(
"- {:<18}{:>10} ({:4.1}%){:>14}", "{} - {:<18}{:>10} ({:4.1}%){:>14}",
prefix,
label, label,
to_readable_str(size), to_readable_str(size),
percent(size, total_size), percent(size, total_size),
@ -158,8 +162,9 @@ impl<'k> StatCollector<'k> {
} }
} }
} }
eprintln!("----------------------------------------------------------------"); eprintln!("{} ----------------------------------------------------------------", prefix);
eprintln!("{:<18}{:>10}\n", "Total", to_readable_str(total_size)); eprintln!("{} {:<18}{:>10}", prefix, "Total", to_readable_str(total_size));
eprintln!("{}", prefix);
} }
} }

View File

@ -1,151 +1,145 @@
ast-stats-1 PRE EXPANSION AST STATS
PRE EXPANSION AST STATS ast-stats-1 Name Accumulated Size Count Item Size
ast-stats-1 ----------------------------------------------------------------
Name Accumulated Size Count Item Size ast-stats-1 ExprField 48 ( 0.6%) 1 48
---------------------------------------------------------------- ast-stats-1 Crate 56 ( 0.7%) 1 56
ExprField 48 ( 0.6%) 1 48 ast-stats-1 Attribute 64 ( 0.8%) 2 32
Crate 56 ( 0.7%) 1 56 ast-stats-1 - Normal 32 ( 0.4%) 1
Attribute 64 ( 0.8%) 2 32 ast-stats-1 - DocComment 32 ( 0.4%) 1
- Normal 32 ( 0.4%) 1 ast-stats-1 GenericArgs 64 ( 0.8%) 1 64
- DocComment 32 ( 0.4%) 1 ast-stats-1 - AngleBracketed 64 ( 0.8%) 1
GenericArgs 64 ( 0.8%) 1 64 ast-stats-1 Local 72 ( 0.9%) 1 72
- AngleBracketed 64 ( 0.8%) 1 ast-stats-1 WherePredicate 72 ( 0.9%) 1 72
Local 72 ( 0.9%) 1 72 ast-stats-1 - BoundPredicate 72 ( 0.9%) 1
WherePredicate 72 ( 0.9%) 1 72 ast-stats-1 Arm 96 ( 1.1%) 2 48
- BoundPredicate 72 ( 0.9%) 1 ast-stats-1 ForeignItem 96 ( 1.1%) 1 96
Arm 96 ( 1.1%) 2 48 ast-stats-1 - Fn 96 ( 1.1%) 1
ForeignItem 96 ( 1.1%) 1 96 ast-stats-1 FieldDef 160 ( 1.9%) 2 80
- Fn 96 ( 1.1%) 1 ast-stats-1 Stmt 160 ( 1.9%) 5 32
FieldDef 160 ( 1.9%) 2 80 ast-stats-1 - Local 32 ( 0.4%) 1
Stmt 160 ( 1.9%) 5 32 ast-stats-1 - MacCall 32 ( 0.4%) 1
- Local 32 ( 0.4%) 1 ast-stats-1 - Expr 96 ( 1.1%) 3
- MacCall 32 ( 0.4%) 1 ast-stats-1 Param 160 ( 1.9%) 4 40
- Expr 96 ( 1.1%) 3 ast-stats-1 FnDecl 200 ( 2.4%) 5 40
Param 160 ( 1.9%) 4 40 ast-stats-1 Variant 240 ( 2.8%) 2 120
FnDecl 200 ( 2.4%) 5 40 ast-stats-1 Block 288 ( 3.4%) 6 48
Variant 240 ( 2.8%) 2 120 ast-stats-1 GenericBound 352 ( 4.2%) 4 88
Block 288 ( 3.4%) 6 48 ast-stats-1 - Trait 352 ( 4.2%) 4
GenericBound 352 ( 4.2%) 4 88 ast-stats-1 AssocItem 416 ( 4.9%) 4 104
- Trait 352 ( 4.2%) 4 ast-stats-1 - TyAlias 208 ( 2.5%) 2
AssocItem 416 ( 4.9%) 4 104 ast-stats-1 - Fn 208 ( 2.5%) 2
- TyAlias 208 ( 2.5%) 2 ast-stats-1 GenericParam 520 ( 6.1%) 5 104
- Fn 208 ( 2.5%) 2 ast-stats-1 PathSegment 720 ( 8.5%) 30 24
GenericParam 520 ( 6.1%) 5 104 ast-stats-1 Expr 832 ( 9.8%) 8 104
PathSegment 720 ( 8.5%) 30 24 ast-stats-1 - Path 104 ( 1.2%) 1
Expr 832 ( 9.8%) 8 104 ast-stats-1 - Match 104 ( 1.2%) 1
- Path 104 ( 1.2%) 1 ast-stats-1 - Struct 104 ( 1.2%) 1
- Match 104 ( 1.2%) 1 ast-stats-1 - Lit 208 ( 2.5%) 2
- Struct 104 ( 1.2%) 1 ast-stats-1 - Block 312 ( 3.7%) 3
- Lit 208 ( 2.5%) 2 ast-stats-1 Pat 840 ( 9.9%) 7 120
- Block 312 ( 3.7%) 3 ast-stats-1 - Struct 120 ( 1.4%) 1
Pat 840 ( 9.9%) 7 120 ast-stats-1 - Wild 120 ( 1.4%) 1
- Struct 120 ( 1.4%) 1 ast-stats-1 - Ident 600 ( 7.1%) 5
- Wild 120 ( 1.4%) 1 ast-stats-1 Ty 1_344 (15.9%) 14 96
- Ident 600 ( 7.1%) 5 ast-stats-1 - Rptr 96 ( 1.1%) 1
Ty 1_344 (15.9%) 14 96 ast-stats-1 - Ptr 96 ( 1.1%) 1
- Rptr 96 ( 1.1%) 1 ast-stats-1 - ImplicitSelf 192 ( 2.3%) 2
- Ptr 96 ( 1.1%) 1 ast-stats-1 - Path 960 (11.4%) 10
- ImplicitSelf 192 ( 2.3%) 2 ast-stats-1 Item 1_656 (19.6%) 9 184
- Path 960 (11.4%) 10 ast-stats-1 - Trait 184 ( 2.2%) 1
Item 1_656 (19.6%) 9 184 ast-stats-1 - Enum 184 ( 2.2%) 1
- Trait 184 ( 2.2%) 1 ast-stats-1 - ForeignMod 184 ( 2.2%) 1
- Enum 184 ( 2.2%) 1 ast-stats-1 - Impl 184 ( 2.2%) 1
- ForeignMod 184 ( 2.2%) 1 ast-stats-1 - Fn 368 ( 4.4%) 2
- Impl 184 ( 2.2%) 1 ast-stats-1 - Use 552 ( 6.5%) 3
- Fn 368 ( 4.4%) 2 ast-stats-1 ----------------------------------------------------------------
- Use 552 ( 6.5%) 3 ast-stats-1 Total 8_456
---------------------------------------------------------------- ast-stats-1
Total 8_456 ast-stats-2 POST EXPANSION AST STATS
ast-stats-2 Name Accumulated Size Count Item Size
ast-stats-2 ----------------------------------------------------------------
POST EXPANSION AST STATS ast-stats-2 ExprField 48 ( 0.5%) 1 48
ast-stats-2 Crate 56 ( 0.6%) 1 56
Name Accumulated Size Count Item Size ast-stats-2 GenericArgs 64 ( 0.7%) 1 64
---------------------------------------------------------------- ast-stats-2 - AngleBracketed 64 ( 0.7%) 1
ExprField 48 ( 0.5%) 1 48 ast-stats-2 Local 72 ( 0.8%) 1 72
Crate 56 ( 0.6%) 1 56 ast-stats-2 WherePredicate 72 ( 0.8%) 1 72
GenericArgs 64 ( 0.7%) 1 64 ast-stats-2 - BoundPredicate 72 ( 0.8%) 1
- AngleBracketed 64 ( 0.7%) 1 ast-stats-2 Arm 96 ( 1.0%) 2 48
Local 72 ( 0.8%) 1 72 ast-stats-2 ForeignItem 96 ( 1.0%) 1 96
WherePredicate 72 ( 0.8%) 1 72 ast-stats-2 - Fn 96 ( 1.0%) 1
- BoundPredicate 72 ( 0.8%) 1 ast-stats-2 InlineAsm 120 ( 1.3%) 1 120
Arm 96 ( 1.0%) 2 48 ast-stats-2 Attribute 128 ( 1.4%) 4 32
ForeignItem 96 ( 1.0%) 1 96 ast-stats-2 - DocComment 32 ( 0.3%) 1
- Fn 96 ( 1.0%) 1 ast-stats-2 - Normal 96 ( 1.0%) 3
InlineAsm 120 ( 1.3%) 1 120 ast-stats-2 FieldDef 160 ( 1.7%) 2 80
Attribute 128 ( 1.4%) 4 32 ast-stats-2 Stmt 160 ( 1.7%) 5 32
- DocComment 32 ( 0.3%) 1 ast-stats-2 - Local 32 ( 0.3%) 1
- Normal 96 ( 1.0%) 3 ast-stats-2 - Semi 32 ( 0.3%) 1
FieldDef 160 ( 1.7%) 2 80 ast-stats-2 - Expr 96 ( 1.0%) 3
Stmt 160 ( 1.7%) 5 32 ast-stats-2 Param 160 ( 1.7%) 4 40
- Local 32 ( 0.3%) 1 ast-stats-2 FnDecl 200 ( 2.2%) 5 40
- Semi 32 ( 0.3%) 1 ast-stats-2 Variant 240 ( 2.6%) 2 120
- Expr 96 ( 1.0%) 3 ast-stats-2 Block 288 ( 3.1%) 6 48
Param 160 ( 1.7%) 4 40 ast-stats-2 GenericBound 352 ( 3.8%) 4 88
FnDecl 200 ( 2.2%) 5 40 ast-stats-2 - Trait 352 ( 3.8%) 4
Variant 240 ( 2.6%) 2 120 ast-stats-2 AssocItem 416 ( 4.5%) 4 104
Block 288 ( 3.1%) 6 48 ast-stats-2 - TyAlias 208 ( 2.3%) 2
GenericBound 352 ( 3.8%) 4 88 ast-stats-2 - Fn 208 ( 2.3%) 2
- Trait 352 ( 3.8%) 4 ast-stats-2 GenericParam 520 ( 5.7%) 5 104
AssocItem 416 ( 4.5%) 4 104 ast-stats-2 PathSegment 792 ( 8.6%) 33 24
- TyAlias 208 ( 2.3%) 2 ast-stats-2 Pat 840 ( 9.1%) 7 120
- Fn 208 ( 2.3%) 2 ast-stats-2 - Struct 120 ( 1.3%) 1
GenericParam 520 ( 5.7%) 5 104 ast-stats-2 - Wild 120 ( 1.3%) 1
PathSegment 792 ( 8.6%) 33 24 ast-stats-2 - Ident 600 ( 6.5%) 5
Pat 840 ( 9.1%) 7 120 ast-stats-2 Expr 936 (10.2%) 9 104
- Struct 120 ( 1.3%) 1 ast-stats-2 - Path 104 ( 1.1%) 1
- Wild 120 ( 1.3%) 1 ast-stats-2 - Match 104 ( 1.1%) 1
- Ident 600 ( 6.5%) 5 ast-stats-2 - Struct 104 ( 1.1%) 1
Expr 936 (10.2%) 9 104 ast-stats-2 - InlineAsm 104 ( 1.1%) 1
- Path 104 ( 1.1%) 1 ast-stats-2 - Lit 208 ( 2.3%) 2
- Match 104 ( 1.1%) 1 ast-stats-2 - Block 312 ( 3.4%) 3
- Struct 104 ( 1.1%) 1 ast-stats-2 Ty 1_344 (14.6%) 14 96
- InlineAsm 104 ( 1.1%) 1 ast-stats-2 - Rptr 96 ( 1.0%) 1
- Lit 208 ( 2.3%) 2 ast-stats-2 - Ptr 96 ( 1.0%) 1
- Block 312 ( 3.4%) 3 ast-stats-2 - ImplicitSelf 192 ( 2.1%) 2
Ty 1_344 (14.6%) 14 96 ast-stats-2 - Path 960 (10.5%) 10
- Rptr 96 ( 1.0%) 1 ast-stats-2 Item 2_024 (22.0%) 11 184
- Ptr 96 ( 1.0%) 1 ast-stats-2 - Trait 184 ( 2.0%) 1
- ImplicitSelf 192 ( 2.1%) 2 ast-stats-2 - Enum 184 ( 2.0%) 1
- Path 960 (10.5%) 10 ast-stats-2 - ExternCrate 184 ( 2.0%) 1
Item 2_024 (22.0%) 11 184 ast-stats-2 - ForeignMod 184 ( 2.0%) 1
- Trait 184 ( 2.0%) 1 ast-stats-2 - Impl 184 ( 2.0%) 1
- Enum 184 ( 2.0%) 1 ast-stats-2 - Fn 368 ( 4.0%) 2
- ExternCrate 184 ( 2.0%) 1 ast-stats-2 - Use 736 ( 8.0%) 4
- ForeignMod 184 ( 2.0%) 1 ast-stats-2 ----------------------------------------------------------------
- Impl 184 ( 2.0%) 1 ast-stats-2 Total 9_184
- Fn 368 ( 4.0%) 2 ast-stats-2
- Use 736 ( 8.0%) 4 hir-stats HIR STATS
---------------------------------------------------------------- hir-stats Name Accumulated Size Count Item Size
Total 9_184 hir-stats ----------------------------------------------------------------
hir-stats Param 64 ( 0.7%) 2 32
hir-stats Local 64 ( 0.7%) 1 64
HIR STATS hir-stats ForeignItem 72 ( 0.8%) 1 72
hir-stats FieldDef 96 ( 1.0%) 2 48
Name Accumulated Size Count Item Size hir-stats Arm 96 ( 1.0%) 2 48
---------------------------------------------------------------- hir-stats Stmt 96 ( 1.0%) 3 32
Param 64 ( 0.7%) 2 32 hir-stats FnDecl 120 ( 1.3%) 3 40
Local 64 ( 0.7%) 1 64 hir-stats Attribute 128 ( 1.4%) 4 32
ForeignItem 72 ( 0.8%) 1 72 hir-stats Lifetime 128 ( 1.4%) 4 32
FieldDef 96 ( 1.0%) 2 48 hir-stats Variant 160 ( 1.7%) 2 80
Arm 96 ( 1.0%) 2 48 hir-stats ImplItem 176 ( 1.9%) 2 88
Stmt 96 ( 1.0%) 3 32 hir-stats GenericBound 192 ( 2.1%) 4 48
FnDecl 120 ( 1.3%) 3 40 hir-stats TraitItem 192 ( 2.1%) 2 96
Attribute 128 ( 1.4%) 4 32 hir-stats WherePredicate 216 ( 2.3%) 3 72
Lifetime 128 ( 1.4%) 4 32 hir-stats Block 288 ( 3.1%) 6 48
Variant 160 ( 1.7%) 2 80 hir-stats QPath 408 ( 4.4%) 17 24
ImplItem 176 ( 1.9%) 2 88 hir-stats Pat 440 ( 4.8%) 5 88
GenericBound 192 ( 2.1%) 4 48 hir-stats Expr 672 ( 7.3%) 12 56
TraitItem 192 ( 2.1%) 2 96 hir-stats Item 960 (10.4%) 12 80
WherePredicate 216 ( 2.3%) 3 72 hir-stats Ty 1_152 (12.4%) 16 72
Block 288 ( 3.1%) 6 48 hir-stats Path 1_296 (14.0%) 27 48
QPath 408 ( 4.4%) 17 24 hir-stats PathSegment 2_240 (24.2%) 40 56
Pat 440 ( 4.8%) 5 88 hir-stats ----------------------------------------------------------------
Expr 672 ( 7.3%) 12 56 hir-stats Total 9_256
Item 960 (10.4%) 12 80 hir-stats
Ty 1_152 (12.4%) 16 72
Path 1_296 (14.0%) 27 48
PathSegment 2_240 (24.2%) 40 56
----------------------------------------------------------------
Total 9_256