Skip to content

Commit 9ee0192

Browse files
committed
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.
1 parent 223d16e commit 9ee0192

File tree

3 files changed

+164
-165
lines changed

3 files changed

+164
-165
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn parse<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
6969
}
7070

7171
if sess.opts.unstable_opts.hir_stats {
72-
hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS");
72+
hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS", "ast-stats-1");
7373
}
7474

7575
Ok(krate)
@@ -416,7 +416,7 @@ pub fn configure_and_expand(
416416
}
417417

418418
if sess.opts.unstable_opts.hir_stats {
419-
hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS");
419+
hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS", "ast-stats-2");
420420
}
421421

422422
resolver.resolve_crate(&krate);

compiler/rustc_passes/src/hir_stats.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ pub fn print_hir_stats(tcx: TyCtxt<'_>) {
7474
};
7575
tcx.hir().walk_toplevel_module(&mut collector);
7676
tcx.hir().walk_attributes(&mut collector);
77-
collector.print("HIR STATS");
77+
collector.print("HIR STATS", "hir-stats");
7878
}
7979

80-
pub fn print_ast_stats(krate: &ast::Crate, title: &str) {
80+
pub fn print_ast_stats(krate: &ast::Crate, title: &str, prefix: &str) {
8181
use rustc_ast::visit::Visitor;
8282

8383
let mut collector =
8484
StatCollector { krate: None, nodes: FxHashMap::default(), seen: FxHashSet::default() };
8585
collector.visit_crate(krate);
86-
collector.print(title);
86+
collector.print(title, prefix);
8787
}
8888

8989
impl<'k> StatCollector<'k> {
@@ -119,23 +119,26 @@ impl<'k> StatCollector<'k> {
119119
}
120120
}
121121

122-
fn print(&self, title: &str) {
122+
fn print(&self, title: &str, prefix: &str) {
123123
let mut nodes: Vec<_> = self.nodes.iter().collect();
124124
nodes.sort_by_key(|&(_, ref node)| node.stats.count * node.stats.size);
125125

126126
let total_size = nodes.iter().map(|(_, node)| node.stats.count * node.stats.size).sum();
127127

128-
eprintln!("\n{}\n", title);
129-
130-
eprintln!("{:<18}{:>18}{:>14}{:>14}", "Name", "Accumulated Size", "Count", "Item Size");
131-
eprintln!("----------------------------------------------------------------");
128+
eprintln!("{} {}", prefix, title);
129+
eprintln!(
130+
"{} {:<18}{:>18}{:>14}{:>14}",
131+
prefix, "Name", "Accumulated Size", "Count", "Item Size"
132+
);
133+
eprintln!("{} ----------------------------------------------------------------", prefix);
132134

133135
let percent = |m, n| (m * 100) as f64 / n as f64;
134136

135137
for (label, node) in nodes {
136138
let size = node.stats.count * node.stats.size;
137139
eprintln!(
138-
"{:<18}{:>10} ({:4.1}%){:>14}{:>14}",
140+
"{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
141+
prefix,
139142
label,
140143
to_readable_str(size),
141144
percent(size, total_size),
@@ -149,7 +152,8 @@ impl<'k> StatCollector<'k> {
149152
for (label, subnode) in subnodes {
150153
let size = subnode.count * subnode.size;
151154
eprintln!(
152-
"- {:<18}{:>10} ({:4.1}%){:>14}",
155+
"{} - {:<18}{:>10} ({:4.1}%){:>14}",
156+
prefix,
153157
label,
154158
to_readable_str(size),
155159
percent(size, total_size),
@@ -158,8 +162,9 @@ impl<'k> StatCollector<'k> {
158162
}
159163
}
160164
}
161-
eprintln!("----------------------------------------------------------------");
162-
eprintln!("{:<18}{:>10}\n", "Total", to_readable_str(total_size));
165+
eprintln!("{} ----------------------------------------------------------------", prefix);
166+
eprintln!("{} {:<18}{:>10}", prefix, "Total", to_readable_str(total_size));
167+
eprintln!("{}", prefix);
163168
}
164169
}
165170

0 commit comments

Comments
 (0)