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 @@ -104,10 +104,10 @@ /// current Format uses MD5 to represent the string. static inline StringRef getRepInFormat(StringRef Name, bool UseMD5, std::string &GUIDBuf) { - if (Name.empty()) + if (Name.empty() || !UseMD5) return Name; GUIDBuf = std::to_string(Function::getGUID(Name)); - return UseMD5 ? StringRef(GUIDBuf) : Name; + return GUIDBuf; } static inline uint64_t SPVersion() { return 103; } diff --git a/llvm/include/llvm/ProfileData/SampleProfReader.h b/llvm/include/llvm/ProfileData/SampleProfReader.h --- a/llvm/include/llvm/ProfileData/SampleProfReader.h +++ b/llvm/include/llvm/ProfileData/SampleProfReader.h @@ -407,7 +407,15 @@ std::string FGUID; StringRef CanonName = FunctionSamples::getCanonicalFnName(F); CanonName = getRepInFormat(CanonName, useMD5(), FGUID); - return &Profiles[CanonName]; + auto It = Profiles.find(CanonName); + if (It == Profiles.end()) { + if (!FGUID.empty()) { + assert(useMD5()); + CanonName = *MD5NameBuffer.insert(FGUID).first; + } + return &Profiles[CanonName]; + } + return &It->second; } /// Return the samples collected for function \p F. @@ -503,6 +511,10 @@ /// Memory buffer holding the profile file. std::unique_ptr Buffer; + /// Extra name buffer holding names created on demand. + /// This should only be needed for md5 profiles. + std::set MD5NameBuffer; + /// Profile summary information. std::unique_ptr Summary;