Skip to content

Commit d27a210

Browse files
fanju110ict-qltblah
authored
Add IR Profile-Guided Optimization (IR PGO) support to the Flang compiler (#136098)
This patch implements IR-based Profile-Guided Optimization support in Flang through the following flags: - `-fprofile-generate` for instrumentation-based profile generation - `-fprofile-use=<dir>/file` for profile-guided optimization Resolves #74216 (implements IR PGO support phase) **Key changes:** - Frontend flag handling aligned with Clang/GCC semantics - Instrumentation hooks into LLVM PGO infrastructure - LIT tests verifying: - Instrumentation metadata generation - Profile loading from specified path - Branch weight attribution (IR checks) **Tests:** - Added gcc-flag-compatibility.f90 test module verifying: - Flag parsing boundary conditions - IR-level profile annotation consistency - Profile input path normalization rules - SPEC2006 benchmark results will be shared in comments For details on LLVM's PGO framework, refer to [Clang PGO Documentation](https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization). This implementation was developed by [XSCC Compiler Team](https://github.com/orgs/OpenXiangShan/teams/xscc). --------- Co-authored-by: ict-ql <168183727+ict-ql@users.noreply.github.com> Co-authored-by: Tom Eccles <t@freedommail.info>
1 parent 4d650ef commit d27a210

File tree

21 files changed

+236
-58
lines changed

21 files changed

+236
-58
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,35 +518,41 @@ class CodeGenOptions : public CodeGenOptionsBase {
518518

519519
/// Check if Clang profile instrumenation is on.
520520
bool hasProfileClangInstr() const {
521-
return getProfileInstr() == ProfileClangInstr;
521+
return getProfileInstr() ==
522+
llvm::driver::ProfileInstrKind::ProfileClangInstr;
522523
}
523524

524525
/// Check if IR level profile instrumentation is on.
525526
bool hasProfileIRInstr() const {
526-
return getProfileInstr() == ProfileIRInstr;
527+
return getProfileInstr() == llvm::driver::ProfileInstrKind::ProfileIRInstr;
527528
}
528529

529530
/// Check if CS IR level profile instrumentation is on.
530531
bool hasProfileCSIRInstr() const {
531-
return getProfileInstr() == ProfileCSIRInstr;
532+
return getProfileInstr() ==
533+
llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
532534
}
533535

534536
/// Check if any form of instrumentation is on.
535-
bool hasProfileInstr() const { return getProfileInstr() != ProfileNone; }
537+
bool hasProfileInstr() const {
538+
return getProfileInstr() != llvm::driver::ProfileInstrKind::ProfileNone;
539+
}
536540

537541
/// Check if Clang profile use is on.
538542
bool hasProfileClangUse() const {
539-
return getProfileUse() == ProfileClangInstr;
543+
return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileClangInstr;
540544
}
541545

542546
/// Check if IR level profile use is on.
543547
bool hasProfileIRUse() const {
544-
return getProfileUse() == ProfileIRInstr ||
545-
getProfileUse() == ProfileCSIRInstr;
548+
return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileIRInstr ||
549+
getProfileUse() == llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
546550
}
547551

548552
/// Check if CSIR profile use is on.
549-
bool hasProfileCSIRUse() const { return getProfileUse() == ProfileCSIRInstr; }
553+
bool hasProfileCSIRUse() const {
554+
return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
555+
}
550556

551557
/// Check if type and variable info should be emitted.
552558
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: 2 additions & 2 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">,

clang/lib/Basic/ProfileList.cpp

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

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

73-
static StringRef getSectionName(CodeGenOptions::ProfileInstrKind Kind) {
73+
static StringRef getSectionName(llvm::driver::ProfileInstrKind Kind) {
7474
switch (Kind) {
75-
case CodeGenOptions::ProfileNone:
75+
case llvm::driver::ProfileInstrKind::ProfileNone:
7676
return "";
77-
case CodeGenOptions::ProfileClangInstr:
77+
case llvm::driver::ProfileInstrKind::ProfileClangInstr:
7878
return "clang";
79-
case CodeGenOptions::ProfileIRInstr:
79+
case llvm::driver::ProfileInstrKind::ProfileIRInstr:
8080
return "llvm";
81-
case CodeGenOptions::ProfileCSIRInstr:
81+
case llvm::driver::ProfileInstrKind::ProfileCSIRInstr:
8282
return "csllvm";
8383
case CodeGenOptions::ProfileIRSampleColdCov:
8484
return "sample-coldcov";
8585
}
86-
llvm_unreachable("Unhandled CodeGenOptions::ProfileInstrKind enum");
86+
llvm_unreachable("Unhandled llvm::driver::ProfileInstrKind enum");
8787
}
8888

8989
ProfileList::ExclusionType
90-
ProfileList::getDefault(CodeGenOptions::ProfileInstrKind Kind) const {
90+
ProfileList::getDefault(llvm::driver::ProfileInstrKind Kind) const {
9191
StringRef Section = getSectionName(Kind);
9292
// Check for "default:<type>"
9393
if (SCL->inSection(Section, "default", "allow"))
@@ -118,7 +118,7 @@ ProfileList::inSection(StringRef Section, StringRef Prefix,
118118

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

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

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

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,10 @@ namespace clang {
123123
extern llvm::cl::opt<bool> ClSanitizeGuardChecks;
124124
}
125125

126-
// Default filename used for profile generation.
127-
static std::string getDefaultProfileGenName() {
128-
return DebugInfoCorrelate || ProfileCorrelate != InstrProfCorrelator::NONE
129-
? "default_%m.proflite"
130-
: "default_%m.profraw";
131-
}
132-
133126
// Path and name of file used for profile generation
134127
static std::string getProfileGenName(const CodeGenOptions &CodeGenOpts) {
135128
std::string FileName = CodeGenOpts.InstrProfileOutput.empty()
136-
? getDefaultProfileGenName()
129+
? llvm::driver::getDefaultProfileGenName()
137130
: CodeGenOpts.InstrProfileOutput;
138131
if (CodeGenOpts.ContinuousProfileSync)
139132
FileName = "%c" + FileName;
@@ -835,44 +828,45 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
835828

836829
if (CodeGenOpts.hasProfileIRInstr())
837830
// -fprofile-generate.
838-
PGOOpt = PGOOptions(getProfileGenName(CodeGenOpts), "", "",
839-
CodeGenOpts.MemoryProfileUsePath, nullptr,
840-
PGOOptions::IRInstr, PGOOptions::NoCSAction,
841-
ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling,
842-
/*PseudoProbeForProfiling=*/false,
843-
CodeGenOpts.AtomicProfileUpdate);
831+
PGOOpt = PGOOptions(
832+
getProfileGenName(CodeGenOpts), "", "",
833+
CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
834+
PGOOptions::NoCSAction, llvm::ClPGOColdFuncAttr,
835+
CodeGenOpts.DebugInfoForProfiling,
836+
/*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
844837
else if (CodeGenOpts.hasProfileIRUse()) {
845838
// -fprofile-use.
846839
auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
847840
: PGOOptions::NoCSAction;
848841
PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
849842
CodeGenOpts.ProfileRemappingFile,
850843
CodeGenOpts.MemoryProfileUsePath, VFS,
851-
PGOOptions::IRUse, CSAction, ClPGOColdFuncAttr,
844+
PGOOptions::IRUse, CSAction, llvm::ClPGOColdFuncAttr,
852845
CodeGenOpts.DebugInfoForProfiling);
853846
} else if (!CodeGenOpts.SampleProfileFile.empty())
854847
// -fprofile-sample-use
855848
PGOOpt = PGOOptions(
856849
CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
857850
CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
858-
PGOOptions::NoCSAction, ClPGOColdFuncAttr,
851+
PGOOptions::NoCSAction, llvm::ClPGOColdFuncAttr,
859852
CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling);
860853
else if (!CodeGenOpts.MemoryProfileUsePath.empty())
861854
// -fmemory-profile-use (without any of the above options)
862-
PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
863-
PGOOptions::NoAction, PGOOptions::NoCSAction,
864-
ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
865-
else if (CodeGenOpts.PseudoProbeForProfiling)
866-
// -fpseudo-probe-for-profiling
867855
PGOOpt =
868-
PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
856+
PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
869857
PGOOptions::NoAction, PGOOptions::NoCSAction,
870-
ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling, true);
858+
llvm::ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
859+
else if (CodeGenOpts.PseudoProbeForProfiling)
860+
// -fpseudo-probe-for-profiling
861+
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
862+
PGOOptions::NoAction, PGOOptions::NoCSAction,
863+
llvm::ClPGOColdFuncAttr,
864+
CodeGenOpts.DebugInfoForProfiling, true);
871865
else if (CodeGenOpts.DebugInfoForProfiling)
872866
// -fdebug-info-for-profiling
873867
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
874868
PGOOptions::NoAction, PGOOptions::NoCSAction,
875-
ClPGOColdFuncAttr, true);
869+
llvm::ClPGOColdFuncAttr, true);
876870

877871
// Check to see if we want to generate a CS profile.
878872
if (CodeGenOpts.hasProfileCSIRInstr()) {

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
@@ -940,7 +940,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
940940
}
941941
}
942942

943-
if (CGM.getCodeGenOpts().getProfileInstr() != CodeGenOptions::ProfileNone) {
943+
if (CGM.getCodeGenOpts().getProfileInstr() !=
944+
llvm::driver::ProfileInstrKind::ProfileNone) {
944945
switch (CGM.isFunctionBlockedFromProfileInstr(Fn, Loc)) {
945946
case ProfileList::Skip:
946947
Fn->addFnAttr(llvm::Attribute::SkipProfile);

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3601,7 +3601,7 @@ CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
36013601
// If the profile list is empty, then instrument everything.
36023602
if (ProfileList.isEmpty())
36033603
return ProfileList::Allow;
3604-
CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
3604+
llvm::driver::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
36053605
// First, check the function name.
36063606
if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
36073607
return *V;

clang/lib/Driver/ToolChains/Flang.cpp

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

886+
// recognise options: fprofile-generate -fprofile-use=
887+
Args.addAllArgs(
888+
CmdArgs, {options::OPT_fprofile_generate, options::OPT_fprofile_use_EQ});
889+
886890
// Forward flags for OpenMP. We don't do this if the current action is an
887891
// device offloading action other than OpenMP.
888892
if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,

0 commit comments

Comments
 (0)