[llvm-profgen] Truncate the context with zero probe ID
authorwlei <wlei@fb.com>
Fri, 19 Nov 2021 21:57:05 +0000 (13:57 -0800)
committerwlei <wlei@fb.com>
Wed, 1 Dec 2021 00:21:25 +0000 (16:21 -0800)
Due to the debug info merging, there may have some contexts with zero probe id, we should truncate the context to avoid misleading pre-inliner.

Reviewed By: hoy, wenlei

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

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

index 7d28bda..0b90352 100644 (file)
@@ -842,12 +842,12 @@ void CSProfileGenerator::populateBodySamplesWithProbes(
       FunctionProfile.addHeadSamples(Count);
       // Look up for the caller's function profile
       const auto *InlinerDesc = Binary->getInlinerDescForProbe(Probe);
-      if (InlinerDesc != nullptr) {
+      SampleContextFrames CalleeContextId =
+          FunctionProfile.getContext().getContextFrames();
+      if (InlinerDesc != nullptr && CalleeContextId.size() > 1) {
         // Since the context id will be compressed, we have to use callee's
         // context id to infer caller's context id to ensure they share the
         // same context prefix.
-        SampleContextFrames CalleeContextId =
-            FunctionProfile.getContext().getContextFrames();
         SampleContextFrameVector CallerContextId;
         SampleContextFrame &&CallerLeafFrameLoc =
             getCallerContext(CalleeContextId, CallerContextId);
index a28ba82..3d32cba 100644 (file)
@@ -476,7 +476,13 @@ public:
     SmallVector<MCPseduoProbeFrameLocation, 16> ProbeInlineContext;
     ProbeDecoder.getInlineContextForProbe(Probe, ProbeInlineContext,
                                           IncludeLeaf);
-    for (auto &Callsite : ProbeInlineContext) {
+    for (uint32_t I = 0; I < ProbeInlineContext.size(); I++) {
+      auto &Callsite = ProbeInlineContext[I];
+      // Clear the current context for an unknown probe.
+      if (Callsite.second == 0 && I != ProbeInlineContext.size() - 1) {
+        InlineContextStack.clear();
+        continue;
+      }
       InlineContextStack.emplace_back(Callsite.first,
                                       LineLocation(Callsite.second, 0));
     }