Rollup merge of #115736 - Zoxc:time-cleanup, r=wesleywiser

Remove `verbose_generic_activity_with_arg`

This removes `verbose_generic_activity_with_arg` and changes users to `generic_activity_with_arg`. This keeps the output of `-Z time` readable while these repeated events are still available with the self profiling mechanism.
This commit is contained in:
Matthias Krüger 2023-09-13 18:37:41 +02:00 committed by GitHub
commit 5dc37c1966
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 29 deletions

View file

@ -269,7 +269,7 @@ fn module_codegen(
), ),
) -> OngoingModuleCodegen { ) -> OngoingModuleCodegen {
let (cgu_name, mut cx, mut module, codegened_functions) = let (cgu_name, mut cx, mut module, codegened_functions) =
tcx.prof.verbose_generic_activity_with_arg("codegen cgu", cgu_name.as_str()).run(|| { tcx.prof.generic_activity_with_arg("codegen cgu", cgu_name.as_str()).run(|| {
let cgu = tcx.codegen_unit(cgu_name); let cgu = tcx.codegen_unit(cgu_name);
let mono_items = cgu.items_in_deterministic_order(tcx); let mono_items = cgu.items_in_deterministic_order(tcx);
@ -322,35 +322,24 @@ fn module_codegen(
}); });
OngoingModuleCodegen::Async(std::thread::spawn(move || { OngoingModuleCodegen::Async(std::thread::spawn(move || {
cx.profiler.clone().verbose_generic_activity_with_arg("compile functions", &*cgu_name).run( cx.profiler.clone().generic_activity_with_arg("compile functions", &*cgu_name).run(|| {
|| {
cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler( cranelift_codegen::timing::set_thread_profiler(Box::new(super::MeasuremeProfiler(
cx.profiler.clone(), cx.profiler.clone(),
))); )));
let mut cached_context = Context::new(); let mut cached_context = Context::new();
for codegened_func in codegened_functions { for codegened_func in codegened_functions {
crate::base::compile_fn( crate::base::compile_fn(&mut cx, &mut cached_context, &mut module, codegened_func);
&mut cx,
&mut cached_context,
&mut module,
codegened_func,
);
} }
}, });
);
let global_asm_object_file = cx let global_asm_object_file =
.profiler cx.profiler.generic_activity_with_arg("compile assembly", &*cgu_name).run(|| {
.verbose_generic_activity_with_arg("compile assembly", &*cgu_name)
.run(|| {
crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm) crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm)
})?; })?;
let codegen_result = cx let codegen_result =
.profiler cx.profiler.generic_activity_with_arg("write object file", &*cgu_name).run(|| {
.verbose_generic_activity_with_arg("write object file", &*cgu_name)
.run(|| {
emit_cgu( emit_cgu(
&global_asm_config.output_filenames, &global_asm_config.output_filenames,
&cx.profiler, &cx.profiler,

View file

@ -605,7 +605,7 @@ pub(crate) fn run_pass_manager(
module: &mut ModuleCodegen<ModuleLlvm>, module: &mut ModuleCodegen<ModuleLlvm>,
thin: bool, thin: bool,
) -> Result<(), FatalError> { ) -> Result<(), FatalError> {
let _timer = cgcx.prof.verbose_generic_activity_with_arg("LLVM_lto_optimize", &*module.name); let _timer = cgcx.prof.generic_activity_with_arg("LLVM_lto_optimize", &*module.name);
let config = cgcx.config(module.kind); let config = cgcx.config(module.kind);
// Now we have one massive module inside of llmod. Time to run the // Now we have one massive module inside of llmod. Time to run the

View file

@ -26,6 +26,7 @@ use rustc_target::abi::{FieldIdx, Size, VariantIdx};
use polonius_engine::Atom; use polonius_engine::Atom;
pub use rustc_ast::Mutability; pub use rustc_ast::Mutability;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::graph::dominators::Dominators; use rustc_data_structures::graph::dominators::Dominators;
use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_index::{Idx, IndexSlice, IndexVec};
@ -36,6 +37,8 @@ use rustc_span::{Span, DUMMY_SP};
use either::Either; use either::Either;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::fmt::{self, Debug, Display, Formatter, Write}; use std::fmt::{self, Debug, Display, Formatter, Write};
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::{iter, mem}; use std::{iter, mem};
@ -98,6 +101,36 @@ impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
} }
} }
thread_local! {
static PASS_NAMES: RefCell<FxHashMap<&'static str, &'static str>> = {
RefCell::new(FxHashMap::default())
};
}
/// Converts a MIR pass name into a snake case form to match the profiling naming style.
fn to_profiler_name(type_name: &'static str) -> &'static str {
PASS_NAMES.with(|names| match names.borrow_mut().entry(type_name) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
let snake_case: String = type_name
.chars()
.flat_map(|c| {
if c.is_ascii_uppercase() {
vec!['_', c.to_ascii_lowercase()]
} else if c == '-' {
vec!['_']
} else {
vec![c]
}
})
.collect();
let result = &*String::leak(format!("mir_pass{}", snake_case));
e.insert(result);
result
}
})
}
/// A streamlined trait that you can implement to create a pass; the /// A streamlined trait that you can implement to create a pass; the
/// pass will be named after the type, and it will consist of a main /// pass will be named after the type, and it will consist of a main
/// loop that goes over each available MIR and applies `run_pass`. /// loop that goes over each available MIR and applies `run_pass`.
@ -107,6 +140,10 @@ pub trait MirPass<'tcx> {
if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name } if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }
} }
fn profiler_name(&self) -> &'static str {
to_profiler_name(self.name())
}
/// Returns `true` if this pass is enabled with the current combination of compiler flags. /// Returns `true` if this pass is enabled with the current combination of compiler flags.
fn is_enabled(&self, _sess: &Session) -> bool { fn is_enabled(&self, _sess: &Session) -> bool {
true true

View file

@ -94,6 +94,8 @@ fn run_passes_inner<'tcx>(
let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes; let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
trace!(?overridden_passes); trace!(?overridden_passes);
let prof_arg = tcx.sess.prof.enabled().then(|| format!("{:?}", body.source.def_id()));
if !body.should_skip() { if !body.should_skip() {
for pass in passes { for pass in passes {
let name = pass.name(); let name = pass.name();
@ -121,7 +123,14 @@ fn run_passes_inner<'tcx>(
validate_body(tcx, body, format!("before pass {name}")); validate_body(tcx, body, format!("before pass {name}"));
} }
tcx.sess.time(name, || pass.run_pass(tcx, body)); if let Some(prof_arg) = &prof_arg {
tcx.sess
.prof
.generic_activity_with_arg(pass.profiler_name(), &**prof_arg)
.run(|| pass.run_pass(tcx, body));
} else {
pass.run_pass(tcx, body);
}
if dump_enabled { if dump_enabled {
dump_mir_for_pass(tcx, body, &name, true); dump_mir_for_pass(tcx, body, &name, true);

View file

@ -348,8 +348,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
Q: super::QueryConfigRestored<'tcx>, Q: super::QueryConfigRestored<'tcx>,
Q::RestoredValue: Encodable<CacheEncoder<'a, 'tcx>>, Q::RestoredValue: Encodable<CacheEncoder<'a, 'tcx>>,
{ {
let _timer = let _timer = qcx.profiler().generic_activity_with_arg("encode_query_results_for", query.name());
qcx.profiler().verbose_generic_activity_with_arg("encode_query_results_for", query.name());
assert!(query.query_state(qcx).all_inactive()); assert!(query.query_state(qcx).all_inactive());
let cache = query.query_cache(qcx); let cache = query.query_cache(qcx);