[clang] [llvm] [InstrPGO] Instrument sampling profile based cold function (PR #109837)

Lei Wang via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 21 11:33:26 PDT 2024


================
@@ -649,6 +649,24 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
     }
   }
 
+  if (auto *ColdFuncCoverageArg = Args.getLastArg(
+          options::OPT_fprofile_generate_cold_function_coverage,
+          options::OPT_fprofile_generate_cold_function_coverage_EQ)) {
+    SmallString<128> Path(
+        ColdFuncCoverageArg->getOption().matches(
+            options::OPT_fprofile_generate_cold_function_coverage_EQ)
+            ? ColdFuncCoverageArg->getValue()
+            : "");
+    llvm::sys::path::append(Path, "default_%m.profraw");
+    CmdArgs.push_back("-mllvm");
+    CmdArgs.push_back(Args.MakeArgString(
+        Twine("--instrument-cold-function-coverage-path=") + Path));
+    CmdArgs.push_back("-mllvm");
+    CmdArgs.push_back("--instrument-cold-function-coverage");
----------------
wlei-llvm wrote:

I really like this idea, however, this seems indeed not easy(needs a lot of refactoring). 

As you said, the `InstrProfileOutput` filed is shared with other PGO flag(in our case, it's the `fprofile-sample-use=`) , see [`PGOOptions.ProfileFIle`](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/PGOOptions.h#L43). One thing we can do is to split this field into two: `ProfileUseFile` and `ProfileGenFole`, let all `profile-<instr>-gererate` go to  `ProfileGenFile` and let `sample-use/instr-use go` to `ProfileUseFile`. I think that's doable and not a big change(maybe a separate diff). Then in sample-use case, we can use the  `ProfileGenFIle` path for cold coverage work. WDYT?

Though it would still not be perfect, the logic here https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/BackendUtil.cpp#L811-L841  is complicated/ would still need more refactoring. I was stuck here when thinking if `-fprofile-sample-use` and `--fprofile-instrument-path=` are both used, which branch(`CodeGenOpts.hasProfileIRInstr()` or `!CodeGenOpts.SampleProfileFile.empty()`) should it belongs to.

For this version, I just removed the duplicated flag(`--pgo-instrument-cold-function-only`) and added a FIXME comment to explain this. 




https://github.com/llvm/llvm-project/pull/109837


More information about the cfe-commits mailing list