Skip to content

Commit 546ec64

Browse files
committed
Restore "[MemProf] Use new option/pass for profile feedback and matching"
This restores commit b4a82b6, reverted in 3ab7ef2 because it was thought to cause a bot failure, which ended up being unrelated to this patch set. Differential Revision: https://reviews.llvm.org/D154856
1 parent 9501405 commit 546ec64

File tree

18 files changed

+206
-66
lines changed

18 files changed

+206
-66
lines changed

clang/include/clang/Basic/CodeGenOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
282282
/// Name of the profile file to use as output for with -fmemory-profile.
283283
std::string MemoryProfileOutput;
284284

285+
/// Name of the profile file to use as input for -fmemory-profile-use.
286+
std::string MemoryProfileUsePath;
287+
285288
/// Name of the profile file to use as input for -fprofile-instr-use
286289
std::string ProfileInstrumentUsePath;
287290

clang/include/clang/Driver/Options.td

+4
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,10 @@ defm memory_profile : OptInCC1FFlag<"memory-profile", "Enable", "Disable", " hea
17721772
def fmemory_profile_EQ : Joined<["-"], "fmemory-profile=">,
17731773
Group<f_Group>, Flags<[CC1Option]>, MetaVarName<"<directory>">,
17741774
HelpText<"Enable heap memory profiling and dump results into <directory>">;
1775+
def fmemory_profile_use_EQ : Joined<["-"], "fmemory-profile-use=">,
1776+
Group<f_Group>, Flags<[CC1Option, CoreOption]>, MetaVarName<"<pathname>">,
1777+
HelpText<"Use memory profile for profile-guided memory optimization">,
1778+
MarshallingInfoString<CodeGenOpts<"MemoryProfileUsePath">>;
17751779

17761780
// Begin sanitizer flags. These should all be core options exposed in all driver
17771781
// modes.

clang/lib/CodeGen/BackendUtil.cpp

+20-14
Original file line numberDiff line numberDiff line change
@@ -762,31 +762,37 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
762762
PGOOpt = PGOOptions(
763763
CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
764764
: CodeGenOpts.InstrProfileOutput,
765-
"", "", nullptr, PGOOptions::IRInstr, PGOOptions::NoCSAction,
766-
CodeGenOpts.DebugInfoForProfiling);
765+
"", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
766+
PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
767767
else if (CodeGenOpts.hasProfileIRUse()) {
768768
// -fprofile-use.
769769
auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
770770
: PGOOptions::NoCSAction;
771-
PGOOpt =
772-
PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
773-
CodeGenOpts.ProfileRemappingFile, VFS, PGOOptions::IRUse,
774-
CSAction, CodeGenOpts.DebugInfoForProfiling);
771+
PGOOpt = PGOOptions(
772+
CodeGenOpts.ProfileInstrumentUsePath, "",
773+
CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, VFS,
774+
PGOOptions::IRUse, CSAction, CodeGenOpts.DebugInfoForProfiling);
775775
} else if (!CodeGenOpts.SampleProfileFile.empty())
776776
// -fprofile-sample-use
777777
PGOOpt = PGOOptions(
778778
CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
779-
VFS, PGOOptions::SampleUse, PGOOptions::NoCSAction,
780-
CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling);
779+
CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
780+
PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
781+
CodeGenOpts.PseudoProbeForProfiling);
782+
else if (!CodeGenOpts.MemoryProfileUsePath.empty())
783+
// -fmemory-profile-use (without any of the above options)
784+
PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
785+
PGOOptions::NoAction, PGOOptions::NoCSAction,
786+
CodeGenOpts.DebugInfoForProfiling);
781787
else if (CodeGenOpts.PseudoProbeForProfiling)
782788
// -fpseudo-probe-for-profiling
783-
PGOOpt = PGOOptions("", "", "", nullptr, PGOOptions::NoAction,
784-
PGOOptions::NoCSAction,
789+
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
790+
PGOOptions::NoAction, PGOOptions::NoCSAction,
785791
CodeGenOpts.DebugInfoForProfiling, true);
786792
else if (CodeGenOpts.DebugInfoForProfiling)
787793
// -fdebug-info-for-profiling
788-
PGOOpt = PGOOptions("", "", "", nullptr, PGOOptions::NoAction,
789-
PGOOptions::NoCSAction, true);
794+
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
795+
PGOOptions::NoAction, PGOOptions::NoCSAction, true);
790796

791797
// Check to see if we want to generate a CS profile.
792798
if (CodeGenOpts.hasProfileCSIRInstr()) {
@@ -808,8 +814,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
808814
CodeGenOpts.InstrProfileOutput.empty()
809815
? getDefaultProfileGenName()
810816
: CodeGenOpts.InstrProfileOutput,
811-
"", nullptr, PGOOptions::NoAction, PGOOptions::CSIRInstr,
812-
CodeGenOpts.DebugInfoForProfiling);
817+
"", /*MemoryProfile=*/"", nullptr, PGOOptions::NoAction,
818+
PGOOptions::CSIRInstr, CodeGenOpts.DebugInfoForProfiling);
813819
}
814820
if (TM)
815821
TM->setPGOOption(PGOOpt);

clang/lib/Driver/ToolChains/Clang.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -4946,6 +4946,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
49464946
!MemProfArg->getOption().matches(options::OPT_fno_memory_profile))
49474947
MemProfArg->render(Args, CmdArgs);
49484948

4949+
if (auto *MemProfUseArg =
4950+
Args.getLastArg(options::OPT_fmemory_profile_use_EQ)) {
4951+
if (MemProfArg)
4952+
D.Diag(diag::err_drv_argument_not_allowed_with)
4953+
<< MemProfUseArg->getAsString(Args) << MemProfArg->getAsString(Args);
4954+
if (auto *PGOInstrArg = Args.getLastArg(options::OPT_fprofile_generate,
4955+
options::OPT_fprofile_generate_EQ))
4956+
D.Diag(diag::err_drv_argument_not_allowed_with)
4957+
<< MemProfUseArg->getAsString(Args) << PGOInstrArg->getAsString(Args);
4958+
MemProfUseArg->render(Args, CmdArgs);
4959+
}
4960+
49494961
// Embed-bitcode option.
49504962
// Only white-listed flags below are allowed to be embedded.
49514963
if (C.getDriver().embedBitcodeInObject() && !IsUsingLTO &&

clang/test/CodeGen/memprof.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
// Profile use:
1818
// Ensure Pass PGOInstrumentationUse is invoked with the memprof-only profile.
19-
// RUN: %clang_cc1 -O2 -fprofile-instrument-use-path=%t.memprofdata %s -fdebug-pass-manager -emit-llvm -o - 2>&1 | FileCheck %s -check-prefix=USE
20-
// USE: Running pass: PGOInstrumentationUse on [module]
19+
// RUN: %clang_cc1 -O2 -fmemory-profile-use=%t.memprofdata %s -fdebug-pass-manager -emit-llvm -o - 2>&1 | FileCheck %s -check-prefix=USE
20+
// USE: Running pass: MemProfUsePass on [module]
2121

2222
char *foo() {
2323
return new char[10];

clang/test/Driver/fmemprof.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@
88
// DIR: ld{{.*}}libclang_rt.memprof{{.*}}libclang_rt.memprof_cxx
99
// OFF-NOT: "-fmemory-profile"
1010
// OFF-NOT: libclang_rt.memprof
11+
12+
// RUN: %clangxx -target x86_64-linux-gnu -fmemory-profile-use=foo %s -### 2>&1 | FileCheck %s --check-prefix=USE
13+
// USE: "-cc1" {{.*}} "-fmemory-profile-use=foo"
14+
15+
// RUN: %clangxx -target x86_64-linux-gnu -fmemory-profile -fmemory-profile-use=foo %s -### 2>&1 | FileCheck %s --check-prefix=CONFLICTWITHMEMPROFINSTR
16+
// CONFLICTWITHMEMPROFINSTR: error: invalid argument '-fmemory-profile-use=foo' not allowed with '-fmemory-profile'
17+
18+
// RUN: %clangxx -target x86_64-linux-gnu -fprofile-generate -fmemory-profile-use=foo %s -### 2>&1 | FileCheck %s --check-prefix=CONFLICTWITHPGOINSTR
19+
// CONFLICTWITHPGOINSTR: error: invalid argument '-fmemory-profile-use=foo' not allowed with '-fprofile-generate'

llvm/include/llvm/Support/PGOOptions.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct PGOOptions {
2828
enum PGOAction { NoAction, IRInstr, IRUse, SampleUse };
2929
enum CSPGOAction { NoCSAction, CSIRInstr, CSIRUse };
3030
PGOOptions(std::string ProfileFile, std::string CSProfileGenFile,
31-
std::string ProfileRemappingFile,
31+
std::string ProfileRemappingFile, std::string MemoryProfile,
3232
IntrusiveRefCntPtr<vfs::FileSystem> FS,
3333
PGOAction Action = NoAction, CSPGOAction CSAction = NoCSAction,
3434
bool DebugInfoForProfiling = false,
@@ -40,6 +40,7 @@ struct PGOOptions {
4040
std::string ProfileFile;
4141
std::string CSProfileGenFile;
4242
std::string ProfileRemappingFile;
43+
std::string MemoryProfile;
4344
PGOAction Action;
4445
CSPGOAction CSAction;
4546
bool DebugInfoForProfiling;

llvm/include/llvm/Transforms/Instrumentation/MemProfiler.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFILER_H
1313
#define LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFILER_H
1414

15+
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1516
#include "llvm/IR/PassManager.h"
1617

1718
namespace llvm {
@@ -20,6 +21,10 @@ class FunctionPass;
2021
class Module;
2122
class ModulePass;
2223

24+
namespace vfs {
25+
class FileSystem;
26+
} // namespace vfs
27+
2328
/// Public interface to the memory profiler pass for instrumenting code to
2429
/// profile memory accesses.
2530
///
@@ -43,12 +48,16 @@ class ModuleMemProfilerPass : public PassInfoMixin<ModuleMemProfilerPass> {
4348
static bool isRequired() { return true; }
4449
};
4550

46-
// TODO: Remove this declaration and make readMemprof static once the matching
47-
// is moved into its own pass.
48-
class IndexedInstrProfReader;
49-
class TargetLibraryInfo;
50-
void readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
51-
const TargetLibraryInfo &TLI);
51+
class MemProfUsePass : public PassInfoMixin<MemProfUsePass> {
52+
public:
53+
explicit MemProfUsePass(std::string MemoryProfileFile,
54+
IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
55+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
56+
57+
private:
58+
std::string MemoryProfileFileName;
59+
IntrusiveRefCntPtr<vfs::FileSystem> FS;
60+
};
5261

5362
} // namespace llvm
5463

llvm/lib/LTO/LTOBackend.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,21 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
236236
auto FS = vfs::getRealFileSystem();
237237
std::optional<PGOOptions> PGOOpt;
238238
if (!Conf.SampleProfile.empty())
239-
PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping, FS,
240-
PGOOptions::SampleUse, PGOOptions::NoCSAction, true);
239+
PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,
240+
/*MemoryProfile=*/"", FS, PGOOptions::SampleUse,
241+
PGOOptions::NoCSAction, true);
241242
else if (Conf.RunCSIRInstr) {
242-
PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping, FS,
243-
PGOOptions::IRUse, PGOOptions::CSIRInstr,
244-
Conf.AddFSDiscriminator);
243+
PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping,
244+
/*MemoryProfile=*/"", FS, PGOOptions::IRUse,
245+
PGOOptions::CSIRInstr, Conf.AddFSDiscriminator);
245246
} else if (!Conf.CSIRProfile.empty()) {
246-
PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping, FS,
247-
PGOOptions::IRUse, PGOOptions::CSIRUse,
248-
Conf.AddFSDiscriminator);
247+
PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping,
248+
/*MemoryProfile=*/"", FS, PGOOptions::IRUse,
249+
PGOOptions::CSIRUse, Conf.AddFSDiscriminator);
249250
NoPGOWarnMismatch = !Conf.PGOWarnMismatch;
250251
} else if (Conf.AddFSDiscriminator) {
251-
PGOOpt = PGOOptions("", "", "", nullptr, PGOOptions::NoAction,
252-
PGOOptions::NoCSAction, true);
252+
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
253+
PGOOptions::NoAction, PGOOptions::NoCSAction, true);
253254
}
254255
TM->setPGOOption(PGOOpt);
255256

llvm/lib/Passes/PassBuilder.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,23 @@ Expected<bool> parseMemorySSAPrinterPassOptions(StringRef Params) {
10711071
"MemorySSAPrinterPass");
10721072
}
10731073

1074+
Expected<std::string> parseMemProfUsePassOptions(StringRef Params) {
1075+
std::string Result;
1076+
while (!Params.empty()) {
1077+
StringRef ParamName;
1078+
std::tie(ParamName, Params) = Params.split(';');
1079+
1080+
if (ParamName.consume_front("profile-filename=")) {
1081+
Result = ParamName.str();
1082+
} else {
1083+
return make_error<StringError>(
1084+
formatv("invalid MemProfUse pass parameter '{0}' ", ParamName).str(),
1085+
inconvertibleErrorCode());
1086+
}
1087+
}
1088+
return Result;
1089+
}
1090+
10741091
} // namespace
10751092

10761093
/// Tests whether a pass name starts with a valid prefix for a default pipeline

llvm/lib/Passes/PassBuilderPipelines.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,10 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
11021102
PGOOpt->CSAction == PGOOptions::CSIRInstr)
11031103
MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile));
11041104

1105+
if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink &&
1106+
!PGOOpt->MemoryProfile.empty())
1107+
MPM.addPass(MemProfUsePass(PGOOpt->MemoryProfile, PGOOpt->FS));
1108+
11051109
// Synthesize function entry counts for non-PGO compilation.
11061110
if (EnableSyntheticCounts && !PGOOpt)
11071111
MPM.addPass(SyntheticCountsPropagation());

llvm/lib/Passes/PassRegistry.def

+7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ MODULE_PASS_WITH_PARAMS("embed-bitcode",
183183
},
184184
parseEmbedBitcodePassOptions,
185185
"thinlto;emit-summary")
186+
MODULE_PASS_WITH_PARAMS("memprof-use",
187+
"MemProfUsePass",
188+
[](std::string Opts) {
189+
return MemProfUsePass(Opts);
190+
},
191+
parseMemProfUsePassOptions,
192+
"profile-filename=S")
186193
#undef MODULE_PASS_WITH_PARAMS
187194

188195
#ifndef CGSCC_ANALYSIS

llvm/lib/Support/PGOOptions.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ using namespace llvm;
1313

1414
PGOOptions::PGOOptions(std::string ProfileFile, std::string CSProfileGenFile,
1515
std::string ProfileRemappingFile,
16+
std::string MemoryProfile,
1617
IntrusiveRefCntPtr<vfs::FileSystem> FS, PGOAction Action,
1718
CSPGOAction CSAction, bool DebugInfoForProfiling,
1819
bool PseudoProbeForProfiling)
1920
: ProfileFile(ProfileFile), CSProfileGenFile(CSProfileGenFile),
20-
ProfileRemappingFile(ProfileRemappingFile), Action(Action),
21-
CSAction(CSAction),
21+
ProfileRemappingFile(ProfileRemappingFile), MemoryProfile(MemoryProfile),
22+
Action(Action), CSAction(CSAction),
2223
DebugInfoForProfiling(DebugInfoForProfiling ||
2324
(Action == SampleUse && !PseudoProbeForProfiling)),
2425
PseudoProbeForProfiling(PseudoProbeForProfiling), FS(std::move(FS)) {
@@ -36,13 +37,18 @@ PGOOptions::PGOOptions(std::string ProfileFile, std::string CSProfileGenFile,
3637
// a profile.
3738
assert(this->CSAction != CSIRUse || this->Action == IRUse);
3839

39-
// If neither Action nor CSAction, DebugInfoForProfiling or
40-
// PseudoProbeForProfiling needs to be true.
40+
// Cannot optimize with MemProf profile during IR instrumentation.
41+
assert(this->MemoryProfile.empty() || this->Action != PGOOptions::IRInstr);
42+
43+
// If neither Action nor CSAction nor MemoryProfile are set,
44+
// DebugInfoForProfiling or PseudoProbeForProfiling needs to be true.
4145
assert(this->Action != NoAction || this->CSAction != NoCSAction ||
42-
this->DebugInfoForProfiling || this->PseudoProbeForProfiling);
46+
!this->MemoryProfile.empty() || this->DebugInfoForProfiling ||
47+
this->PseudoProbeForProfiling);
4348

4449
// If we need to use the profile, the VFS cannot be nullptr.
45-
assert(this->FS || !(this->Action == IRUse || this->CSAction == CSIRUse));
50+
assert(this->FS || !(this->Action == IRUse || this->CSAction == CSIRUse ||
51+
!this->MemoryProfile.empty()));
4652
}
4753

4854
PGOOptions::PGOOptions(const PGOOptions &) = default;

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

+50-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/Support/CommandLine.h"
3939
#include "llvm/Support/Debug.h"
4040
#include "llvm/Support/HashBuilder.h"
41+
#include "llvm/Support/VirtualFileSystem.h"
4142
#include "llvm/TargetParser/Triple.h"
4243
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
4344
#include "llvm/Transforms/Utils/ModuleUtils.h"
@@ -673,9 +674,9 @@ stackFrameIncludesInlinedCallStack(ArrayRef<Frame> ProfileCallStack,
673674
return InlCallStackIter == InlinedCallStack.end();
674675
}
675676

676-
void llvm::readMemprof(Module &M, Function &F,
677-
IndexedInstrProfReader *MemProfReader,
678-
const TargetLibraryInfo &TLI) {
677+
static void readMemprof(Module &M, Function &F,
678+
IndexedInstrProfReader *MemProfReader,
679+
const TargetLibraryInfo &TLI) {
679680
auto &Ctx = M.getContext();
680681

681682
auto FuncName = getPGOFuncName(F);
@@ -865,3 +866,49 @@ void llvm::readMemprof(Module &M, Function &F,
865866
}
866867
}
867868
}
869+
870+
MemProfUsePass::MemProfUsePass(std::string MemoryProfileFile,
871+
IntrusiveRefCntPtr<vfs::FileSystem> FS)
872+
: MemoryProfileFileName(MemoryProfileFile), FS(FS) {
873+
if (!FS)
874+
this->FS = vfs::getRealFileSystem();
875+
}
876+
877+
PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
878+
LLVM_DEBUG(dbgs() << "Read in memory profile:");
879+
auto &Ctx = M.getContext();
880+
auto ReaderOrErr = IndexedInstrProfReader::create(MemoryProfileFileName, *FS);
881+
if (Error E = ReaderOrErr.takeError()) {
882+
handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
883+
Ctx.diagnose(
884+
DiagnosticInfoPGOProfile(MemoryProfileFileName.data(), EI.message()));
885+
});
886+
return PreservedAnalyses::all();
887+
}
888+
889+
std::unique_ptr<IndexedInstrProfReader> MemProfReader =
890+
std::move(ReaderOrErr.get());
891+
if (!MemProfReader) {
892+
Ctx.diagnose(DiagnosticInfoPGOProfile(
893+
MemoryProfileFileName.data(), StringRef("Cannot get MemProfReader")));
894+
return PreservedAnalyses::all();
895+
}
896+
897+
if (!MemProfReader->hasMemoryProfile()) {
898+
Ctx.diagnose(DiagnosticInfoPGOProfile(MemoryProfileFileName.data(),
899+
"Not a memory profile"));
900+
return PreservedAnalyses::all();
901+
}
902+
903+
auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
904+
905+
for (auto &F : M) {
906+
if (F.isDeclaration())
907+
continue;
908+
909+
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
910+
readMemprof(M, F, MemProfReader.get(), TLI);
911+
}
912+
913+
return PreservedAnalyses::none();
914+
}

0 commit comments

Comments
 (0)