Skip to content

Commit 2bceb2a

Browse files
authored
Rollup merge of rust-lang#108400 - csmoe:cgu-instr-perf, r=bjorn3
add llvm cgu instructions stats to perf r? ``@bjorn3``
2 parents a461014 + a30de6e commit 2bceb2a

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,8 @@ dependencies = [
38103810
"rustc_span",
38113811
"rustc_symbol_mangling",
38123812
"rustc_target",
3813+
"serde",
3814+
"serde_json",
38133815
"smallvec",
38143816
"tempfile",
38153817
"tracing",

compiler/rustc_codegen_llvm/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
3636
rustc_ast = { path = "../rustc_ast" }
3737
rustc_span = { path = "../rustc_span" }
3838
tempfile = "3.2.0"
39+
serde = { version = "1", features = [ "derive" ]}
40+
serde_json = "1"

compiler/rustc_codegen_llvm/src/back/write.rs

+21
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ pub(crate) unsafe fn codegen(
761761
EmitObj::None => {}
762762
}
763763

764+
record_llvm_cgu_instructions_stats(&cgcx.prof, llmod);
764765
drop(handlers);
765766
}
766767

@@ -974,3 +975,23 @@ fn record_artifact_size(
974975
self_profiler_ref.artifact_size(artifact_kind, artifact_name.to_string_lossy(), file_size);
975976
}
976977
}
978+
979+
fn record_llvm_cgu_instructions_stats(prof: &SelfProfilerRef, llmod: &llvm::Module) {
980+
if !prof.enabled() {
981+
return;
982+
}
983+
984+
let raw_stats =
985+
llvm::build_string(|s| unsafe { llvm::LLVMRustModuleInstructionStats(&llmod, s) })
986+
.expect("cannot get module instruction stats");
987+
988+
#[derive(serde::Deserialize)]
989+
struct InstructionsStats {
990+
module: String,
991+
total: u64,
992+
}
993+
994+
let InstructionsStats { module, total } =
995+
serde_json::from_str(&raw_stats).expect("cannot parse llvm cgu instructions stats");
996+
prof.artifact_size("cgu_instructions", module, total);
997+
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,8 @@ extern "C" {
24102410
pub fn LLVMRustModuleBufferLen(p: &ModuleBuffer) -> usize;
24112411
pub fn LLVMRustModuleBufferFree(p: &'static mut ModuleBuffer);
24122412
pub fn LLVMRustModuleCost(M: &Module) -> u64;
2413+
#[allow(improper_ctypes)]
2414+
pub fn LLVMRustModuleInstructionStats(M: &Module, Str: &RustString);
24132415

24142416
pub fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool) -> &'static mut ThinLTOBuffer;
24152417
pub fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer);

compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/Support/Debug.h"
1515
#include "llvm/Support/DynamicLibrary.h"
1616
#include "llvm/Support/FormattedStream.h"
17+
#include "llvm/Support/JSON.h"
1718
#include "llvm/Support/Host.h"
1819
#include "llvm/Support/Memory.h"
1920
#include "llvm/Support/SourceMgr.h"

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,19 @@ LLVMRustModuleCost(LLVMModuleRef M) {
17511751
return std::distance(std::begin(f), std::end(f));
17521752
}
17531753

1754+
extern "C" void
1755+
LLVMRustModuleInstructionStats(LLVMModuleRef M, RustStringRef Str)
1756+
{
1757+
RawRustStringOstream OS(Str);
1758+
llvm::json::OStream JOS(OS);
1759+
auto Module = unwrap(M);
1760+
1761+
JOS.object([&] {
1762+
JOS.attribute("module", Module->getName());
1763+
JOS.attribute("total", Module->getInstructionCount());
1764+
});
1765+
}
1766+
17541767
// Vector reductions:
17551768
extern "C" LLVMValueRef
17561769
LLVMRustBuildVectorReduceFAdd(LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Src) {

0 commit comments

Comments
 (0)