How to use PGO with Clang

I am working on apply PGO to my project with LLJIT. I read the documents and understand that we need to invoke -fprofile-instr-generate to inject the profiling code and then run the code to get the profiling data, so that we can compile the code again to leverage the profiling data.

The compilation is done in runtime by the code written in C++. I believe we can do this simply by setting up the profile instrumentation option by codeGenOpts.setProfileInstr(clang::CodeGenOptions::ProfileClangInstr);. However, with this set, I can’t find the profiling result after running the function. I also tried to do this in the IR level with codeGenOpts.setProfileInstr(clang::CodeGenOptions::ProfileIRInstr);, but it shows JIT session error: Symbols not found: [ __llvm_profile_instrument_target ].

I believe this is an error caused by not linking the instrumenting code with the input code, but I think this should be done by Clang CompilerInstance since I can’t find any useful attributes in the source code.

Could anyone point out what I am missing? Thank you so much in advance!