about summary refs log tree commit diff
path: root/src/librustc_codegen_llvm/llvm_util.rs
diff options
context:
space:
mode:
authorWesley Wiser <wwiser@gmail.com>2020-01-31 18:58:28 -0500
committerWesley Wiser <wwiser@gmail.com>2020-02-01 14:19:17 -0500
commitf5f86be1d40f30b3183ec148f443afa73d0cbe15 (patch)
treeb502ea8bf896068391dadc47ac11b9f6858fc219 /src/librustc_codegen_llvm/llvm_util.rs
parentcd1ef390e731ed77b90b11b1f77e2c5ca641b261 (diff)
downloadrust-f5f86be1d40f30b3183ec148f443afa73d0cbe15.tar.gz
rust-f5f86be1d40f30b3183ec148f443afa73d0cbe15.zip
Add support for enabling the LLVM time-trace feature
I found this helpful while investigating an LLVM performance issue.
Passing `-Z llvm-time-trace` causes a `llvm_timings.json` file to be
created. This file can be inspected in either the Chrome Profiler tools
or with any other compatible tool like SpeedScope.

More information on the LLVM feature:

- https://aras-p.info/blog/2019/01/16/time-trace-timeline-flame-chart-profiler-for-Clang/

- https://reviews.llvm.org/rL357340
Diffstat (limited to 'src/librustc_codegen_llvm/llvm_util.rs')
-rw-r--r--src/librustc_codegen_llvm/llvm_util.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/librustc_codegen_llvm/llvm_util.rs b/src/librustc_codegen_llvm/llvm_util.rs
index 4823fe10c46..6d3498f8b80 100644
--- a/src/librustc_codegen_llvm/llvm_util.rs
+++ b/src/librustc_codegen_llvm/llvm_util.rs
@@ -113,6 +113,15 @@ unsafe fn configure_llvm(sess: &Session) {
         }
     }
 
+    if sess.opts.debugging_opts.llvm_time_trace && get_major_version() >= 9 {
+        // time-trace is not thread safe and running it in parallel will cause seg faults.
+        if !sess.opts.debugging_opts.no_parallel_llvm {
+            bug!("`-Z llvm-time-trace` requires `-Z no-parallel-llvm")
+        }
+
+        llvm::LLVMTimeTraceProfilerInitialize();
+    }
+
     llvm::LLVMInitializePasses();
 
     ::rustc_llvm::initialize_available_targets();
@@ -120,6 +129,15 @@ unsafe fn configure_llvm(sess: &Session) {
     llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
 }
 
+pub fn time_trace_profiler_finish(file_name: &str) {
+    unsafe {
+        if get_major_version() >= 9 {
+            let file_name = CString::new(file_name).unwrap();
+            llvm::LLVMTimeTraceProfilerFinish(file_name.as_ptr());
+        }
+    }
+}
+
 // WARNING: the features after applying `to_llvm_feature` must be known
 // to LLVM or the feature detection code will walk past the end of the feature
 // array, leading to crashes.