Skip to content

Commit 52d3486

Browse files
fanju110ict-qlliliumShadetarunprabhu
authored
Fix and reapply IR PGO support for Flang (#142892)
This PR resubmits the changes from #136098, which was previously reverted due to a build failure during the linking stage: ``` undefined reference to `llvm::DebugInfoCorrelate' undefined reference to `llvm::ProfileCorrelate' ``` The root cause was that `llvm/lib/Frontend/Driver/CodeGenOptions.cpp` references symbols from the `Instrumentation` component, but the `LINK_COMPONENTS` in the `llvm/lib/Frontend/CMakeLists.txt` for `LLVMFrontendDriver` did not include it. As a result, linking failed in configurations where these components were not transitively linked. ### Fix: This updated patch explicitly adds `Instrumentation` to `LINK_COMPONENTS` in the relevant `llvm/lib/Frontend/CMakeLists.txt` file to ensure the required symbols are properly resolved. --------- Co-authored-by: ict-ql <[email protected]> Co-authored-by: Chyaka <[email protected]> Co-authored-by: Tarun Prabhu <[email protected]>
1 parent f82cf74 commit 52d3486

File tree

22 files changed

+223
-54
lines changed

22 files changed

+223
-54
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@ AFFECTING_VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is
223223
CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic
224224
CODEGENOPT(ContinuousProfileSync, 1, 0) ///< Enable continuous instrumentation profiling
225225
/// Choose profile instrumenation kind or no instrumentation.
226-
ENUM_CODEGENOPT(ProfileInstr, ProfileInstrKind, 4, ProfileNone)
226+
227+
ENUM_CODEGENOPT(ProfileInstr, llvm::driver::ProfileInstrKind, 4, llvm::driver::ProfileInstrKind::ProfileNone)
228+
227229
/// Choose profile kind for PGO use compilation.
228-
ENUM_CODEGENOPT(ProfileUse, ProfileInstrKind, 2, ProfileNone)
230+
ENUM_CODEGENOPT(ProfileUse, llvm::driver::ProfileInstrKind, 2, llvm::driver::ProfileInstrKind::ProfileNone)
229231
/// Partition functions into N groups and select only functions in group i to be
230232
/// instrumented. Selected group numbers can be 0 to N-1 inclusive.
231233
VALUE_CODEGENOPT(ProfileTotalFunctionGroups, 32, 1)

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@ class CodeGenOptions : public CodeGenOptionsBase {
8080
SRCK_InRegs // Small structs in registers (-freg-struct-return).
8181
};
8282

83-
enum ProfileInstrKind {
84-
ProfileNone, // Profile instrumentation is turned off.
85-
ProfileClangInstr, // Clang instrumentation to generate execution counts
86-
// to use with PGO.
87-
ProfileIRInstr, // IR level PGO instrumentation in LLVM.
88-
ProfileCSIRInstr, // IR level PGO context sensitive instrumentation in LLVM.
89-
ProfileIRSampleColdCov, // IR level sample pgo based cold function coverage
90-
// instrumentation in LLVM.
91-
};
92-
9383
enum EmbedBitcodeKind {
9484
Embed_Off, // No embedded bitcode.
9585
Embed_All, // Embed both bitcode and commandline in the output.
@@ -522,35 +512,41 @@ class CodeGenOptions : public CodeGenOptionsBase {
522512

523513
/// Check if Clang profile instrumenation is on.
524514
bool hasProfileClangInstr() const {
525-
return getProfileInstr() == ProfileClangInstr;
515+
return getProfileInstr() ==
516+
llvm::driver::ProfileInstrKind::ProfileClangInstr;
526517
}
527518

528519
/// Check if IR level profile instrumentation is on.
529520
bool hasProfileIRInstr() const {
530-
return getProfileInstr() == ProfileIRInstr;
521+
return getProfileInstr() == llvm::driver::ProfileInstrKind::ProfileIRInstr;
531522
}
532523

533524
/// Check if CS IR level profile instrumentation is on.
534525
bool hasProfileCSIRInstr() const {
535-
return getProfileInstr() == ProfileCSIRInstr;
526+
return getProfileInstr() ==
527+
llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
536528
}
537529

538530
/// Check if any form of instrumentation is on.
539-
bool hasProfileInstr() const { return getProfileInstr() != ProfileNone; }
531+
bool hasProfileInstr() const {
532+
return getProfileInstr() != llvm::driver::ProfileInstrKind::ProfileNone;
533+
}
540534

541535
/// Check if Clang profile use is on.
542536
bool hasProfileClangUse() const {
543-
return getProfileUse() == ProfileClangInstr;
537+
return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileClangInstr;
544538
}
545539

546540
/// Check if IR level profile use is on.
547541
bool hasProfileIRUse() const {
548-
return getProfileUse() == ProfileIRInstr ||
549-
getProfileUse() == ProfileCSIRInstr;
542+
return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileIRInstr ||
543+
getProfileUse() == llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
550544
}
551545

552546
/// Check if CSIR profile use is on.
553-
bool hasProfileCSIRUse() const { return getProfileUse() == ProfileCSIRInstr; }
547+
bool hasProfileCSIRUse() const {
548+
return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
549+
}
554550

555551
/// Check if type and variable info should be emitted.
556552
bool hasReducedDebugInfo() const {

clang/include/clang/Basic/ProfileList.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ class ProfileList {
4949
~ProfileList();
5050

5151
bool isEmpty() const { return Empty; }
52-
ExclusionType getDefault(CodeGenOptions::ProfileInstrKind Kind) const;
52+
ExclusionType getDefault(llvm::driver::ProfileInstrKind Kind) const;
5353

5454
std::optional<ExclusionType>
5555
isFunctionExcluded(StringRef FunctionName,
56-
CodeGenOptions::ProfileInstrKind Kind) const;
56+
llvm::driver::ProfileInstrKind Kind) const;
5757
std::optional<ExclusionType>
5858
isLocationExcluded(SourceLocation Loc,
59-
CodeGenOptions::ProfileInstrKind Kind) const;
59+
llvm::driver::ProfileInstrKind Kind) const;
6060
std::optional<ExclusionType>
61-
isFileExcluded(StringRef FileName,
62-
CodeGenOptions::ProfileInstrKind Kind) const;
61+
isFileExcluded(StringRef FileName, llvm::driver::ProfileInstrKind Kind) const;
6362
};
6463

6564
} // namespace clang

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ def fmcdc_max_test_vectors_EQ : Joined<["-"], "fmcdc-max-test-vectors=">,
17721772
HelpText<"Maximum number of test vectors in MC/DC coverage">,
17731773
MarshallingInfoInt<CodeGenOpts<"MCDCMaxTVs">, "0x7FFFFFFE">;
17741774
def fprofile_generate : Flag<["-"], "fprofile-generate">,
1775-
Group<f_Group>, Visibility<[ClangOption, CLOption]>,
1775+
Group<f_Group>, Visibility<[ClangOption, CLOption, FlangOption, FC1Option]>,
17761776
HelpText<"Generate instrumented code to collect execution counts into default.profraw (overridden by LLVM_PROFILE_FILE env var)">;
17771777
def fprofile_generate_EQ : Joined<["-"], "fprofile-generate=">,
17781778
Group<f_Group>, Visibility<[ClangOption, CLOption]>,
@@ -1789,7 +1789,7 @@ def fprofile_use : Flag<["-"], "fprofile-use">, Group<f_Group>,
17891789
Visibility<[ClangOption, CLOption]>, Alias<fprofile_instr_use>;
17901790
def fprofile_use_EQ : Joined<["-"], "fprofile-use=">,
17911791
Group<f_Group>,
1792-
Visibility<[ClangOption, CLOption]>,
1792+
Visibility<[ClangOption, CLOption, FlangOption, FC1Option]>,
17931793
MetaVarName<"<pathname>">,
17941794
HelpText<"Use instrumentation data for profile-guided optimization. If pathname is a directory, it reads from <pathname>/default.profdata. Otherwise, it reads from file <pathname>.">;
17951795
def fno_profile_instr_generate : Flag<["-"], "fno-profile-instr-generate">,
@@ -7761,7 +7761,7 @@ def fpatchable_function_entry_section_EQ
77617761
MarshallingInfoString<CodeGenOpts<"PatchableFunctionEntrySection">>;
77627762
def fprofile_instrument_EQ : Joined<["-"], "fprofile-instrument=">,
77637763
HelpText<"Enable PGO instrumentation">, Values<"none,clang,llvm,csllvm,sample-coldcov">,
7764-
NormalizedValuesScope<"CodeGenOptions">,
7764+
NormalizedValuesScope<"llvm::driver::ProfileInstrKind">,
77657765
NormalizedValues<["ProfileNone", "ProfileClangInstr", "ProfileIRInstr", "ProfileCSIRInstr", "ProfileIRSampleColdCov"]>,
77667766
MarshallingInfoEnum<CodeGenOpts<"ProfileInstr">, "ProfileNone">;
77677767
def fprofile_instrument_path_EQ : Joined<["-"], "fprofile-instrument-path=">,

clang/lib/Basic/ProfileList.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,24 @@ ProfileList::ProfileList(ArrayRef<std::string> Paths, SourceManager &SM)
6969

7070
ProfileList::~ProfileList() = default;
7171

72-
static StringRef getSectionName(CodeGenOptions::ProfileInstrKind Kind) {
72+
static StringRef getSectionName(llvm::driver::ProfileInstrKind Kind) {
7373
switch (Kind) {
74-
case CodeGenOptions::ProfileNone:
74+
case llvm::driver::ProfileInstrKind::ProfileNone:
7575
return "";
76-
case CodeGenOptions::ProfileClangInstr:
76+
case llvm::driver::ProfileInstrKind::ProfileClangInstr:
7777
return "clang";
78-
case CodeGenOptions::ProfileIRInstr:
78+
case llvm::driver::ProfileInstrKind::ProfileIRInstr:
7979
return "llvm";
80-
case CodeGenOptions::ProfileCSIRInstr:
80+
case llvm::driver::ProfileInstrKind::ProfileCSIRInstr:
8181
return "csllvm";
82-
case CodeGenOptions::ProfileIRSampleColdCov:
82+
case llvm::driver::ProfileInstrKind::ProfileIRSampleColdCov:
8383
return "sample-coldcov";
8484
}
85-
llvm_unreachable("Unhandled CodeGenOptions::ProfileInstrKind enum");
85+
llvm_unreachable("Unhandled llvm::driver::ProfileInstrKind enum");
8686
}
8787

8888
ProfileList::ExclusionType
89-
ProfileList::getDefault(CodeGenOptions::ProfileInstrKind Kind) const {
89+
ProfileList::getDefault(llvm::driver::ProfileInstrKind Kind) const {
9090
StringRef Section = getSectionName(Kind);
9191
// Check for "default:<type>"
9292
if (SCL->inSection(Section, "default", "allow"))
@@ -117,7 +117,7 @@ ProfileList::inSection(StringRef Section, StringRef Prefix,
117117

118118
std::optional<ProfileList::ExclusionType>
119119
ProfileList::isFunctionExcluded(StringRef FunctionName,
120-
CodeGenOptions::ProfileInstrKind Kind) const {
120+
llvm::driver::ProfileInstrKind Kind) const {
121121
StringRef Section = getSectionName(Kind);
122122
// Check for "function:<regex>=<case>"
123123
if (auto V = inSection(Section, "function", FunctionName))
@@ -131,13 +131,13 @@ ProfileList::isFunctionExcluded(StringRef FunctionName,
131131

132132
std::optional<ProfileList::ExclusionType>
133133
ProfileList::isLocationExcluded(SourceLocation Loc,
134-
CodeGenOptions::ProfileInstrKind Kind) const {
134+
llvm::driver::ProfileInstrKind Kind) const {
135135
return isFileExcluded(SM.getFilename(SM.getFileLoc(Loc)), Kind);
136136
}
137137

138138
std::optional<ProfileList::ExclusionType>
139139
ProfileList::isFileExcluded(StringRef FileName,
140-
CodeGenOptions::ProfileInstrKind Kind) const {
140+
llvm::driver::ProfileInstrKind Kind) const {
141141
StringRef Section = getSectionName(Kind);
142142
// Check for "source:<regex>=<case>"
143143
if (auto V = inSection(Section, "source", FileName))

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,10 @@ namespace clang {
124124
extern llvm::cl::opt<bool> ClSanitizeGuardChecks;
125125
}
126126

127-
// Default filename used for profile generation.
128-
static std::string getDefaultProfileGenName() {
129-
return DebugInfoCorrelate || ProfileCorrelate != InstrProfCorrelator::NONE
130-
? "default_%m.proflite"
131-
: "default_%m.profraw";
132-
}
133-
134127
// Path and name of file used for profile generation
135128
static std::string getProfileGenName(const CodeGenOptions &CodeGenOpts) {
136129
std::string FileName = CodeGenOpts.InstrProfileOutput.empty()
137-
? getDefaultProfileGenName()
130+
? llvm::driver::getDefaultProfileGenName()
138131
: CodeGenOpts.InstrProfileOutput;
139132
if (CodeGenOpts.ContinuousProfileSync)
140133
FileName = "%c" + FileName;

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {
273273
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
274274
std::move(*OptRecordFileOrErr);
275275

276-
if (OptRecordFile &&
277-
CodeGenOpts.getProfileUse() != CodeGenOptions::ProfileNone)
276+
if (OptRecordFile && CodeGenOpts.getProfileUse() !=
277+
llvm::driver::ProfileInstrKind::ProfileNone)
278278
Ctx.setDiagnosticsHotnessRequested(true);
279279

280280
if (CodeGenOpts.MisExpect) {

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
943943
}
944944
}
945945

946-
if (CGM.getCodeGenOpts().getProfileInstr() != CodeGenOptions::ProfileNone) {
946+
if (CGM.getCodeGenOpts().getProfileInstr() !=
947+
llvm::driver::ProfileInstrKind::ProfileNone) {
947948
switch (CGM.isFunctionBlockedFromProfileInstr(Fn, Loc)) {
948949
case ProfileList::Skip:
949950
Fn->addFnAttr(llvm::Attribute::SkipProfile);

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3608,7 +3608,7 @@ CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
36083608
// If the profile list is empty, then instrument everything.
36093609
if (ProfileList.isEmpty())
36103610
return ProfileList::Allow;
3611-
CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
3611+
llvm::driver::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
36123612
// First, check the function name.
36133613
if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
36143614
return *V;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,10 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
887887
// TODO: Handle interactions between -w, -pedantic, -Wall, -WOption
888888
Args.AddLastArg(CmdArgs, options::OPT_w);
889889

890+
// recognise options: fprofile-generate -fprofile-use=
891+
Args.addAllArgs(
892+
CmdArgs, {options::OPT_fprofile_generate, options::OPT_fprofile_use_EQ});
893+
890894
// Forward flags for OpenMP. We don't do this if the current action is an
891895
// device offloading action other than OpenMP.
892896
if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,11 +1492,11 @@ static void setPGOUseInstrumentor(CodeGenOptions &Opts,
14921492
// which is available (might be one or both).
14931493
if (PGOReader->isIRLevelProfile() || PGOReader->hasMemoryProfile()) {
14941494
if (PGOReader->hasCSIRLevelProfile())
1495-
Opts.setProfileUse(CodeGenOptions::ProfileCSIRInstr);
1495+
Opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileCSIRInstr);
14961496
else
1497-
Opts.setProfileUse(CodeGenOptions::ProfileIRInstr);
1497+
Opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileIRInstr);
14981498
} else
1499-
Opts.setProfileUse(CodeGenOptions::ProfileClangInstr);
1499+
Opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileClangInstr);
15001500
}
15011501

15021502
void CompilerInvocation::setDefaultPointerAuthOptions(

flang/include/flang/Frontend/CodeGenOptions.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
2424
CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
2525
///< pass manager.
2626

27+
28+
/// Choose profile instrumenation kind or no instrumentation.
29+
ENUM_CODEGENOPT(ProfileInstr, llvm::driver::ProfileInstrKind, 2, llvm::driver::ProfileInstrKind::ProfileNone)
30+
/// Choose profile kind for PGO use compilation.
31+
ENUM_CODEGENOPT(ProfileUse, llvm::driver::ProfileInstrKind, 2, llvm::driver::ProfileInstrKind::ProfileNone)
32+
2733
CODEGENOPT(InstrumentFunctions, 1, 0) ///< Set when -finstrument_functions is
2834
///< enabled on the compile step.
35+
2936
CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level.
3037
CODEGENOPT(PICLevel, 2, 0) ///< PIC level of the LLVM module.
3138
CODEGENOPT(PrepareForFullLTO , 1, 0) ///< Set when -flto is enabled on the

flang/include/flang/Frontend/CodeGenOptions.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,44 @@ class CodeGenOptions : public CodeGenOptionsBase {
154154
/// OpenMP is enabled.
155155
using DoConcurrentMappingKind = flangomp::DoConcurrentMappingKind;
156156

157+
/// Name of the profile file to use as output for -fprofile-instr-generate,
158+
/// -fprofile-generate, and -fcs-profile-generate.
159+
std::string InstrProfileOutput;
160+
161+
/// Name of the profile file to use as input for -fmemory-profile-use.
162+
std::string MemoryProfileUsePath;
163+
164+
/// Name of the profile file to use as input for -fprofile-instr-use
165+
std::string ProfileInstrumentUsePath;
166+
167+
/// Name of the profile remapping file to apply to the profile data supplied
168+
/// by -fprofile-sample-use or -fprofile-instr-use.
169+
std::string ProfileRemappingFile;
170+
171+
/// Check if Clang profile instrumenation is on.
172+
bool hasProfileClangInstr() const {
173+
return getProfileInstr() == llvm::driver::ProfileClangInstr;
174+
}
175+
176+
/// Check if IR level profile instrumentation is on.
177+
bool hasProfileIRInstr() const {
178+
return getProfileInstr() == llvm::driver::ProfileIRInstr;
179+
}
180+
181+
/// Check if CS IR level profile instrumentation is on.
182+
bool hasProfileCSIRInstr() const {
183+
return getProfileInstr() == llvm::driver::ProfileCSIRInstr;
184+
}
185+
/// Check if IR level profile use is on.
186+
bool hasProfileIRUse() const {
187+
return getProfileUse() == llvm::driver::ProfileIRInstr ||
188+
getProfileUse() == llvm::driver::ProfileCSIRInstr;
189+
}
190+
/// Check if CSIR profile use is on.
191+
bool hasProfileCSIRUse() const {
192+
return getProfileUse() == llvm::driver::ProfileCSIRInstr;
193+
}
194+
157195
// Define accessors/mutators for code generation options of enumeration type.
158196
#define CODEGENOPT(Name, Bits, Default)
159197
#define ENUM_CODEGENOPT(Name, Type, Bits, Default) \

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/ADT/StringRef.h"
3030
#include "llvm/ADT/StringSwitch.h"
3131
#include "llvm/Frontend/Debug/Options.h"
32+
#include "llvm/Frontend/Driver/CodeGenOptions.h"
3233
#include "llvm/Option/Arg.h"
3334
#include "llvm/Option/ArgList.h"
3435
#include "llvm/Option/OptTable.h"
@@ -441,6 +442,15 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
441442
opts.IsPIE = 1;
442443
}
443444

445+
if (args.hasArg(clang::driver::options::OPT_fprofile_generate)) {
446+
opts.setProfileInstr(llvm::driver::ProfileInstrKind::ProfileIRInstr);
447+
}
448+
449+
if (auto A = args.getLastArg(clang::driver::options::OPT_fprofile_use_EQ)) {
450+
opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileIRInstr);
451+
opts.ProfileInstrumentUsePath = A->getValue();
452+
}
453+
444454
// -mcmodel option.
445455
if (const llvm::opt::Arg *a =
446456
args.getLastArg(clang::driver::options::OPT_mcmodel_EQ)) {

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,20 @@
5656
#include "llvm/Passes/PassBuilder.h"
5757
#include "llvm/Passes/PassPlugin.h"
5858
#include "llvm/Passes/StandardInstrumentations.h"
59+
#include "llvm/ProfileData/InstrProfCorrelator.h"
5960
#include "llvm/Support/AMDGPUAddrSpace.h"
6061
#include "llvm/Support/Error.h"
6162
#include "llvm/Support/ErrorHandling.h"
6263
#include "llvm/Support/FileSystem.h"
64+
#include "llvm/Support/PGOOptions.h"
6365
#include "llvm/Support/Path.h"
6466
#include "llvm/Support/SourceMgr.h"
6567
#include "llvm/Support/ToolOutputFile.h"
6668
#include "llvm/Target/TargetMachine.h"
6769
#include "llvm/TargetParser/RISCVISAInfo.h"
6870
#include "llvm/TargetParser/RISCVTargetParser.h"
6971
#include "llvm/Transforms/IPO/Internalize.h"
72+
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
7073
#include "llvm/Transforms/Utils/ModuleUtils.h"
7174
#include <memory>
7275
#include <system_error>
@@ -919,6 +922,29 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
919922
llvm::PassInstrumentationCallbacks pic;
920923
llvm::PipelineTuningOptions pto;
921924
std::optional<llvm::PGOOptions> pgoOpt;
925+
926+
if (opts.hasProfileIRInstr()) {
927+
// -fprofile-generate.
928+
pgoOpt = llvm::PGOOptions(opts.InstrProfileOutput.empty()
929+
? llvm::driver::getDefaultProfileGenName()
930+
: opts.InstrProfileOutput,
931+
"", "", opts.MemoryProfileUsePath, nullptr,
932+
llvm::PGOOptions::IRInstr,
933+
llvm::PGOOptions::NoCSAction,
934+
llvm::PGOOptions::ColdFuncOpt::Default, false,
935+
/*PseudoProbeForProfiling=*/false, false);
936+
} else if (opts.hasProfileIRUse()) {
937+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
938+
llvm::vfs::getRealFileSystem();
939+
// -fprofile-use.
940+
auto CSAction = opts.hasProfileCSIRUse() ? llvm::PGOOptions::CSIRUse
941+
: llvm::PGOOptions::NoCSAction;
942+
pgoOpt = llvm::PGOOptions(
943+
opts.ProfileInstrumentUsePath, "", opts.ProfileRemappingFile,
944+
opts.MemoryProfileUsePath, VFS, llvm::PGOOptions::IRUse, CSAction,
945+
llvm::PGOOptions::ColdFuncOpt::Default, false);
946+
}
947+
922948
llvm::StandardInstrumentations si(llvmModule->getContext(),
923949
opts.DebugPassManager);
924950
si.registerCallbacks(pic, &mam);

0 commit comments

Comments
 (0)