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 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<Self>,

View file

@ -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;

View file

@ -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);

View file

@ -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" 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" void LLVMRustPrintStatistics() {
raw_fd_ostream OS(2, false); // stderr.
llvm::PrintStatistics(OS);
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,

View file

@ -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<String> = (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<String> = (None, parse_opt_string, [UNTRACKED],
"print the result of the monomorphization collection pass"),
print_type_sizes: bool = (false, parse_bool, [UNTRACKED],

View file

@ -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 {