From 2d47816cbaebb3b8f400b11fa122feae00fd5c58 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Sat, 5 Nov 2022 01:08:57 -0700 Subject: [PATCH 1/4] 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 --- compiler/rustc_codegen_gcc/src/lib.rs | 4 ++++ compiler/rustc_codegen_llvm/src/lib.rs | 5 +++++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 3 +++ compiler/rustc_codegen_llvm/src/llvm_util.rs | 4 ++++ compiler/rustc_codegen_ssa/src/back/write.rs | 4 ++++ compiler/rustc_codegen_ssa/src/traits/write.rs | 1 + compiler/rustc_interface/src/tests.rs | 1 + compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 6 ++++++ compiler/rustc_session/src/options.rs | 3 +++ compiler/rustc_session/src/session.rs | 4 ++++ 10 files changed, 35 insertions(+) diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index 2a6b642782dfd..04ac0254a81ba 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -239,6 +239,10 @@ impl WriteBackendMethods for GccCodegenBackend { unimplemented!(); } + fn print_statistics(&self) { + unimplemented!() + } + unsafe fn optimize(_cgcx: &CodegenContext, _diag_handler: &Handler, module: &ModuleCodegen, config: &ModuleConfig) -> Result<(), FatalError> { module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level)); Ok(()) diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index 24ba28bbc82c2..713c22ebfebad 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -181,6 +181,11 @@ impl WriteBackendMethods for LlvmCodegenBackend { llvm::LLVMRustPrintPassTimings(); } } + fn print_statistics(&self) { + unsafe { + llvm::LLVMRustPrintStatistics(); + } + } fn run_link( cgcx: &CodegenContext, diag_handler: &Handler, diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 605f0154773a7..3eb0455574973 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1870,6 +1870,9 @@ extern "C" { /// Print the pass timings since static dtors aren't picking them up. pub fn LLVMRustPrintPassTimings(); + /// Print the statistics since static dtors aren't picking them up. + pub fn LLVMRustPrintStatistics(); + pub fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type; pub fn LLVMStructSetBody<'a>( diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 03be0654b50bb..12649de5866fc 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -110,6 +110,10 @@ unsafe fn configure_llvm(sess: &Session) { // Use non-zero `import-instr-limit` multiplier for cold callsites. add("-import-cold-multiplier=0.1", false); + if sess.print_llvm_stats() { + add("-stats", false); + } + for arg in sess_args { add(&(*arg), true); } diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index ececa29b23155..0ce6129a46252 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1945,6 +1945,10 @@ impl OngoingCodegen { self.backend.print_pass_timings() } + if sess.print_llvm_stats() { + self.backend.print_statistics() + } + ( CodegenResults { metadata: self.metadata, diff --git a/compiler/rustc_codegen_ssa/src/traits/write.rs b/compiler/rustc_codegen_ssa/src/traits/write.rs index 9826256a4c5d5..a3b11ed4fe8f6 100644 --- a/compiler/rustc_codegen_ssa/src/traits/write.rs +++ b/compiler/rustc_codegen_ssa/src/traits/write.rs @@ -35,6 +35,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone { cached_modules: Vec<(SerializedModule, WorkProduct)>, ) -> Result<(Vec>, Vec), FatalError>; fn print_pass_timings(&self); + fn print_statistics(&self); unsafe fn optimize( cgcx: &CodegenContext, diag_handler: &Handler, diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index 09141afd13707..0eac098e8a3e3 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -715,6 +715,7 @@ fn test_unstable_options_tracking_hash() { // `pre_link_arg` is omitted because it just forwards to `pre_link_args`. untracked!(pre_link_args, vec![String::from("abc"), String::from("def")]); untracked!(print_llvm_passes, true); + untracked!(print_llvm_stats, true); untracked!(print_mono_items, Some(String::from("abc"))); untracked!(print_type_sizes, true); untracked!(proc_macro_backtrace, true); diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index ab5fa961b95a4..89beb09db7550 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1,4 +1,5 @@ #include "LLVMWrapper.h" +#include "llvm/ADT/Statistic.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DiagnosticHandler.h" #include "llvm/IR/DiagnosticInfo.h" @@ -116,6 +117,11 @@ extern "C" void LLVMRustPrintPassTimings() { TimerGroup::printAll(OS); } +extern "C" void LLVMRustPrintStatistics() { + raw_fd_ostream OS(2, false); // stderr. + llvm::PrintStatistics(OS); +} + extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name, size_t NameLen) { return wrap(unwrap(M)->getNamedValue(StringRef(Name, NameLen))); diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 87d67c099cedb..90007d7c8496a 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1672,6 +1672,9 @@ options! { "make rustc print the total optimization fuel used by a crate"), print_llvm_passes: bool = (false, parse_bool, [UNTRACKED], "print the LLVM optimization passes being run (default: no)"), + #[rustc_lint_opt_deny_field_access("use `Session::print_llvm_stats` instead of this field")] + print_llvm_stats: bool = (true, parse_bool, [UNTRACKED], + "print LLVM statistics (default: no)"), print_mono_items: Option = (None, parse_opt_string, [UNTRACKED], "print the result of the monomorphization collection pass"), print_type_sizes: bool = (false, parse_bool, [UNTRACKED], diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 5be122ffbdeb0..c2588b9a99a1b 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1057,6 +1057,10 @@ impl Session { self.opts.unstable_opts.verbose } + pub fn print_llvm_stats(&self) -> bool { + self.opts.unstable_opts.print_llvm_stats + } + pub fn verify_llvm_ir(&self) -> bool { self.opts.unstable_opts.verify_llvm_ir || option_env!("RUSTC_VERIFY_LLVM_IR").is_some() } From 138f522b590492d1ef80f1483382a2a678dec7d9 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Sat, 5 Nov 2022 01:13:42 -0700 Subject: [PATCH 2/4] Don't enable by default :) --- compiler/rustc_session/src/options.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 90007d7c8496a..5b5cafa965625 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1673,7 +1673,7 @@ options! { print_llvm_passes: bool = (false, parse_bool, [UNTRACKED], "print the LLVM optimization passes being run (default: no)"), #[rustc_lint_opt_deny_field_access("use `Session::print_llvm_stats` instead of this field")] - print_llvm_stats: bool = (true, parse_bool, [UNTRACKED], + print_llvm_stats: bool = (false, parse_bool, [UNTRACKED], "print LLVM statistics (default: no)"), print_mono_items: Option = (None, parse_opt_string, [UNTRACKED], "print the result of the monomorphization collection pass"), From 4d307c482271ea3575a13b6c04222911e7706189 Mon Sep 17 00:00:00 2001 From: khei4 Date: Mon, 17 Jul 2023 00:37:52 +0900 Subject: [PATCH 3/4] print on rustc_codegen_llvm and rename malloc and cpy c_char --- compiler/rustc_codegen_llvm/src/lib.rs | 30 +++++++++++++++---- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 4 +-- compiler/rustc_interface/src/tests.rs | 2 +- .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 26 +++++++++++----- compiler/rustc_session/src/options.rs | 6 ++-- compiler/rustc_session/src/session.rs | 2 +- 6 files changed, 49 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index 713c22ebfebad..c03b218882445 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -177,14 +177,32 @@ impl WriteBackendMethods for LlvmCodegenBackend { type ThinData = back::lto::ThinData; type ThinBuffer = back::lto::ThinBuffer; fn print_pass_timings(&self) { - unsafe { - llvm::LLVMRustPrintPassTimings(); - } + let msg = unsafe { + let cstr = llvm::LLVMRustPrintPassTimings(); + if cstr.is_null() { + "failed to get pass timings".into() + } else { + let timings = CStr::from_ptr(cstr).to_bytes(); + let timings = String::from_utf8_lossy(timings).to_string(); + libc::free(cstr as *mut _); + timings + } + }; + println!("{}", msg); } fn print_statistics(&self) { - unsafe { - llvm::LLVMRustPrintStatistics(); - } + let msg = unsafe { + let cstr = llvm::LLVMRustPrintStatistics(); + if cstr.is_null() { + "failed to get stats".into() + } else { + let stats = CStr::from_ptr(cstr).to_bytes(); + let stats = String::from_utf8_lossy(stats).to_string(); + libc::free(cstr as *mut _); + stats + } + }; + println!("{}", msg); } fn run_link( cgcx: &CodegenContext, diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 3eb0455574973..7cc79d859a389 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1868,10 +1868,10 @@ extern "C" { pub fn LLVMRustGetLastError() -> *const c_char; /// Print the pass timings since static dtors aren't picking them up. - pub fn LLVMRustPrintPassTimings(); + pub fn LLVMRustPrintPassTimings() -> *const c_char; /// Print the statistics since static dtors aren't picking them up. - pub fn LLVMRustPrintStatistics(); + pub fn LLVMRustPrintStatistics() -> *const c_char; pub fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type; diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index 0eac098e8a3e3..9aee39962df40 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -714,8 +714,8 @@ fn test_unstable_options_tracking_hash() { untracked!(perf_stats, true); // `pre_link_arg` is omitted because it just forwards to `pre_link_args`. untracked!(pre_link_args, vec![String::from("abc"), String::from("def")]); + untracked!(print_codegen_stats, true); untracked!(print_llvm_passes, true); - untracked!(print_llvm_stats, true); untracked!(print_mono_items, Some(String::from("abc"))); untracked!(print_type_sizes, true); untracked!(proc_macro_backtrace, true); diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 89beb09db7550..695b8847a976d 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -112,14 +112,24 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M, unwrap(M)->setTargetTriple(Triple::normalize(Triple)); } -extern "C" void LLVMRustPrintPassTimings() { - raw_fd_ostream OS(2, false); // stderr. - TimerGroup::printAll(OS); -} - -extern "C" void LLVMRustPrintStatistics() { - raw_fd_ostream OS(2, false); // stderr. - llvm::PrintStatistics(OS); +extern "C" const char *LLVMRustPrintPassTimings(void) { + std::string buf; + raw_string_ostream SS(buf); + TimerGroup::printAll(SS); + SS.flush(); + char* CStr = (char*) malloc((buf.length() + 1) * sizeof(char)); + strcpy(CStr, buf.c_str()); + return CStr; +} + +extern "C" const char *LLVMRustPrintStatistics(void) { + std::string buf; + raw_string_ostream SS(buf); + llvm::PrintStatistics(SS); + SS.flush(); + char* CStr = (char*) malloc((buf.length() + 1) * sizeof(char)); + strcpy(CStr, buf.c_str()); + return CStr; } extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name, diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 5b5cafa965625..39efe9abeecdb 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1668,13 +1668,13 @@ options! { "use a more precise version of drop elaboration for matches on enums (default: yes). \ This results in better codegen, but has caused miscompilations on some tier 2 platforms. \ See #77382 and #74551."), + #[rustc_lint_opt_deny_field_access("use `Session::print_codegen_stats` instead of this field")] + print_codegen_stats: bool = (false, parse_bool, [UNTRACKED], + "print codegen statistics (default: no)"), print_fuel: Option = (None, parse_opt_string, [TRACKED], "make rustc print the total optimization fuel used by a crate"), print_llvm_passes: bool = (false, parse_bool, [UNTRACKED], "print the LLVM optimization passes being run (default: no)"), - #[rustc_lint_opt_deny_field_access("use `Session::print_llvm_stats` instead of this field")] - print_llvm_stats: bool = (false, parse_bool, [UNTRACKED], - "print LLVM statistics (default: no)"), print_mono_items: Option = (None, parse_opt_string, [UNTRACKED], "print the result of the monomorphization collection pass"), print_type_sizes: bool = (false, parse_bool, [UNTRACKED], diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index c2588b9a99a1b..6ebba596a9433 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1058,7 +1058,7 @@ impl Session { } pub fn print_llvm_stats(&self) -> bool { - self.opts.unstable_opts.print_llvm_stats + self.opts.unstable_opts.print_codegen_stats } pub fn verify_llvm_ir(&self) -> bool { From c7bf20dfdcbedbba05445035bcabd4f706ba9e42 Mon Sep 17 00:00:00 2001 From: khei4 Date: Wed, 19 Jul 2023 17:00:06 +0900 Subject: [PATCH 4/4] address feedback from nikic and oli-obk https://github.com/rust-lang/rust/pull/113723/files use slice memcpy rather than strcpy and write it on stdout use println on failure Co-authored-by: Oli Scherer --- compiler/rustc_codegen_llvm/src/lib.rs | 31 +++++++++---------- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 4 +-- .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 14 +++++---- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index c03b218882445..b035437621033 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -46,6 +46,7 @@ use rustc_span::symbol::Symbol; use std::any::Any; use std::ffi::CStr; +use std::io::Write; mod back { pub mod archive; @@ -177,32 +178,30 @@ impl WriteBackendMethods for LlvmCodegenBackend { type ThinData = back::lto::ThinData; type ThinBuffer = back::lto::ThinBuffer; fn print_pass_timings(&self) { - let msg = unsafe { - let cstr = llvm::LLVMRustPrintPassTimings(); + unsafe { + let mut size = 0; + let cstr = llvm::LLVMRustPrintPassTimings(&mut size as *mut usize); if cstr.is_null() { - "failed to get pass timings".into() + println!("failed to get pass timings"); } else { - let timings = CStr::from_ptr(cstr).to_bytes(); - let timings = String::from_utf8_lossy(timings).to_string(); + let timings = std::slice::from_raw_parts(cstr as *const u8, size); + std::io::stdout().write_all(timings).unwrap(); libc::free(cstr as *mut _); - timings } - }; - println!("{}", msg); + } } fn print_statistics(&self) { - let msg = unsafe { - let cstr = llvm::LLVMRustPrintStatistics(); + unsafe { + let mut size = 0; + let cstr = llvm::LLVMRustPrintStatistics(&mut size as *mut usize); if cstr.is_null() { - "failed to get stats".into() + println!("failed to get pass stats"); } else { - let stats = CStr::from_ptr(cstr).to_bytes(); - let stats = String::from_utf8_lossy(stats).to_string(); + let stats = std::slice::from_raw_parts(cstr as *const u8, size); + std::io::stdout().write_all(stats).unwrap(); libc::free(cstr as *mut _); - stats } - }; - println!("{}", msg); + } } fn run_link( cgcx: &CodegenContext, diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 7cc79d859a389..fd88994c31c2a 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1868,10 +1868,10 @@ extern "C" { pub fn LLVMRustGetLastError() -> *const c_char; /// Print the pass timings since static dtors aren't picking them up. - pub fn LLVMRustPrintPassTimings() -> *const c_char; + pub fn LLVMRustPrintPassTimings(size: *const size_t) -> *const c_char; /// Print the statistics since static dtors aren't picking them up. - pub fn LLVMRustPrintStatistics() -> *const c_char; + pub fn LLVMRustPrintStatistics(size: *const size_t) -> *const c_char; pub fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type; diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 695b8847a976d..870a2e02cbaa5 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -112,23 +112,25 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M, unwrap(M)->setTargetTriple(Triple::normalize(Triple)); } -extern "C" const char *LLVMRustPrintPassTimings(void) { +extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) { std::string buf; raw_string_ostream SS(buf); TimerGroup::printAll(SS); SS.flush(); - char* CStr = (char*) malloc((buf.length() + 1) * sizeof(char)); - strcpy(CStr, buf.c_str()); + *Len = buf.length(); + char *CStr = (char *)malloc(*Len); + memcpy(CStr, buf.c_str(), *Len); return CStr; } -extern "C" const char *LLVMRustPrintStatistics(void) { +extern "C" const char *LLVMRustPrintStatistics(size_t *Len) { std::string buf; raw_string_ostream SS(buf); llvm::PrintStatistics(SS); SS.flush(); - char* CStr = (char*) malloc((buf.length() + 1) * sizeof(char)); - strcpy(CStr, buf.c_str()); + *Len = buf.length(); + char *CStr = (char *)malloc(*Len); + memcpy(CStr, buf.c_str(), *Len); return CStr; }