Skip to content

Commit 1350854

Browse files
committed
Add PGO support
1 parent 8d9fb48 commit 1350854

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/librustc_codegen_llvm/back/write.rs

+20
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,24 @@ pub(crate) unsafe fn optimize(
346346
|| cgcx.lto == Lto::ThinLocal
347347
|| (cgcx.lto != Lto::Fat && cgcx.opts.cg.linker_plugin_lto.enabled());
348348
let using_thin_buffers = prepare_for_thin_lto || config.bitcode_needed();
349+
350+
let pgo_gen_path = match config.pgo_gen {
351+
SwitchWithOptPath::Enabled(ref opt_dir_path) => {
352+
let path = if let Some(dir_path) = opt_dir_path {
353+
dir_path.join("default_%m.profraw")
354+
} else {
355+
PathBuf::from("default_%m.profraw")
356+
};
357+
358+
Some(CString::new(format!("{}", path.display())).unwrap())
359+
}
360+
SwitchWithOptPath::Disabled => None,
361+
};
362+
let pgo_use_path = config
363+
.pgo_use
364+
.as_ref()
365+
.map(|path_buf| CString::new(path_buf.to_string_lossy().as_bytes()).unwrap());
366+
349367
// FIXME: NewPM doesn't seem to have a facility to provide custom InlineParams.
350368
// FIXME: Extra passes.
351369
llvm::LLVMRustOptimizeWithNewPassManager(
@@ -367,6 +385,8 @@ pub(crate) unsafe fn optimize(
367385
Some(Sanitizer::Address) == config.sanitizer,
368386
config.sanitizer.as_ref().map_or(false, |s| config.sanitizer_recover.contains(s)),
369387
config.sanitizer_memory_track_origins as c_int,
388+
pgo_gen_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
389+
pgo_use_path.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()),
370390
);
371391
return Ok(());
372392
}

src/librustc_codegen_llvm/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,8 @@ extern "C" {
19241924
SanitizeAddress: bool,
19251925
SanitizeRecover: bool,
19261926
SanitizeMemoryTrackOrigins: c_int,
1927+
PGOGenPath: *const c_char,
1928+
PGOUsePath: *const c_char,
19271929
);
19281930
pub fn LLVMRustPrintModule(
19291931
M: &'a Module,

src/rustllvm/PassWrapper.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ LLVMRustOptimizeWithNewPassManager(
613613
bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize,
614614
bool DisableSimplifyLibCalls,
615615
bool SanitizeMemory, bool SanitizeThread, bool SanitizeAddress,
616-
bool SanitizeRecover, int SanitizeMemoryTrackOrigins) {
616+
bool SanitizeRecover, int SanitizeMemoryTrackOrigins,
617+
const char *PGOGenPath, const char *PGOUsePath) {
617618
Module *TheModule = unwrap(ModuleRef);
618619
TargetMachine *TM = unwrap(TMRef);
619620
PassBuilder::OptimizationLevel OptLevel = fromRust(OptLevelRust);
@@ -634,6 +635,14 @@ LLVMRustOptimizeWithNewPassManager(
634635

635636
// FIXME: PGOOpt
636637
Optional<PGOOptions> PGOOpt;
638+
if (PGOGenPath) {
639+
assert(!PGOUsePath);
640+
PGOOpt = PGOOptions(PGOGenPath, "", "", PGOOptions::IRInstr);
641+
} else if (PGOUsePath) {
642+
assert(!PGOGenPath);
643+
PGOOpt = PGOOptions(PGOUsePath, "", "", PGOOptions::IRUse);
644+
}
645+
637646
PassBuilder PB(TM, PTO, PGOOpt, &PIC);
638647

639648
bool DebugPassManager = false;

0 commit comments

Comments
 (0)