Skip to content

Commit 70c9b3b

Browse files
committed
Better self profiling integration
1 parent d65ba25 commit 70c9b3b

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ target
44
*.o
55
perf.data
66
perf.data.old
7+
*.events
8+
*.string*
79
/build_sysroot/sysroot
810
/build_sysroot/sysroot_src
911
/build_sysroot/Cargo.lock

src/base.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
6161
fx.bcx.switch_to_block(entry_block);
6262
crate::trap::trap_unreachable(&mut fx, "function has uninhabited argument");
6363
} else {
64-
crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
65-
codegen_fn_content(&mut fx);
64+
tcx.sess.time("codegen clif ir", || {
65+
tcx.sess.time("codegen prelude", || crate::abi::codegen_fn_prelude(&mut fx, start_ebb));
66+
codegen_fn_content(&mut fx);
67+
});
6668
}
6769

6870
// Recover all necessary data from fx, before accessing func will prevent future access to it.
@@ -78,10 +80,13 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
7880
verify_func(tcx, &clif_comments, &context.func);
7981

8082
// Perform rust specific optimizations
81-
crate::optimize::optimize_function(cx.tcx, instance, context, &mut clif_comments);
83+
tcx.sess.time("optimize clif ir", || {
84+
crate::optimize::optimize_function(tcx, instance, context, &mut clif_comments);
85+
});
8286

8387
// Define function
84-
cx.module.define_function(func_id, context).unwrap();
88+
let module = &mut cx.module;
89+
tcx.sess.time("define function", || module.define_function(func_id, context).unwrap());
8590

8691
// Write optimized function to file for debugging
8792
#[cfg(debug_assertions)]
@@ -102,30 +107,34 @@ pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
102107

103108
// Define debuginfo for function
104109
let isa = cx.module.isa();
105-
debug_context
106-
.as_mut()
107-
.map(|x| x.define(context, isa, &source_info_set, local_map));
110+
tcx.sess.time("generate debug info", || {
111+
debug_context
112+
.as_mut()
113+
.map(|x| x.define(context, isa, &source_info_set, local_map));
114+
});
108115

109116
// Clear context to make it usable for the next function
110117
context.clear();
111118
}
112119

113120
pub fn verify_func(tcx: TyCtxt, writer: &crate::pretty_clif::CommentWriter, func: &Function) {
114-
let flags = settings::Flags::new(settings::builder());
115-
match ::cranelift_codegen::verify_function(&func, &flags) {
116-
Ok(_) => {}
117-
Err(err) => {
118-
tcx.sess.err(&format!("{:?}", err));
119-
let pretty_error = ::cranelift_codegen::print_errors::pretty_verifier_error(
120-
&func,
121-
None,
122-
Some(Box::new(writer)),
123-
err,
124-
);
125-
tcx.sess
126-
.fatal(&format!("cranelift verify error:\n{}", pretty_error));
121+
tcx.sess.time("verify clif ir", || {
122+
let flags = settings::Flags::new(settings::builder());
123+
match ::cranelift_codegen::verify_function(&func, &flags) {
124+
Ok(_) => {}
125+
Err(err) => {
126+
tcx.sess.err(&format!("{:?}", err));
127+
let pretty_error = ::cranelift_codegen::print_errors::pretty_verifier_error(
128+
&func,
129+
None,
130+
Some(Box::new(writer)),
131+
err,
132+
);
133+
tcx.sess
134+
.fatal(&format!("cranelift verify error:\n{}", pretty_error));
135+
}
127136
}
128-
}
137+
});
129138
}
130139

131140
fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
@@ -225,13 +234,13 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
225234
cleanup: _,
226235
from_hir_call: _,
227236
} => {
228-
crate::abi::codegen_terminator_call(
237+
fx.tcx.sess.time("codegen call", || crate::abi::codegen_terminator_call(
229238
fx,
230239
func,
231240
args,
232241
destination,
233242
bb_data.terminator().source_info.span,
234-
);
243+
));
235244
}
236245
TerminatorKind::Resume | TerminatorKind::Abort => {
237246
trap_unreachable(fx, "[corruption] Unwinding bb reached.");

src/driver.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,20 @@ fn codegen_mono_items<'tcx>(
294294
) {
295295
let mut cx = CodegenCx::new(tcx, module, debug_context);
296296

297-
time("codegen mono items", move || {
298-
for (&mono_item, &(linkage, visibility)) in &mono_items {
299-
match mono_item {
300-
MonoItem::Fn(instance) => {
301-
let (name, sig) =
302-
get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false);
303-
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
304-
cx.module.declare_function(&name, linkage, &sig).unwrap();
297+
time(tcx.sess, "codegen mono items", move || {
298+
tcx.sess.time("predefine functions", || {
299+
for (&mono_item, &(linkage, visibility)) in &mono_items {
300+
match mono_item {
301+
MonoItem::Fn(instance) => {
302+
let (name, sig) =
303+
get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false);
304+
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
305+
cx.module.declare_function(&name, linkage, &sig).unwrap();
306+
}
307+
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
305308
}
306-
MonoItem::Static(_) | MonoItem::GlobalAsm(_) => {}
307309
}
308-
}
310+
});
309311

310312
for (mono_item, (linkage, visibility)) in mono_items {
311313
crate::unimpl::try_unimpl(tcx, || {
@@ -314,7 +316,7 @@ fn codegen_mono_items<'tcx>(
314316
});
315317
}
316318

317-
cx.finalize();
319+
tcx.sess.time("finalize CodegenCx", || cx.finalize());
318320
});
319321
}
320322

@@ -350,7 +352,7 @@ fn trans_mono_item<'clif, 'tcx, B: Backend + 'static>(
350352
}
351353
});
352354

353-
crate::base::trans_fn(cx, inst, linkage);
355+
cx.tcx.sess.time("codegen fn", || crate::base::trans_fn(cx, inst, linkage));
354356
}
355357
MonoItem::Static(def_id) => {
356358
crate::constant::codegen_static(&mut cx.constants_cx, def_id);
@@ -361,10 +363,10 @@ fn trans_mono_item<'clif, 'tcx, B: Backend + 'static>(
361363
}
362364
}
363365

364-
fn time<R>(name: &str, f: impl FnOnce() -> R) -> R {
366+
fn time<R>(sess: &Session, name: &str, f: impl FnOnce() -> R) -> R {
365367
println!("[{}] start", name);
366368
let before = std::time::Instant::now();
367-
let res = f();
369+
let res = sess.time(name, f);
368370
let after = std::time::Instant::now();
369371
println!("[{}] end time: {:?}", name, after - before);
370372
res

0 commit comments

Comments
 (0)