From e0fb2a0ea351de2d6316e7c3155c64c7f8fb0d13 Mon Sep 17 00:00:00 2001 From: csmoe Date: Thu, 3 Nov 2022 22:34:24 +0800 Subject: [PATCH 1/3] add instruction count helper function --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 6f36281af23cc..67759854d8217 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1696,6 +1696,17 @@ LLVMRustModuleCost(LLVMModuleRef M) { return std::distance(std::begin(f), std::end(f)); } +extern "C" uint64_t +LLVMRustModuleInstructionStats(LLVMModuleRef M) { + auto f = unwrap(M)->functions(); + for (auto &func : f) + { + auto name = func.getName(); + auto count = func.getInstructionCount(); + } + return unwrap(M)->getInstructionCount(); +} + // Vector reductions: extern "C" LLVMValueRef LLVMRustBuildVectorReduceFAdd(LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Src) { From b6f9ed1049e7a66ce40851ce0288fa0407424c16 Mon Sep 17 00:00:00 2001 From: csmoe Date: Sat, 5 Nov 2022 13:43:44 +0000 Subject: [PATCH 2/3] print stats --- compiler/rustc_codegen_llvm/src/back/write.rs | 2 ++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 3 +++ 3 files changed, 6 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 11053a8f6c452..fcc3bdfd308f4 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -567,6 +567,8 @@ pub(crate) unsafe fn codegen( let llcx = &*module.module_llvm.llcx; let tm = &*module.module_llvm.tm; let module_name = module.name.clone(); + let count = unsafe { llvm::LLVMRustModuleInstructionStats(&llmod) }; + println!("llvm-module: {module_name} = {count}"); let module_name = Some(&module_name[..]); let handlers = DiagnosticHandlers::new(cgcx, diag_handler, llcx); diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 42cb694c0e75a..cdb9a6421c7f0 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -2389,6 +2389,7 @@ extern "C" { pub fn LLVMRustModuleBufferLen(p: &ModuleBuffer) -> usize; pub fn LLVMRustModuleBufferFree(p: &'static mut ModuleBuffer); pub fn LLVMRustModuleCost(M: &Module) -> u64; + pub fn LLVMRustModuleInstructionStats(M: &Module) -> u64; pub fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool) -> &'static mut ThinLTOBuffer; pub fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer); diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 67759854d8217..05f47fe0e822a 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1699,10 +1699,13 @@ LLVMRustModuleCost(LLVMModuleRef M) { extern "C" uint64_t LLVMRustModuleInstructionStats(LLVMModuleRef M) { auto f = unwrap(M)->functions(); + raw_fd_ostream OS(2, false); // stderr. for (auto &func : f) { auto name = func.getName(); + auto count = func.getInstructionCount(); + OS << name << "\t" << count << "\n"; } return unwrap(M)->getInstructionCount(); } From 9ada004b0837fc5cd00857666f5607ec90b489a2 Mon Sep 17 00:00:00 2001 From: csmoe Date: Wed, 9 Nov 2022 01:06:56 +0000 Subject: [PATCH 3/3] dump json --- compiler/rustc_codegen_llvm/src/back/write.rs | 6 ++-- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 3 +- .../rustc_llvm/llvm-wrapper/LLVMWrapper.h | 1 + .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 32 ++++++++++++------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index fcc3bdfd308f4..ece03396718fc 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -567,8 +567,10 @@ pub(crate) unsafe fn codegen( let llcx = &*module.module_llvm.llcx; let tm = &*module.module_llvm.tm; let module_name = module.name.clone(); - let count = unsafe { llvm::LLVMRustModuleInstructionStats(&llmod) }; - println!("llvm-module: {module_name} = {count}"); + let stats = + llvm::build_string(|s| unsafe { llvm::LLVMRustModuleInstructionStats(&llmod, s) }) + .expect("cannot get module instruction stats"); + println!("llvm-module: {stats}"); let module_name = Some(&module_name[..]); let handlers = DiagnosticHandlers::new(cgcx, diag_handler, llcx); diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index cdb9a6421c7f0..ada64f552d59f 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -2389,7 +2389,8 @@ extern "C" { pub fn LLVMRustModuleBufferLen(p: &ModuleBuffer) -> usize; pub fn LLVMRustModuleBufferFree(p: &'static mut ModuleBuffer); pub fn LLVMRustModuleCost(M: &Module) -> u64; - pub fn LLVMRustModuleInstructionStats(M: &Module) -> u64; + #[allow(improper_ctypes)] + pub fn LLVMRustModuleInstructionStats(M: &Module, Str: &RustString); pub fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool) -> &'static mut ThinLTOBuffer; pub fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer); diff --git a/compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h b/compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h index 015c1c52bef77..ac042e16b627d 100644 --- a/compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h +++ b/compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h @@ -15,6 +15,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/FormattedStream.h" +#include "llvm/Support/JSON.h" #include "llvm/Support/Host.h" #include "llvm/Support/Memory.h" #include "llvm/Support/SourceMgr.h" diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 05f47fe0e822a..40a49ae7899d6 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1696,18 +1696,26 @@ LLVMRustModuleCost(LLVMModuleRef M) { return std::distance(std::begin(f), std::end(f)); } -extern "C" uint64_t -LLVMRustModuleInstructionStats(LLVMModuleRef M) { - auto f = unwrap(M)->functions(); - raw_fd_ostream OS(2, false); // stderr. - for (auto &func : f) - { - auto name = func.getName(); - - auto count = func.getInstructionCount(); - OS << name << "\t" << count << "\n"; - } - return unwrap(M)->getInstructionCount(); +extern "C" void +LLVMRustModuleInstructionStats(LLVMModuleRef M, RustStringRef Str) +{ + RawRustStringOstream OS(Str); + llvm::json::OStream JOS(OS); + auto Module = unwrap(M); + + JOS.object([&] + { + JOS.attribute("module", Module->getName()); + JOS.attribute("total", Module->getInstructionCount()); + JOS.attributeArray("functions", [&] { + auto functions = Module->functions(); + for (auto &f: functions) { + JOS.object([&] { + JOS.attribute(f.getName(),f.getInstructionCount()); + }); + } + + }); }); } // Vector reductions: