[clang] d27a210 - Add IR Profile-Guided Optimization (IR PGO) support to the Flang compiler (#136098)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 30 07:13:59 PDT 2025
Author: FYK
Date: 2025-05-30T08:13:53-06:00
New Revision: d27a210a77af63568db9f829702b4b2c98473a46
URL: https://github.com/llvm/llvm-project/commit/d27a210a77af63568db9f829702b4b2c98473a46
DIFF: https://github.com/llvm/llvm-project/commit/d27a210a77af63568db9f829702b4b2c98473a46.diff
LOG: 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 at users.noreply.github.com>
Co-authored-by: Tom Eccles <t at freedommail.info>
Added:
flang/test/Profile/Inputs/gcc-flag-compatibility_IR.proftext
flang/test/Profile/Inputs/gcc-flag-compatibility_IR_entry.proftext
flang/test/Profile/gcc-flag-compatibility.f90
Modified:
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Basic/CodeGenOptions.h
clang/include/clang/Basic/ProfileList.h
clang/include/clang/Driver/Options.td
clang/lib/Basic/ProfileList.cpp
clang/lib/CodeGen/BackendUtil.cpp
clang/lib/CodeGen/CodeGenAction.cpp
clang/lib/CodeGen/CodeGenFunction.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/Driver/ToolChains/Flang.cpp
clang/lib/Frontend/CompilerInvocation.cpp
flang/include/flang/Frontend/CodeGenOptions.def
flang/include/flang/Frontend/CodeGenOptions.h
flang/lib/Frontend/CompilerInvocation.cpp
flang/lib/Frontend/FrontendActions.cpp
flang/test/Driver/flang-f-opts.f90
llvm/include/llvm/Frontend/Driver/CodeGenOptions.h
llvm/lib/Frontend/Driver/CodeGenOptions.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index aad4e107cbeb3..11dad53a52efe 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -223,9 +223,11 @@ AFFECTING_VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is
CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic
CODEGENOPT(ContinuousProfileSync, 1, 0) ///< Enable continuous instrumentation profiling
/// Choose profile instrumenation kind or no instrumentation.
-ENUM_CODEGENOPT(ProfileInstr, ProfileInstrKind, 4, ProfileNone)
+
+ENUM_CODEGENOPT(ProfileInstr, llvm::driver::ProfileInstrKind, 4, llvm::driver::ProfileInstrKind::ProfileNone)
+
/// Choose profile kind for PGO use compilation.
-ENUM_CODEGENOPT(ProfileUse, ProfileInstrKind, 2, ProfileNone)
+ENUM_CODEGENOPT(ProfileUse, llvm::driver::ProfileInstrKind, 2, llvm::driver::ProfileInstrKind::ProfileNone)
/// Partition functions into N groups and select only functions in group i to be
/// instrumented. Selected group numbers can be 0 to N-1 inclusive.
VALUE_CODEGENOPT(ProfileTotalFunctionGroups, 32, 1)
diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h
index 278803f7bb960..bffbd00b1bd72 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -518,35 +518,41 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// Check if Clang profile instrumenation is on.
bool hasProfileClangInstr() const {
- return getProfileInstr() == ProfileClangInstr;
+ return getProfileInstr() ==
+ llvm::driver::ProfileInstrKind::ProfileClangInstr;
}
/// Check if IR level profile instrumentation is on.
bool hasProfileIRInstr() const {
- return getProfileInstr() == ProfileIRInstr;
+ return getProfileInstr() == llvm::driver::ProfileInstrKind::ProfileIRInstr;
}
/// Check if CS IR level profile instrumentation is on.
bool hasProfileCSIRInstr() const {
- return getProfileInstr() == ProfileCSIRInstr;
+ return getProfileInstr() ==
+ llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
}
/// Check if any form of instrumentation is on.
- bool hasProfileInstr() const { return getProfileInstr() != ProfileNone; }
+ bool hasProfileInstr() const {
+ return getProfileInstr() != llvm::driver::ProfileInstrKind::ProfileNone;
+ }
/// Check if Clang profile use is on.
bool hasProfileClangUse() const {
- return getProfileUse() == ProfileClangInstr;
+ return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileClangInstr;
}
/// Check if IR level profile use is on.
bool hasProfileIRUse() const {
- return getProfileUse() == ProfileIRInstr ||
- getProfileUse() == ProfileCSIRInstr;
+ return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileIRInstr ||
+ getProfileUse() == llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
}
/// Check if CSIR profile use is on.
- bool hasProfileCSIRUse() const { return getProfileUse() == ProfileCSIRInstr; }
+ bool hasProfileCSIRUse() const {
+ return getProfileUse() == llvm::driver::ProfileInstrKind::ProfileCSIRInstr;
+ }
/// Check if type and variable info should be emitted.
bool hasReducedDebugInfo() const {
diff --git a/clang/include/clang/Basic/ProfileList.h b/clang/include/clang/Basic/ProfileList.h
index b4217e49c18a3..5338ef3992ade 100644
--- a/clang/include/clang/Basic/ProfileList.h
+++ b/clang/include/clang/Basic/ProfileList.h
@@ -49,17 +49,16 @@ class ProfileList {
~ProfileList();
bool isEmpty() const { return Empty; }
- ExclusionType getDefault(CodeGenOptions::ProfileInstrKind Kind) const;
+ ExclusionType getDefault(llvm::driver::ProfileInstrKind Kind) const;
std::optional<ExclusionType>
isFunctionExcluded(StringRef FunctionName,
- CodeGenOptions::ProfileInstrKind Kind) const;
+ llvm::driver::ProfileInstrKind Kind) const;
std::optional<ExclusionType>
isLocationExcluded(SourceLocation Loc,
- CodeGenOptions::ProfileInstrKind Kind) const;
+ llvm::driver::ProfileInstrKind Kind) const;
std::optional<ExclusionType>
- isFileExcluded(StringRef FileName,
- CodeGenOptions::ProfileInstrKind Kind) const;
+ isFileExcluded(StringRef FileName, llvm::driver::ProfileInstrKind Kind) const;
};
} // namespace clang
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 5ca31c253ed8f..5c79c66b55eb3 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1772,7 +1772,7 @@ def fmcdc_max_test_vectors_EQ : Joined<["-"], "fmcdc-max-test-vectors=">,
HelpText<"Maximum number of test vectors in MC/DC coverage">,
MarshallingInfoInt<CodeGenOpts<"MCDCMaxTVs">, "0x7FFFFFFE">;
def fprofile_generate : Flag<["-"], "fprofile-generate">,
- Group<f_Group>, Visibility<[ClangOption, CLOption]>,
+ Group<f_Group>, Visibility<[ClangOption, CLOption, FlangOption, FC1Option]>,
HelpText<"Generate instrumented code to collect execution counts into default.profraw (overridden by LLVM_PROFILE_FILE env var)">;
def fprofile_generate_EQ : Joined<["-"], "fprofile-generate=">,
Group<f_Group>, Visibility<[ClangOption, CLOption]>,
@@ -1789,7 +1789,7 @@ def fprofile_use : Flag<["-"], "fprofile-use">, Group<f_Group>,
Visibility<[ClangOption, CLOption]>, Alias<fprofile_instr_use>;
def fprofile_use_EQ : Joined<["-"], "fprofile-use=">,
Group<f_Group>,
- Visibility<[ClangOption, CLOption]>,
+ Visibility<[ClangOption, CLOption, FlangOption, FC1Option]>,
MetaVarName<"<pathname>">,
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>.">;
def fno_profile_instr_generate : Flag<["-"], "fno-profile-instr-generate">,
diff --git a/clang/lib/Basic/ProfileList.cpp b/clang/lib/Basic/ProfileList.cpp
index 2d37014294b92..bea65579f396b 100644
--- a/clang/lib/Basic/ProfileList.cpp
+++ b/clang/lib/Basic/ProfileList.cpp
@@ -70,24 +70,24 @@ ProfileList::ProfileList(ArrayRef<std::string> Paths, SourceManager &SM)
ProfileList::~ProfileList() = default;
-static StringRef getSectionName(CodeGenOptions::ProfileInstrKind Kind) {
+static StringRef getSectionName(llvm::driver::ProfileInstrKind Kind) {
switch (Kind) {
- case CodeGenOptions::ProfileNone:
+ case llvm::driver::ProfileInstrKind::ProfileNone:
return "";
- case CodeGenOptions::ProfileClangInstr:
+ case llvm::driver::ProfileInstrKind::ProfileClangInstr:
return "clang";
- case CodeGenOptions::ProfileIRInstr:
+ case llvm::driver::ProfileInstrKind::ProfileIRInstr:
return "llvm";
- case CodeGenOptions::ProfileCSIRInstr:
+ case llvm::driver::ProfileInstrKind::ProfileCSIRInstr:
return "csllvm";
case CodeGenOptions::ProfileIRSampleColdCov:
return "sample-coldcov";
}
- llvm_unreachable("Unhandled CodeGenOptions::ProfileInstrKind enum");
+ llvm_unreachable("Unhandled llvm::driver::ProfileInstrKind enum");
}
ProfileList::ExclusionType
-ProfileList::getDefault(CodeGenOptions::ProfileInstrKind Kind) const {
+ProfileList::getDefault(llvm::driver::ProfileInstrKind Kind) const {
StringRef Section = getSectionName(Kind);
// Check for "default:<type>"
if (SCL->inSection(Section, "default", "allow"))
@@ -118,7 +118,7 @@ ProfileList::inSection(StringRef Section, StringRef Prefix,
std::optional<ProfileList::ExclusionType>
ProfileList::isFunctionExcluded(StringRef FunctionName,
- CodeGenOptions::ProfileInstrKind Kind) const {
+ llvm::driver::ProfileInstrKind Kind) const {
StringRef Section = getSectionName(Kind);
// Check for "function:<regex>=<case>"
if (auto V = inSection(Section, "function", FunctionName))
@@ -132,13 +132,13 @@ ProfileList::isFunctionExcluded(StringRef FunctionName,
std::optional<ProfileList::ExclusionType>
ProfileList::isLocationExcluded(SourceLocation Loc,
- CodeGenOptions::ProfileInstrKind Kind) const {
+ llvm::driver::ProfileInstrKind Kind) const {
return isFileExcluded(SM.getFilename(SM.getFileLoc(Loc)), Kind);
}
std::optional<ProfileList::ExclusionType>
ProfileList::isFileExcluded(StringRef FileName,
- CodeGenOptions::ProfileInstrKind Kind) const {
+ llvm::driver::ProfileInstrKind Kind) const {
StringRef Section = getSectionName(Kind);
// Check for "source:<regex>=<case>"
if (auto V = inSection(Section, "source", FileName))
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index cd5fc48c4a22b..03e10b1138a71 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -123,17 +123,10 @@ namespace clang {
extern llvm::cl::opt<bool> ClSanitizeGuardChecks;
}
-// Default filename used for profile generation.
-static std::string getDefaultProfileGenName() {
- return DebugInfoCorrelate || ProfileCorrelate != InstrProfCorrelator::NONE
- ? "default_%m.proflite"
- : "default_%m.profraw";
-}
-
// Path and name of file used for profile generation
static std::string getProfileGenName(const CodeGenOptions &CodeGenOpts) {
std::string FileName = CodeGenOpts.InstrProfileOutput.empty()
- ? getDefaultProfileGenName()
+ ? llvm::driver::getDefaultProfileGenName()
: CodeGenOpts.InstrProfileOutput;
if (CodeGenOpts.ContinuousProfileSync)
FileName = "%c" + FileName;
@@ -835,12 +828,12 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
if (CodeGenOpts.hasProfileIRInstr())
// -fprofile-generate.
- PGOOpt = PGOOptions(getProfileGenName(CodeGenOpts), "", "",
- CodeGenOpts.MemoryProfileUsePath, nullptr,
- PGOOptions::IRInstr, PGOOptions::NoCSAction,
- ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling,
- /*PseudoProbeForProfiling=*/false,
- CodeGenOpts.AtomicProfileUpdate);
+ PGOOpt = PGOOptions(
+ getProfileGenName(CodeGenOpts), "", "",
+ CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
+ PGOOptions::NoCSAction, llvm::ClPGOColdFuncAttr,
+ CodeGenOpts.DebugInfoForProfiling,
+ /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
else if (CodeGenOpts.hasProfileIRUse()) {
// -fprofile-use.
auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
@@ -848,31 +841,32 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
CodeGenOpts.ProfileRemappingFile,
CodeGenOpts.MemoryProfileUsePath, VFS,
- PGOOptions::IRUse, CSAction, ClPGOColdFuncAttr,
+ PGOOptions::IRUse, CSAction, llvm::ClPGOColdFuncAttr,
CodeGenOpts.DebugInfoForProfiling);
} else if (!CodeGenOpts.SampleProfileFile.empty())
// -fprofile-sample-use
PGOOpt = PGOOptions(
CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
- PGOOptions::NoCSAction, ClPGOColdFuncAttr,
+ PGOOptions::NoCSAction, llvm::ClPGOColdFuncAttr,
CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling);
else if (!CodeGenOpts.MemoryProfileUsePath.empty())
// -fmemory-profile-use (without any of the above options)
- PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
- PGOOptions::NoAction, PGOOptions::NoCSAction,
- ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
- else if (CodeGenOpts.PseudoProbeForProfiling)
- // -fpseudo-probe-for-profiling
PGOOpt =
- PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
+ PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
PGOOptions::NoAction, PGOOptions::NoCSAction,
- ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling, true);
+ llvm::ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
+ else if (CodeGenOpts.PseudoProbeForProfiling)
+ // -fpseudo-probe-for-profiling
+ PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
+ PGOOptions::NoAction, PGOOptions::NoCSAction,
+ llvm::ClPGOColdFuncAttr,
+ CodeGenOpts.DebugInfoForProfiling, true);
else if (CodeGenOpts.DebugInfoForProfiling)
// -fdebug-info-for-profiling
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
PGOOptions::NoAction, PGOOptions::NoCSAction,
- ClPGOColdFuncAttr, true);
+ llvm::ClPGOColdFuncAttr, true);
// Check to see if we want to generate a CS profile.
if (CodeGenOpts.hasProfileCSIRInstr()) {
diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp
index 1f5eb427b566f..5493cc92bd8b0 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -273,8 +273,8 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
std::move(*OptRecordFileOrErr);
- if (OptRecordFile &&
- CodeGenOpts.getProfileUse() != CodeGenOptions::ProfileNone)
+ if (OptRecordFile && CodeGenOpts.getProfileUse() !=
+ llvm::driver::ProfileInstrKind::ProfileNone)
Ctx.setDiagnosticsHotnessRequested(true);
if (CodeGenOpts.MisExpect) {
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 4193f0a1b278f..30aec87c909eb 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -940,7 +940,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
}
}
- if (CGM.getCodeGenOpts().getProfileInstr() != CodeGenOptions::ProfileNone) {
+ if (CGM.getCodeGenOpts().getProfileInstr() !=
+ llvm::driver::ProfileInstrKind::ProfileNone) {
switch (CGM.isFunctionBlockedFromProfileInstr(Fn, Loc)) {
case ProfileList::Skip:
Fn->addFnAttr(llvm::Attribute::SkipProfile);
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 6d2c705338ecf..264f1bdee81c6 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3601,7 +3601,7 @@ CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
// If the profile list is empty, then instrument everything.
if (ProfileList.isEmpty())
return ProfileList::Allow;
- CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
+ llvm::driver::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
// First, check the function name.
if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
return *V;
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index dcc46469df3e9..e303631cc1d57 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -883,6 +883,10 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
// TODO: Handle interactions between -w, -pedantic, -Wall, -WOption
Args.AddLastArg(CmdArgs, options::OPT_w);
+ // recognise options: fprofile-generate -fprofile-use=
+ Args.addAllArgs(
+ CmdArgs, {options::OPT_fprofile_generate, options::OPT_fprofile_use_EQ});
+
// Forward flags for OpenMP. We don't do this if the current action is an
// device offloading action other than OpenMP.
if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 9c33910eff57e..11d0dc6b7b6f1 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1499,11 +1499,11 @@ static void setPGOUseInstrumentor(CodeGenOptions &Opts,
// which is available (might be one or both).
if (PGOReader->isIRLevelProfile() || PGOReader->hasMemoryProfile()) {
if (PGOReader->hasCSIRLevelProfile())
- Opts.setProfileUse(CodeGenOptions::ProfileCSIRInstr);
+ Opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileCSIRInstr);
else
- Opts.setProfileUse(CodeGenOptions::ProfileIRInstr);
+ Opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileIRInstr);
} else
- Opts.setProfileUse(CodeGenOptions::ProfileClangInstr);
+ Opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileClangInstr);
}
void CompilerInvocation::setDefaultPointerAuthOptions(
diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def
index a697872836569..ae12aec518108 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -24,8 +24,15 @@ CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
///< pass manager.
+
+/// Choose profile instrumenation kind or no instrumentation.
+ENUM_CODEGENOPT(ProfileInstr, llvm::driver::ProfileInstrKind, 2, llvm::driver::ProfileInstrKind::ProfileNone)
+/// Choose profile kind for PGO use compilation.
+ENUM_CODEGENOPT(ProfileUse, llvm::driver::ProfileInstrKind, 2, llvm::driver::ProfileInstrKind::ProfileNone)
+
CODEGENOPT(InstrumentFunctions, 1, 0) ///< Set when -finstrument_functions is
///< enabled on the compile step.
+
CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level.
CODEGENOPT(PICLevel, 2, 0) ///< PIC level of the LLVM module.
CODEGENOPT(PrepareForFullLTO , 1, 0) ///< Set when -flto is enabled on the
diff --git a/flang/include/flang/Frontend/CodeGenOptions.h b/flang/include/flang/Frontend/CodeGenOptions.h
index 61e56e51c4bbb..06203670f97b9 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.h
+++ b/flang/include/flang/Frontend/CodeGenOptions.h
@@ -151,6 +151,44 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// OpenMP is enabled.
using DoConcurrentMappingKind = flangomp::DoConcurrentMappingKind;
+ /// Name of the profile file to use as output for -fprofile-instr-generate,
+ /// -fprofile-generate, and -fcs-profile-generate.
+ std::string InstrProfileOutput;
+
+ /// Name of the profile file to use as input for -fmemory-profile-use.
+ std::string MemoryProfileUsePath;
+
+ /// Name of the profile file to use as input for -fprofile-instr-use
+ std::string ProfileInstrumentUsePath;
+
+ /// Name of the profile remapping file to apply to the profile data supplied
+ /// by -fprofile-sample-use or -fprofile-instr-use.
+ std::string ProfileRemappingFile;
+
+ /// Check if Clang profile instrumenation is on.
+ bool hasProfileClangInstr() const {
+ return getProfileInstr() == llvm::driver::ProfileClangInstr;
+ }
+
+ /// Check if IR level profile instrumentation is on.
+ bool hasProfileIRInstr() const {
+ return getProfileInstr() == llvm::driver::ProfileIRInstr;
+ }
+
+ /// Check if CS IR level profile instrumentation is on.
+ bool hasProfileCSIRInstr() const {
+ return getProfileInstr() == llvm::driver::ProfileCSIRInstr;
+ }
+ /// Check if IR level profile use is on.
+ bool hasProfileIRUse() const {
+ return getProfileUse() == llvm::driver::ProfileIRInstr ||
+ getProfileUse() == llvm::driver::ProfileCSIRInstr;
+ }
+ /// Check if CSIR profile use is on.
+ bool hasProfileCSIRUse() const {
+ return getProfileUse() == llvm::driver::ProfileCSIRInstr;
+ }
+
// Define accessors/mutators for code generation options of enumeration type.
#define CODEGENOPT(Name, Bits, Default)
#define ENUM_CODEGENOPT(Name, Type, Bits, Default) \
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 90a002929eff0..0571aea8ec801 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -30,6 +30,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Frontend/Debug/Options.h"
+#include "llvm/Frontend/Driver/CodeGenOptions.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/OptTable.h"
@@ -452,6 +453,15 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
opts.IsPIE = 1;
}
+ if (args.hasArg(clang::driver::options::OPT_fprofile_generate)) {
+ opts.setProfileInstr(llvm::driver::ProfileInstrKind::ProfileIRInstr);
+ }
+
+ if (auto A = args.getLastArg(clang::driver::options::OPT_fprofile_use_EQ)) {
+ opts.setProfileUse(llvm::driver::ProfileInstrKind::ProfileIRInstr);
+ opts.ProfileInstrumentUsePath = A->getValue();
+ }
+
// -mcmodel option.
if (const llvm::opt::Arg *a =
args.getLastArg(clang::driver::options::OPT_mcmodel_EQ)) {
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 012d0fdfe645f..da8fa518ab3e1 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -56,10 +56,12 @@
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Passes/StandardInstrumentations.h"
+#include "llvm/ProfileData/InstrProfCorrelator.h"
#include "llvm/Support/AMDGPUAddrSpace.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/PGOOptions.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"
@@ -67,6 +69,7 @@
#include "llvm/TargetParser/RISCVISAInfo.h"
#include "llvm/TargetParser/RISCVTargetParser.h"
#include "llvm/Transforms/IPO/Internalize.h"
+#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <memory>
#include <system_error>
@@ -918,6 +921,29 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
llvm::PassInstrumentationCallbacks pic;
llvm::PipelineTuningOptions pto;
std::optional<llvm::PGOOptions> pgoOpt;
+
+ if (opts.hasProfileIRInstr()) {
+ // -fprofile-generate.
+ pgoOpt = llvm::PGOOptions(opts.InstrProfileOutput.empty()
+ ? llvm::driver::getDefaultProfileGenName()
+ : opts.InstrProfileOutput,
+ "", "", opts.MemoryProfileUsePath, nullptr,
+ llvm::PGOOptions::IRInstr,
+ llvm::PGOOptions::NoCSAction,
+ llvm::PGOOptions::ColdFuncOpt::Default, false,
+ /*PseudoProbeForProfiling=*/false, false);
+ } else if (opts.hasProfileIRUse()) {
+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
+ llvm::vfs::getRealFileSystem();
+ // -fprofile-use.
+ auto CSAction = opts.hasProfileCSIRUse() ? llvm::PGOOptions::CSIRUse
+ : llvm::PGOOptions::NoCSAction;
+ pgoOpt = llvm::PGOOptions(
+ opts.ProfileInstrumentUsePath, "", opts.ProfileRemappingFile,
+ opts.MemoryProfileUsePath, VFS, llvm::PGOOptions::IRUse, CSAction,
+ llvm::PGOOptions::ColdFuncOpt::Default, false);
+ }
+
llvm::StandardInstrumentations si(llvmModule->getContext(),
opts.DebugPassManager);
si.registerCallbacks(pic, &mam);
diff --git a/flang/test/Driver/flang-f-opts.f90 b/flang/test/Driver/flang-f-opts.f90
index 4493a519e2010..b972b9b7b2a59 100644
--- a/flang/test/Driver/flang-f-opts.f90
+++ b/flang/test/Driver/flang-f-opts.f90
@@ -8,3 +8,8 @@
! CHECK-LABEL: "-fc1"
! CHECK: -ffp-contract=off
! CHECK: -O3
+
+! RUN: %flang -### -S -fprofile-generate %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-GENERATE-LLVM %s
+! CHECK-PROFILE-GENERATE-LLVM: "-fprofile-generate"
+! RUN: %flang -### -S -fprofile-use=%S %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-USE-DIR %s
+! CHECK-PROFILE-USE-DIR: "-fprofile-use={{.*}}"
diff --git a/flang/test/Profile/Inputs/gcc-flag-compatibility_IR.proftext b/flang/test/Profile/Inputs/gcc-flag-compatibility_IR.proftext
new file mode 100644
index 0000000000000..2650fb5ebfd35
--- /dev/null
+++ b/flang/test/Profile/Inputs/gcc-flag-compatibility_IR.proftext
@@ -0,0 +1,18 @@
+# IR level Instrumentation Flag
+:ir
+_QQmain
+# Func Hash:
+146835646621254984
+# Num Counters:
+2
+# Counter Values:
+100
+1
+
+main
+# Func Hash:
+742261418966908927
+# Num Counters:
+1
+# Counter Values:
+1
\ No newline at end of file
diff --git a/flang/test/Profile/Inputs/gcc-flag-compatibility_IR_entry.proftext b/flang/test/Profile/Inputs/gcc-flag-compatibility_IR_entry.proftext
new file mode 100644
index 0000000000000..c4a2a26557e80
--- /dev/null
+++ b/flang/test/Profile/Inputs/gcc-flag-compatibility_IR_entry.proftext
@@ -0,0 +1,11 @@
+# IR level Instrumentation Flag
+:ir
+:entry_first
+_QQmain
+# Func Hash:
+146835646621254984
+# Num Counters:
+2
+# Counter Values:
+100
+1
\ No newline at end of file
diff --git a/flang/test/Profile/gcc-flag-compatibility.f90 b/flang/test/Profile/gcc-flag-compatibility.f90
new file mode 100644
index 0000000000000..4490c45232d28
--- /dev/null
+++ b/flang/test/Profile/gcc-flag-compatibility.f90
@@ -0,0 +1,32 @@
+! Tests for -fprofile-generate and -fprofile-use flag compatibility. These two
+! flags behave similarly to their GCC counterparts:
+!
+! -fprofile-generate Generates the profile file ./default.profraw
+! -fprofile-use=<dir>/file Uses the profile file <dir>/file
+
+! On AIX, -flto used to be required with -fprofile-generate. gcc-flag-compatibility-aix.c is used to do the testing on AIX with -flto
+! RUN: %flang %s -c -S -o - -emit-llvm -fprofile-generate | FileCheck -check-prefix=PROFILE-GEN %s
+! PROFILE-GEN: @__profc_{{_?}}main = {{(private|internal)}} global [1 x i64] zeroinitializer, section
+! PROFILE-GEN: @__profd_{{_?}}main =
+
+! Check that -fprofile-use=some/path/file.prof reads some/path/file.prof
+! This uses LLVM IR format profile.
+! RUN: rm -rf %t.dir
+! RUN: mkdir -p %t.dir/some/path
+! RUN: llvm-profdata merge %S/Inputs/gcc-flag-compatibility_IR.proftext -o %t.dir/some/path/file.prof
+! RUN: %flang %s -o - -emit-llvm -S -fprofile-use=%t.dir/some/path/file.prof | FileCheck -check-prefix=PROFILE-USE-IR1 %s
+! RUN: llvm-profdata merge %S/Inputs/gcc-flag-compatibility_IR_entry.proftext -o %t.dir/some/path/file.prof
+! RUN: %flang %s -o - -emit-llvm -S -fprofile-use=%t.dir/some/path/file.prof | FileCheck -check-prefix=PROFILE-USE-IR2 %s
+! PROFILE-USE-IR1: = !{!"branch_weights", i32 100, i32 1}
+! PROFILE-USE-IR2: = !{!"branch_weights", i32 1, i32 100}
+
+program main
+ implicit none
+ integer :: i
+ integer :: X = 0
+
+ do i = 0, 99
+ X = X + i
+ end do
+
+end program main
diff --git a/llvm/include/llvm/Frontend/Driver/CodeGenOptions.h b/llvm/include/llvm/Frontend/Driver/CodeGenOptions.h
index ee52645f2e51b..82f583bc459e6 100644
--- a/llvm/include/llvm/Frontend/Driver/CodeGenOptions.h
+++ b/llvm/include/llvm/Frontend/Driver/CodeGenOptions.h
@@ -13,6 +13,8 @@
#ifndef LLVM_FRONTEND_DRIVER_CODEGENOPTIONS_H
#define LLVM_FRONTEND_DRIVER_CODEGENOPTIONS_H
+#include <string>
+
namespace llvm {
class Triple;
class TargetLibraryInfoImpl;
@@ -46,9 +48,19 @@ enum class VectorLibrary {
AMDLIBM // AMD vector math library.
};
+enum ProfileInstrKind {
+ ProfileNone, // Profile instrumentation is turned off.
+ ProfileClangInstr, // Clang instrumentation to generate execution counts
+ // to use with PGO.
+ ProfileIRInstr, // IR level PGO instrumentation in LLVM.
+ ProfileCSIRInstr, // IR level PGO context sensitive instrumentation in LLVM.
+};
+
TargetLibraryInfoImpl *createTLII(const llvm::Triple &TargetTriple,
VectorLibrary Veclib);
+// Default filename used for profile generation.
+std::string getDefaultProfileGenName();
} // end namespace llvm::driver
#endif
diff --git a/llvm/lib/Frontend/Driver/CodeGenOptions.cpp b/llvm/lib/Frontend/Driver/CodeGenOptions.cpp
index 52080dea93c98..df884908845d2 100644
--- a/llvm/lib/Frontend/Driver/CodeGenOptions.cpp
+++ b/llvm/lib/Frontend/Driver/CodeGenOptions.cpp
@@ -8,8 +8,15 @@
#include "llvm/Frontend/Driver/CodeGenOptions.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/ProfileData/InstrProfCorrelator.h"
#include "llvm/TargetParser/Triple.h"
+namespace llvm {
+extern llvm::cl::opt<bool> DebugInfoCorrelate;
+extern llvm::cl::opt<llvm::InstrProfCorrelator::ProfCorrelatorKind>
+ ProfileCorrelate;
+} // namespace llvm
+
namespace llvm::driver {
TargetLibraryInfoImpl *createTLII(const llvm::Triple &TargetTriple,
@@ -56,4 +63,10 @@ TargetLibraryInfoImpl *createTLII(const llvm::Triple &TargetTriple,
return TLII;
}
+std::string getDefaultProfileGenName() {
+ return llvm::DebugInfoCorrelate ||
+ llvm::ProfileCorrelate != InstrProfCorrelator::NONE
+ ? "default_%m.proflite"
+ : "default_%m.profraw";
+}
} // namespace llvm::driver
More information about the cfe-commits
mailing list