diff options
author | Teresa Johnson <tejohnson@google.com> | 2023-07-11 12:08:08 -0700 |
---|---|---|
committer | Teresa Johnson <tejohnson@google.com> | 2023-07-11 13:16:20 -0700 |
commit | 546ec641b4b1bbbf9e66a53983b635fe85d365e6 (patch) | |
tree | 8fd8db251d11189371e8805ae74d35ebab5edb7d /llvm/lib | |
parent | 95014050dac528f53ab704d62a16a44ed0df032b (diff) | |
download | llvm-546ec641b4b1bbbf9e66a53983b635fe85d365e6.zip llvm-546ec641b4b1bbbf9e66a53983b635fe85d365e6.tar.gz llvm-546ec641b4b1bbbf9e66a53983b635fe85d365e6.tar.bz2 |
Restore "[MemProf] Use new option/pass for profile feedback and matching"
This restores commit b4a82b62258c5f650a1cccf5b179933e6bae4867, reverted
in 3ab7ef28eebf9019eb3d3c4efd7ebfd160106bb1 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
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/LTO/LTOBackend.cpp | 21 | ||||
-rw-r--r-- | llvm/lib/Passes/PassBuilder.cpp | 17 | ||||
-rw-r--r-- | llvm/lib/Passes/PassBuilderPipelines.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/Passes/PassRegistry.def | 7 | ||||
-rw-r--r-- | llvm/lib/Support/PGOOptions.cpp | 18 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/MemProfiler.cpp | 53 | ||||
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp | 11 |
7 files changed, 102 insertions, 29 deletions
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index 897380c..29e2887 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -236,20 +236,21 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM, auto FS = vfs::getRealFileSystem(); std::optional<PGOOptions> PGOOpt; if (!Conf.SampleProfile.empty()) - PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping, FS, - PGOOptions::SampleUse, PGOOptions::NoCSAction, true); + PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping, + /*MemoryProfile=*/"", FS, PGOOptions::SampleUse, + PGOOptions::NoCSAction, true); else if (Conf.RunCSIRInstr) { - PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping, FS, - PGOOptions::IRUse, PGOOptions::CSIRInstr, - Conf.AddFSDiscriminator); + PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping, + /*MemoryProfile=*/"", FS, PGOOptions::IRUse, + PGOOptions::CSIRInstr, Conf.AddFSDiscriminator); } else if (!Conf.CSIRProfile.empty()) { - PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping, FS, - PGOOptions::IRUse, PGOOptions::CSIRUse, - Conf.AddFSDiscriminator); + PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping, + /*MemoryProfile=*/"", FS, PGOOptions::IRUse, + PGOOptions::CSIRUse, Conf.AddFSDiscriminator); NoPGOWarnMismatch = !Conf.PGOWarnMismatch; } else if (Conf.AddFSDiscriminator) { - PGOOpt = PGOOptions("", "", "", nullptr, PGOOptions::NoAction, - PGOOptions::NoCSAction, true); + PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr, + PGOOptions::NoAction, PGOOptions::NoCSAction, true); } TM->setPGOOption(PGOOpt); diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index aea3873..d0cbbcc 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -1071,6 +1071,23 @@ Expected<bool> parseMemorySSAPrinterPassOptions(StringRef Params) { "MemorySSAPrinterPass"); } +Expected<std::string> parseMemProfUsePassOptions(StringRef Params) { + std::string Result; + while (!Params.empty()) { + StringRef ParamName; + std::tie(ParamName, Params) = Params.split(';'); + + if (ParamName.consume_front("profile-filename=")) { + Result = ParamName.str(); + } else { + return make_error<StringError>( + formatv("invalid MemProfUse pass parameter '{0}' ", ParamName).str(), + inconvertibleErrorCode()); + } + } + return Result; +} + } // namespace /// Tests whether a pass name starts with a valid prefix for a default pipeline diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp index 3756540..660cb2e 100644 --- a/llvm/lib/Passes/PassBuilderPipelines.cpp +++ b/llvm/lib/Passes/PassBuilderPipelines.cpp @@ -1102,6 +1102,10 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, PGOOpt->CSAction == PGOOptions::CSIRInstr) MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile)); + if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && + !PGOOpt->MemoryProfile.empty()) + MPM.addPass(MemProfUsePass(PGOOpt->MemoryProfile, PGOOpt->FS)); + // Synthesize function entry counts for non-PGO compilation. if (EnableSyntheticCounts && !PGOOpt) MPM.addPass(SyntheticCountsPropagation()); diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index 5b0e3b8..e10dc99 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -183,6 +183,13 @@ MODULE_PASS_WITH_PARAMS("embed-bitcode", }, parseEmbedBitcodePassOptions, "thinlto;emit-summary") +MODULE_PASS_WITH_PARAMS("memprof-use", + "MemProfUsePass", + [](std::string Opts) { + return MemProfUsePass(Opts); + }, + parseMemProfUsePassOptions, + "profile-filename=S") #undef MODULE_PASS_WITH_PARAMS #ifndef CGSCC_ANALYSIS diff --git a/llvm/lib/Support/PGOOptions.cpp b/llvm/lib/Support/PGOOptions.cpp index d11528c..04d50cc 100644 --- a/llvm/lib/Support/PGOOptions.cpp +++ b/llvm/lib/Support/PGOOptions.cpp @@ -13,12 +13,13 @@ using namespace llvm; PGOOptions::PGOOptions(std::string ProfileFile, std::string CSProfileGenFile, std::string ProfileRemappingFile, + std::string MemoryProfile, IntrusiveRefCntPtr<vfs::FileSystem> FS, PGOAction Action, CSPGOAction CSAction, bool DebugInfoForProfiling, bool PseudoProbeForProfiling) : ProfileFile(ProfileFile), CSProfileGenFile(CSProfileGenFile), - ProfileRemappingFile(ProfileRemappingFile), Action(Action), - CSAction(CSAction), + ProfileRemappingFile(ProfileRemappingFile), MemoryProfile(MemoryProfile), + Action(Action), CSAction(CSAction), DebugInfoForProfiling(DebugInfoForProfiling || (Action == SampleUse && !PseudoProbeForProfiling)), PseudoProbeForProfiling(PseudoProbeForProfiling), FS(std::move(FS)) { @@ -36,13 +37,18 @@ PGOOptions::PGOOptions(std::string ProfileFile, std::string CSProfileGenFile, // a profile. assert(this->CSAction != CSIRUse || this->Action == IRUse); - // If neither Action nor CSAction, DebugInfoForProfiling or - // PseudoProbeForProfiling needs to be true. + // Cannot optimize with MemProf profile during IR instrumentation. + assert(this->MemoryProfile.empty() || this->Action != PGOOptions::IRInstr); + + // If neither Action nor CSAction nor MemoryProfile are set, + // DebugInfoForProfiling or PseudoProbeForProfiling needs to be true. assert(this->Action != NoAction || this->CSAction != NoCSAction || - this->DebugInfoForProfiling || this->PseudoProbeForProfiling); + !this->MemoryProfile.empty() || this->DebugInfoForProfiling || + this->PseudoProbeForProfiling); // If we need to use the profile, the VFS cannot be nullptr. - assert(this->FS || !(this->Action == IRUse || this->CSAction == CSIRUse)); + assert(this->FS || !(this->Action == IRUse || this->CSAction == CSIRUse || + !this->MemoryProfile.empty())); } PGOOptions::PGOOptions(const PGOOptions &) = default; diff --git a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp index 416060a..789ed00 100644 --- a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp +++ b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp @@ -38,6 +38,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/HashBuilder.h" +#include "llvm/Support/VirtualFileSystem.h" #include "llvm/TargetParser/Triple.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/ModuleUtils.h" @@ -673,9 +674,9 @@ stackFrameIncludesInlinedCallStack(ArrayRef<Frame> ProfileCallStack, return InlCallStackIter == InlinedCallStack.end(); } -void llvm::readMemprof(Module &M, Function &F, - IndexedInstrProfReader *MemProfReader, - const TargetLibraryInfo &TLI) { +static void readMemprof(Module &M, Function &F, + IndexedInstrProfReader *MemProfReader, + const TargetLibraryInfo &TLI) { auto &Ctx = M.getContext(); auto FuncName = getPGOFuncName(F); @@ -865,3 +866,49 @@ void llvm::readMemprof(Module &M, Function &F, } } } + +MemProfUsePass::MemProfUsePass(std::string MemoryProfileFile, + IntrusiveRefCntPtr<vfs::FileSystem> FS) + : MemoryProfileFileName(MemoryProfileFile), FS(FS) { + if (!FS) + this->FS = vfs::getRealFileSystem(); +} + +PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) { + LLVM_DEBUG(dbgs() << "Read in memory profile:"); + auto &Ctx = M.getContext(); + auto ReaderOrErr = IndexedInstrProfReader::create(MemoryProfileFileName, *FS); + if (Error E = ReaderOrErr.takeError()) { + handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { + Ctx.diagnose( + DiagnosticInfoPGOProfile(MemoryProfileFileName.data(), EI.message())); + }); + return PreservedAnalyses::all(); + } + + std::unique_ptr<IndexedInstrProfReader> MemProfReader = + std::move(ReaderOrErr.get()); + if (!MemProfReader) { + Ctx.diagnose(DiagnosticInfoPGOProfile( + MemoryProfileFileName.data(), StringRef("Cannot get MemProfReader"))); + return PreservedAnalyses::all(); + } + + if (!MemProfReader->hasMemoryProfile()) { + Ctx.diagnose(DiagnosticInfoPGOProfile(MemoryProfileFileName.data(), + "Not a memory profile")); + return PreservedAnalyses::all(); + } + + auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); + + for (auto &F : M) { + if (F.isDeclaration()) + continue; + + const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F); + readMemprof(M, F, MemProfReader.get(), TLI); + } + + return PreservedAnalyses::none(); +} diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index 1a67710..82cabbd 100644 --- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -110,7 +110,6 @@ #include "llvm/Transforms/Instrumentation.h" #include "llvm/Transforms/Instrumentation/BlockCoverageInference.h" #include "llvm/Transforms/Instrumentation/CFGMST.h" -#include "llvm/Transforms/Instrumentation/MemProfiler.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/MisExpect.h" #include "llvm/Transforms/Utils/ModuleUtils.h" @@ -1997,7 +1996,7 @@ static bool annotateAllFunctions( return false; // TODO: might need to change the warning once the clang option is finalized. - if (!PGOReader->isIRLevelProfile() && !PGOReader->hasMemoryProfile()) { + if (!PGOReader->isIRLevelProfile()) { Ctx.diagnose(DiagnosticInfoPGOProfile( ProfileFileName.data(), "Not an IR level instrumentation profile")); return false; @@ -2042,14 +2041,6 @@ static bool annotateAllFunctions( } PGOUseFunc Func(F, &M, TLI, ComdatMembers, BPI, BFI, PSI, IsCS, InstrumentFuncEntry, HasSingleByteCoverage); - // Read and match memprof first since we do this via debug info and can - // match even if there is an IR mismatch detected for regular PGO below. - if (PGOReader->hasMemoryProfile()) - readMemprof(M, F, PGOReader.get(), TLI); - - if (!PGOReader->isIRLevelProfile()) - continue; - if (HasSingleByteCoverage) { Func.populateCoverage(PGOReader.get()); continue; |