diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h --- a/llvm/include/llvm/ProfileData/SampleProf.h +++ b/llvm/include/llvm/ProfileData/SampleProf.h @@ -380,6 +380,7 @@ /// GUID to \p S. Also traverse the BodySamples to add hot CallTarget's GUID /// to \p S. void findInlinedFunctions(DenseSet &S, const Module *M, + DenseMap &GUIDToFuncNameMap, uint64_t Threshold) const { if (TotalSamples <= Threshold) return; @@ -389,14 +390,14 @@ for (const auto &BS : BodySamples) for (const auto &TS : BS.second.getCallTargets()) if (TS.getValue() > Threshold) { - const Function *Callee = - M->getFunction(getNameInModule(TS.getKey(), M)); + const Function *Callee = M->getFunction( + getNameInModule(TS.getKey(), M, GUIDToFuncNameMap)); if (!Callee || !Callee->getSubprogram()) S.insert(getGUID(TS.getKey())); } for (const auto &CS : CallsiteSamples) for (const auto &NameFS : CS.second) - NameFS.second.findInlinedFunctions(S, M, Threshold); + NameFS.second.findInlinedFunctions(S, M, GUIDToFuncNameMap, Threshold); } /// Set the name of the function. @@ -406,8 +407,10 @@ StringRef getName() const { return Name; } /// Return the original function name if it exists in Module \p M. - StringRef getFuncNameInModule(const Module *M) const { - return getNameInModule(Name, M); + StringRef + getFuncNameInModule(const Module *M, + DenseMap &GUIDToFuncNameMap) const { + return getNameInModule(Name, M, GUIDToFuncNameMap); } /// Return the canonical name for a function, taking into account @@ -444,12 +447,11 @@ /// is actually GUID of the original function name. getNameInModule will /// translate \p Name in current FunctionSamples into its original name. /// If the original name doesn't exist in \p M, return empty StringRef. - StringRef getNameInModule(StringRef Name, const Module *M) const { + StringRef + getNameInModule(StringRef Name, const Module *M, + DenseMap &GUIDToFuncNameMap) const { if (Format != SPF_Compact_Binary) return Name; - // Expect CurrentModule to be initialized by GUIDToFuncNameMapper. - if (M != CurrentModule) - llvm_unreachable("Input Module should be the same as CurrentModule"); auto iter = GUIDToFuncNameMap.find(std::stoull(Name.data())); if (iter == GUIDToFuncNameMap.end()) return StringRef(); @@ -472,20 +474,19 @@ const FunctionSamples *findFunctionSamples(const DILocation *DIL) const; static SampleProfileFormat Format; - /// GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for - /// all the function symbols defined or declared in CurrentModule. - static DenseMap GUIDToFuncNameMap; - static Module *CurrentModule; class GUIDToFuncNameMapper { public: - GUIDToFuncNameMapper(Module &M) { + GUIDToFuncNameMapper(Module &M, + DenseMap &GUIDToFuncNameMap) + : CurrentGUIDToFuncNameMap(GUIDToFuncNameMap) { if (Format != SPF_Compact_Binary) return; for (const auto &F : M) { StringRef OrigName = F.getName(); - GUIDToFuncNameMap.insert({Function::getGUID(OrigName), OrigName}); + CurrentGUIDToFuncNameMap.insert( + {Function::getGUID(OrigName), OrigName}); /// Local to global var promotion used by optimization like thinlto /// will rename the var and add suffix like ".llvm.xxx" to the /// original local name. In sample profile, the suffixes of function @@ -495,18 +496,20 @@ /// into the GUIDToFuncNameMap. StringRef CanonName = getCanonicalFnName(F); if (CanonName != OrigName) - GUIDToFuncNameMap.insert({Function::getGUID(CanonName), CanonName}); + CurrentGUIDToFuncNameMap.insert( + {Function::getGUID(CanonName), CanonName}); } - CurrentModule = &M; } ~GUIDToFuncNameMapper() { if (Format != SPF_Compact_Binary) return; - GUIDToFuncNameMap.clear(); - CurrentModule = nullptr; + CurrentGUIDToFuncNameMap.clear(); } + + private: + DenseMap &CurrentGUIDToFuncNameMap; }; // Assume the input \p Name is a name coming from FunctionSamples itself. diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp --- a/llvm/lib/ProfileData/SampleProf.cpp +++ b/llvm/lib/ProfileData/SampleProf.cpp @@ -28,8 +28,6 @@ namespace llvm { namespace sampleprof { SampleProfileFormat FunctionSamples::Format; -DenseMap FunctionSamples::GUIDToFuncNameMap; -Module *FunctionSamples::CurrentModule; } // namespace sampleprof } // namespace llvm diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -326,6 +326,10 @@ uint64_t entryCount; }; DenseMap notInlinedCallInfo; + + /// GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for + /// all the function symbols defined or declared in current module. + DenseMap GUIDToFuncNameMap; }; class SampleProfileLoaderLegacyPass : public ModulePass { @@ -823,10 +827,12 @@ for (const auto *FS : findIndirectCallFunctionSamples(*I, Sum)) { if (IsThinLTOPreLink) { FS->findInlinedFunctions(InlinedGUIDs, F.getParent(), + GUIDToFuncNameMap, PSI->getOrCompHotCountThreshold()); continue; } - auto CalleeFunctionName = FS->getFuncNameInModule(F.getParent()); + auto CalleeFunctionName = + FS->getFuncNameInModule(F.getParent(), GUIDToFuncNameMap); // If it is a recursive call, we do not inline it as it could bloat // the code exponentially. There is way to better handle this, e.g. // clone the caller first, and inline the cloned caller if it is @@ -869,7 +875,8 @@ } } else if (IsThinLTOPreLink) { findCalleeFunctionSamples(*I)->findInlinedFunctions( - InlinedGUIDs, F.getParent(), PSI->getOrCompHotCountThreshold()); + InlinedGUIDs, F.getParent(), GUIDToFuncNameMap, + PSI->getOrCompHotCountThreshold()); } } if (LocalChanged) { @@ -1594,7 +1601,7 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM, ProfileSummaryInfo *_PSI) { - FunctionSamples::GUIDToFuncNameMapper Mapper(M); + FunctionSamples::GUIDToFuncNameMapper Mapper(M, GUIDToFuncNameMap); if (!ProfileIsValid) return false; @@ -1651,7 +1658,7 @@ } bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM) { - + DILocation2SampleMap.clear(); // By default the entry count is initialized to -1, which will be treated // conservatively by getEntryCount as the same as unknown (None). This is