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 <168183727+ict-ql@users.noreply.github.com> Co-authored-by: Chyaka <52224511+liliumshade@users.noreply.github.com> Co-authored-by: Tarun Prabhu <tarunprabhu@gmail.com>
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,

0 commit comments

Comments
 (0)