Skip to content

Commit

Permalink
address feedback from nikic and oli-obk https://github.com/rust-lang/…
Browse files Browse the repository at this point in the history
…rust/pull/113723/files

use slice memcpy rather than strcpy and write it on stdout

use println on failure

Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
  • Loading branch information
khei4 and oli-obk committed Jul 20, 2023
1 parent 4d307c4 commit c7bf20d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
31 changes: 15 additions & 16 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self>,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit c7bf20d

Please sign in to comment.