[PATCH] D158689: [llvm-profdata] Use llvm::DenseMap in SampleProfileMap

William Junda Huang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 28 17:48:38 PDT 2023


huangjd updated this revision to Diff 554125.
huangjd edited the summary of this revision.
huangjd added a comment.

nit: remove extra new line


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,8 +1614,6 @@
                            ? TotalSamples - CalleeProfile.getTotalSamples()
                            : 0;
         TotalSamples += CalleeProfile.getHeadSamplesEstimate();
-        // Recursively convert callee profile.
-        flattenNestedProfile(OutputProfiles, CalleeProfile);
       }
     }
     Profile.addTotalSamples(TotalSamples);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158689.554125.patch
Type: text/x-patch
Size: 2715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230829/170ea8b0/attachment.bin>


More information about the llvm-commits mailing list