Skip to content

Commit 6bb5e46

Browse files
committed
emit egraph stats
1 parent 1654ef8 commit 6bb5e46

File tree

5 files changed

+58
-30
lines changed

5 files changed

+58
-30
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cranelift/codegen/src/context.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
use crate::alias_analysis::AliasAnalysis;
1313
use crate::dominator_tree::DominatorTree;
14-
use crate::egraph::EgraphPass;
14+
use crate::egraph::{EgraphPass, Stats};
1515
use crate::flowgraph::ControlFlowGraph;
1616
use crate::inline::{Inline, do_inlining};
1717
use crate::ir::Function;
@@ -55,6 +55,9 @@ pub struct Context {
5555

5656
/// Flag: do we want a disassembly with the CompiledCode?
5757
pub want_disasm: bool,
58+
59+
/// Stats.
60+
pub stats: Option<Stats>,
5861
}
5962

6063
impl Context {
@@ -78,6 +81,7 @@ impl Context {
7881
loop_analysis: LoopAnalysis::new(),
7982
compiled_code: None,
8083
want_disasm: false,
84+
stats: None,
8185
}
8286
}
8387

@@ -109,6 +113,11 @@ impl Context {
109113
self.want_disasm = val;
110114
}
111115

116+
/// Take the stats, if any.
117+
pub fn take_stats(&mut self) -> Option<Stats> {
118+
self.stats.take()
119+
}
120+
112121
/// Compile the function, and emit machine code into a `Vec<u8>`.
113122
#[deprecated = "use Context::compile"]
114123
pub fn compile_and_emit(
@@ -392,6 +401,7 @@ impl Context {
392401
);
393402
pass.run();
394403
log::debug!("egraph stats: {:?}", pass.stats);
404+
self.stats = Some(pass.stats);
395405
trace!("After egraph optimization:\n{}", self.func.display());
396406

397407
self.verify_if(fisa)

cranelift/codegen/src/egraph.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use cranelift_control::ControlPlane;
2626
use cranelift_entity::SecondaryMap;
2727
use cranelift_entity::packed_option::ReservedValue;
2828
use rustc_hash::FxHashSet;
29+
#[cfg(feature = "enable-serde")]
30+
use serde_derive::{Deserialize, Serialize};
2931
use smallvec::SmallVec;
3032

3133
mod cost;
@@ -1153,32 +1155,33 @@ impl<'a> CtxHash<(Type, InstructionData)> for GVNContext<'a> {
11531155

11541156
/// Statistics collected during egraph-based processing.
11551157
#[derive(Clone, Debug, Default)]
1156-
pub(crate) struct Stats {
1157-
pub(crate) pure_inst: u64,
1158-
pub(crate) pure_inst_deduped: u64,
1159-
pub(crate) pure_inst_subsume: u64,
1160-
pub(crate) pure_inst_rewrite_to_self: u64,
1161-
pub(crate) pure_inst_insert_orig: u64,
1162-
pub(crate) pure_inst_insert_new: u64,
1163-
pub(crate) skeleton_inst: u64,
1164-
pub(crate) skeleton_inst_simplified: u64,
1165-
pub(crate) skeleton_inst_gvn: u64,
1166-
pub(crate) alias_analysis_removed: u64,
1167-
pub(crate) new_inst: u64,
1168-
pub(crate) union: u64,
1169-
pub(crate) subsume: u64,
1170-
pub(crate) remat: u64,
1171-
pub(crate) rewrite_rule_invoked: u64,
1172-
pub(crate) rewrite_rule_results: u64,
1173-
pub(crate) rewrite_depth_limit: u64,
1174-
pub(crate) elaborate_visit_node: u64,
1175-
pub(crate) elaborate_memoize_hit: u64,
1176-
pub(crate) elaborate_memoize_miss: u64,
1177-
pub(crate) elaborate_remat: u64,
1178-
pub(crate) elaborate_licm_hoist: u64,
1179-
pub(crate) elaborate_func: u64,
1180-
pub(crate) elaborate_func_pre_insts: u64,
1181-
pub(crate) elaborate_func_post_insts: u64,
1182-
pub(crate) eclass_size_limit: u64,
1183-
pub(crate) elaborate_not_best_chosen: u64,
1158+
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
1159+
pub struct Stats {
1160+
pub pure_inst: u64,
1161+
pub pure_inst_deduped: u64,
1162+
pub pure_inst_subsume: u64,
1163+
pub pure_inst_rewrite_to_self: u64,
1164+
pub pure_inst_insert_orig: u64,
1165+
pub pure_inst_insert_new: u64,
1166+
pub skeleton_inst: u64,
1167+
pub skeleton_inst_simplified: u64,
1168+
pub skeleton_inst_gvn: u64,
1169+
pub alias_analysis_removed: u64,
1170+
pub new_inst: u64,
1171+
pub union: u64,
1172+
pub subsume: u64,
1173+
pub remat: u64,
1174+
pub rewrite_rule_invoked: u64,
1175+
pub rewrite_rule_results: u64,
1176+
pub rewrite_depth_limit: u64,
1177+
pub elaborate_visit_node: u64,
1178+
pub elaborate_memoize_hit: u64,
1179+
pub elaborate_memoize_miss: u64,
1180+
pub elaborate_remat: u64,
1181+
pub elaborate_licm_hoist: u64,
1182+
pub elaborate_func: u64,
1183+
pub elaborate_func_pre_insts: u64,
1184+
pub elaborate_func_post_insts: u64,
1185+
pub eclass_size_limit: u64,
1186+
pub elaborate_not_best_chosen: u64,
11841187
}

crates/cranelift/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ workspace = true
1818
anyhow = { workspace = true }
1919
log = { workspace = true }
2020
wasmtime-environ = { workspace = true, features = ['compile'] }
21-
cranelift-codegen = { workspace = true, features = ["host-arch", "timing"] }
21+
cranelift-codegen = { workspace = true, features = ["host-arch", "timing", "enable-serde"] }
2222
cranelift-frontend = { workspace = true }
2323
cranelift-entity = { workspace = true }
2424
cranelift-native = { workspace = true }
@@ -35,6 +35,7 @@ itertools = { workspace = true }
3535
pulley-interpreter = { workspace = true, optional = true }
3636
wasmtime-math = { workspace = true }
3737
wasmtime-unwinder = { workspace = true, features = ["cranelift"] }
38+
serde_json = { workspace = true }
3839

3940
[features]
4041
all-arch = ["cranelift-codegen/all-arch"]

crates/cranelift/src/compiler.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,19 @@ impl FunctionCompiler<'_> {
14731473
)
14741474
.unwrap();
14751475
write!(output, "{}", context.func.display()).unwrap();
1476+
1477+
if let Some(stats) = context.take_stats() {
1478+
let mut path = self
1479+
.compiler
1480+
.clif_dir
1481+
.as_ref()
1482+
.unwrap()
1483+
.join(symbol.replace(":", "-"));
1484+
path.set_extension("json");
1485+
1486+
let mut output = std::fs::File::create(path).unwrap();
1487+
serde_json::to_writer_pretty(&mut output, &stats).unwrap();
1488+
}
14761489
}
14771490

14781491
let compiled_code = compilation_result?;

0 commit comments

Comments
 (0)