Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5ecbe7f

Browse files
committed
Explicitly register instrprof pass
Don't use "passes" for this purpose, explicitly insert it into the correct place in the pipeline instead.
1 parent 0318883 commit 5ecbe7f

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
472472
sanitizer_options.as_ref(),
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()),
475+
config.instrument_coverage,
475476
llvm_selfprofiler,
476477
selfprofile_before_pass_callback,
477478
selfprofile_after_pass_callback,
@@ -545,7 +546,7 @@ pub(crate) unsafe fn optimize(
545546
llvm::LLVMRustAddPass(fpm, find_pass("lint").unwrap());
546547
continue;
547548
}
548-
if pass_name == "insert-gcov-profiling" || pass_name == "instrprof" {
549+
if pass_name == "insert-gcov-profiling" {
549550
// Instrumentation must be inserted before optimization,
550551
// otherwise LLVM may optimize some functions away which
551552
// breaks llvm-cov.
@@ -566,6 +567,10 @@ pub(crate) unsafe fn optimize(
566567
}
567568
}
568569

570+
if config.instrument_coverage {
571+
llvm::LLVMRustAddPass(mpm, find_pass("instrprof").unwrap());
572+
}
573+
569574
add_sanitizer_passes(config, &mut extra_passes);
570575

571576
// Some options cause LLVM bitcode to be emitted, which uses ThinLTOBuffers, so we need

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,7 @@ extern "C" {
22032203
SanitizerOptions: Option<&SanitizerOptions>,
22042204
PGOGenPath: *const c_char,
22052205
PGOUsePath: *const c_char,
2206+
InstrumentCoverage: bool,
22062207
llvm_selfprofiler: *mut c_void,
22072208
begin_callback: SelfProfileBeforePassCallback,
22082209
end_callback: SelfProfileAfterPassCallback,

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub struct ModuleConfig {
8484

8585
pub pgo_gen: SwitchWithOptPath,
8686
pub pgo_use: Option<PathBuf>,
87+
pub instrument_coverage: bool,
8788

8889
pub sanitizer: SanitizerSet,
8990
pub sanitizer_recover: SanitizerSet,
@@ -174,12 +175,6 @@ impl ModuleConfig {
174175
if sess.opts.debugging_opts.profile && !is_compiler_builtins {
175176
passes.push("insert-gcov-profiling".to_owned());
176177
}
177-
178-
// The rustc option `-Zinstrument_coverage` injects intrinsic calls to
179-
// `llvm.instrprof.increment()`, which requires the LLVM `instrprof` pass.
180-
if sess.instrument_coverage() {
181-
passes.push("instrprof".to_owned());
182-
}
183178
passes
184179
},
185180
vec![]
@@ -193,6 +188,7 @@ impl ModuleConfig {
193188
SwitchWithOptPath::Disabled
194189
),
195190
pgo_use: if_regular!(sess.opts.cg.profile_use.clone(), None),
191+
instrument_coverage: if_regular!(sess.instrument_coverage(), false),
196192

197193
sanitizer: if_regular!(sess.opts.debugging_opts.sanitizer, SanitizerSet::empty()),
198194
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/InstrProfiling.h"
3536
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
3637
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
3738
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
@@ -744,7 +745,7 @@ LLVMRustOptimizeWithNewPassManager(
744745
bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize,
745746
bool DisableSimplifyLibCalls, bool EmitLifetimeMarkers,
746747
LLVMRustSanitizerOptions *SanitizerOptions,
747-
const char *PGOGenPath, const char *PGOUsePath,
748+
const char *PGOGenPath, const char *PGOUsePath, bool InstrumentCoverage,
748749
void* LlvmSelfProfiler,
749750
LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
750751
LLVMRustSelfProfileAfterPassCallback AfterPassCallback) {
@@ -834,6 +835,15 @@ LLVMRustOptimizeWithNewPassManager(
834835
);
835836
}
836837

838+
if (InstrumentCoverage) {
839+
PipelineStartEPCallbacks.push_back(
840+
[](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
841+
InstrProfOptions Options;
842+
MPM.addPass(InstrProfiling(Options, false));
843+
}
844+
);
845+
}
846+
837847
if (SanitizerOptions) {
838848
if (SanitizerOptions->SanitizeMemory) {
839849
MemorySanitizerOptions Options(

0 commit comments

Comments
 (0)