Simplify FFI calls for -Ztime-llvm-passes and -Zprint-codegen-stats

This commit is contained in:
Zalathar 2024-11-03 15:15:46 +11:00
parent 43c78051ea
commit 5bfa0b106e
3 changed files with 16 additions and 45 deletions

View File

@ -22,7 +22,6 @@
use std::any::Any; use std::any::Any;
use std::ffi::CStr; use std::ffi::CStr;
use std::io::Write;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use back::owned_target_machine::OwnedTargetMachine; use back::owned_target_machine::OwnedTargetMachine;
@ -165,30 +164,12 @@ 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 timings = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintPassTimings(s) }).unwrap();
let mut size = 0; print!("{timings}");
let cstr = llvm::LLVMRustPrintPassTimings(&raw mut size);
if cstr.is_null() {
println!("failed to get pass timings");
} else {
let timings = std::slice::from_raw_parts(cstr as *const u8, size);
std::io::stdout().write_all(timings).unwrap();
libc::free(cstr as *mut _);
}
}
} }
fn print_statistics(&self) { fn print_statistics(&self) {
unsafe { let stats = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintStatistics(s) }).unwrap();
let mut size = 0; print!("{stats}");
let cstr = llvm::LLVMRustPrintStatistics(&raw mut size);
if cstr.is_null() {
println!("failed to get pass stats");
} else {
let stats = std::slice::from_raw_parts(cstr as *const u8, size);
std::io::stdout().write_all(stats).unwrap();
libc::free(cstr as *mut _);
}
}
} }
fn run_link( fn run_link(
cgcx: &CodegenContext<Self>, cgcx: &CodegenContext<Self>,

View File

@ -1765,11 +1765,13 @@ pub fn LLVMRustBuildAtomicStore<'a>(
/// Returns a string describing the last error caused by an LLVMRust* call. /// Returns a string describing the last error caused by an LLVMRust* call.
pub fn LLVMRustGetLastError() -> *const c_char; pub fn LLVMRustGetLastError() -> *const c_char;
/// Print the pass timings since static dtors aren't picking them up. /// Prints the timing information collected by `-Ztime-llvm-passes`.
pub fn LLVMRustPrintPassTimings(size: *const size_t) -> *const c_char; #[expect(improper_ctypes)]
pub(crate) fn LLVMRustPrintPassTimings(OutStr: &RustString);
/// Print the statistics since static dtors aren't picking them up. /// Prints the statistics collected by `-Zprint-codegen-stats`.
pub fn LLVMRustPrintStatistics(size: *const size_t) -> *const c_char; #[expect(improper_ctypes)]
pub(crate) fn LLVMRustPrintStatistics(OutStr: &RustString);
/// Prepares inline assembly. /// Prepares inline assembly.
pub fn LLVMRustInlineAsm( pub fn LLVMRustInlineAsm(

View File

@ -140,26 +140,14 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
unwrap(M)->setTargetTriple(Triple::normalize(Triple)); unwrap(M)->setTargetTriple(Triple::normalize(Triple));
} }
extern "C" const char *LLVMRustPrintPassTimings(size_t *Len) { extern "C" void LLVMRustPrintPassTimings(RustStringRef OutBuf) {
std::string buf; auto OS = RawRustStringOstream(OutBuf);
auto SS = raw_string_ostream(buf); TimerGroup::printAll(OS);
TimerGroup::printAll(SS);
SS.flush();
*Len = buf.length();
char *CStr = (char *)malloc(*Len);
memcpy(CStr, buf.c_str(), *Len);
return CStr;
} }
extern "C" const char *LLVMRustPrintStatistics(size_t *Len) { extern "C" void LLVMRustPrintStatistics(RustStringRef OutBuf) {
std::string buf; auto OS = RawRustStringOstream(OutBuf);
auto SS = raw_string_ostream(buf); llvm::PrintStatistics(OS);
llvm::PrintStatistics(SS);
SS.flush();
*Len = buf.length();
char *CStr = (char *)malloc(*Len);
memcpy(CStr, buf.c_str(), *Len);
return CStr;
} }
extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name, extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name,