[clang] [flang] Add IR Profile-Guided Optimization (IR PGO) support to the Flang compiler (PR #136098)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 22 04:03:25 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- clang/lib/Driver/ToolChains/Flang.cpp flang/include/flang/Frontend/CodeGenOptions.h flang/lib/Frontend/CompilerInvocation.cpp flang/lib/Frontend/FrontendActions.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index fcdbe8a6a..563ab15a2 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -882,12 +882,12 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
// TODO: Handle interactions between -w, -pedantic, -Wall, -WOption
Args.AddLastArg(CmdArgs, options::OPT_w);
-
- if (Args.hasArg(options::OPT_fprofile_generate)){
+ if (Args.hasArg(options::OPT_fprofile_generate)) {
CmdArgs.push_back("-fprofile-generate");
}
if (const Arg *A = Args.getLastArg(options::OPT_fprofile_use_EQ)) {
- CmdArgs.push_back(Args.MakeArgString(std::string("-fprofile-use=") + A->getValue()));
+ CmdArgs.push_back(
+ Args.MakeArgString(std::string("-fprofile-use=") + A->getValue()));
}
// Forward flags for OpenMP. We don't do this if the current action is an
diff --git a/flang/include/flang/Frontend/CodeGenOptions.h b/flang/include/flang/Frontend/CodeGenOptions.h
index e052250f9..c9577862d 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.h
+++ b/flang/include/flang/Frontend/CodeGenOptions.h
@@ -156,7 +156,6 @@ public:
ProfileCSIRInstr, // IR level PGO context sensitive instrumentation in LLVM.
};
-
/// Name of the profile file to use as output for -fprofile-instr-generate,
/// -fprofile-generate, and -fcs-profile-generate.
std::string InstrProfileOutput;
@@ -171,7 +170,7 @@ public:
/// 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
+ /// Name of the profile remapping file to apply to the profile data supplied
/// by -fprofile-sample-use or -fprofile-instr-use.
std::string ProfileRemappingFile;
@@ -181,19 +180,17 @@ public:
}
/// Check if IR level profile instrumentation is on.
- bool hasProfileIRInstr() const {
- return getProfileInstr() == ProfileIRInstr;
- }
+ bool hasProfileIRInstr() const { return getProfileInstr() == ProfileIRInstr; }
/// Check if CS IR level profile instrumentation is on.
bool hasProfileCSIRInstr() const {
return getProfileInstr() == ProfileCSIRInstr;
}
- /// Check if IR level profile use is on.
- bool hasProfileIRUse() const {
- return getProfileUse() == ProfileIRInstr ||
- getProfileUse() == ProfileCSIRInstr;
- }
+ /// Check if IR level profile use is on.
+ bool hasProfileIRUse() const {
+ return getProfileUse() == ProfileIRInstr ||
+ getProfileUse() == ProfileCSIRInstr;
+ }
/// Check if CSIR profile use is on.
bool hasProfileCSIRUse() const { return getProfileUse() == ProfileCSIRInstr; }
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index f013fce2f..b28c2c004 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -27,7 +27,6 @@
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/OptionUtils.h"
#include "clang/Driver/Options.h"
-#include "clang/Driver/Driver.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Frontend/Debug/Options.h"
@@ -433,13 +432,17 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
}
if (args.hasArg(clang::driver::options::OPT_fprofile_generate)) {
- opts.setProfileInstr(Fortran::frontend::CodeGenOptions::ProfileInstrKind::ProfileIRInstr);
- opts.DebugInfoForProfiling = args.hasArg(clang::driver::options::OPT_fdebug_info_for_profiling);
- opts.AtomicProfileUpdate = args.hasArg(clang::driver::options::OPT_fprofile_update_EQ);
+ opts.setProfileInstr(
+ Fortran::frontend::CodeGenOptions::ProfileInstrKind::ProfileIRInstr);
+ opts.DebugInfoForProfiling =
+ args.hasArg(clang::driver::options::OPT_fdebug_info_for_profiling);
+ opts.AtomicProfileUpdate =
+ args.hasArg(clang::driver::options::OPT_fprofile_update_EQ);
}
-
+
if (auto A = args.getLastArg(clang::driver::options::OPT_fprofile_use_EQ)) {
- opts.setProfileUse(Fortran::frontend::CodeGenOptions::ProfileInstrKind::ProfileIRInstr);
+ opts.setProfileUse(
+ Fortran::frontend::CodeGenOptions::ProfileInstrKind::ProfileIRInstr);
opts.ProfileInstrumentUsePath = A->getValue();
}
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 68880bdee..aea5b2677 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -56,21 +56,21 @@
#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"
-#include "llvm/Support/PGOOptions.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/RISCVISAInfo.h"
#include "llvm/TargetParser/RISCVTargetParser.h"
#include "llvm/Transforms/IPO/Internalize.h"
-#include "llvm/Transforms/Utils/ModuleUtils.h"
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
-#include "llvm/ProfileData/InstrProfCorrelator.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <memory>
#include <system_error>
@@ -133,19 +133,20 @@ static bool saveMLIRTempFile(const CompilerInvocation &ci,
// Custom BeginSourceFileAction
//===----------------------------------------------------------------------===//
-
static llvm::cl::opt<llvm::PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr(
- "pgo-cold-func-opt", llvm::cl::init(llvm::PGOOptions::ColdFuncOpt::Default), llvm::cl::Hidden,
- llvm::cl::desc(
- "Function attribute to apply to cold functions as determined by PGO"),
- llvm::cl::values(clEnumValN(llvm::PGOOptions::ColdFuncOpt::Default, "default",
- "Default (no attribute)"),
- clEnumValN(llvm::PGOOptions::ColdFuncOpt::OptSize, "optsize",
- "Mark cold functions with optsize."),
- clEnumValN(llvm::PGOOptions::ColdFuncOpt::MinSize, "minsize",
- "Mark cold functions with minsize."),
- clEnumValN(llvm::PGOOptions::ColdFuncOpt::OptNone, "optnone",
- "Mark cold functions with optnone.")));
+ "pgo-cold-func-opt", llvm::cl::init(llvm::PGOOptions::ColdFuncOpt::Default),
+ llvm::cl::Hidden,
+ llvm::cl::desc(
+ "Function attribute to apply to cold functions as determined by PGO"),
+ llvm::cl::values(clEnumValN(llvm::PGOOptions::ColdFuncOpt::Default,
+ "default", "Default (no attribute)"),
+ clEnumValN(llvm::PGOOptions::ColdFuncOpt::OptSize,
+ "optsize", "Mark cold functions with optsize."),
+ clEnumValN(llvm::PGOOptions::ColdFuncOpt::MinSize,
+ "minsize", "Mark cold functions with minsize."),
+ clEnumValN(llvm::PGOOptions::ColdFuncOpt::OptNone,
+ "optnone",
+ "Mark cold functions with optnone.")));
bool PrescanAction::beginSourceFileAction() { return runPrescan(); }
@@ -909,19 +910,18 @@ static void generateMachineCodeOrAssemblyImpl(clang::DiagnosticsEngine &diags,
delete tlii;
}
-
// Default filename used for profile generation.
namespace llvm {
- extern llvm::cl::opt<bool> DebugInfoCorrelate;
- extern llvm::cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
-
+extern llvm::cl::opt<bool> DebugInfoCorrelate;
+extern llvm::cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
std::string getDefaultProfileGenName() {
- return DebugInfoCorrelate || ProfileCorrelate != llvm::InstrProfCorrelator::NONE
+ return DebugInfoCorrelate ||
+ ProfileCorrelate != llvm::InstrProfCorrelator::NONE
? "default_%m.proflite"
: "default_%m.profraw";
}
-}
+} // namespace llvm
void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
CompilerInstance &ci = getInstance();
@@ -940,29 +940,28 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
llvm::PassInstrumentationCallbacks pic;
llvm::PipelineTuningOptions pto;
std::optional<llvm::PGOOptions> pgoOpt;
-
- if (opts.hasProfileIRInstr()){
+
+ if (opts.hasProfileIRInstr()) {
// // -fprofile-generate.
pgoOpt = llvm::PGOOptions(
- opts.InstrProfileOutput.empty() ? llvm::getDefaultProfileGenName()
- : opts.InstrProfileOutput,
- "", "", opts.MemoryProfileUsePath, nullptr, llvm::PGOOptions::IRInstr,
- llvm::PGOOptions::NoCSAction, ClPGOColdFuncAttr,
- opts.DebugInfoForProfiling,
- /*PseudoProbeForProfiling=*/false, opts.AtomicProfileUpdate);
- }
- 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, ClPGOColdFuncAttr,
- opts.DebugInfoForProfiling);
- }
-
+ opts.InstrProfileOutput.empty() ? llvm::getDefaultProfileGenName()
+ : opts.InstrProfileOutput,
+ "", "", opts.MemoryProfileUsePath, nullptr, llvm::PGOOptions::IRInstr,
+ llvm::PGOOptions::NoCSAction, ClPGOColdFuncAttr,
+ opts.DebugInfoForProfiling,
+ /*PseudoProbeForProfiling=*/false, opts.AtomicProfileUpdate);
+ } 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,
+ ClPGOColdFuncAttr, opts.DebugInfoForProfiling);
+ }
+
llvm::StandardInstrumentations si(llvmModule->getContext(),
opts.DebugPassManager);
si.registerCallbacks(pic, &mam);
``````````
</details>
https://github.com/llvm/llvm-project/pull/136098
More information about the cfe-commits
mailing list