Skip to content

Commit 2c229a8

Browse files
authored
Merge pull request rust-lang#1346 from bjorn3/self_profile_cleanup
Improve -Ztime-passes and -Zself-profile output
2 parents 21bdff8 + a0d3b09 commit 2c229a8

File tree

5 files changed

+136
-123
lines changed

5 files changed

+136
-123
lines changed

src/base.rs

+36-24
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,6 @@ pub(crate) struct CodegenedFunction {
2121
func_debug_cx: Option<FunctionDebugContext>,
2222
}
2323

24-
#[cfg_attr(not(feature = "jit"), allow(dead_code))]
25-
pub(crate) fn codegen_and_compile_fn<'tcx>(
26-
tcx: TyCtxt<'tcx>,
27-
cx: &mut crate::CodegenCx,
28-
cached_context: &mut Context,
29-
module: &mut dyn Module,
30-
instance: Instance<'tcx>,
31-
) {
32-
let _inst_guard =
33-
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
34-
35-
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
36-
let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance);
37-
38-
compile_fn(cx, cached_context, module, codegened_func);
39-
}
40-
4124
pub(crate) fn codegen_fn<'tcx>(
4225
tcx: TyCtxt<'tcx>,
4326
cx: &mut crate::CodegenCx,
@@ -47,6 +30,9 @@ pub(crate) fn codegen_fn<'tcx>(
4730
) -> CodegenedFunction {
4831
debug_assert!(!instance.substs.needs_infer());
4932

33+
let symbol_name = tcx.symbol_name(instance).name.to_string();
34+
let _timer = tcx.prof.generic_activity_with_arg("codegen fn", &*symbol_name);
35+
5036
let mir = tcx.instance_mir(instance.def);
5137
let _mir_guard = crate::PrintOnPanic(|| {
5238
let mut buf = Vec::new();
@@ -58,7 +44,6 @@ pub(crate) fn codegen_fn<'tcx>(
5844
});
5945

6046
// Declare function
61-
let symbol_name = tcx.symbol_name(instance).name.to_string();
6247
let sig = get_function_sig(tcx, module.target_config().default_call_conv, instance);
6348
let func_id = module.declare_function(&symbol_name, Linkage::Local, &sig).unwrap();
6449

@@ -112,7 +97,7 @@ pub(crate) fn codegen_fn<'tcx>(
11297
next_ssa_var: 0,
11398
};
11499

115-
tcx.sess.time("codegen clif ir", || codegen_fn_body(&mut fx, start_block));
100+
tcx.prof.generic_activity("codegen clif ir").run(|| codegen_fn_body(&mut fx, start_block));
116101
fx.bcx.seal_all_blocks();
117102
fx.bcx.finalize();
118103

@@ -146,6 +131,9 @@ pub(crate) fn compile_fn(
146131
module: &mut dyn Module,
147132
codegened_func: CodegenedFunction,
148133
) {
134+
let _timer =
135+
cx.profiler.generic_activity_with_arg("compile function", &*codegened_func.symbol_name);
136+
149137
let clif_comments = codegened_func.clif_comments;
150138

151139
// Store function in context
@@ -191,9 +179,30 @@ pub(crate) fn compile_fn(
191179
};
192180

193181
// Define function
194-
cx.profiler.verbose_generic_activity("define function").run(|| {
182+
cx.profiler.generic_activity("define function").run(|| {
195183
context.want_disasm = cx.should_write_ir;
196184
module.define_function(codegened_func.func_id, context).unwrap();
185+
186+
if cx.profiler.enabled() {
187+
let mut recording_args = false;
188+
cx.profiler
189+
.generic_activity_with_arg_recorder(
190+
"define function (clif pass timings)",
191+
|recorder| {
192+
let pass_times = cranelift_codegen::timing::take_current();
193+
// Replace newlines with | as measureme doesn't allow control characters like
194+
// newlines inside strings.
195+
recorder.record_arg(format!("{}", pass_times).replace("\n", " | "));
196+
recording_args = true;
197+
},
198+
)
199+
.run(|| {
200+
if recording_args {
201+
// Wait a tiny bit to ensure chrome's profiler doesn't hide the event
202+
std::thread::sleep(std::time::Duration::from_nanos(2))
203+
}
204+
});
205+
}
197206
});
198207

199208
if cx.should_write_ir {
@@ -220,7 +229,7 @@ pub(crate) fn compile_fn(
220229
let isa = module.isa();
221230
let debug_context = &mut cx.debug_context;
222231
let unwind_context = &mut cx.unwind_context;
223-
cx.profiler.verbose_generic_activity("generate debug info").run(|| {
232+
cx.profiler.generic_activity("generate debug info").run(|| {
224233
if let Some(debug_context) = debug_context {
225234
codegened_func.func_debug_cx.unwrap().finalize(
226235
debug_context,
@@ -237,7 +246,7 @@ pub(crate) fn verify_func(
237246
writer: &crate::pretty_clif::CommentWriter,
238247
func: &Function,
239248
) {
240-
tcx.sess.time("verify clif ir", || {
249+
tcx.prof.generic_activity("verify clif ir").run(|| {
241250
let flags = cranelift_codegen::settings::Flags::new(cranelift_codegen::settings::builder());
242251
match cranelift_codegen::verify_function(&func, &flags) {
243252
Ok(_) => {}
@@ -273,7 +282,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
273282
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
274283
return;
275284
}
276-
fx.tcx.sess.time("codegen prelude", || crate::abi::codegen_fn_prelude(fx, start_block));
285+
fx.tcx
286+
.prof
287+
.generic_activity("codegen prelude")
288+
.run(|| crate::abi::codegen_fn_prelude(fx, start_block));
277289

278290
for (bb, bb_data) in fx.mir.basic_blocks.iter_enumerated() {
279291
let block = fx.get_block(bb);
@@ -434,7 +446,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
434446
cleanup: _,
435447
from_hir_call: _,
436448
} => {
437-
fx.tcx.sess.time("codegen call", || {
449+
fx.tcx.prof.generic_activity("codegen call").run(|| {
438450
crate::abi::codegen_terminator_call(
439451
fx,
440452
mir::SourceInfo { span: *fn_span, ..source_info },

src/config.rs

-8
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ pub struct BackendConfig {
4242
/// Defaults to the value of `CG_CLIF_JIT_ARGS`.
4343
pub jit_args: Vec<String>,
4444

45-
/// Display the time it took to perform codegen for a crate.
46-
///
47-
/// Defaults to true when the `CG_CLIF_DISPLAY_CG_TIME` env var is set to 1 or false otherwise.
48-
/// Can be set using `-Cllvm-args=display_cg_time=...`.
49-
pub display_cg_time: bool,
50-
5145
/// Enable the Cranelift ir verifier for all compilation passes. If not set it will only run
5246
/// once before passing the clif ir to Cranelift for compilation.
5347
///
@@ -73,7 +67,6 @@ impl Default for BackendConfig {
7367
let args = std::env::var("CG_CLIF_JIT_ARGS").unwrap_or_else(|_| String::new());
7468
args.split(' ').map(|arg| arg.to_string()).collect()
7569
},
76-
display_cg_time: bool_env_var("CG_CLIF_DISPLAY_CG_TIME"),
7770
enable_verifier: cfg!(debug_assertions) || bool_env_var("CG_CLIF_ENABLE_VERIFIER"),
7871
disable_incr_cache: bool_env_var("CG_CLIF_DISABLE_INCR_CACHE"),
7972
}
@@ -92,7 +85,6 @@ impl BackendConfig {
9285
if let Some((name, value)) = opt.split_once('=') {
9386
match name {
9487
"mode" => config.codegen_mode = value.parse()?,
95-
"display_cg_time" => config.display_cg_time = parse_bool(name, value)?,
9688
"enable_verifier" => config.enable_verifier = parse_bool(name, value)?,
9789
"disable_incr_cache" => config.disable_incr_cache = parse_bool(name, value)?,
9890
_ => return Err(format!("Unknown option `{}`", name)),

src/driver/aot.rs

+72-58
Original file line numberDiff line numberDiff line change
@@ -272,25 +272,25 @@ fn module_codegen(
272272
ConcurrencyLimiterToken,
273273
),
274274
) -> OngoingModuleCodegen {
275-
let (cgu_name, mut cx, mut module, codegened_functions) = tcx.sess.time("codegen cgu", || {
276-
let cgu = tcx.codegen_unit(cgu_name);
277-
let mono_items = cgu.items_in_deterministic_order(tcx);
278-
279-
let mut module = make_module(tcx.sess, &backend_config, cgu_name.as_str().to_string());
280-
281-
let mut cx = crate::CodegenCx::new(
282-
tcx,
283-
backend_config.clone(),
284-
module.isa(),
285-
tcx.sess.opts.debuginfo != DebugInfo::None,
286-
cgu_name,
287-
);
288-
super::predefine_mono_items(tcx, &mut module, &mono_items);
289-
let mut codegened_functions = vec![];
290-
for (mono_item, _) in mono_items {
291-
match mono_item {
292-
MonoItem::Fn(inst) => {
293-
tcx.sess.time("codegen fn", || {
275+
let (cgu_name, mut cx, mut module, codegened_functions) =
276+
tcx.prof.verbose_generic_activity_with_arg("codegen cgu", cgu_name.as_str()).run(|| {
277+
let cgu = tcx.codegen_unit(cgu_name);
278+
let mono_items = cgu.items_in_deterministic_order(tcx);
279+
280+
let mut module = make_module(tcx.sess, &backend_config, cgu_name.as_str().to_string());
281+
282+
let mut cx = crate::CodegenCx::new(
283+
tcx,
284+
backend_config.clone(),
285+
module.isa(),
286+
tcx.sess.opts.debuginfo != DebugInfo::None,
287+
cgu_name,
288+
);
289+
super::predefine_mono_items(tcx, &mut module, &mono_items);
290+
let mut codegened_functions = vec![];
291+
for (mono_item, _) in mono_items {
292+
match mono_item {
293+
MonoItem::Fn(inst) => {
294294
let codegened_function = crate::base::codegen_fn(
295295
tcx,
296296
&mut cx,
@@ -299,53 +299,68 @@ fn module_codegen(
299299
inst,
300300
);
301301
codegened_functions.push(codegened_function);
302-
});
303-
}
304-
MonoItem::Static(def_id) => {
305-
crate::constant::codegen_static(tcx, &mut module, def_id)
306-
}
307-
MonoItem::GlobalAsm(item_id) => {
308-
crate::global_asm::codegen_global_asm_item(tcx, &mut cx.global_asm, item_id);
302+
}
303+
MonoItem::Static(def_id) => {
304+
crate::constant::codegen_static(tcx, &mut module, def_id)
305+
}
306+
MonoItem::GlobalAsm(item_id) => {
307+
crate::global_asm::codegen_global_asm_item(
308+
tcx,
309+
&mut cx.global_asm,
310+
item_id,
311+
);
312+
}
309313
}
310314
}
311-
}
312-
crate::main_shim::maybe_create_entry_wrapper(
313-
tcx,
314-
&mut module,
315-
&mut cx.unwind_context,
316-
false,
317-
cgu.is_primary(),
318-
);
315+
crate::main_shim::maybe_create_entry_wrapper(
316+
tcx,
317+
&mut module,
318+
&mut cx.unwind_context,
319+
false,
320+
cgu.is_primary(),
321+
);
319322

320-
let cgu_name = cgu.name().as_str().to_owned();
323+
let cgu_name = cgu.name().as_str().to_owned();
321324

322-
(cgu_name, cx, module, codegened_functions)
323-
});
325+
(cgu_name, cx, module, codegened_functions)
326+
});
324327

325328
OngoingModuleCodegen::Async(std::thread::spawn(move || {
326-
cx.profiler.clone().verbose_generic_activity("compile functions").run(|| {
327-
let mut cached_context = Context::new();
328-
for codegened_func in codegened_functions {
329-
crate::base::compile_fn(&mut cx, &mut cached_context, &mut module, codegened_func);
330-
}
331-
});
329+
cx.profiler.clone().verbose_generic_activity_with_arg("compile functions", &*cgu_name).run(
330+
|| {
331+
let mut cached_context = Context::new();
332+
for codegened_func in codegened_functions {
333+
crate::base::compile_fn(
334+
&mut cx,
335+
&mut cached_context,
336+
&mut module,
337+
codegened_func,
338+
);
339+
}
340+
},
341+
);
332342

333-
let global_asm_object_file =
334-
cx.profiler.verbose_generic_activity("compile assembly").run(|| {
343+
let global_asm_object_file = cx
344+
.profiler
345+
.verbose_generic_activity_with_arg("compile assembly", &*cgu_name)
346+
.run(|| {
335347
crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm)
336348
})?;
337349

338-
let codegen_result = cx.profiler.verbose_generic_activity("write object file").run(|| {
339-
emit_cgu(
340-
&global_asm_config.output_filenames,
341-
&cx.profiler,
342-
cgu_name,
343-
module,
344-
cx.debug_context,
345-
cx.unwind_context,
346-
global_asm_object_file,
347-
)
348-
});
350+
let codegen_result = cx
351+
.profiler
352+
.verbose_generic_activity_with_arg("write object file", &*cgu_name)
353+
.run(|| {
354+
emit_cgu(
355+
&global_asm_config.output_filenames,
356+
&cx.profiler,
357+
cgu_name,
358+
module,
359+
cx.debug_context,
360+
cx.unwind_context,
361+
global_asm_object_file,
362+
)
363+
});
349364
std::mem::drop(token);
350365
codegen_result
351366
}))
@@ -375,7 +390,7 @@ pub(crate) fn run_aot(
375390

376391
let mut concurrency_limiter = ConcurrencyLimiter::new(tcx.sess, cgus.len());
377392

378-
let modules = super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
393+
let modules = tcx.sess.time("codegen mono items", || {
379394
cgus.iter()
380395
.map(|cgu| {
381396
let cgu_reuse = if backend_config.disable_incr_cache {
@@ -437,7 +452,6 @@ pub(crate) fn run_aot(
437452
};
438453

439454
let metadata_module = if need_metadata_module {
440-
let _timer = tcx.prof.generic_activity("codegen crate metadata");
441455
let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || {
442456
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
443457

0 commit comments

Comments
 (0)