3939#include " llvm/Support/CommandLine.h"
4040#include " llvm/Support/Debug.h"
4141#include " llvm/Support/HashBuilder.h"
42+ #include " llvm/Support/VirtualFileSystem.h"
4243#include " llvm/TargetParser/Triple.h"
4344#include " llvm/Transforms/Utils/BasicBlockUtils.h"
4445#include " llvm/Transforms/Utils/ModuleUtils.h"
@@ -54,7 +55,6 @@ namespace llvm {
5455extern cl::opt<bool > PGOWarnMissing;
5556extern cl::opt<bool > NoPGOWarnMismatch;
5657extern cl::opt<bool > NoPGOWarnMismatchComdatWeak;
57- using AllocMatchInfo = ::llvm::MemProfUsePass::AllocMatchInfo;
5858} // namespace llvm
5959
6060constexpr int LLVM_MEM_PROFILER_VERSION = 1 ;
@@ -148,11 +148,10 @@ static cl::opt<int> ClDebugMax("memprof-debug-max", cl::desc("Debug max inst"),
148148
149149// By default disable matching of allocation profiles onto operator new that
150150// already explicitly pass a hot/cold hint, since we don't currently
151- // override these hints anyway. Not static so that it can be set in the unit
152- // test too.
153- cl::opt<bool > ClMemProfMatchHotColdNew (
151+ // override these hints anyway.
152+ static cl::opt<bool > ClMemProfMatchHotColdNew (
154153 " memprof-match-hot-cold-new" ,
155- cl::desc (
154+ cl::desc (
156155 " Match allocation profiles onto existing hot/cold operator new calls" ),
157156 cl::Hidden, cl::init(false ));
158157
@@ -790,11 +789,17 @@ static bool isAllocationWithHotColdVariant(Function *Callee,
790789 }
791790}
792791
793- void MemProfUsePass::readMemprof (
794- Function &F, const IndexedMemProfReader &MemProfReader,
795- const TargetLibraryInfo &TLI,
796- std::map<uint64_t , AllocMatchInfo> &FullStackIdToAllocMatchInfo) {
797- auto &Ctx = F.getContext ();
792+ struct AllocMatchInfo {
793+ uint64_t TotalSize = 0 ;
794+ AllocationType AllocType = AllocationType::None;
795+ bool Matched = false ;
796+ };
797+
798+ static void
799+ readMemprof (Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
800+ const TargetLibraryInfo &TLI,
801+ std::map<uint64_t , AllocMatchInfo> &FullStackIdToAllocMatchInfo) {
802+ auto &Ctx = M.getContext ();
798803 // Previously we used getIRPGOFuncName() here. If F is local linkage,
799804 // getIRPGOFuncName() returns FuncName with prefix 'FileName;'. But
800805 // llvm-profdata uses FuncName in dwarf to create GUID which doesn't
@@ -805,7 +810,7 @@ void MemProfUsePass::readMemprof(
805810 auto FuncName = F.getName ();
806811 auto FuncGUID = Function::getGUID (FuncName);
807812 std::optional<memprof::MemProfRecord> MemProfRec;
808- auto Err = MemProfReader. getMemProfRecord (FuncGUID).moveInto (MemProfRec);
813+ auto Err = MemProfReader-> getMemProfRecord (FuncGUID).moveInto (MemProfRec);
809814 if (Err) {
810815 handleAllErrors (std::move (Err), [&](const InstrProfError &IPE) {
811816 auto Err = IPE.get ();
@@ -833,8 +838,8 @@ void MemProfUsePass::readMemprof(
833838 Twine (" Hash = " ) + std::to_string (FuncGUID))
834839 .str ();
835840
836- Ctx.diagnose (DiagnosticInfoPGOProfile (F. getParent ()-> getName (). data (),
837- Msg, DS_Warning));
841+ Ctx.diagnose (
842+ DiagnosticInfoPGOProfile (M. getName (). data (), Msg, DS_Warning));
838843 });
839844 return ;
840845 }
@@ -1031,15 +1036,15 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
10311036 return PreservedAnalyses::all ();
10321037 }
10331038
1034- std::unique_ptr<IndexedInstrProfReader> IndexedReader =
1039+ std::unique_ptr<IndexedInstrProfReader> MemProfReader =
10351040 std::move (ReaderOrErr.get ());
1036- if (!IndexedReader ) {
1041+ if (!MemProfReader ) {
10371042 Ctx.diagnose (DiagnosticInfoPGOProfile (
1038- MemoryProfileFileName.data (), StringRef (" Cannot get IndexedReader " )));
1043+ MemoryProfileFileName.data (), StringRef (" Cannot get MemProfReader " )));
10391044 return PreservedAnalyses::all ();
10401045 }
10411046
1042- if (!IndexedReader ->hasMemoryProfile ()) {
1047+ if (!MemProfReader ->hasMemoryProfile ()) {
10431048 Ctx.diagnose (DiagnosticInfoPGOProfile (MemoryProfileFileName.data (),
10441049 " Not a memory profile" ));
10451050 return PreservedAnalyses::all ();
@@ -1052,13 +1057,12 @@ PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) {
10521057 // it to an allocation in the IR.
10531058 std::map<uint64_t , AllocMatchInfo> FullStackIdToAllocMatchInfo;
10541059
1055- const auto &MemProfReader = IndexedReader->getIndexedMemProfReader ();
10561060 for (auto &F : M) {
10571061 if (F.isDeclaration ())
10581062 continue ;
10591063
10601064 const TargetLibraryInfo &TLI = FAM.getResult <TargetLibraryAnalysis>(F);
1061- readMemprof (F, MemProfReader, TLI, FullStackIdToAllocMatchInfo);
1065+ readMemprof (M, F, MemProfReader. get () , TLI, FullStackIdToAllocMatchInfo);
10621066 }
10631067
10641068 if (ClPrintMemProfMatchInfo) {
0 commit comments