Skip to content

Commit 223c7e5

Browse files
self-profile: Support arguments for generic_activities.
1 parent b5e21db commit 223c7e5

File tree

8 files changed

+165
-114
lines changed

8 files changed

+165
-114
lines changed

src/librustc/ty/query/on_disk_cache.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,10 @@ where
10531053
Q: super::config::QueryDescription<'tcx, Value: Encodable>,
10541054
E: 'a + TyEncoder,
10551055
{
1056-
let desc = &format!("encode_query_results_for_{}", ::std::any::type_name::<Q>());
1057-
let _timer = tcx.sess.prof.extra_verbose_generic_activity(desc);
1056+
let _timer = tcx
1057+
.sess
1058+
.prof
1059+
.extra_verbose_generic_activity("encode_query_results_for", ::std::any::type_name::<Q>());
10581060

10591061
let shards = Q::query_cache(tcx).lock_shards();
10601062
assert!(shards.iter().all(|shard| shard.active.is_empty()));

src/librustc_codegen_llvm/back/lto.rs

+32-25
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,21 @@ fn prepare_lto(
110110
symbol_white_list.extend(exported_symbols[&cnum].iter().filter_map(symbol_filter));
111111
}
112112

113-
let _timer = cgcx.prof.generic_activity("LLVM_lto_load_upstream_bitcode");
114113
let archive = ArchiveRO::open(&path).expect("wanted an rlib");
115114
let bytecodes = archive
116115
.iter()
117116
.filter_map(|child| child.ok().and_then(|c| c.name().map(|name| (name, c))))
118117
.filter(|&(name, _)| name.ends_with(RLIB_BYTECODE_EXTENSION));
119118
for (name, data) in bytecodes {
119+
let _timer =
120+
cgcx.prof.generic_activity_with_arg("LLVM_lto_load_upstream_bitcode", name);
120121
info!("adding bytecode {}", name);
121122
let bc_encoded = data.data();
122123

123-
let (bc, id) = cgcx
124-
.prof
125-
.extra_verbose_generic_activity(&format!("decode {}", name))
126-
.run(|| match DecodedBytecode::new(bc_encoded) {
127-
Ok(b) => Ok((b.bytecode(), b.identifier().to_string())),
128-
Err(e) => Err(diag_handler.fatal(&e)),
129-
})?;
124+
let (bc, id) = match DecodedBytecode::new(bc_encoded) {
125+
Ok(b) => Ok((b.bytecode(), b.identifier().to_string())),
126+
Err(e) => Err(diag_handler.fatal(&e)),
127+
}?;
130128
let bc = SerializedModule::FromRlib(bc);
131129
upstream_modules.push((bc, CString::new(id).unwrap()));
132130
}
@@ -281,14 +279,14 @@ fn fat_lto(
281279
// save and persist everything with the original module.
282280
let mut linker = Linker::new(llmod);
283281
for (bc_decoded, name) in serialized_modules {
284-
let _timer = cgcx.prof.generic_activity("LLVM_fat_lto_link_module");
282+
let _timer = cgcx
283+
.prof
284+
.generic_activity_with_arg("LLVM_fat_lto_link_module", format!("{:?}", name));
285285
info!("linking {:?}", name);
286-
cgcx.prof.extra_verbose_generic_activity(&format!("ll link {:?}", name)).run(|| {
287-
let data = bc_decoded.data();
288-
linker.add(&data).map_err(|()| {
289-
let msg = format!("failed to load bc of {:?}", name);
290-
write::llvm_err(&diag_handler, &msg)
291-
})
286+
let data = bc_decoded.data();
287+
linker.add(&data).map_err(|()| {
288+
let msg = format!("failed to load bc of {:?}", name);
289+
write::llvm_err(&diag_handler, &msg)
292290
})?;
293291
serialized_bitcode.push(bc_decoded);
294292
}
@@ -577,6 +575,8 @@ pub(crate) fn run_pass_manager(
577575
config: &ModuleConfig,
578576
thin: bool,
579577
) {
578+
let _timer = cgcx.prof.extra_verbose_generic_activity("LLVM_lto_optimize", &module.name[..]);
579+
580580
// Now we have one massive module inside of llmod. Time to run the
581581
// LTO-specific optimization passes that LLVM provides.
582582
//
@@ -634,9 +634,7 @@ pub(crate) fn run_pass_manager(
634634
llvm::LLVMRustAddPass(pm, pass.unwrap());
635635
}
636636

637-
cgcx.prof
638-
.extra_verbose_generic_activity("LTO_passes")
639-
.run(|| llvm::LLVMRunPassManager(pm, module.module_llvm.llmod()));
637+
llvm::LLVMRunPassManager(pm, module.module_llvm.llmod());
640638

641639
llvm::LLVMDisposePassManager(pm);
642640
}
@@ -760,7 +758,9 @@ pub unsafe fn optimize_thin_module(
760758
// Like with "fat" LTO, get some better optimizations if landing pads
761759
// are disabled by removing all landing pads.
762760
if cgcx.no_landing_pads {
763-
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_remove_landing_pads");
761+
let _timer = cgcx
762+
.prof
763+
.generic_activity_with_arg("LLVM_thin_lto_remove_landing_pads", thin_module.name());
764764
llvm::LLVMRustMarkAllFunctionsNounwind(llmod);
765765
save_temp_bitcode(&cgcx, &module, "thin-lto-after-nounwind");
766766
}
@@ -774,7 +774,8 @@ pub unsafe fn optimize_thin_module(
774774
// You can find some more comments about these functions in the LLVM
775775
// bindings we've got (currently `PassWrapper.cpp`)
776776
{
777-
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_rename");
777+
let _timer =
778+
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_rename", thin_module.name());
778779
if !llvm::LLVMRustPrepareThinLTORename(thin_module.shared.data.0, llmod) {
779780
let msg = "failed to prepare thin LTO module";
780781
return Err(write::llvm_err(&diag_handler, msg));
@@ -783,7 +784,9 @@ pub unsafe fn optimize_thin_module(
783784
}
784785

785786
{
786-
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_resolve_weak");
787+
let _timer = cgcx
788+
.prof
789+
.generic_activity_with_arg("LLVM_thin_lto_resolve_weak", thin_module.name());
787790
if !llvm::LLVMRustPrepareThinLTOResolveWeak(thin_module.shared.data.0, llmod) {
788791
let msg = "failed to prepare thin LTO module";
789792
return Err(write::llvm_err(&diag_handler, msg));
@@ -792,7 +795,9 @@ pub unsafe fn optimize_thin_module(
792795
}
793796

794797
{
795-
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_internalize");
798+
let _timer = cgcx
799+
.prof
800+
.generic_activity_with_arg("LLVM_thin_lto_internalize", thin_module.name());
796801
if !llvm::LLVMRustPrepareThinLTOInternalize(thin_module.shared.data.0, llmod) {
797802
let msg = "failed to prepare thin LTO module";
798803
return Err(write::llvm_err(&diag_handler, msg));
@@ -801,7 +806,8 @@ pub unsafe fn optimize_thin_module(
801806
}
802807

803808
{
804-
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_import");
809+
let _timer =
810+
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_import", thin_module.name());
805811
if !llvm::LLVMRustPrepareThinLTOImport(thin_module.shared.data.0, llmod) {
806812
let msg = "failed to prepare thin LTO module";
807813
return Err(write::llvm_err(&diag_handler, msg));
@@ -839,7 +845,9 @@ pub unsafe fn optimize_thin_module(
839845
// so it appears). Hopefully we can remove this once upstream bugs are
840846
// fixed in LLVM.
841847
{
842-
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_patch_debuginfo");
848+
let _timer = cgcx
849+
.prof
850+
.generic_activity_with_arg("LLVM_thin_lto_patch_debuginfo", thin_module.name());
843851
llvm::LLVMRustThinLTOPatchDICompileUnit(llmod, cu1);
844852
save_temp_bitcode(cgcx, &module, "thin-lto-after-patch");
845853
}
@@ -850,7 +858,6 @@ pub unsafe fn optimize_thin_module(
850858
// populate a thin-specific pass manager, which presumably LLVM treats a
851859
// little differently.
852860
{
853-
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_optimize");
854861
info!("running thin lto passes over {}", module.name);
855862
let config = cgcx.config(module.kind);
856863
run_pass_manager(cgcx, &module, config, true);

src/librustc_codegen_llvm/back/write.rs

+37-32
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ pub(crate) unsafe fn optimize(
310310
module: &ModuleCodegen<ModuleLlvm>,
311311
config: &ModuleConfig,
312312
) -> Result<(), FatalError> {
313-
let _timer = cgcx.prof.generic_activity("LLVM_module_optimize");
313+
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &module.name[..]);
314314

315315
let llmod = module.module_llvm.llmod();
316316
let llcx = &*module.module_llvm.llcx;
@@ -424,23 +424,17 @@ pub(crate) unsafe fn optimize(
424424

425425
// Finally, run the actual optimization passes
426426
{
427-
let _timer = cgcx.prof.generic_activity("LLVM_module_optimize_function_passes");
428-
let desc = &format!("llvm function passes [{}]", module_name.unwrap());
429-
let _timer = if config.time_module {
430-
Some(cgcx.prof.extra_verbose_generic_activity(desc))
431-
} else {
432-
None
433-
};
427+
let _timer = cgcx.prof.extra_verbose_generic_activity(
428+
"LLVM_module_optimize_function_passes",
429+
&module.name[..],
430+
);
434431
llvm::LLVMRustRunFunctionPassManager(fpm, llmod);
435432
}
436433
{
437-
let _timer = cgcx.prof.generic_activity("LLVM_module_optimize_module_passes");
438-
let desc = &format!("llvm module passes [{}]", module_name.unwrap());
439-
let _timer = if config.time_module {
440-
Some(cgcx.prof.extra_verbose_generic_activity(desc))
441-
} else {
442-
None
443-
};
434+
let _timer = cgcx.prof.extra_verbose_generic_activity(
435+
"LLVM_module_optimize_module_passes",
436+
&module.name[..],
437+
);
444438
llvm::LLVMRunPassManager(mpm, llmod);
445439
}
446440

@@ -480,7 +474,7 @@ pub(crate) unsafe fn codegen(
480474
module: ModuleCodegen<ModuleLlvm>,
481475
config: &ModuleConfig,
482476
) -> Result<CompiledModule, FatalError> {
483-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen");
477+
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &module.name[..]);
484478
{
485479
let llmod = module.module_llvm.llmod();
486480
let llcx = &*module.module_llvm.llcx;
@@ -533,26 +527,36 @@ pub(crate) unsafe fn codegen(
533527
let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
534528

535529
if write_bc || config.emit_bc_compressed || config.embed_bitcode {
536-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_make_bitcode");
530+
let _timer = cgcx
531+
.prof
532+
.generic_activity_with_arg("LLVM_module_codegen_make_bitcode", &module.name[..]);
537533
let thin = ThinBuffer::new(llmod);
538534
let data = thin.data();
539535

540536
if write_bc {
541-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_bitcode");
537+
let _timer = cgcx.prof.generic_activity_with_arg(
538+
"LLVM_module_codegen_emit_bitcode",
539+
&module.name[..],
540+
);
542541
if let Err(e) = fs::write(&bc_out, data) {
543542
let msg = format!("failed to write bytecode to {}: {}", bc_out.display(), e);
544543
diag_handler.err(&msg);
545544
}
546545
}
547546

548547
if config.embed_bitcode {
549-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_embed_bitcode");
548+
let _timer = cgcx.prof.generic_activity_with_arg(
549+
"LLVM_module_codegen_embed_bitcode",
550+
&module.name[..],
551+
);
550552
embed_bitcode(cgcx, llcx, llmod, Some(data));
551553
}
552554

553555
if config.emit_bc_compressed {
554-
let _timer =
555-
cgcx.prof.generic_activity("LLVM_module_codegen_emit_compressed_bitcode");
556+
let _timer = cgcx.prof.generic_activity_with_arg(
557+
"LLVM_module_codegen_emit_compressed_bitcode",
558+
&module.name[..],
559+
);
556560
let dst = bc_out.with_extension(RLIB_BYTECODE_EXTENSION);
557561
let data = bytecode::encode(&module.name, data);
558562
if let Err(e) = fs::write(&dst, data) {
@@ -565,15 +569,10 @@ pub(crate) unsafe fn codegen(
565569
}
566570

567571
{
568-
let desc = &format!("codegen passes [{}]", module_name.unwrap());
569-
let _timer = if config.time_module {
570-
Some(cgcx.prof.extra_verbose_generic_activity(desc))
571-
} else {
572-
None
573-
};
574-
575572
if config.emit_ir {
576-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_ir");
573+
let _timer = cgcx
574+
.prof
575+
.generic_activity_with_arg("LLVM_module_codegen_emit_ir", &module.name[..]);
577576
let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
578577
let out_c = path_to_c_string(&out);
579578

@@ -618,7 +617,9 @@ pub(crate) unsafe fn codegen(
618617
}
619618

620619
if config.emit_asm || asm_to_obj {
621-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_asm");
620+
let _timer = cgcx
621+
.prof
622+
.generic_activity_with_arg("LLVM_module_codegen_emit_asm", &module.name[..]);
622623
let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
623624

624625
// We can't use the same module for asm and binary output, because that triggers
@@ -638,7 +639,9 @@ pub(crate) unsafe fn codegen(
638639
}
639640

640641
if write_obj {
641-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_obj");
642+
let _timer = cgcx
643+
.prof
644+
.generic_activity_with_arg("LLVM_module_codegen_emit_obj", &module.name[..]);
642645
with_codegen(tm, llmod, config.no_builtins, |cpm| {
643646
write_output_file(
644647
diag_handler,
@@ -650,7 +653,9 @@ pub(crate) unsafe fn codegen(
650653
)
651654
})?;
652655
} else if asm_to_obj {
653-
let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_asm_to_obj");
656+
let _timer = cgcx
657+
.prof
658+
.generic_activity_with_arg("LLVM_module_codegen_asm_to_obj", &module.name[..]);
654659
let assembly = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
655660
run_assembler(cgcx, diag_handler, &assembly, &obj_out);
656661

src/librustc_codegen_ssa/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
16481648
let name = cratepath.file_name().unwrap().to_str().unwrap();
16491649
let name = &name[3..name.len() - 5]; // chop off lib/.rlib
16501650

1651-
sess.prof.extra_verbose_generic_activity(&format!("altering {}.rlib", name)).run(|| {
1651+
sess.prof.generic_activity_with_arg("link_altering_rlib", name).run(|| {
16521652
let mut archive = <B as ArchiveBuilder>::new(sess, &dst, Some(cratepath));
16531653
archive.update_symbols();
16541654

src/librustc_codegen_ssa/back/write.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc::session::Session;
2121
use rustc::ty::TyCtxt;
2222
use rustc_data_structures::fx::FxHashMap;
2323
use rustc_data_structures::profiling::SelfProfilerRef;
24+
use rustc_data_structures::profiling::TimingGuard;
2425
use rustc_data_structures::profiling::VerboseTimingGuard;
2526
use rustc_data_structures::svh::Svh;
2627
use rustc_data_structures::sync::Lrc;
@@ -691,11 +692,17 @@ impl<B: WriteBackendMethods> WorkItem<B> {
691692
}
692693
}
693694

694-
fn profiling_event_id(&self) -> &'static str {
695+
fn start_profiling<'a>(&self, cgcx: &'a CodegenContext<B>) -> TimingGuard<'a> {
695696
match *self {
696-
WorkItem::Optimize(_) => "codegen_module_optimize",
697-
WorkItem::CopyPostLtoArtifacts(_) => "codegen_copy_artifacts_from_incr_cache",
698-
WorkItem::LTO(_) => "codegen_module_perform_lto",
697+
WorkItem::Optimize(ref m) => {
698+
cgcx.prof.generic_activity_with_arg("codegen_module_optimize", &m.name[..])
699+
}
700+
WorkItem::CopyPostLtoArtifacts(ref m) => cgcx
701+
.prof
702+
.generic_activity_with_arg("codegen_copy_artifacts_from_incr_cache", &m.name[..]),
703+
WorkItem::LTO(ref m) => {
704+
cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", m.name())
705+
}
699706
}
700707
}
701708
}
@@ -1520,7 +1527,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
15201527
llvm_start_time: &mut Option<VerboseTimingGuard<'a>>,
15211528
) {
15221529
if config.time_module && llvm_start_time.is_none() {
1523-
*llvm_start_time = Some(prof.extra_verbose_generic_activity("LLVM_passes"));
1530+
*llvm_start_time = Some(prof.extra_verbose_generic_activity("LLVM_passes", "crate"));
15241531
}
15251532
}
15261533
}
@@ -1575,7 +1582,7 @@ fn spawn_work<B: ExtraBackendMethods>(cgcx: CodegenContext<B>, work: WorkItem<B>
15751582
// as a diagnostic was already sent off to the main thread - just
15761583
// surface that there was an error in this worker.
15771584
bomb.result = {
1578-
let _prof_timer = cgcx.prof.generic_activity(work.profiling_event_id());
1585+
let _prof_timer = work.start_profiling(&cgcx);
15791586
Some(execute_work_item(&cgcx, work))
15801587
};
15811588
});

0 commit comments

Comments
 (0)