about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkhei4 <kk.asano.luxy@gmail.com>2023-07-17 00:37:52 +0900
committerkhei4 <kk.asano.luxy@gmail.com>2023-07-17 00:37:52 +0900
commit4d307c482271ea3575a13b6c04222911e7706189 (patch)
tree32cf391f1ab50bf3d0a56199b8599064b232c325
parent138f522b590492d1ef80f1483382a2a678dec7d9 (diff)
print on rustc_codegen_llvm and rename malloc and cpy c_char
-rw-r--r--compiler/rustc_codegen_llvm/src/lib.rs30
-rw-r--r--compiler/rustc_codegen_llvm/src/llvm/ffi.rs4
-rw-r--r--compiler/rustc_interface/src/tests.rs2
-rw-r--r--compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp26
-rw-r--r--compiler/rustc_session/src/options.rs6
-rw-r--r--compiler/rustc_session/src/session.rs2
6 files changed, 49 insertions, 21 deletions
diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs
index 713c22ebfeb..c03b2188824 100644
--- a/compiler/rustc_codegen_llvm/src/lib.rs
+++ b/compiler/rustc_codegen_llvm/src/lib.rs
@@ -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>,
diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
index 3eb04555749..7cc79d859a3 100644
--- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
+++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
@@ -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;
 
diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs
index 0eac098e8a3..9aee39962df 100644
--- a/compiler/rustc_interface/src/tests.rs
+++ b/compiler/rustc_interface/src/tests.rs
@@ -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);
diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
index 89beb09db75..695b8847a97 100644
--- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
+++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
@@ -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" void LLVMRustPrintStatistics() {
-  raw_fd_ostream OS(2, false); // stderr.
-  llvm::PrintStatistics(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" 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,
diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs
index 5b5cafa9656..39efe9abeec 100644
--- a/compiler/rustc_session/src/options.rs
+++ b/compiler/rustc_session/src/options.rs
@@ -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],
diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs
index c2588b9a99a..6ebba596a94 100644
--- a/compiler/rustc_session/src/session.rs
+++ b/compiler/rustc_session/src/session.rs
@@ -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 {