Skip to content

Commit 1b85b43

Browse files
committed
add mir-stats to analysis-stats
1 parent b7b9ae5 commit 1b85b43

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! errors.
33
44
use std::{
5+
collections::HashMap,
56
env,
67
time::{SystemTime, UNIX_EPOCH},
78
};
@@ -153,6 +154,10 @@ impl flags::AnalysisStats {
153154
self.run_inference(&host, db, &vfs, &funcs, verbosity);
154155
}
155156

157+
if self.mir_stats {
158+
self.lower_mir(db, &funcs);
159+
}
160+
156161
let total_span = analysis_sw.elapsed();
157162
eprintln!("{:<20} {total_span}", "Total:");
158163
report_metric("total time", total_span.time.as_millis() as u64, "ms");
@@ -189,6 +194,24 @@ impl flags::AnalysisStats {
189194
Ok(())
190195
}
191196

197+
fn lower_mir(&self, db: &RootDatabase, funcs: &[Function]) {
198+
let all = funcs.len();
199+
let mut fail = 0;
200+
let mut h: HashMap<String, usize> = HashMap::new();
201+
for f in funcs {
202+
let f = FunctionId::from(*f);
203+
let Err(e) = db.mir_body(f.into()) else {
204+
continue;
205+
};
206+
let es = format!("{:?}", e);
207+
*h.entry(es).or_default() += 1;
208+
fail += 1;
209+
}
210+
let h = h.into_iter().sorted_by_key(|x| x.1).collect::<Vec<_>>();
211+
eprintln!("Mir failed reasons: {:#?}", h);
212+
eprintln!("Mir failed bodies: {fail} ({}%)", fail * 100 / all);
213+
}
214+
192215
fn run_inference(
193216
&self,
194217
host: &AnalysisHost,

crates/rust-analyzer/src/cli/flags.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ xflags::xflags! {
6666
optional --memory-usage
6767
/// Print the total length of all source and macro files (whitespace is not counted).
6868
optional --source-stats
69+
/// Print the number of bodies that fail to lower to mir, in addition to failed reasons.
70+
optional --mir-stats
6971

7072
/// Only analyze items matching this path.
7173
optional -o, --only path: String
@@ -172,6 +174,7 @@ pub struct AnalysisStats {
172174
pub parallel: bool,
173175
pub memory_usage: bool,
174176
pub source_stats: bool,
177+
pub mir_stats: bool,
175178
pub only: Option<String>,
176179
pub with_deps: bool,
177180
pub no_sysroot: bool,

0 commit comments

Comments
 (0)