Skip to content

Commit 2d47816

Browse files
pcwaltonkhei4
authored andcommitted
rustc_llvm: Add a -Z print-llvm-stats option to expose LLVM statistics.
LLVM has a neat [statistics] feature that tracks how often optimizations kick in. It's very handy for optimization work. Since we expose the LLVM pass timings, I thought it made sense to expose the LLVM statistics too. [statistics]: https://llvm.org/docs/ProgrammersManual.html#the-statistic-class-stats-option
1 parent 55be59d commit 2d47816

File tree

10 files changed

+35
-0
lines changed

10 files changed

+35
-0
lines changed

compiler/rustc_codegen_gcc/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ impl WriteBackendMethods for GccCodegenBackend {
239239
unimplemented!();
240240
}
241241

242+
fn print_statistics(&self) {
243+
unimplemented!()
244+
}
245+
242246
unsafe fn optimize(_cgcx: &CodegenContext<Self>, _diag_handler: &Handler, module: &ModuleCodegen<Self::Module>, config: &ModuleConfig) -> Result<(), FatalError> {
243247
module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));
244248
Ok(())

compiler/rustc_codegen_llvm/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ impl WriteBackendMethods for LlvmCodegenBackend {
181181
llvm::LLVMRustPrintPassTimings();
182182
}
183183
}
184+
fn print_statistics(&self) {
185+
unsafe {
186+
llvm::LLVMRustPrintStatistics();
187+
}
188+
}
184189
fn run_link(
185190
cgcx: &CodegenContext<Self>,
186191
diag_handler: &Handler,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,9 @@ extern "C" {
18701870
/// Print the pass timings since static dtors aren't picking them up.
18711871
pub fn LLVMRustPrintPassTimings();
18721872

1873+
/// Print the statistics since static dtors aren't picking them up.
1874+
pub fn LLVMRustPrintStatistics();
1875+
18731876
pub fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type;
18741877

18751878
pub fn LLVMStructSetBody<'a>(

compiler/rustc_codegen_llvm/src/llvm_util.rs

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ unsafe fn configure_llvm(sess: &Session) {
110110
// Use non-zero `import-instr-limit` multiplier for cold callsites.
111111
add("-import-cold-multiplier=0.1", false);
112112

113+
if sess.print_llvm_stats() {
114+
add("-stats", false);
115+
}
116+
113117
for arg in sess_args {
114118
add(&(*arg), true);
115119
}

compiler/rustc_codegen_ssa/src/back/write.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,10 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
19451945
self.backend.print_pass_timings()
19461946
}
19471947

1948+
if sess.print_llvm_stats() {
1949+
self.backend.print_statistics()
1950+
}
1951+
19481952
(
19491953
CodegenResults {
19501954
metadata: self.metadata,

compiler/rustc_codegen_ssa/src/traits/write.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
3535
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
3636
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError>;
3737
fn print_pass_timings(&self);
38+
fn print_statistics(&self);
3839
unsafe fn optimize(
3940
cgcx: &CodegenContext<Self>,
4041
diag_handler: &Handler,

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ fn test_unstable_options_tracking_hash() {
715715
// `pre_link_arg` is omitted because it just forwards to `pre_link_args`.
716716
untracked!(pre_link_args, vec![String::from("abc"), String::from("def")]);
717717
untracked!(print_llvm_passes, true);
718+
untracked!(print_llvm_stats, true);
718719
untracked!(print_mono_items, Some(String::from("abc")));
719720
untracked!(print_type_sizes, true);
720721
untracked!(proc_macro_backtrace, true);

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "LLVMWrapper.h"
2+
#include "llvm/ADT/Statistic.h"
23
#include "llvm/IR/DebugInfoMetadata.h"
34
#include "llvm/IR/DiagnosticHandler.h"
45
#include "llvm/IR/DiagnosticInfo.h"
@@ -116,6 +117,11 @@ extern "C" void LLVMRustPrintPassTimings() {
116117
TimerGroup::printAll(OS);
117118
}
118119

120+
extern "C" void LLVMRustPrintStatistics() {
121+
raw_fd_ostream OS(2, false); // stderr.
122+
llvm::PrintStatistics(OS);
123+
}
124+
119125
extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name,
120126
size_t NameLen) {
121127
return wrap(unwrap(M)->getNamedValue(StringRef(Name, NameLen)));

compiler/rustc_session/src/options.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,9 @@ options! {
16721672
"make rustc print the total optimization fuel used by a crate"),
16731673
print_llvm_passes: bool = (false, parse_bool, [UNTRACKED],
16741674
"print the LLVM optimization passes being run (default: no)"),
1675+
#[rustc_lint_opt_deny_field_access("use `Session::print_llvm_stats` instead of this field")]
1676+
print_llvm_stats: bool = (true, parse_bool, [UNTRACKED],
1677+
"print LLVM statistics (default: no)"),
16751678
print_mono_items: Option<String> = (None, parse_opt_string, [UNTRACKED],
16761679
"print the result of the monomorphization collection pass"),
16771680
print_type_sizes: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_session/src/session.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,10 @@ impl Session {
10571057
self.opts.unstable_opts.verbose
10581058
}
10591059

1060+
pub fn print_llvm_stats(&self) -> bool {
1061+
self.opts.unstable_opts.print_llvm_stats
1062+
}
1063+
10601064
pub fn verify_llvm_ir(&self) -> bool {
10611065
self.opts.unstable_opts.verify_llvm_ir || option_env!("RUSTC_VERIFY_LLVM_IR").is_some()
10621066
}

0 commit comments

Comments
 (0)