Skip to content

Commit 4d307c4

Browse files
committed
print on rustc_codegen_llvm and rename malloc and cpy c_char
1 parent 138f522 commit 4d307c4

File tree

6 files changed

+49
-21
lines changed

6 files changed

+49
-21
lines changed

compiler/rustc_codegen_llvm/src/lib.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,32 @@ impl WriteBackendMethods for LlvmCodegenBackend {
177177
type ThinData = back::lto::ThinData;
178178
type ThinBuffer = back::lto::ThinBuffer;
179179
fn print_pass_timings(&self) {
180-
unsafe {
181-
llvm::LLVMRustPrintPassTimings();
182-
}
180+
let msg = unsafe {
181+
let cstr = llvm::LLVMRustPrintPassTimings();
182+
if cstr.is_null() {
183+
"failed to get pass timings".into()
184+
} else {
185+
let timings = CStr::from_ptr(cstr).to_bytes();
186+
let timings = String::from_utf8_lossy(timings).to_string();
187+
libc::free(cstr as *mut _);
188+
timings
189+
}
190+
};
191+
println!("{}", msg);
183192
}
184193
fn print_statistics(&self) {
185-
unsafe {
186-
llvm::LLVMRustPrintStatistics();
187-
}
194+
let msg = unsafe {
195+
let cstr = llvm::LLVMRustPrintStatistics();
196+
if cstr.is_null() {
197+
"failed to get stats".into()
198+
} else {
199+
let stats = CStr::from_ptr(cstr).to_bytes();
200+
let stats = String::from_utf8_lossy(stats).to_string();
201+
libc::free(cstr as *mut _);
202+
stats
203+
}
204+
};
205+
println!("{}", msg);
188206
}
189207
fn run_link(
190208
cgcx: &CodegenContext<Self>,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1868,10 +1868,10 @@ extern "C" {
18681868
pub fn LLVMRustGetLastError() -> *const c_char;
18691869

18701870
/// Print the pass timings since static dtors aren't picking them up.
1871-
pub fn LLVMRustPrintPassTimings();
1871+
pub fn LLVMRustPrintPassTimings() -> *const c_char;
18721872

18731873
/// Print the statistics since static dtors aren't picking them up.
1874-
pub fn LLVMRustPrintStatistics();
1874+
pub fn LLVMRustPrintStatistics() -> *const c_char;
18751875

18761876
pub fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type;
18771877

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,8 @@ fn test_unstable_options_tracking_hash() {
714714
untracked!(perf_stats, true);
715715
// `pre_link_arg` is omitted because it just forwards to `pre_link_args`.
716716
untracked!(pre_link_args, vec![String::from("abc"), String::from("def")]);
717+
untracked!(print_codegen_stats, true);
717718
untracked!(print_llvm_passes, true);
718-
untracked!(print_llvm_stats, true);
719719
untracked!(print_mono_items, Some(String::from("abc")));
720720
untracked!(print_type_sizes, true);
721721
untracked!(proc_macro_backtrace, true);

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,24 @@ extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
112112
unwrap(M)->setTargetTriple(Triple::normalize(Triple));
113113
}
114114

115-
extern "C" void LLVMRustPrintPassTimings() {
116-
raw_fd_ostream OS(2, false); // stderr.
117-
TimerGroup::printAll(OS);
118-
}
119-
120-
extern "C" void LLVMRustPrintStatistics() {
121-
raw_fd_ostream OS(2, false); // stderr.
122-
llvm::PrintStatistics(OS);
115+
extern "C" const char *LLVMRustPrintPassTimings(void) {
116+
std::string buf;
117+
raw_string_ostream SS(buf);
118+
TimerGroup::printAll(SS);
119+
SS.flush();
120+
char* CStr = (char*) malloc((buf.length() + 1) * sizeof(char));
121+
strcpy(CStr, buf.c_str());
122+
return CStr;
123+
}
124+
125+
extern "C" const char *LLVMRustPrintStatistics(void) {
126+
std::string buf;
127+
raw_string_ostream SS(buf);
128+
llvm::PrintStatistics(SS);
129+
SS.flush();
130+
char* CStr = (char*) malloc((buf.length() + 1) * sizeof(char));
131+
strcpy(CStr, buf.c_str());
132+
return CStr;
123133
}
124134

125135
extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M, const char *Name,

compiler/rustc_session/src/options.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1668,13 +1668,13 @@ options! {
16681668
"use a more precise version of drop elaboration for matches on enums (default: yes). \
16691669
This results in better codegen, but has caused miscompilations on some tier 2 platforms. \
16701670
See #77382 and #74551."),
1671+
#[rustc_lint_opt_deny_field_access("use `Session::print_codegen_stats` instead of this field")]
1672+
print_codegen_stats: bool = (false, parse_bool, [UNTRACKED],
1673+
"print codegen statistics (default: no)"),
16711674
print_fuel: Option<String> = (None, parse_opt_string, [TRACKED],
16721675
"make rustc print the total optimization fuel used by a crate"),
16731676
print_llvm_passes: bool = (false, parse_bool, [UNTRACKED],
16741677
"print the LLVM optimization passes being run (default: no)"),
1675-
#[rustc_lint_opt_deny_field_access("use `Session::print_llvm_stats` instead of this field")]
1676-
print_llvm_stats: bool = (false, parse_bool, [UNTRACKED],
1677-
"print LLVM statistics (default: no)"),
16781678
print_mono_items: Option<String> = (None, parse_opt_string, [UNTRACKED],
16791679
"print the result of the monomorphization collection pass"),
16801680
print_type_sizes: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_session/src/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ impl Session {
10581058
}
10591059

10601060
pub fn print_llvm_stats(&self) -> bool {
1061-
self.opts.unstable_opts.print_llvm_stats
1061+
self.opts.unstable_opts.print_codegen_stats
10621062
}
10631063

10641064
pub fn verify_llvm_ir(&self) -> bool {

0 commit comments

Comments
 (0)