[llvm] 91cc53d - [llvm-profgen] Do not cache the frame location stack during computing inlined context size

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 25 21:11:02 PDT 2022


Author: wlei
Date: 2022-10-25T21:08:36-07:00
New Revision: 91cc53d5a455e807507b08ca976e266621da07a8

URL: https://github.com/llvm/llvm-project/commit/91cc53d5a455e807507b08ca976e266621da07a8
DIFF: https://github.com/llvm/llvm-project/commit/91cc53d5a455e807507b08ca976e266621da07a8.diff

LOG: [llvm-profgen] Do not cache the frame location stack during computing inlined context size

In `computeInlinedContextSizeForRange`, the offset of range is only used one time, there is no need to cache the frame location stack.
Measured on one internal service binary, this can save 2GB memory usage and reduce a small run time (avoid one hash search).

Reviewed By: hoy, wenlei

Differential Revision: https://reviews.llvm.org/D128859

Added: 
    

Modified: 
    llvm/tools/llvm-profgen/ProfileGenerator.cpp
    llvm/tools/llvm-profgen/ProfiledBinary.cpp
    llvm/tools/llvm-profgen/ProfiledBinary.h

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
index b67fe1ceae50..dbf10402f73a 100644
--- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp
+++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
@@ -667,7 +667,7 @@ void ProfileGenerator::populateBodySamplesForAllFunctions(
       continue;
 
     do {
-      const SampleContextFrameVector &FrameVec =
+      const SampleContextFrameVector FrameVec =
           Binary->getFrameLocationStack(IP.Address);
       if (!FrameVec.empty()) {
         // FIXME: As accumulating total count per instruction caused some
@@ -709,7 +709,7 @@ void ProfileGenerator::populateBoundarySamplesForAllFunctions(
       continue;
     // Record called target sample and its count.
     const SampleContextFrameVector &FrameVec =
-        Binary->getFrameLocationStack(SourceAddress);
+        Binary->getCachedFrameLocationStack(SourceAddress);
     if (!FrameVec.empty()) {
       FunctionSamples &FunctionProfile =
           getLeafProfileAndAddTotalSamples(FrameVec, 0);

diff  --git a/llvm/tools/llvm-profgen/ProfiledBinary.cpp b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
index 7648c8e64f57..2c9873cf18aa 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.cpp
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
@@ -233,8 +233,10 @@ void ProfiledBinary::load() {
 }
 
 bool ProfiledBinary::inlineContextEqual(uint64_t Address1, uint64_t Address2) {
-  const SampleContextFrameVector &Context1 = getFrameLocationStack(Address1);
-  const SampleContextFrameVector &Context2 = getFrameLocationStack(Address2);
+  const SampleContextFrameVector &Context1 =
+      getCachedFrameLocationStack(Address1);
+  const SampleContextFrameVector &Context2 =
+      getCachedFrameLocationStack(Address2);
   if (Context1.size() != Context2.size())
     return false;
   if (Context1.empty())
@@ -254,7 +256,7 @@ ProfiledBinary::getExpandedContext(const SmallVectorImpl<uint64_t> &Stack,
   // Process from frame root to leaf
   for (auto Address : Stack) {
     const SampleContextFrameVector &ExpandedContext =
-        getFrameLocationStack(Address);
+        getCachedFrameLocationStack(Address);
     // An instruction without a valid debug line will be ignored by sample
     // processing
     if (ExpandedContext.empty())
@@ -806,10 +808,9 @@ void ProfiledBinary::computeInlinedContextSizeForRange(uint64_t RangeBegin,
     return;
 
   do {
-    const SampleContextFrameVector &SymbolizedCallStack =
+    const SampleContextFrameVector SymbolizedCallStack =
         getFrameLocationStack(IP.Address, UsePseudoProbes);
     uint64_t Size = AddressToInstSizeMap[IP.Address];
-
     // Record instruction size for the corresponding context
     FuncSizeTracker.addInstructionForContext(SymbolizedCallStack, Size);
 

diff  --git a/llvm/tools/llvm-profgen/ProfiledBinary.h b/llvm/tools/llvm-profgen/ProfiledBinary.h
index 4bc58ef3f2d8..2073b4e8ed21 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.h
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.h
@@ -477,18 +477,24 @@ class ProfiledBinary {
   // Load the symbols from debug table and populate into symbol list.
   void populateSymbolListFromDWARF(ProfileSymbolList &SymbolList);
 
-  const SampleContextFrameVector &
+  SampleContextFrameVector
   getFrameLocationStack(uint64_t Address, bool UseProbeDiscriminator = false) {
+    InstructionPointer IP(this, Address);
+    return symbolize(IP, true, UseProbeDiscriminator);
+  }
+
+  const SampleContextFrameVector &
+  getCachedFrameLocationStack(uint64_t Address,
+                              bool UseProbeDiscriminator = false) {
     auto I = AddressToLocStackMap.emplace(Address, SampleContextFrameVector());
     if (I.second) {
-      InstructionPointer IP(this, Address);
-      I.first->second = symbolize(IP, true, UseProbeDiscriminator);
+      I.first->second = getFrameLocationStack(Address, UseProbeDiscriminator);
     }
     return I.first->second;
   }
 
-  Optional<SampleContextFrame> getInlineLeafFrameLoc(uint64_t Address) {
-    const auto &Stack = getFrameLocationStack(Address);
+  Optional<SampleContextFrame> getInlineLeafFrameLoc(uint64_t Offset) {
+    const auto &Stack = getCachedFrameLocationStack(Offset);
     if (Stack.empty())
       return {};
     return Stack.back();


        


More information about the llvm-commits mailing list