Skip to content

Commit 1008822

Browse files
Add support for generating unique *.profraw files by default when using the -C instrument-coverage flag.
Respond to PR comments.
1 parent 20ffea6 commit 1008822

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+10
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,14 @@ fn get_pgo_sample_use_path(config: &ModuleConfig) -> Option<CString> {
423423
.map(|path_buf| CString::new(path_buf.to_string_lossy().as_bytes()).unwrap())
424424
}
425425

426+
fn get_instr_profile_output_path(config: &ModuleConfig) -> Option<CString> {
427+
if config.instrument_coverage {
428+
Some(CString::new(format!("{}", PathBuf::from("default_%m_%p.profraw").display())).unwrap())
429+
} else {
430+
None
431+
}
432+
}
433+
426434
pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
427435
cgcx: &CodegenContext<LlvmCodegenBackend>,
428436
diag_handler: &Handler,
@@ -438,6 +446,7 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
438446
let pgo_use_path = get_pgo_use_path(config);
439447
let pgo_sample_use_path = get_pgo_sample_use_path(config);
440448
let is_lto = opt_stage == llvm::OptStage::ThinLTO || opt_stage == llvm::OptStage::FatLTO;
449+
let instr_profile_output_path = get_instr_profile_output_path(config);
441450
// Sanitizer instrumentation is only inserted during the pre-link optimization stage.
442451
let sanitizer_options = if !is_lto {
443452
Some(llvm::SanitizerOptions {
@@ -488,6 +497,7 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
488497
pgo_gen_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
489498
pgo_use_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
490499
config.instrument_coverage,
500+
instr_profile_output_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
491501
config.instrument_gcov,
492502
pgo_sample_use_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
493503
config.debug_info_for_profiling,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,7 @@ extern "C" {
23472347
PGOGenPath: *const c_char,
23482348
PGOUsePath: *const c_char,
23492349
InstrumentCoverage: bool,
2350+
InstrProfileOutput: *const c_char,
23502351
InstrumentGCOV: bool,
23512352
PGOSampleUsePath: *const c_char,
23522353
DebugInfoForProfiling: bool,

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,8 @@ LLVMRustOptimizeWithNewPassManager(
822822
bool DisableSimplifyLibCalls, bool EmitLifetimeMarkers,
823823
LLVMRustSanitizerOptions *SanitizerOptions,
824824
const char *PGOGenPath, const char *PGOUsePath,
825-
bool InstrumentCoverage, bool InstrumentGCOV,
825+
bool InstrumentCoverage, const char *InstrProfileOutput,
826+
bool InstrumentGCOV,
826827
const char *PGOSampleUsePath, bool DebugInfoForProfiling,
827828
void* LlvmSelfProfiler,
828829
LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
@@ -922,8 +923,11 @@ LLVMRustOptimizeWithNewPassManager(
922923

923924
if (InstrumentCoverage) {
924925
PipelineStartEPCallbacks.push_back(
925-
[](ModulePassManager &MPM, OptimizationLevel Level) {
926+
[InstrProfileOutput](ModulePassManager &MPM, OptimizationLevel Level) {
926927
InstrProfOptions Options;
928+
if (InstrProfileOutput) {
929+
Options.InstrProfileOutput = InstrProfileOutput;
930+
}
927931
MPM.addPass(InstrProfiling(Options, false));
928932
}
929933
);
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Test that `-Cinstrument-coverage` creates expected __llvm_profile_filename symbol in LLVM IR.
2+
3+
// needs-profiler-support
4+
// compile-flags: -Cinstrument-coverage
5+
6+
// CHECK: @__llvm_profile_filename = {{.*}}"default_%m_%p.profraw\00"{{.*}}
7+
8+
#![crate_type="lib"]
9+
10+
#[inline(never)]
11+
fn some_function() {
12+
13+
}
14+
15+
pub fn some_other_function() {
16+
some_function();
17+
}

0 commit comments

Comments
 (0)