1
Fork 0

print on rustc_codegen_llvm and rename malloc and cpy c_char

This commit is contained in:
khei4 2023-07-17 00:37:52 +09:00
parent 138f522b59
commit 4d307c4822
6 changed files with 47 additions and 19 deletions

View file

@ -177,14 +177,32 @@ impl WriteBackendMethods for LlvmCodegenBackend {
type ThinData = back::lto::ThinData; type ThinData = back::lto::ThinData;
type ThinBuffer = back::lto::ThinBuffer; type ThinBuffer = back::lto::ThinBuffer;
fn print_pass_timings(&self) { fn print_pass_timings(&self) {
unsafe { let msg = unsafe {
llvm::LLVMRustPrintPassTimings(); 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) { fn print_statistics(&self) {
unsafe { let msg = unsafe {
llvm::LLVMRustPrintStatistics(); 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( fn run_link(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,

View file

@ -1868,10 +1868,10 @@ extern "C" {
pub fn LLVMRustGetLastError() -> *const c_char; pub fn LLVMRustGetLastError() -> *const c_char;
/// Print the pass timings since static dtors aren't picking them up. /// 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. /// 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; pub fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type;

View file

@ -714,8 +714,8 @@ fn test_unstable_options_tracking_hash() {
untracked!(perf_stats, true); untracked!(perf_stats, true);
// `pre_link_arg` is omitted because it just forwards to `pre_link_args`. // `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!(pre_link_args, vec![String::from("abc"), String::from("def")]);
untracked!(print_codegen_stats, true);
untracked!(print_llvm_passes, true); untracked!(print_llvm_passes, true);
untracked!(print_llvm_stats, true);
untracked!(print_mono_items, Some(String::from("abc"))); untracked!(print_mono_items, Some(String::from("abc")));
untracked!(print_type_sizes, true); untracked!(print_type_sizes, true);
untracked!(proc_macro_backtrace, true); untracked!(proc_macro_backtrace, true);

View file

@ -112,14 +112,24 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
unwrap(M)->setTargetTriple(Triple::normalize(Triple)); unwrap(M)->setTargetTriple(Triple::normalize(Triple));
} }
extern "C" void LLVMRustPrintPassTimings() { extern "C" const char *LLVMRustPrintPassTimings(void) {
raw_fd_ostream OS(2, false); // stderr. std::string buf;
TimerGroup::printAll(OS); 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" void LLVMRustPrintStatistics() { extern "C" const char *LLVMRustPrintStatistics(void) {
raw_fd_ostream OS(2, false); // stderr. std::string buf;
llvm::PrintStatistics(OS); 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, extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name,

View file

@ -1668,13 +1668,13 @@ options! {
"use a more precise version of drop elaboration for matches on enums (default: yes). \ "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. \ This results in better codegen, but has caused miscompilations on some tier 2 platforms. \
See #77382 and #74551."), 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<String> = (None, parse_opt_string, [TRACKED], print_fuel: Option<String> = (None, parse_opt_string, [TRACKED],
"make rustc print the total optimization fuel used by a crate"), "make rustc print the total optimization fuel used by a crate"),
print_llvm_passes: bool = (false, parse_bool, [UNTRACKED], print_llvm_passes: bool = (false, parse_bool, [UNTRACKED],
"print the LLVM optimization passes being run (default: no)"), "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<String> = (None, parse_opt_string, [UNTRACKED], print_mono_items: Option<String> = (None, parse_opt_string, [UNTRACKED],
"print the result of the monomorphization collection pass"), "print the result of the monomorphization collection pass"),
print_type_sizes: bool = (false, parse_bool, [UNTRACKED], print_type_sizes: bool = (false, parse_bool, [UNTRACKED],

View file

@ -1058,7 +1058,7 @@ impl Session {
} }
pub fn print_llvm_stats(&self) -> bool { 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 { pub fn verify_llvm_ir(&self) -> bool {