Index: lib/Passes/PassBuilder.cpp =================================================================== --- lib/Passes/PassBuilder.cpp +++ lib/Passes/PassBuilder.cpp @@ -172,6 +172,11 @@ "enable-npm-gvn-sink", cl::init(false), cl::Hidden, cl::desc("Enable the GVN hoisting pass for the new PM (default = off)")); +static cl::opt PGOOptString( + "pgo-opt", cl::init(""), cl::Hidden, + cl::desc("PGOOpt string in the format of " + "{gen|use|sample_gen|sample_use}=value")); + static Regex DefaultAliasRegex( "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$"); @@ -1101,6 +1106,34 @@ return false; } +/// Use user specifiy -pgo-opt option to override PGOOpt +static void processPGOOpt(Optional &PGOOpt) { + if (PGOOptString.empty()) + return; + auto pos = PGOOptString.find('='); + if (pos == std::string::npos) { + report_fatal_error("Invalid -pgo-opt."); + return; + } + PGOOptions pgoopt; + std::string command = PGOOptString.substr(0, pos); + if (command == "gen") { + pgoopt.ProfileGenFile = PGOOptString.substr(pos + 1); + pgoopt.RunProfileGen = true; + } else if (command == "use") { + pgoopt.ProfileUseFile = PGOOptString.substr(pos + 1); + } else if (command == "sample-gen") { + // FIXME: add -fdebug-info-for-profiling logic + ; + } else if (command == "sample-use") { + pgoopt.SampleProfileFile = PGOOptString.substr(pos + 1); + } else { + report_fatal_error("Invalid -pgo-opt."); + return; + } + PGOOpt = pgoopt; +} + template static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) { // Manually handle aliases for pre-configured pipeline fragments. @@ -1668,6 +1701,8 @@ if (!Pipeline || Pipeline->empty()) return false; + processPGOOpt(PGOOpt); + // If the first name isn't at the module layer, wrap the pipeline up // automatically. StringRef FirstName = Pipeline->front().Name; Index: test/Other/Inputs/new-pm-pgo.prof =================================================================== --- /dev/null +++ test/Other/Inputs/new-pm-pgo.prof @@ -0,0 +1 @@ +foo:0:0 Index: test/Other/Inputs/new-pm-pgo.proftext =================================================================== --- /dev/null +++ test/Other/Inputs/new-pm-pgo.proftext @@ -0,0 +1 @@ +:ir Index: test/Other/new-pm-pgo.ll =================================================================== --- /dev/null +++ test/Other/new-pm-pgo.ll @@ -0,0 +1,12 @@ +; RUN: opt -debug-pass-manager -passes='default' -pgo-opt='gen=temp' %s 2>&1 |FileCheck %s --check-prefixes=GEN +; RUN: llvm-profdata merge %S/Inputs/new-pm-pgo.proftext -o %t.profdata +; RUN: opt -debug-pass-manager -passes='default' -pgo-opt='use=%t.profdata' %s 2>&1 |FileCheck %s --check-prefixes=USE +; RUN: opt -debug-pass-manager -passes='default' -pgo-opt='sample-use=%S/Inputs/new-pm-pgo.prof' %s 2>&1 |FileCheck %s --check-prefixes=SAMPLE_USE +; +; GEN: Running pass: PGOInstrumentationGen +; USE: Running pass: PGOInstrumentationUse +; SAMPLE_USE: Running pass: SampleProfileLoaderPass + +define void @foo() { + ret void +}