Skip to content

Commit 324ca7a

Browse files
committed
rustc_llvm: rustc_trans: Thread the PGO config down to the pass manager builder.
Signed-off-by: Emilio Cobos Álvarez <emilio@crisal.io>
1 parent 50a3872 commit 324ca7a

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/librustc_llvm/ffi.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,9 @@ extern "C" {
16411641
OptLevel: CodeGenOptLevel,
16421642
MergeFunctions: bool,
16431643
SLPVectorize: bool,
1644-
LoopVectorize: bool);
1644+
LoopVectorize: bool,
1645+
PGOGenPath: *const c_char,
1646+
PGOUsePath: *const c_char);
16451647
pub fn LLVMRustAddLibraryInfo(PM: PassManagerRef,
16461648
M: ModuleRef,
16471649
DisableSimplifyLibCalls: bool);

src/librustc_trans/back/write.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ pub struct ModuleConfig {
240240
/// Some(level) to optimize binary size, or None to not affect program size.
241241
opt_size: Option<llvm::CodeGenOptSize>,
242242

243+
pgo_gen: Option<String>,
244+
pgo_use: String,
245+
243246
// Flags indicating which outputs to produce.
244247
emit_no_opt_bc: bool,
245248
emit_bc: bool,
@@ -274,6 +277,9 @@ impl ModuleConfig {
274277
opt_level: None,
275278
opt_size: None,
276279

280+
pgo_gen: None,
281+
pgo_use: String::new(),
282+
277283
emit_no_opt_bc: false,
278284
emit_bc: false,
279285
emit_bc_compressed: false,
@@ -932,6 +938,9 @@ pub fn start_async_translation(tcx: TyCtxt,
932938
modules_config.passes.push("insert-gcov-profiling".to_owned())
933939
}
934940

941+
modules_config.pgo_gen = sess.opts.cg.pgo_gen.clone();
942+
modules_config.pgo_use = sess.opts.cg.pgo_use.clone();
943+
935944
modules_config.opt_level = Some(get_llvm_opt_level(sess.opts.optimize));
936945
modules_config.opt_size = Some(get_llvm_opt_size(sess.opts.optimize));
937946

@@ -2046,18 +2055,36 @@ pub unsafe fn with_llvm_pmb(llmod: ModuleRef,
20462055
config: &ModuleConfig,
20472056
opt_level: llvm::CodeGenOptLevel,
20482057
f: &mut FnMut(llvm::PassManagerBuilderRef)) {
2058+
use std::ptr;
2059+
20492060
// Create the PassManagerBuilder for LLVM. We configure it with
20502061
// reasonable defaults and prepare it to actually populate the pass
20512062
// manager.
20522063
let builder = llvm::LLVMPassManagerBuilderCreate();
20532064
let opt_size = config.opt_size.unwrap_or(llvm::CodeGenOptSizeNone);
20542065
let inline_threshold = config.inline_threshold;
20552066

2056-
llvm::LLVMRustConfigurePassManagerBuilder(builder,
2057-
opt_level,
2058-
config.merge_functions,
2059-
config.vectorize_slp,
2060-
config.vectorize_loop);
2067+
let pgo_gen_path = config.pgo_gen.as_ref().map(|s| {
2068+
let s = if s.is_empty() { "default_%m.profraw" } else { s };
2069+
CString::new(s.as_bytes()).unwrap()
2070+
});
2071+
2072+
let pgo_use_path = if config.pgo_use.is_empty() {
2073+
None
2074+
} else {
2075+
Some(CString::new(config.pgo_use.as_bytes()).unwrap())
2076+
};
2077+
2078+
llvm::LLVMRustConfigurePassManagerBuilder(
2079+
builder,
2080+
opt_level,
2081+
config.merge_functions,
2082+
config.vectorize_slp,
2083+
config.vectorize_loop,
2084+
pgo_gen_path.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
2085+
pgo_use_path.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
2086+
);
2087+
20612088
llvm::LLVMPassManagerBuilderSetSizeLevel(builder, opt_size as u32);
20622089

20632090
if opt_size != llvm::CodeGenOptSizeNone {

src/rustllvm/PassWrapper.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,22 @@ extern "C" void LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
428428

429429
extern "C" void LLVMRustConfigurePassManagerBuilder(
430430
LLVMPassManagerBuilderRef PMBR, LLVMRustCodeGenOptLevel OptLevel,
431-
bool MergeFunctions, bool SLPVectorize, bool LoopVectorize) {
431+
bool MergeFunctions, bool SLPVectorize, bool LoopVectorize,
432+
const char* PGOGenPath, const char* PGOUsePath) {
432433
// Ignore mergefunc for now as enabling it causes crashes.
433434
// unwrap(PMBR)->MergeFunctions = MergeFunctions;
434435
unwrap(PMBR)->SLPVectorize = SLPVectorize;
435436
unwrap(PMBR)->OptLevel = fromRust(OptLevel);
436437
unwrap(PMBR)->LoopVectorize = LoopVectorize;
438+
if (PGOGenPath) {
439+
assert(!PGOUsePath);
440+
unwrap(PMBR)->EnablePGOInstrGen = true;
441+
unwrap(PMBR)->PGOInstrGen = PGOGenPath;
442+
}
443+
if (PGOUsePath) {
444+
assert(!PGOGenPath);
445+
unwrap(PMBR)->PGOInstrUse = PGOUsePath;
446+
}
437447
}
438448

439449
// Unfortunately, the LLVM C API doesn't provide a way to set the `LibraryInfo`

0 commit comments

Comments
 (0)