[llvm-branch-commits] [llvm] [llvm-profgen] Infer callsite samples from callee head samples (PR #235988)

Kunal Pathak via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 18 14:09:06 PST 2026


================
@@ -1377,6 +1386,96 @@ void CSProfileGenerator::populateBoundarySamplesWithProbes(
   }
 }
 
+void CSProfileGenerator::inferCallsiteSamples() {
+  // Walk the context trie bottom-up (children before parents) and for each
+  // node, ensure that callsite body samples are at least as large as callee
+  // entry (head) samples. This serves two purposes:
+  // 1. Create FunctionSamples for nodes without samples and infer called
+  //    target samples using children's head samples.
+  // 2. Fix under-counted callsites for nodes that have callsite counts
+  //    lower than callee entry counts.
+  SmallVector<ContextTrieNode *, 256> AllNodes;
+  for (auto *Node : ContextTracker)
+    AllNodes.push_back(Node);
+
+  for (auto It = AllNodes.rbegin(), End = AllNodes.rend(); It != End; ++It) {
+    ContextTrieNode *Node = *It;
+
+    if (Node == &getRootContext())
+      continue;
+
+    bool HasChildWithSamples = false;
+    for (auto &Child : Node->getAllChildContext()) {
+      if (Child.second.getFunctionSamples()) {
+        HasChildWithSamples = true;
+        break;
+      }
+    }
+    if (!HasChildWithSamples)
+      continue;
+
+    FunctionSamples *FSamples = Node->getFunctionSamples();
+    if (!FSamples) {
+      FSamples = getOrCreateFunctionSamples(Node);
----------------
kunalspathak wrote:

If every child has a zero head estimate, we might not need to create an empty profile here. Below we skip them by `if (ChildHeadSamples == 0)` check.

https://github.com/llvm/llvm-project/pull/235988


More information about the llvm-branch-commits mailing list