[PATCH] D158689: [llvm-profdata] Fix dangling reference after D147740
William Junda Huang via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 28 17:37:30 PDT 2023
huangjd updated this revision to Diff 554123.
huangjd edited the summary of this revision.
huangjd added a comment.
Update patch to use llvm::DenseMap, use postorder in flattenNestedProfile to prevent reference reassignment
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D158689/new/
https://reviews.llvm.org/D158689
Files:
llvm/include/llvm/ProfileData/SampleProf.h
Index: llvm/include/llvm/ProfileData/SampleProf.h
===================================================================
--- llvm/include/llvm/ProfileData/SampleProf.h
+++ llvm/include/llvm/ProfileData/SampleProf.h
@@ -1412,7 +1412,7 @@
/// Note: when populating container, make sure to assign the SampleContext to
/// the mapped value immediately because the key no longer holds it.
class SampleProfileMap
- : public HashKeyMap<std::unordered_map, SampleContext, FunctionSamples> {
+ : public HashKeyMap<llvm::DenseMap, SampleContext, FunctionSamples> {
public:
// Convenience method because this is being used in many places. Set the
// FunctionSamples' context if its newly inserted.
@@ -1424,12 +1424,12 @@
}
iterator find(const SampleContext &Ctx) {
- return HashKeyMap<std::unordered_map, SampleContext, FunctionSamples>::find(
+ return HashKeyMap<llvm::DenseMap, SampleContext, FunctionSamples>::find(
Ctx);
}
const_iterator find(const SampleContext &Ctx) const {
- return HashKeyMap<std::unordered_map, SampleContext, FunctionSamples>::find(
+ return HashKeyMap<llvm::DenseMap, SampleContext, FunctionSamples>::find(
Ctx);
}
@@ -1449,8 +1449,8 @@
}
size_t erase(const SampleContext &Ctx) {
- return HashKeyMap<std::unordered_map, SampleContext, FunctionSamples>::
- erase(Ctx);
+ return HashKeyMap<llvm::DenseMap, SampleContext, FunctionSamples>::erase(
+ Ctx);
}
size_t erase(const key_type &Key) { return base_type::erase(Key); }
@@ -1562,6 +1562,17 @@
private:
static void flattenNestedProfile(SampleProfileMap &OutputProfiles,
const FunctionSamples &FS) {
+ // Use postorder traversal here so that the reference to the newly added
+ // profile remains valid for duration of the function, even if the output
+ // profile map relocates.
+ for (const auto &I : FS.getCallsiteSamples()) {
+ for (const auto &Callee : I.second) {
+ const auto &CalleeProfile = Callee.second;
+ // Recursively convert callee profile.
+ flattenNestedProfile(OutputProfiles, CalleeProfile);
+ }
+ }
+
// To retain the context, checksum, attributes of the original profile, make
// a copy of it if no profile is found.
SampleContext &Context = FS.getContext();
@@ -1603,10 +1614,9 @@
? TotalSamples - CalleeProfile.getTotalSamples()
: 0;
TotalSamples += CalleeProfile.getHeadSamplesEstimate();
- // Recursively convert callee profile.
- flattenNestedProfile(OutputProfiles, CalleeProfile);
}
}
+
Profile.addTotalSamples(TotalSamples);
Profile.setHeadSamples(Profile.getHeadSamplesEstimate());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158689.554123.patch
Type: text/x-patch
Size: 2783 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230829/a6d64484/attachment.bin>
More information about the llvm-commits
mailing list