[llvm-profgen] Fixing a context attribure update issue due to a non-derministic proce...
authorHongtao Yu <hoy@fb.com>
Thu, 31 Mar 2022 19:35:40 +0000 (12:35 -0700)
committerHongtao Yu <hoy@fb.com>
Fri, 1 Apr 2022 00:33:58 +0000 (17:33 -0700)
 A context can  be created by invoking the `getFunctionProfileForContext` function in two ways:
      - by using a probe and its calling context.
      - by removing the leaf frame from an existing contexts. The first way is used when generating a function profile for a given LBR range, and the input `WasLeafInlined` is computed depending on the actually probe in the LBR range. The second way is used when using the entry count of an inlinee function profile to update its inliner callsite count, so `WasLeafInlined` is unknown for the inliner frame.

The two  invocations can happen in different order on different platforms, since the lbr ranges are stored in an unordered_map, and  we are making sure `ContextWasInlined` is always set correctly.

This should fix the random test failure introduced by D121655

Reviewed By: wenlei

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

llvm/tools/llvm-profgen/ProfileGenerator.cpp

index c497dff..85214f6 100644 (file)
@@ -712,7 +712,21 @@ FunctionSamples &CSProfileGenerator::getFunctionProfileForContext(
     FunctionSamples &FProfile = Ret.first->second;
     FProfile.setContext(FContext);
     return Ret.first->second;
+  } else {
+    // Update ContextWasInlined attribute for existing contexts.
+    // The current function can be called in two ways:
+    //  - when processing a probe of the current frame
+    //  - when processing the entry probe of an inlinee's frame, which
+    //    is then used to update the callsite count of the current frame.
+    // The two can happen in any order, hence here we are making sure
+    // `ContextWasInlined` is always set as expected.
+    // TODO: Note that the former does not always happen if no probes of the
+    // current frame has samples, and if the latter happens, we could lose the
+    // attribute. This should be fixed.
+    if (WasLeafInlined)
+      I->second.getContext().setAttribute(ContextWasInlined);
   }
+
   return I->second;
 }