[llvm] 1ed69bb - [llvm-profgen] Fix a dangling vector reference in CS line number based generator

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 22 18:34:08 PDT 2021


Author: wlei
Date: 2021-09-22T18:33:28-07:00
New Revision: 1ed69bb86eb188ab23f62c266d2d23846588e768

URL: https://github.com/llvm/llvm-project/commit/1ed69bb86eb188ab23f62c266d2d23846588e768
DIFF: https://github.com/llvm/llvm-project/commit/1ed69bb86eb188ab23f62c266d2d23846588e768.diff

LOG: [llvm-profgen] Fix a dangling vector reference in CS line number based generator

It seems we missed one spot to persist `SampleContextFrameVector` into the global table (CSProfileGenerator::populateFunctionBoundarySamples:340) which causes a crash.

This change tried to fix it in a centralized way i. e. where we generate the `FunctionSamples`.

Reviewed By: hoy, wenlei

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
index 32641328f649..3d3abda731d9 100644
--- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp
+++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
@@ -216,19 +216,21 @@ void ProfileGenerator::findDisjointRanges(RangeSample &DisjointRanges,
   }
 }
 
-FunctionSamples &
-CSProfileGenerator::getFunctionProfileForContext(SampleContextFrames Context,
-                                                 bool WasLeafInlined) {
-  SampleContext FContext(Context);
-  auto Ret = ProfileMap.emplace(Context, FunctionSamples());
-  if (Ret.second) {
-    SampleContext FContext(Context, RawContext);
+FunctionSamples &CSProfileGenerator::getFunctionProfileForContext(
+    const SampleContextFrameVector &Context, bool WasLeafInlined) {
+  auto I = ProfileMap.find(SampleContext(Context));
+  if (I == ProfileMap.end()) {
+    // Save the new context for future references.
+    SampleContextFrames NewContext = *Contexts.insert(Context).first;
+    SampleContext FContext(NewContext, RawContext);
+    auto Ret = ProfileMap.emplace(FContext, FunctionSamples());
     if (WasLeafInlined)
       FContext.setAttribute(ContextWasInlined);
     FunctionSamples &FProfile = Ret.first->second;
     FProfile.setContext(FContext);
+    return Ret.first->second;
   }
-  return Ret.first->second;
+  return I->second;
 }
 
 void CSProfileGenerator::generateProfile() {
@@ -543,11 +545,8 @@ void PseudoProbeCSProfileGenerator::populateBodySamplesWithProbes(
         uint64_t CallerIndex = CallerLeafFrameLoc.Callsite.LineOffset;
         assert(CallerIndex &&
                "Inferred caller's location index shouldn't be zero!");
-        // Save the new context for future references.
-        SampleContextFrames CallerContext =
-            *Contexts.insert(CallerContextId).first;
         FunctionSamples &CallerProfile =
-            getFunctionProfileForContext(CallerContext);
+            getFunctionProfileForContext(CallerContextId);
         CallerProfile.setFunctionHash(InlinerDesc->FuncHash);
         CallerProfile.addBodySamples(CallerIndex, 0, Count);
         CallerProfile.addTotalSamples(Count);
@@ -610,13 +609,11 @@ FunctionSamples &PseudoProbeCSProfileGenerator::getFunctionProfileForLeafProbe(
   CSProfileGenerator::compressRecursionContext(NewContextStack);
   CSProfileGenerator::trimContext(NewContextStack);
   NewContextStack.push_back(LeafFrame);
-  // Save the new context for future references.
-  SampleContextFrames NewContext = *Contexts.insert(NewContextStack).first;
 
   const auto *FuncDesc = Binary->getFuncDescForGUID(LeafProbe->getGuid());
   bool WasLeafInlined = LeafProbe->getInlineTreeNode()->hasInlineSite();
   FunctionSamples &FunctionProile =
-      getFunctionProfileForContext(NewContext, WasLeafInlined);
+      getFunctionProfileForContext(NewContextStack, WasLeafInlined);
   FunctionProile.setFunctionHash(FuncDesc->FuncHash);
   return FunctionProile;
 }

diff  --git a/llvm/tools/llvm-profgen/ProfileGenerator.h b/llvm/tools/llvm-profgen/ProfileGenerator.h
index 0b065d70e9ba..96b3675cdb86 100644
--- a/llvm/tools/llvm-profgen/ProfileGenerator.h
+++ b/llvm/tools/llvm-profgen/ProfileGenerator.h
@@ -190,8 +190,9 @@ class CSProfileGenerator : public ProfileGenerator {
 
 protected:
   // Lookup or create FunctionSamples for the context
-  FunctionSamples &getFunctionProfileForContext(SampleContextFrames ContextId,
-                                                bool WasLeafInlined = false);
+  FunctionSamples &
+  getFunctionProfileForContext(const SampleContextFrameVector &Context,
+                               bool WasLeafInlined = false);
   // Post processing for profiles before writing out, such as mermining
   // and trimming cold profiles, running preinliner on profiles.
   void postProcessProfiles();
@@ -203,6 +204,9 @@ class CSProfileGenerator : public ProfileGenerator {
   uint64_t HotCountThreshold;
   uint64_t ColdCountThreshold;
 
+  // Underlying context table serves for sample profile writer.
+  std::unordered_set<SampleContextFrameVector, SampleContextFrameHash> Contexts;
+
 private:
   // Helper function for updating body sample for a leaf location in
   // FunctionProfile
@@ -249,9 +253,6 @@ class PseudoProbeCSProfileGenerator : public CSProfileGenerator {
   FunctionSamples &
   getFunctionProfileForLeafProbe(SampleContextFrames ContextStack,
                                  const MCDecodedPseudoProbe *LeafProbe);
-
-  // Underlying context table serves for sample profile writer.
-  std::unordered_set<SampleContextFrameVector, SampleContextFrameHash> Contexts;
 };
 
 } // end namespace sampleprof


        


More information about the llvm-commits mailing list