diff --git a/src/librustc_ast_lowering/lib.rs b/src/librustc_ast_lowering/lib.rs index 77cb9ee35fff7..58b8e8a089ad3 100644 --- a/src/librustc_ast_lowering/lib.rs +++ b/src/librustc_ast_lowering/lib.rs @@ -268,7 +268,7 @@ pub fn lower_crate<'a, 'hir>( // incr. comp. yet. dep_graph.assert_ignored(); - let _prof_timer = sess.prof.generic_activity("hir_lowering"); + let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering"); LoweringContext { crate_root: sess.parse_sess.injected_crate_name.try_get().copied(), diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index 17582862c4d6a..53ee5996432ce 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -53,6 +53,7 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>( crate_name: &str, target_cpu: &str, ) { + let _timer = sess.timer("link_binary"); let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata); for &crate_type in sess.crate_types.borrow().iter() { // Ignore executable crates if we have -Z no-codegen, as they will error. @@ -71,9 +72,11 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>( ); } - for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) { - check_file_is_writeable(obj, sess); - } + sess.time("link_binary_check_files_are_writeable", || { + for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) { + check_file_is_writeable(obj, sess); + } + }); let tmpdir = TempFileBuilder::new() .prefix("rustc") @@ -84,6 +87,7 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>( let out_filename = out_filename(sess, crate_type, outputs, crate_name); match crate_type { config::CrateType::Rlib => { + let _timer = sess.timer("link_rlib"); link_rlib::( sess, codegen_results, @@ -118,29 +122,34 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>( } // Remove the temporary object file and metadata if we aren't saving temps - if !sess.opts.cg.save_temps { - if sess.opts.output_types.should_codegen() && !preserve_objects_for_their_debuginfo(sess) { - for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) { - remove(sess, obj); + sess.time("link_binary_remove_temps", || { + if !sess.opts.cg.save_temps { + if sess.opts.output_types.should_codegen() + && !preserve_objects_for_their_debuginfo(sess) + { + for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) { + remove(sess, obj); + } } - } - for obj in codegen_results.modules.iter().filter_map(|m| m.bytecode_compressed.as_ref()) { - remove(sess, obj); - } - if let Some(ref metadata_module) = codegen_results.metadata_module { - if let Some(ref obj) = metadata_module.object { + for obj in codegen_results.modules.iter().filter_map(|m| m.bytecode_compressed.as_ref()) + { remove(sess, obj); } - } - if let Some(ref allocator_module) = codegen_results.allocator_module { - if let Some(ref obj) = allocator_module.object { - remove(sess, obj); + if let Some(ref metadata_module) = codegen_results.metadata_module { + if let Some(ref obj) = metadata_module.object { + remove(sess, obj); + } } - if let Some(ref bc) = allocator_module.bytecode_compressed { - remove(sess, bc); + if let Some(ref allocator_module) = codegen_results.allocator_module { + if let Some(ref obj) = allocator_module.object { + remove(sess, obj); + } + if let Some(ref bc) = allocator_module.bytecode_compressed { + remove(sess, bc); + } } } - } + }); } // The third parameter is for env vars, used on windows to set up the diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs index 1ce0a29d55d9d..4d09e23ee7f7a 100644 --- a/src/librustc_codegen_ssa/back/write.rs +++ b/src/librustc_codegen_ssa/back/write.rs @@ -479,6 +479,8 @@ fn copy_all_cgu_workproducts_to_incr_comp_cache_dir( return work_products; } + let _timer = sess.timer("incr_comp_copy_cgu_workproducts"); + for module in compiled_modules.modules.iter().filter(|m| m.kind == ModuleKind::Regular) { let mut files = vec![]; @@ -1714,8 +1716,11 @@ pub struct OngoingCodegen { impl OngoingCodegen { pub fn join(self, sess: &Session) -> (CodegenResults, FxHashMap) { + let _timer = sess.timer("finish_ongoing_codegen"); + self.shared_emitter_main.check(sess, true); - let compiled_modules = match self.future.join() { + let future = self.future; + let compiled_modules = sess.time("join_worker_thread", || match future.join() { Ok(Ok(compiled_modules)) => compiled_modules, Ok(Err(())) => { sess.abort_if_errors(); @@ -1724,7 +1729,7 @@ impl OngoingCodegen { Err(_) => { bug!("panic during codegen/LLVM phase"); } - }; + }); sess.cgu_reuse_tracker.check_expected_reuse(sess.diagnostic()); diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index e8a70d58f0cff..8deb43d50f938 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -495,6 +495,12 @@ impl<'a> TimingGuard<'a> { pub fn none() -> TimingGuard<'a> { TimingGuard(None) } + + #[inline(always)] + pub fn run(self, f: impl FnOnce() -> R) -> R { + let _timer = self; + f() + } } #[must_use] diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index a1318d3506711..3d31f240a34e0 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -389,6 +389,7 @@ pub fn run_compiler( })?; } else { // Drop AST after creating GlobalCtxt to free memory + let _timer = sess.prof.generic_activity("drop_ast"); mem::drop(queries.expansion()?.take()); } @@ -413,6 +414,7 @@ pub fn run_compiler( })?; if let Some(linker) = linker { + let _timer = sess.timer("link"); linker.link()? } diff --git a/src/librustc_incremental/persist/fs.rs b/src/librustc_incremental/persist/fs.rs index adf8f57f01d08..ba20006d73ccc 100644 --- a/src/librustc_incremental/persist/fs.rs +++ b/src/librustc_incremental/persist/fs.rs @@ -190,6 +190,8 @@ pub fn prepare_session_directory( return; } + let _timer = sess.timer("incr_comp_prepare_session_directory"); + debug!("prepare_session_directory"); // {incr-comp-dir}/{crate-name-and-disambiguator} @@ -306,6 +308,8 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) { return; } + let _timer = sess.timer("incr_comp_finalize_session_directory"); + let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone(); if sess.has_errors_or_delayed_span_bugs() { diff --git a/src/librustc_incremental/persist/load.rs b/src/librustc_incremental/persist/load.rs index 0732ddd3261a4..6c57f79e1a7fb 100644 --- a/src/librustc_incremental/persist/load.rs +++ b/src/librustc_incremental/persist/load.rs @@ -102,6 +102,8 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture { return MaybeAsync::Sync(LoadResult::Ok { data: Default::default() }); } + let _timer = sess.prof.generic_activity("incr_comp_prepare_load_dep_graph"); + // Calling `sess.incr_comp_session_dir()` will panic if `sess.opts.incremental.is_none()`. // Fortunately, we just checked that this isn't the case. let path = dep_graph_path_from(&sess.incr_comp_session_dir()); diff --git a/src/librustc_interface/interface.rs b/src/librustc_interface/interface.rs index c4449945dd19d..d00875f6fee88 100644 --- a/src/librustc_interface/interface.rs +++ b/src/librustc_interface/interface.rs @@ -177,11 +177,17 @@ pub fn run_compiler_in_existing_thread_pool( override_queries: config.override_queries, }; - let _sess_abort_error = OnDrop(|| { - compiler.sess.diagnostic().print_error_count(registry); - }); + let r = { + let _sess_abort_error = OnDrop(|| { + compiler.sess.diagnostic().print_error_count(registry); + }); - f(&compiler) + f(&compiler) + }; + + let prof = compiler.sess.prof.clone(); + prof.generic_activity("drop_compiler").run(move || drop(compiler)); + r } pub fn run_compiler(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R { diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 6322cc4b9a321..9119466cbc048 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -610,6 +610,8 @@ pub fn prepare_outputs( boxed_resolver: &Steal>>, crate_name: &str, ) -> Result { + let _timer = sess.timer("prepare_outputs"); + // FIXME: rustdoc passes &[] instead of &krate.attrs here let outputs = util::build_output_filenames( &compiler.input, @@ -733,20 +735,22 @@ pub fn create_global_ctxt<'tcx>( callback(sess, &mut local_providers, &mut extern_providers); } - let gcx = global_ctxt.init_locking(|| { - TyCtxt::create_global_ctxt( - sess, - lint_store, - local_providers, - extern_providers, - &all_arenas, - arena, - resolver_outputs, - hir_map, - query_result_on_disk_cache, - &crate_name, - &outputs, - ) + let gcx = sess.time("setup_global_ctxt", || { + global_ctxt.init_locking(|| { + TyCtxt::create_global_ctxt( + sess, + lint_store, + local_providers, + extern_providers, + &all_arenas, + arena, + resolver_outputs, + hir_map, + query_result_on_disk_cache, + &crate_name, + &outputs, + ) + }) }); // Do some initialization of the DepGraph that can only be done with the tcx available. diff --git a/src/librustc_interface/queries.rs b/src/librustc_interface/queries.rs index 6033569d765b4..7de1c36ce4b2e 100644 --- a/src/librustc_interface/queries.rs +++ b/src/librustc_interface/queries.rs @@ -176,6 +176,7 @@ impl<'tcx> Queries<'tcx> { self.expansion.compute(|| { let crate_name = self.crate_name()?.peek().clone(); let (krate, lint_store) = self.register_plugins()?.take(); + let _timer = self.session().timer("configure_and_expand"); passes::configure_and_expand( self.session().clone(), lint_store.clone(), @@ -256,6 +257,7 @@ impl<'tcx> Queries<'tcx> { let lint_store = self.expansion()?.peek().2.clone(); let hir = self.lower_to_hir()?.peek(); let (ref hir_forest, ref resolver_outputs) = &*hir; + let _timer = self.session().timer("create_global_ctxt"); Ok(passes::create_global_ctxt( self.compiler, lint_store, @@ -312,14 +314,19 @@ pub struct Linker { impl Linker { pub fn link(self) -> Result<()> { - self.codegen_backend + let r = self + .codegen_backend .join_codegen_and_link( self.ongoing_codegen, &self.sess, &self.dep_graph, &self.prepare_outputs, ) - .map_err(|_| ErrorReported) + .map_err(|_| ErrorReported); + let prof = self.sess.prof.clone(); + let dep_graph = self.dep_graph; + prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph)); + r } } @@ -328,6 +335,7 @@ impl Compiler { where F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T, { + let mut _timer = None; let queries = Queries::new(&self); let ret = f(&queries); @@ -337,6 +345,8 @@ impl Compiler { } } + _timer = Some(self.session().timer("free_global_ctxt")); + ret }