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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 20 additions & 14 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 15 additions & 6 deletions
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

Lines changed: 11 additions & 10 deletions
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

Lines changed: 17 additions & 0 deletions
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

0 commit comments

Comments
 (0)