about summary refs log tree commit diff
path: root/src/librustc_codegen_llvm
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2020-01-01 02:24:05 +0100
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2020-01-05 02:57:14 +0100
commit5a485ce4a3767bcac6bcc7bf66867c59c30eaeb1 (patch)
tree1ea3869a0eb039709e0bd528f6c9c63f1f54daf7 /src/librustc_codegen_llvm
parent7494250106003d698579edef215d0c01810b5156 (diff)
Use self profile infrastructure for -Z time and -Z time-passes
Diffstat (limited to 'src/librustc_codegen_llvm')
-rw-r--r--src/librustc_codegen_llvm/back/lto.rs12
-rw-r--r--src/librustc_codegen_llvm/back/write.rs195
-rw-r--r--src/librustc_codegen_llvm/lib.rs7
3 files changed, 98 insertions, 116 deletions
diff --git a/src/librustc_codegen_llvm/back/lto.rs b/src/librustc_codegen_llvm/back/lto.rs
index 629d303ffec..ca95a316349 100644
--- a/src/librustc_codegen_llvm/back/lto.rs
+++ b/src/librustc_codegen_llvm/back/lto.rs
@@ -11,7 +11,6 @@ use rustc::dep_graph::WorkProduct;
 use rustc::hir::def_id::LOCAL_CRATE;
 use rustc::middle::exported_symbols::SymbolExportLevel;
 use rustc::session::config::{self, Lto};
-use rustc::util::common::time_ext;
 use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
 use rustc_codegen_ssa::back::symbol_export;
 use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig};
@@ -121,7 +120,7 @@ fn prepare_lto(
                 info!("adding bytecode {}", name);
                 let bc_encoded = data.data();
 
-                let (bc, id) = time_ext(cgcx.time_passes, &format!("decode {}", name), || {
+                let (bc, id) = cgcx.prof.generic_pass(&format!("decode {}", name)).run(|| {
                     match DecodedBytecode::new(bc_encoded) {
                         Ok(b) => Ok((b.bytecode(), b.identifier().to_string())),
                         Err(e) => Err(diag_handler.fatal(&e)),
@@ -281,9 +280,8 @@ fn fat_lto(
         // save and persist everything with the original module.
         let mut linker = Linker::new(llmod);
         for (bc_decoded, name) in serialized_modules {
-            let _timer = cgcx.prof.generic_activity("LLVM_fat_lto_link_module");
             info!("linking {:?}", name);
-            time_ext(cgcx.time_passes, &format!("ll link {:?}", name), || {
+            cgcx.prof.generic_pass(&format!("ll link {:?}", name)).run(|| {
                 let data = bc_decoded.data();
                 linker.add(&data).map_err(|()| {
                     let msg = format!("failed to load bc of {:?}", name);
@@ -634,9 +632,9 @@ pub(crate) fn run_pass_manager(
             llvm::LLVMRustAddPass(pm, pass.unwrap());
         }
 
-        time_ext(cgcx.time_passes, "LTO passes", || {
-            llvm::LLVMRunPassManager(pm, module.module_llvm.llmod())
-        });
+        cgcx.prof
+            .generic_pass("LTO passes")
+            .run(|| llvm::LLVMRunPassManager(pm, module.module_llvm.llmod()));
 
         llvm::LLVMDisposePassManager(pm);
     }
diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs
index 818d09ce691..fa4d8a8d847 100644
--- a/src/librustc_codegen_llvm/back/write.rs
+++ b/src/librustc_codegen_llvm/back/write.rs
@@ -16,7 +16,6 @@ use rustc::hir::def_id::LOCAL_CRATE;
 use rustc::session::config::{self, Lto, OutputType, Passes, Sanitizer, SwitchWithOptPath};
 use rustc::session::Session;
 use rustc::ty::TyCtxt;
-use rustc::util::common::time_ext;
 use rustc_codegen_ssa::back::write::{run_assembler, CodegenContext, ModuleConfig};
 use rustc_codegen_ssa::traits::*;
 use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, RLIB_BYTECODE_EXTENSION};
@@ -425,20 +424,14 @@ pub(crate) unsafe fn optimize(
 
         // Finally, run the actual optimization passes
         {
-            let _timer = cgcx.prof.generic_activity("LLVM_module_optimize_function_passes");
-            time_ext(
-                config.time_passes,
-                &format!("llvm function passes [{}]", module_name.unwrap()),
-                || llvm::LLVMRustRunFunctionPassManager(fpm, llmod),
-            );
+            let desc = &format!("llvm function passes [{}]", module_name.unwrap());
+            let _timer = if config.time_module { Some(cgcx.prof.generic_pass(desc)) } else { None };
+            llvm::LLVMRustRunFunctionPassManager(fpm, llmod);
         }
         {
-            let _timer = cgcx.prof.generic_activity("LLVM_module_optimize_module_passes");
-            time_ext(
-                config.time_passes,
-                &format!("llvm module passes [{}]", module_name.unwrap()),
-                || llvm::LLVMRunPassManager(mpm, llmod),
-            );
+            let desc = &format!("llvm module passes [{}]", module_name.unwrap());
+            let _timer = if config.time_module { Some(cgcx.prof.generic_pass(desc)) } else { None };
+            llvm::LLVMRunPassManager(mpm, llmod);
         }
 
         // Deallocate managers that we're now done with
@@ -561,103 +554,97 @@ pub(crate) unsafe fn codegen(
             embed_bitcode(cgcx, llcx, llmod, None);
         }
 
-        time_ext(
-            config.time_passes,
-            &format!("codegen passes [{}]", module_name.unwrap()),
-            || -> Result<(), FatalError> {
-                if config.emit_ir {
-                    let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_ir");
-                    let out =
-                        cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
-                    let out_c = path_to_c_string(&out);
-
-                    extern "C" fn demangle_callback(
-                        input_ptr: *const c_char,
-                        input_len: size_t,
-                        output_ptr: *mut c_char,
-                        output_len: size_t,
-                    ) -> size_t {
-                        let input = unsafe {
-                            slice::from_raw_parts(input_ptr as *const u8, input_len as usize)
-                        };
-
-                        let input = match str::from_utf8(input) {
-                            Ok(s) => s,
-                            Err(_) => return 0,
-                        };
-
-                        let output = unsafe {
-                            slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize)
-                        };
-                        let mut cursor = io::Cursor::new(output);
-
-                        let demangled = match rustc_demangle::try_demangle(input) {
-                            Ok(d) => d,
-                            Err(_) => return 0,
-                        };
-
-                        if let Err(_) = write!(cursor, "{:#}", demangled) {
-                            // Possible only if provided buffer is not big enough
-                            return 0;
-                        }
-
-                        cursor.position() as size_t
+        {
+            let desc = &format!("codegen passes [{}]", module_name.unwrap());
+            let _timer = if config.time_module { Some(cgcx.prof.generic_pass(desc)) } else { None };
+
+            if config.emit_ir {
+                let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_ir");
+                let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
+                let out_c = path_to_c_string(&out);
+
+                extern "C" fn demangle_callback(
+                    input_ptr: *const c_char,
+                    input_len: size_t,
+                    output_ptr: *mut c_char,
+                    output_len: size_t,
+                ) -> size_t {
+                    let input = unsafe {
+                        slice::from_raw_parts(input_ptr as *const u8, input_len as usize)
+                    };
+
+                    let input = match str::from_utf8(input) {
+                        Ok(s) => s,
+                        Err(_) => return 0,
+                    };
+
+                    let output = unsafe {
+                        slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize)
+                    };
+                    let mut cursor = io::Cursor::new(output);
+
+                    let demangled = match rustc_demangle::try_demangle(input) {
+                        Ok(d) => d,
+                        Err(_) => return 0,
+                    };
+
+                    if let Err(_) = write!(cursor, "{:#}", demangled) {
+                        // Possible only if provided buffer is not big enough
+                        return 0;
                     }
 
-                    let result =
-                        llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback);
-                    result.into_result().map_err(|()| {
-                        let msg = format!("failed to write LLVM IR to {}", out.display());
-                        llvm_err(diag_handler, &msg)
-                    })?;
+                    cursor.position() as size_t
                 }
 
-                if config.emit_asm || asm_to_obj {
-                    let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_asm");
-                    let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
-
-                    // We can't use the same module for asm and binary output, because that triggers
-                    // various errors like invalid IR or broken binaries, so we might have to clone the
-                    // module to produce the asm output
-                    let llmod = if config.emit_obj { llvm::LLVMCloneModule(llmod) } else { llmod };
-                    with_codegen(tm, llmod, config.no_builtins, |cpm| {
-                        write_output_file(
-                            diag_handler,
-                            tm,
-                            cpm,
-                            llmod,
-                            &path,
-                            llvm::FileType::AssemblyFile,
-                        )
-                    })?;
-                }
+                let result = llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback);
+                result.into_result().map_err(|()| {
+                    let msg = format!("failed to write LLVM IR to {}", out.display());
+                    llvm_err(diag_handler, &msg)
+                })?;
+            }
 
-                if write_obj {
-                    let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_obj");
-                    with_codegen(tm, llmod, config.no_builtins, |cpm| {
-                        write_output_file(
-                            diag_handler,
-                            tm,
-                            cpm,
-                            llmod,
-                            &obj_out,
-                            llvm::FileType::ObjectFile,
-                        )
-                    })?;
-                } else if asm_to_obj {
-                    let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_asm_to_obj");
-                    let assembly =
-                        cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
-                    run_assembler(cgcx, diag_handler, &assembly, &obj_out);
-
-                    if !config.emit_asm && !cgcx.save_temps {
-                        drop(fs::remove_file(&assembly));
-                    }
-                }
+            if config.emit_asm || asm_to_obj {
+                let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_asm");
+                let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
+
+                // We can't use the same module for asm and binary output, because that triggers
+                // various errors like invalid IR or broken binaries, so we might have to clone the
+                // module to produce the asm output
+                let llmod = if config.emit_obj { llvm::LLVMCloneModule(llmod) } else { llmod };
+                with_codegen(tm, llmod, config.no_builtins, |cpm| {
+                    write_output_file(
+                        diag_handler,
+                        tm,
+                        cpm,
+                        llmod,
+                        &path,
+                        llvm::FileType::AssemblyFile,
+                    )
+                })?;
+            }
 
-                Ok(())
-            },
-        )?;
+            if write_obj {
+                let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_emit_obj");
+                with_codegen(tm, llmod, config.no_builtins, |cpm| {
+                    write_output_file(
+                        diag_handler,
+                        tm,
+                        cpm,
+                        llmod,
+                        &obj_out,
+                        llvm::FileType::ObjectFile,
+                    )
+                })?;
+            } else if asm_to_obj {
+                let _timer = cgcx.prof.generic_activity("LLVM_module_codegen_asm_to_obj");
+                let assembly = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
+                run_assembler(cgcx, diag_handler, &assembly, &obj_out);
+
+                if !config.emit_asm && !cgcx.save_temps {
+                    drop(fs::remove_file(&assembly));
+                }
+            }
+        }
 
         if copy_bc_to_obj {
             debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs
index 0b6a2861c42..349cff79c78 100644
--- a/src/librustc_codegen_llvm/lib.rs
+++ b/src/librustc_codegen_llvm/lib.rs
@@ -275,7 +275,6 @@ impl CodegenBackend for LlvmCodegenBackend {
         dep_graph: &DepGraph,
         outputs: &OutputFilenames,
     ) -> Result<(), ErrorReported> {
-        use rustc::util::common::time;
         let (codegen_results, work_products) = ongoing_codegen
             .downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
             .expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
@@ -284,7 +283,7 @@ impl CodegenBackend for LlvmCodegenBackend {
             rustc_codegen_ssa::back::write::dump_incremental_data(&codegen_results);
         }
 
-        time(sess, "serialize work products", move || {
+        sess.time("serialize work products", move || {
             rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
         });
 
@@ -301,9 +300,7 @@ impl CodegenBackend for LlvmCodegenBackend {
 
         // Run the linker on any artifacts that resulted from the LLVM run.
         // This should produce either a finished executable or library.
-        time(sess, "linking", || {
-            let _prof_timer = sess.prof.generic_activity("link_crate");
-
+        sess.time("linking", || {
             use crate::back::archive::LlvmArchiveBuilder;
             use rustc_codegen_ssa::back::link::link_binary;