Skip to content

Commit c4507f4

Browse files
author
Jonathan Turner
authored
Rollup merge of #37583 - michaelwoerister:hir-stats, r=alexcrichton
Add `-Z hir-stats` for collecting statistics on HIR and AST The data collected will be printed to the commandline and looks like the following: ``` // stats for libcore PRE EXPANSION AST STATS Name Accumulated Size Count Item Size ---------------------------------------------------------------- TypeBinding 2_280 57 40 Mod 3_560 89 40 PathListItem 6_516 181 36 Variant 7_872 82 96 LifetimeDef 21_280 380 56 StructField 22_880 260 88 Lifetime 23_800 1_190 20 Local 30_192 629 48 ForeignItem 31_504 179 176 Arm 42_880 670 64 Mac 46_960 587 80 FnDecl 57_792 1_204 48 TraitItem 69_504 362 192 TyParamBound 98_280 945 104 Block 108_384 2_258 48 Stmt 144_720 3_618 40 ImplItem 230_272 1_028 224 Item 467_456 1_826 256 Pat 517_776 4_623 112 Attribute 745_680 15_535 48 Ty 1_114_848 9_954 112 PathSegment 1_218_528 16_924 72 Expr 3_082_408 20_279 152 ---------------------------------------------------------------- Total 8_095_372 POST EXPANSION AST STATS Name Accumulated Size Count Item Size ---------------------------------------------------------------- MacroDef 1_056 12 88 Mod 3_400 85 40 TypeBinding 4_280 107 40 PathListItem 6_516 181 36 Variant 7_872 82 96 StructField 24_904 283 88 ForeignItem 31_504 179 176 TraitItem 69_504 362 192 Local 85_008 1_771 48 Arm 100_288 1_567 64 Lifetime 123_980 6_199 20 LifetimeDef 126_728 2_263 56 TyParamBound 297_128 2_857 104 FnDecl 305_856 6_372 48 Block 481_104 10_023 48 Stmt 535_120 13_378 40 Item 1_469_952 5_742 256 Attribute 1_629_840 33_955 48 ImplItem 1_732_864 7_736 224 Pat 2_360_176 21_073 112 PathSegment 5_888_448 81_784 72 Ty 6_237_168 55_689 112 Expr 12_013_320 79_035 152 ---------------------------------------------------------------- Total 33_536_016 HIR STATS Name Accumulated Size Count Item Size ---------------------------------------------------------------- MacroDef 864 12 72 Mod 2_720 85 32 TypeBinding 3_424 107 32 PathListItem 5_068 181 28 Variant 6_560 82 80 StructField 20_376 283 72 ForeignItem 27_208 179 152 WherePredicate 43_776 684 64 TraitItem 52_128 362 144 Decl 68_992 2_156 32 Local 89_184 1_858 48 Arm 94_368 1_966 48 LifetimeDef 108_624 2_263 48 Lifetime 123_980 6_199 20 Stmt 168_000 4_200 40 TyParamBound 251_416 2_857 88 FnDecl 254_880 6_372 40 Block 583_968 12_166 48 Item 1_240_272 5_742 216 ImplItem 1_361_536 7_736 176 Attribute 1_620_480 33_760 48 Pat 2_073_120 21_595 96 Path 2_385_856 74_558 32 Ty 4_455_040 55_688 80 PathSegment 5_587_904 87_311 64 Expr 7_588_992 79_052 96 ---------------------------------------------------------------- Total 28_218_736 ```
2 parents c6bd7fe + 94e655e commit c4507f4

File tree

5 files changed

+428
-2
lines changed

5 files changed

+428
-2
lines changed

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
918918
"the directory the MIR is dumped into"),
919919
perf_stats: bool = (false, parse_bool, [UNTRACKED],
920920
"print some performance-related statistics"),
921+
hir_stats: bool = (false, parse_bool, [UNTRACKED],
922+
"print some statistics about AST and HIR"),
921923
}
922924

923925
pub fn default_lib_output() -> CrateType {

src/librustc/util/common.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ pub fn duration_to_secs_str(dur: Duration) -> String {
7575
format!("{:.3}", secs)
7676
}
7777

78+
pub fn to_readable_str(mut val: usize) -> String {
79+
let mut groups = vec![];
80+
loop {
81+
let group = val % 1000;
82+
83+
val /= 1000;
84+
85+
if val == 0 {
86+
groups.push(format!("{}", group));
87+
break
88+
} else {
89+
groups.push(format!("{:03}", group));
90+
}
91+
}
92+
93+
groups.reverse();
94+
95+
groups.join("_")
96+
}
97+
7898
pub fn record_time<T, F>(accu: &Cell<Duration>, f: F) -> T where
7999
F: FnOnce() -> T,
80100
{
@@ -264,3 +284,17 @@ pub fn path2cstr(p: &Path) -> CString {
264284
pub fn path2cstr(p: &Path) -> CString {
265285
CString::new(p.to_str().unwrap()).unwrap()
266286
}
287+
288+
289+
#[test]
290+
fn test_to_readable_str() {
291+
assert_eq!("0", to_readable_str(0));
292+
assert_eq!("1", to_readable_str(1));
293+
assert_eq!("99", to_readable_str(99));
294+
assert_eq!("999", to_readable_str(999));
295+
assert_eq!("1_000", to_readable_str(1_000));
296+
assert_eq!("1_001", to_readable_str(1_001));
297+
assert_eq!("999_999", to_readable_str(999_999));
298+
assert_eq!("1_000_000", to_readable_str(1_000_000));
299+
assert_eq!("1_234_567", to_readable_str(1_234_567));
300+
}

src/librustc_driver/driver.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ use rustc_typeck as typeck;
3737
use rustc_privacy;
3838
use rustc_plugin::registry::Registry;
3939
use rustc_plugin as plugin;
40-
use rustc_passes::{ast_validation, no_asm, loops, consts, rvalues, static_recursion};
40+
use rustc_passes::{ast_validation, no_asm, loops, consts, rvalues,
41+
static_recursion, hir_stats};
4142
use rustc_const_eval::check_match;
4243
use super::Compilation;
4344

@@ -513,6 +514,10 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session, input: &Input) -> PResult<'a,
513514
syntax::show_span::run(sess.diagnostic(), s, &krate);
514515
}
515516

517+
if sess.opts.debugging_opts.hir_stats {
518+
hir_stats::print_ast_stats(&krate, "PRE EXPANSION AST STATS");
519+
}
520+
516521
Ok(krate)
517522
}
518523

@@ -718,6 +723,10 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
718723
println!("Post-expansion node count: {}", count_nodes(&krate));
719724
}
720725

726+
if sess.opts.debugging_opts.hir_stats {
727+
hir_stats::print_ast_stats(&krate, "POST EXPANSION AST STATS");
728+
}
729+
721730
if sess.opts.debugging_opts.ast_json {
722731
println!("{}", json::as_json(&krate));
723732
}
@@ -758,7 +767,13 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
758767

759768
// Lower ast -> hir.
760769
let hir_forest = time(sess.time_passes(), "lowering ast -> hir", || {
761-
hir_map::Forest::new(lower_crate(sess, &krate, &mut resolver), &sess.dep_graph)
770+
let hir_crate = lower_crate(sess, &krate, &mut resolver);
771+
772+
if sess.opts.debugging_opts.hir_stats {
773+
hir_stats::print_hir_stats(&hir_crate);
774+
}
775+
776+
hir_map::Forest::new(hir_crate, &sess.dep_graph)
762777
});
763778

764779
// Discard hygiene data, which isn't required past lowering to HIR.

0 commit comments

Comments
 (0)