[CSSPGO] Rename the field of SampleContextFrame
authorwlei <wlei@fb.com>
Fri, 1 Oct 2021 23:51:38 +0000 (16:51 -0700)
committerwlei <wlei@fb.com>
Tue, 5 Oct 2021 02:06:59 +0000 (19:06 -0700)
Differential Revision: https://reviews.llvm.org/D110980

llvm/include/llvm/ProfileData/SampleProf.h
llvm/lib/ProfileData/SampleProfWriter.cpp
llvm/lib/Transforms/IPO/SampleContextTracker.cpp
llvm/tools/llvm-profgen/CallContext.h
llvm/tools/llvm-profgen/ProfileGenerator.cpp
llvm/tools/llvm-profgen/ProfiledBinary.cpp

index 2e26c5f..3e0753b 100644 (file)
@@ -412,18 +412,18 @@ enum ContextAttributeMask {
   ContextShouldBeInlined = 0x2, // Leaf of context should be inlined
 };
 
-// Represents a callsite with caller function name and line location
+// Represents a context frame with function name and line location
 struct SampleContextFrame {
-  StringRef CallerName;
-  LineLocation Callsite;
+  StringRef FuncName;
+  LineLocation Location;
 
-  SampleContextFrame() : Callsite(0, 0) {}
+  SampleContextFrame() : Location(0, 0) {}
 
-  SampleContextFrame(StringRef CallerName, LineLocation Callsite)
-      : CallerName(CallerName), Callsite(Callsite) {}
+  SampleContextFrame(StringRef FuncName, LineLocation Location)
+      : FuncName(FuncName), Location(Location) {}
 
   bool operator==(const SampleContextFrame &That) const {
-    return Callsite == That.Callsite && CallerName == That.CallerName;
+    return Location == That.Location && FuncName == That.FuncName;
   }
 
   bool operator!=(const SampleContextFrame &That) const {
@@ -432,19 +432,19 @@ struct SampleContextFrame {
 
   std::string toString(bool OutputLineLocation) const {
     std::ostringstream OContextStr;
-    OContextStr << CallerName.str();
+    OContextStr << FuncName.str();
     if (OutputLineLocation) {
-      OContextStr << ":" << Callsite.LineOffset;
-      if (Callsite.Discriminator)
-        OContextStr << "." << Callsite.Discriminator;
+      OContextStr << ":" << Location.LineOffset;
+      if (Location.Discriminator)
+        OContextStr << "." << Location.Discriminator;
     }
     return OContextStr.str();
   }
 };
 
 static inline hash_code hash_value(const SampleContextFrame &arg) {
-  return hash_combine(arg.CallerName, arg.Callsite.LineOffset,
-                      arg.Callsite.Discriminator);
+  return hash_combine(arg.FuncName, arg.Location.LineOffset,
+                      arg.Location.Discriminator);
 }
 
 using SampleContextFrameVector = SmallVector<SampleContextFrame, 10>;
@@ -598,7 +598,7 @@ public:
                   ContextStateMask CState = RawContext) {
     assert(CState != UnknownContext);
     FullContext = Context;
-    Name = Context.back().CallerName;
+    Name = Context.back().FuncName;
     State = CState;
   }
 
@@ -621,11 +621,11 @@ public:
     while (I < std::min(FullContext.size(), That.FullContext.size())) {
       auto &Context1 = FullContext[I];
       auto &Context2 = That.FullContext[I];
-      auto V = Context1.CallerName.compare(Context2.CallerName);
+      auto V = Context1.FuncName.compare(Context2.FuncName);
       if (V)
         return V == -1;
-      if (Context1.Callsite != Context2.Callsite)
-        return Context1.Callsite < Context2.Callsite;
+      if (Context1.Location != Context2.Location)
+        return Context1.Location < Context2.Location;
       I++;
     }
 
@@ -645,7 +645,7 @@ public:
       return false;
     ThatContext = ThatContext.take_front(ThisContext.size());
     // Compare Leaf frame first
-    if (ThisContext.back().CallerName != ThatContext.back().CallerName)
+    if (ThisContext.back().FuncName != ThatContext.back().FuncName)
       return false;
     // Compare leading context
     return ThisContext.drop_back() == ThatContext.drop_back();
index 7fecd0d..36a0b6f 100644 (file)
@@ -269,10 +269,10 @@ std::error_code SampleProfileWriterExtBinaryBase::writeCSNameTableSection() {
     auto Frames = Context.getContextFrames();
     encodeULEB128(Frames.size(), OS);
     for (auto &Callsite : Frames) {
-      if (std::error_code EC = writeNameIdx(Callsite.CallerName))
+      if (std::error_code EC = writeNameIdx(Callsite.FuncName))
         return EC;
-      encodeULEB128(Callsite.Callsite.LineOffset, OS);
-      encodeULEB128(Callsite.Callsite.Discriminator, OS);
+      encodeULEB128(Callsite.Location.LineOffset, OS);
+      encodeULEB128(Callsite.Location.Discriminator, OS);
     }
   }
 
@@ -542,7 +542,7 @@ void SampleProfileWriterExtBinaryBase::addContext(
     const SampleContext &Context) {
   if (Context.hasContext()) {
     for (auto &Callsite : Context.getContextFrames())
-      SampleProfileWriterBinary::addName(Callsite.CallerName);
+      SampleProfileWriterBinary::addName(Callsite.FuncName);
     CSNameTable.insert(std::make_pair(Context, 0));
   } else {
     SampleProfileWriterBinary::addName(Context.getName());
index 2a9a13f..028567f 100644 (file)
@@ -511,13 +511,13 @@ SampleContextTracker::getOrCreateContextPath(const SampleContext &Context,
   for (auto &Callsite : Context.getContextFrames()) {
     // Create child node at parent line/disc location
     if (AllowCreate) {
-      ContextNode = ContextNode->getOrCreateChildContext(CallSiteLoc,
-                                                         Callsite.CallerName);
+      ContextNode =
+          ContextNode->getOrCreateChildContext(CallSiteLoc, Callsite.FuncName);
     } else {
       ContextNode =
-          ContextNode->getChildContext(CallSiteLoc, Callsite.CallerName);
+          ContextNode->getChildContext(CallSiteLoc, Callsite.FuncName);
     }
-    CallSiteLoc = Callsite.Callsite;
+    CallSiteLoc = Callsite.Location;
   }
 
   assert((!AllowCreate || ContextNode) &&
index bb7152a..5e55213 100644 (file)
@@ -18,12 +18,12 @@ namespace llvm {
 namespace sampleprof {
 
 inline std::string getCallSite(const SampleContextFrame &Callsite) {
-  std::string CallsiteStr = Callsite.CallerName.str();
+  std::string CallsiteStr = Callsite.FuncName.str();
   CallsiteStr += ":";
-  CallsiteStr += Twine(Callsite.Callsite.LineOffset).str();
-  if (Callsite.Callsite.Discriminator > 0) {
+  CallsiteStr += Twine(Callsite.Location.LineOffset).str();
+  if (Callsite.Location.Discriminator > 0) {
     CallsiteStr += ".";
-    CallsiteStr += Twine(Callsite.Callsite.Discriminator).str();
+    CallsiteStr += Twine(Callsite.Location.Discriminator).str();
   }
   return CallsiteStr;
 }
index 6e43e53..0cfe021 100644 (file)
@@ -253,15 +253,15 @@ void ProfileGeneratorBase::updateBodySamplesforFunctionProfile(
     FunctionSamples &FunctionProfile, const SampleContextFrame &LeafLoc,
     uint64_t Count) {
   // Filter out invalid negative(int type) lineOffset
-  if (LeafLoc.Callsite.LineOffset & 0x80000000)
+  if (LeafLoc.Location.LineOffset & 0x80000000)
     return;
   // Use the maximum count of samples with same line location
   ErrorOr<uint64_t> R = FunctionProfile.findSamplesAt(
-      LeafLoc.Callsite.LineOffset, LeafLoc.Callsite.Discriminator);
+      LeafLoc.Location.LineOffset, LeafLoc.Location.Discriminator);
   uint64_t PreviousCount = R ? R.get() : 0;
   if (PreviousCount <= Count) {
-    FunctionProfile.addBodySamples(LeafLoc.Callsite.LineOffset,
-                                   LeafLoc.Callsite.Discriminator,
+    FunctionProfile.addBodySamples(LeafLoc.Location.LineOffset,
+                                   LeafLoc.Location.Discriminator,
                                    Count - PreviousCount);
   }
 }
@@ -299,16 +299,16 @@ FunctionSamples &ProfileGenerator::getLeafProfileAndAddTotalSamples(
     const SampleContextFrameVector &FrameVec, uint64_t Count) {
   // Get top level profile
   FunctionSamples *FunctionProfile =
-      &getTopLevelFunctionProfile(FrameVec[0].CallerName);
+      &getTopLevelFunctionProfile(FrameVec[0].FuncName);
   FunctionProfile->addTotalSamples(Count);
 
   for (size_t I = 1; I < FrameVec.size(); I++) {
     FunctionSamplesMap &SamplesMap =
-        FunctionProfile->functionSamplesAt(FrameVec[I - 1].Callsite);
+        FunctionProfile->functionSamplesAt(FrameVec[I - 1].Location);
     auto Ret =
-        SamplesMap.emplace(FrameVec[I].CallerName.str(), FunctionSamples());
+        SamplesMap.emplace(FrameVec[I].FuncName.str(), FunctionSamples());
     if (Ret.second) {
-      SampleContext Context(FrameVec[I].CallerName);
+      SampleContext Context(FrameVec[I].FuncName);
       Ret.first->second.setContext(Context);
     }
     FunctionProfile = &Ret.first->second;
@@ -394,8 +394,8 @@ void ProfileGenerator::populateBoundarySamplesForAllFunctions(
       FunctionSamples &FunctionProfile =
           getLeafProfileAndAddTotalSamples(FrameVec, Count);
       FunctionProfile.addCalledTargetSamples(
-          FrameVec.back().Callsite.LineOffset,
-          FrameVec.back().Callsite.Discriminator, CalleeName, Count);
+          FrameVec.back().Location.LineOffset,
+          FrameVec.back().Location.Discriminator, CalleeName, Count);
     }
     // Add head samples for callee.
     FunctionSamples &CalleeProfile = getTopLevelFunctionProfile(CalleeName);
@@ -534,13 +534,13 @@ void CSProfileGenerator::populateBoundarySamplesForFunction(
     auto LeafLoc = Binary->getInlineLeafFrameLoc(SourceOffset);
     if (!LeafLoc.hasValue())
       continue;
-    FunctionProfile.addCalledTargetSamples(LeafLoc->Callsite.LineOffset,
-                                           LeafLoc->Callsite.Discriminator,
+    FunctionProfile.addCalledTargetSamples(LeafLoc->Location.LineOffset,
+                                           LeafLoc->Location.Discriminator,
                                            CalleeName, Count);
 
     // Record head sample for called target(callee)
     SampleContextFrameVector CalleeCtx(ContextId.begin(), ContextId.end());
-    assert(CalleeCtx.back().CallerName == LeafLoc->CallerName &&
+    assert(CalleeCtx.back().FuncName == LeafLoc->FuncName &&
            "Leaf function name doesn't match");
     CalleeCtx.back() = *LeafLoc;
     CalleeCtx.emplace_back(CalleeName, LineLocation(0, 0));
@@ -556,7 +556,7 @@ getCallerContext(SampleContextFrames CalleeContext,
   CalleeContext = CalleeContext.drop_back();
   CallerContext.assign(CalleeContext.begin(), CalleeContext.end());
   SampleContextFrame CallerFrame = CallerContext.back();
-  CallerContext.back().Callsite = LineLocation(0, 0);
+  CallerContext.back().Location = LineLocation(0, 0);
   return CallerFrame;
 }
 
@@ -595,11 +595,11 @@ void CSProfileGenerator::populateInferredFunctionSamples() {
     if (!EstimatedCallCount && !CalleeProfile.getBodySamples().size())
       EstimatedCallCount = 1;
     CallerProfile.addCalledTargetSamples(
-        CallerLeafFrameLoc.Callsite.LineOffset,
-        CallerLeafFrameLoc.Callsite.Discriminator,
+        CallerLeafFrameLoc.Location.LineOffset,
+        CallerLeafFrameLoc.Location.Discriminator,
         CalleeProfile.getContext().getName(), EstimatedCallCount);
-    CallerProfile.addBodySamples(CallerLeafFrameLoc.Callsite.LineOffset,
-                                 CallerLeafFrameLoc.Callsite.Discriminator,
+    CallerProfile.addBodySamples(CallerLeafFrameLoc.Location.LineOffset,
+                                 CallerLeafFrameLoc.Location.Discriminator,
                                  EstimatedCallCount);
     CallerProfile.addTotalSamples(EstimatedCallCount);
   }
@@ -734,7 +734,7 @@ void CSProfileGenerator::populateBodySamplesWithProbes(
         SampleContextFrameVector CallerContextId;
         SampleContextFrame &&CallerLeafFrameLoc =
             getCallerContext(CalleeContextId, CallerContextId);
-        uint64_t CallerIndex = CallerLeafFrameLoc.Callsite.LineOffset;
+        uint64_t CallerIndex = CallerLeafFrameLoc.Location.LineOffset;
         assert(CallerIndex &&
                "Inferred caller's location index shouldn't be zero!");
         FunctionSamples &CallerProfile =
@@ -795,7 +795,7 @@ FunctionSamples &CSProfileGenerator::getFunctionProfileForLeafProbe(
   // frame's probe id, like:
   // Inlined stack: [foo:1, bar:2], the ContextId will be "foo:1 @ bar"
   auto LeafFrame = NewContextStack.back();
-  LeafFrame.Callsite = LineLocation(0, 0);
+  LeafFrame.Location = LineLocation(0, 0);
   NewContextStack.pop_back();
   // Compress the context string except for the leaf frame
   CSProfileGenerator::compressRecursionContext(NewContextStack);
index aa96f94..b7a18c2 100644 (file)
@@ -62,8 +62,8 @@ void BinarySizeContextTracker::addInstructionForContext(
   ContextTrieNode *CurNode = &RootContext;
   bool IsLeaf = true;
   for (const auto &Callsite : reverse(Context)) {
-    StringRef CallerName = Callsite.CallerName;
-    LineLocation CallsiteLoc = IsLeaf ? LineLocation(0, 0) : Callsite.Callsite;
+    StringRef CallerName = Callsite.FuncName;
+    LineLocation CallsiteLoc = IsLeaf ? LineLocation(0, 0) : Callsite.Location;
     CurNode = CurNode->getOrCreateChildContext(CallsiteLoc, CallerName);
     IsLeaf = false;
   }
@@ -88,7 +88,7 @@ BinarySizeContextTracker::getFuncSizeForContext(const SampleContext &Context) {
     const auto &ChildFrame = Frames[I--];
     PrevNode = CurrNode;
     CurrNode =
-        CurrNode->getChildContext(ChildFrame.Callsite, ChildFrame.CallerName);
+        CurrNode->getChildContext(ChildFrame.Location, ChildFrame.FuncName);
     if (CurrNode && CurrNode->getFunctionSize().hasValue())
       Size = CurrNode->getFunctionSize().getValue();
   }
@@ -222,7 +222,7 @@ ProfiledBinary::getExpandedContext(const SmallVectorImpl<uint64_t> &Stack,
 
   // Compress the context string except for the leaf frame
   auto LeafFrame = ContextVec.back();
-  LeafFrame.Callsite = LineLocation(0, 0);
+  LeafFrame.Location = LineLocation(0, 0);
   ContextVec.pop_back();
   CSProfileGenerator::compressRecursionContext(ContextVec);
   CSProfileGenerator::trimContext(ContextVec);