[llvm] a1e2186 - [SamplePGO] Fixing a memory issue when creating profiles on-demand
Hongtao Yu via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 16 16:30:36 PDT 2021
Author: Hongtao Yu
Date: 2021-08-16T16:30:26-07:00
New Revision: a1e21864df68dd38a308ad6fae965dea52dcde0c
URL: https://github.com/llvm/llvm-project/commit/a1e21864df68dd38a308ad6fae965dea52dcde0c
DIFF: https://github.com/llvm/llvm-project/commit/a1e21864df68dd38a308ad6fae965dea52dcde0c.diff
LOG: [SamplePGO] Fixing a memory issue when creating profiles on-demand
There is a on-dmeand creation of function profile during top-down processing in the sample loader when merging uninlined callees. During the profile creation, a stack string object is used to store a newly-created MD5 name, which is then used by reference as hash key in the profile map. This makes the hash key a dangling reference when later on the stack string object is deallocated.
The issue only happens with md5 profile use and was exposed by context split work for CS profile. I'm making a fix by storing newly created names in the reader.
Reviewed By: wenlei, wmi, wlei
Differential Revision: https://reviews.llvm.org/D108142
Added:
Modified:
llvm/include/llvm/ProfileData/SampleProf.h
llvm/include/llvm/ProfileData/SampleProfReader.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h
index 2f71bbc6bbbe6..41abde767d888 100644
--- a/llvm/include/llvm/ProfileData/SampleProf.h
+++ b/llvm/include/llvm/ProfileData/SampleProf.h
@@ -104,10 +104,10 @@ static inline uint64_t SPMagic(SampleProfileFormat Format = SPF_Binary) {
/// 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
index 2d5925bdb2b43..eae312477199a 100644
--- a/llvm/include/llvm/ProfileData/SampleProfReader.h
+++ b/llvm/include/llvm/ProfileData/SampleProfReader.h
@@ -245,6 +245,7 @@
#include <memory>
#include <string>
#include <system_error>
+#include <unordered_set>
#include <vector>
namespace llvm {
@@ -407,6 +408,13 @@ class SampleProfileReader {
std::string FGUID;
StringRef CanonName = FunctionSamples::getCanonicalFnName(F);
CanonName = getRepInFormat(CanonName, useMD5(), FGUID);
+ auto It = Profiles.find(CanonName);
+ if (It != Profiles.end())
+ return &It->second;
+ if (!FGUID.empty()) {
+ assert(useMD5() && "New name should only be generated for md5 profile");
+ CanonName = *MD5NameBuffer.insert(FGUID).first;
+ }
return &Profiles[CanonName];
}
@@ -503,6 +511,10 @@ class SampleProfileReader {
/// Memory buffer holding the profile file.
std::unique_ptr<MemoryBuffer> Buffer;
+ /// Extra name buffer holding names created on demand.
+ /// This should only be needed for md5 profiles.
+ std::unordered_set<std::string> MD5NameBuffer;
+
/// Profile summary information.
std::unique_ptr<ProfileSummary> Summary;
More information about the llvm-commits
mailing list