Skip to content

Commit db140de

Browse files
committed
Explicitly register GCOV profiling pass as well
1 parent 5ecbe7f commit db140de

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
473473
pgo_gen_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
474474
pgo_use_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
475475
config.instrument_coverage,
476+
config.instrument_gcov,
476477
llvm_selfprofiler,
477478
selfprofile_before_pass_callback,
478479
selfprofile_after_pass_callback,
@@ -546,15 +547,6 @@ pub(crate) unsafe fn optimize(
546547
llvm::LLVMRustAddPass(fpm, find_pass("lint").unwrap());
547548
continue;
548549
}
549-
if pass_name == "insert-gcov-profiling" {
550-
// Instrumentation must be inserted before optimization,
551-
// otherwise LLVM may optimize some functions away which
552-
// breaks llvm-cov.
553-
//
554-
// This mirrors what Clang does in lib/CodeGen/BackendUtil.cpp.
555-
llvm::LLVMRustAddPass(mpm, find_pass(pass_name).unwrap());
556-
continue;
557-
}
558550

559551
if let Some(pass) = find_pass(pass_name) {
560552
extra_passes.push(pass);
@@ -567,6 +559,14 @@ pub(crate) unsafe fn optimize(
567559
}
568560
}
569561

562+
// Instrumentation must be inserted before optimization,
563+
// otherwise LLVM may optimize some functions away which
564+
// breaks llvm-cov.
565+
//
566+
// This mirrors what Clang does in lib/CodeGen/BackendUtil.cpp.
567+
if config.instrument_gcov {
568+
llvm::LLVMRustAddPass(mpm, find_pass("insert-gcov-profiling").unwrap());
569+
}
570570
if config.instrument_coverage {
571571
llvm::LLVMRustAddPass(mpm, find_pass("instrprof").unwrap());
572572
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,7 @@ extern "C" {
22042204
PGOGenPath: *const c_char,
22052205
PGOUsePath: *const c_char,
22062206
InstrumentCoverage: bool,
2207+
InstrumentGCOV: bool,
22072208
llvm_selfprofiler: *mut c_void,
22082209
begin_callback: SelfProfileBeforePassCallback,
22092210
end_callback: SelfProfileAfterPassCallback,

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub struct ModuleConfig {
8585
pub pgo_gen: SwitchWithOptPath,
8686
pub pgo_use: Option<PathBuf>,
8787
pub instrument_coverage: bool,
88+
pub instrument_gcov: bool,
8889

8990
pub sanitizer: SanitizerSet,
9091
pub sanitizer_recover: SanitizerSet,
@@ -166,19 +167,7 @@ impl ModuleConfig {
166167
};
167168

168169
ModuleConfig {
169-
passes: if_regular!(
170-
{
171-
let mut passes = sess.opts.cg.passes.clone();
172-
// compiler_builtins overrides the codegen-units settings,
173-
// which is incompatible with -Zprofile which requires that
174-
// only a single codegen unit is used per crate.
175-
if sess.opts.debugging_opts.profile && !is_compiler_builtins {
176-
passes.push("insert-gcov-profiling".to_owned());
177-
}
178-
passes
179-
},
180-
vec![]
181-
),
170+
passes: if_regular!(sess.opts.cg.passes.clone(), vec![]),
182171

183172
opt_level: opt_level_and_size,
184173
opt_size: opt_level_and_size,
@@ -189,6 +178,13 @@ impl ModuleConfig {
189178
),
190179
pgo_use: if_regular!(sess.opts.cg.profile_use.clone(), None),
191180
instrument_coverage: if_regular!(sess.instrument_coverage(), false),
181+
instrument_gcov: if_regular!(
182+
// compiler_builtins overrides the codegen-units settings,
183+
// which is incompatible with -Zprofile which requires that
184+
// only a single codegen unit is used per crate.
185+
sess.opts.debugging_opts.profile && !is_compiler_builtins,
186+
false
187+
),
192188

193189
sanitizer: if_regular!(sess.opts.debugging_opts.sanitizer, SanitizerSet::empty()),
194190
sanitizer_recover: if_regular!(

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/Transforms/Instrumentation.h"
3333
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
3434
#include "llvm/Support/TimeProfiler.h"
35+
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
3536
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
3637
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
3738
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
@@ -745,7 +746,8 @@ LLVMRustOptimizeWithNewPassManager(
745746
bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize,
746747
bool DisableSimplifyLibCalls, bool EmitLifetimeMarkers,
747748
LLVMRustSanitizerOptions *SanitizerOptions,
748-
const char *PGOGenPath, const char *PGOUsePath, bool InstrumentCoverage,
749+
const char *PGOGenPath, const char *PGOUsePath,
750+
bool InstrumentCoverage, bool InstrumentGCOV,
749751
void* LlvmSelfProfiler,
750752
LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
751753
LLVMRustSelfProfileAfterPassCallback AfterPassCallback) {
@@ -835,6 +837,14 @@ LLVMRustOptimizeWithNewPassManager(
835837
);
836838
}
837839

840+
if (InstrumentGCOV) {
841+
PipelineStartEPCallbacks.push_back(
842+
[](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
843+
MPM.addPass(GCOVProfilerPass(GCOVOptions::getDefault()));
844+
}
845+
);
846+
}
847+
838848
if (InstrumentCoverage) {
839849
PipelineStartEPCallbacks.push_back(
840850
[](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {

0 commit comments

Comments
 (0)