Skip to content

[ctxprof][nfc] Move profile annotator to Analysis #135871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented Apr 15, 2025

This moves the utility that propagates counter values such that we can reuse it elsewhere. Specifically, in a subsequent patch, it'll be used to guide ICP: we need to prioritize promoting indirect calls that dominate larger portions of the dynamic instruction count. We can compare them based on the dynamic count of IR instructions, and we can get that early with this counter propagation logic.

The patch is mostly a move of the existing logic, with a pimpl - style implementation to hide all the current complexity.

Copy link
Member Author

mtrofin commented Apr 15, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@mtrofin mtrofin force-pushed the users/mtrofin/04-15-_ctxprof_nfc_move_profile_annotator_to_analysis branch from 51c5795 to 037c7fb Compare April 15, 2025 22:36
@mtrofin mtrofin marked this pull request as ready for review April 15, 2025 22:40
@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms labels Apr 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-pgo

@llvm/pr-subscribers-llvm-analysis

Author: Mircea Trofin (mtrofin)

Changes

This moves the utility that propagates counter values such that we can reuse it elsewhere. Specifically, in a subsequent patch, it'll be used to guide ICP: we need to prioritize promoting indirect calls that dominate larger portions of the dynamic instruction count. We can compare them based on the dynamic count of IR instructions, and we can get that early with this counter propagation logic.

The patch is mostly a move of the existing logic, with a pimpl - style implementation to hide all the current complexity.


Patch is 30.08 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/135871.diff

3 Files Affected:

  • (modified) llvm/include/llvm/Analysis/CtxProfAnalysis.h (+28)
  • (modified) llvm/lib/Analysis/CtxProfAnalysis.cpp (+372)
  • (modified) llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp (+24-351)
diff --git a/llvm/include/llvm/Analysis/CtxProfAnalysis.h b/llvm/include/llvm/Analysis/CtxProfAnalysis.h
index 6f1c3696ca78c..aa582cfef1ad1 100644
--- a/llvm/include/llvm/Analysis/CtxProfAnalysis.h
+++ b/llvm/include/llvm/Analysis/CtxProfAnalysis.h
@@ -157,6 +157,34 @@ class CtxProfAnalysisPrinterPass
   const PrintMode Mode;
 };
 
+/// Utility that propagates counter values to each basic block and to each edge
+/// when a basic block has more than one outgoing edge, using an adaptation of
+/// PGOUseFunc::populateCounters.
+// FIXME(mtrofin): look into factoring the code to share one implementation.
+class ProfileAnnotatorImpl;
+class ProfileAnnotator {
+  std::unique_ptr<ProfileAnnotatorImpl> PImpl;
+
+public:
+  ProfileAnnotator(const Function &F, ArrayRef<uint64_t> RawCounters);
+  uint64_t getBBCount(const BasicBlock &BB) const;
+
+  // Finds the true and false counts for the given select instruction. Returns
+  // false if the select doesn't have instrumentation or if the count of the
+  // parent BB is 0.
+  bool getSelectInstrProfile(SelectInst &SI, uint64_t &TrueCount,
+                             uint64_t &FalseCount) const;
+  // Clears Profile and populates it with the edge weights, in the same order as
+  // they need to appear in the MD_prof metadata. Also computes the max of those
+  // weights an returns it in MaxCount. Returs false if:
+  //   - the BB has less than 2 successors
+  //   - the counts are 0
+  bool getOutgoingBranchWeights(BasicBlock &BB,
+                                SmallVectorImpl<uint64_t> &Profile,
+                                uint64_t &MaxCount) const;
+  ~ProfileAnnotator();
+};
+
 /// Assign a GUID to functions as metadata. GUID calculation takes linkage into
 /// account, which may change especially through and after thinlto. By
 /// pre-computing and assigning as metadata, this mechanism is resilient to such
diff --git a/llvm/lib/Analysis/CtxProfAnalysis.cpp b/llvm/lib/Analysis/CtxProfAnalysis.cpp
index d203e277546ea..391631e15aa89 100644
--- a/llvm/lib/Analysis/CtxProfAnalysis.cpp
+++ b/llvm/lib/Analysis/CtxProfAnalysis.cpp
@@ -14,7 +14,9 @@
 #include "llvm/Analysis/CtxProfAnalysis.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Analysis/CFG.h"
 #include "llvm/IR/Analysis.h"
+#include "llvm/IR/Dominators.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassManager.h"
@@ -22,6 +24,8 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
+#include <deque>
+#include <memory>
 
 #define DEBUG_TYPE "ctx_prof"
 
@@ -46,6 +50,374 @@ static cl::opt<bool> ForceIsInSpecializedModule(
 
 const char *AssignGUIDPass::GUIDMetadataName = "guid";
 
+namespace llvm {
+class ProfileAnnotatorImpl final {
+  friend class ProfileAnnotator;
+  class BBInfo;
+  struct EdgeInfo {
+    BBInfo *const Src;
+    BBInfo *const Dest;
+    std::optional<uint64_t> Count;
+
+    explicit EdgeInfo(BBInfo &Src, BBInfo &Dest) : Src(&Src), Dest(&Dest) {}
+  };
+
+  class BBInfo {
+    std::optional<uint64_t> Count;
+    // OutEdges is dimensioned to match the number of terminator operands.
+    // Entries in the vector match the index in the terminator operand list. In
+    // some cases - see `shouldExcludeEdge` and its implementation - an entry
+    // will be nullptr.
+    // InEdges doesn't have the above constraint.
+    SmallVector<EdgeInfo *> OutEdges;
+    SmallVector<EdgeInfo *> InEdges;
+    size_t UnknownCountOutEdges = 0;
+    size_t UnknownCountInEdges = 0;
+
+    // Pass AssumeAllKnown when we try to propagate counts from edges to BBs -
+    // because all the edge counters must be known.
+    // Return std::nullopt if there were no edges to sum. The user can decide
+    // how to interpret that.
+    std::optional<uint64_t> getEdgeSum(const SmallVector<EdgeInfo *> &Edges,
+                                       bool AssumeAllKnown) const {
+      std::optional<uint64_t> Sum;
+      for (const auto *E : Edges) {
+        // `Edges` may be `OutEdges`, case in which `E` could be nullptr.
+        if (E) {
+          if (!Sum.has_value())
+            Sum = 0;
+          *Sum += (AssumeAllKnown ? *E->Count : E->Count.value_or(0U));
+        }
+      }
+      return Sum;
+    }
+
+    bool computeCountFrom(const SmallVector<EdgeInfo *> &Edges) {
+      assert(!Count.has_value());
+      Count = getEdgeSum(Edges, true);
+      return Count.has_value();
+    }
+
+    void setSingleUnknownEdgeCount(SmallVector<EdgeInfo *> &Edges) {
+      uint64_t KnownSum = getEdgeSum(Edges, false).value_or(0U);
+      uint64_t EdgeVal = *Count > KnownSum ? *Count - KnownSum : 0U;
+      EdgeInfo *E = nullptr;
+      for (auto *I : Edges)
+        if (I && !I->Count.has_value()) {
+          E = I;
+#ifdef NDEBUG
+          break;
+#else
+          assert((!E || E == I) &&
+                 "Expected exactly one edge to have an unknown count, "
+                 "found a second one");
+          continue;
+#endif
+        }
+      assert(E && "Expected exactly one edge to have an unknown count");
+      assert(!E->Count.has_value());
+      E->Count = EdgeVal;
+      assert(E->Src->UnknownCountOutEdges > 0);
+      assert(E->Dest->UnknownCountInEdges > 0);
+      --E->Src->UnknownCountOutEdges;
+      --E->Dest->UnknownCountInEdges;
+    }
+
+  public:
+    BBInfo(size_t NumInEdges, size_t NumOutEdges, std::optional<uint64_t> Count)
+        : Count(Count) {
+      // For in edges, we just want to pre-allocate enough space, since we know
+      // it at this stage. For out edges, we will insert edges at the indices
+      // corresponding to positions in this BB's terminator instruction, so we
+      // construct a default (nullptr values)-initialized vector. A nullptr edge
+      // corresponds to those that are excluded (see shouldExcludeEdge).
+      InEdges.reserve(NumInEdges);
+      OutEdges.resize(NumOutEdges);
+    }
+
+    bool tryTakeCountFromKnownOutEdges(const BasicBlock &BB) {
+      if (!UnknownCountOutEdges) {
+        return computeCountFrom(OutEdges);
+      }
+      return false;
+    }
+
+    bool tryTakeCountFromKnownInEdges(const BasicBlock &BB) {
+      if (!UnknownCountInEdges) {
+        return computeCountFrom(InEdges);
+      }
+      return false;
+    }
+
+    void addInEdge(EdgeInfo &Info) {
+      InEdges.push_back(&Info);
+      ++UnknownCountInEdges;
+    }
+
+    // For the out edges, we care about the position we place them in, which is
+    // the position in terminator instruction's list (at construction). Later,
+    // we build branch_weights metadata with edge frequency values matching
+    // these positions.
+    void addOutEdge(size_t Index, EdgeInfo &Info) {
+      OutEdges[Index] = &Info;
+      ++UnknownCountOutEdges;
+    }
+
+    bool hasCount() const { return Count.has_value(); }
+
+    uint64_t getCount() const { return *Count; }
+
+    bool trySetSingleUnknownInEdgeCount() {
+      if (UnknownCountInEdges == 1) {
+        setSingleUnknownEdgeCount(InEdges);
+        return true;
+      }
+      return false;
+    }
+
+    bool trySetSingleUnknownOutEdgeCount() {
+      if (UnknownCountOutEdges == 1) {
+        setSingleUnknownEdgeCount(OutEdges);
+        return true;
+      }
+      return false;
+    }
+    size_t getNumOutEdges() const { return OutEdges.size(); }
+
+    uint64_t getEdgeCount(size_t Index) const {
+      if (auto *E = OutEdges[Index])
+        return *E->Count;
+      return 0U;
+    }
+  };
+
+  const Function &F;
+  ArrayRef<uint64_t> Counters;
+  // To be accessed through getBBInfo() after construction.
+  std::map<const BasicBlock *, BBInfo> BBInfos;
+  std::vector<EdgeInfo> EdgeInfos;
+
+  // The only criteria for exclusion is faux suspend -> exit edges in presplit
+  // coroutines. The API serves for readability, currently.
+  bool shouldExcludeEdge(const BasicBlock &Src, const BasicBlock &Dest) const {
+    return llvm::isPresplitCoroSuspendExitEdge(Src, Dest);
+  }
+
+  BBInfo &getBBInfo(const BasicBlock &BB) { return BBInfos.find(&BB)->second; }
+
+  const BBInfo &getBBInfo(const BasicBlock &BB) const {
+    return BBInfos.find(&BB)->second;
+  }
+
+  // validation function after we propagate the counters: all BBs and edges'
+  // counters must have a value.
+  bool allCountersAreAssigned() const {
+    for (const auto &BBInfo : BBInfos)
+      if (!BBInfo.second.hasCount())
+        return false;
+    for (const auto &EdgeInfo : EdgeInfos)
+      if (!EdgeInfo.Count.has_value())
+        return false;
+    return true;
+  }
+
+  /// Check that all paths from the entry basic block that use edges with
+  /// non-zero counts arrive at a basic block with no successors (i.e. "exit")
+  bool allTakenPathsExit() const {
+    std::deque<const BasicBlock *> Worklist;
+    DenseSet<const BasicBlock *> Visited;
+    Worklist.push_back(&F.getEntryBlock());
+    bool HitExit = false;
+    while (!Worklist.empty()) {
+      const auto *BB = Worklist.front();
+      Worklist.pop_front();
+      if (!Visited.insert(BB).second)
+        continue;
+      if (succ_size(BB) == 0) {
+        if (isa<UnreachableInst>(BB->getTerminator()))
+          return false;
+        HitExit = true;
+        continue;
+      }
+      if (succ_size(BB) == 1) {
+        Worklist.push_back(BB->getUniqueSuccessor());
+        continue;
+      }
+      const auto &BBInfo = getBBInfo(*BB);
+      bool HasAWayOut = false;
+      for (auto I = 0U; I < BB->getTerminator()->getNumSuccessors(); ++I) {
+        const auto *Succ = BB->getTerminator()->getSuccessor(I);
+        if (!shouldExcludeEdge(*BB, *Succ)) {
+          if (BBInfo.getEdgeCount(I) > 0) {
+            HasAWayOut = true;
+            Worklist.push_back(Succ);
+          }
+        }
+      }
+      if (!HasAWayOut)
+        return false;
+    }
+    return HitExit;
+  }
+
+  bool allNonColdSelectsHaveProfile() const {
+    for (const auto &BB : F) {
+      if (getBBInfo(BB).getCount() > 0) {
+        for (const auto &I : BB) {
+          if (const auto *SI = dyn_cast<SelectInst>(&I)) {
+            if (const auto *Inst = CtxProfAnalysis::getSelectInstrumentation(
+                    *const_cast<SelectInst *>(SI))) {
+              auto Index = Inst->getIndex()->getZExtValue();
+              assert(Index < Counters.size());
+              if (Counters[Index] == 0)
+                return false;
+            }
+          }
+        }
+      }
+    }
+    return true;
+  }
+
+  // This is an adaptation of PGOUseFunc::populateCounters.
+  // FIXME(mtrofin): look into factoring the code to share one implementation.
+  void propagateCounterValues() {
+    bool KeepGoing = true;
+    while (KeepGoing) {
+      KeepGoing = false;
+      for (const auto &BB : F) {
+        auto &Info = getBBInfo(BB);
+        if (!Info.hasCount())
+          KeepGoing |= Info.tryTakeCountFromKnownOutEdges(BB) ||
+                       Info.tryTakeCountFromKnownInEdges(BB);
+        if (Info.hasCount()) {
+          KeepGoing |= Info.trySetSingleUnknownOutEdgeCount();
+          KeepGoing |= Info.trySetSingleUnknownInEdgeCount();
+        }
+      }
+    }
+    assert(allCountersAreAssigned() &&
+           "[ctx-prof] Expected all counters have been assigned.");
+    assert(allTakenPathsExit() &&
+           "[ctx-prof] Encountered a BB with more than one successor, where "
+           "all outgoing edges have a 0 count. This occurs in non-exiting "
+           "functions (message pumps, usually) which are not supported in the "
+           "contextual profiling case");
+    assert(allNonColdSelectsHaveProfile() &&
+           "[ctx-prof] All non-cold select instructions were expected to have "
+           "a profile.");
+  }
+
+public:
+  ProfileAnnotatorImpl(const Function &F, ArrayRef<uint64_t> Counters)
+      : F(F), Counters(Counters) {
+    assert(!F.isDeclaration());
+    assert(!Counters.empty());
+    size_t NrEdges = 0;
+    for (const auto &BB : F) {
+      std::optional<uint64_t> Count;
+      if (auto *Ins = CtxProfAnalysis::getBBInstrumentation(
+              const_cast<BasicBlock &>(BB))) {
+        auto Index = Ins->getIndex()->getZExtValue();
+        assert(Index < Counters.size() &&
+               "The index must be inside the counters vector by construction - "
+               "tripping this assertion indicates a bug in how the contextual "
+               "profile is managed by IPO transforms");
+        (void)Index;
+        Count = Counters[Ins->getIndex()->getZExtValue()];
+      } else if (isa<UnreachableInst>(BB.getTerminator())) {
+        // The program presumably didn't crash.
+        Count = 0;
+      }
+      auto [It, Ins] =
+          BBInfos.insert({&BB, {pred_size(&BB), succ_size(&BB), Count}});
+      (void)Ins;
+      assert(Ins && "We iterate through the function's BBs, no reason to "
+                    "insert one more than once");
+      NrEdges += llvm::count_if(successors(&BB), [&](const auto *Succ) {
+        return !shouldExcludeEdge(BB, *Succ);
+      });
+    }
+    // Pre-allocate the vector, we want references to its contents to be stable.
+    EdgeInfos.reserve(NrEdges);
+    for (const auto &BB : F) {
+      auto &Info = getBBInfo(BB);
+      for (auto I = 0U; I < BB.getTerminator()->getNumSuccessors(); ++I) {
+        const auto *Succ = BB.getTerminator()->getSuccessor(I);
+        if (!shouldExcludeEdge(BB, *Succ)) {
+          auto &EI = EdgeInfos.emplace_back(getBBInfo(BB), getBBInfo(*Succ));
+          Info.addOutEdge(I, EI);
+          getBBInfo(*Succ).addInEdge(EI);
+        }
+      }
+    }
+    assert(EdgeInfos.capacity() == NrEdges &&
+           "The capacity of EdgeInfos should have stayed unchanged it was "
+           "populated, because we need pointers to its contents to be stable");
+    propagateCounterValues();
+  }
+
+  uint64_t getBBCount(const BasicBlock &BB) { return getBBInfo(BB).getCount(); }
+};
+
+} // namespace llvm
+
+ProfileAnnotator::ProfileAnnotator(const Function &F,
+                                   ArrayRef<uint64_t> RawCounters)
+    : PImpl(std::make_unique<ProfileAnnotatorImpl>(F, RawCounters)) {}
+
+ProfileAnnotator::~ProfileAnnotator() = default;
+
+uint64_t ProfileAnnotator::getBBCount(const BasicBlock &BB) const {
+  return PImpl->getBBCount(BB);
+}
+
+bool ProfileAnnotator::getSelectInstrProfile(SelectInst &SI,
+                                             uint64_t &TrueCount,
+                                             uint64_t &FalseCount) const {
+  const auto &BBInfo = PImpl->getBBInfo(*SI.getParent());
+  TrueCount = FalseCount = 0;
+  if (BBInfo.getCount() == 0)
+    return false;
+
+  auto *Step = CtxProfAnalysis::getSelectInstrumentation(SI);
+  if (!Step)
+    return false;
+  auto Index = Step->getIndex()->getZExtValue();
+  assert(Index < PImpl->Counters.size() &&
+         "The index of the step instruction must be inside the "
+         "counters vector by "
+         "construction - tripping this assertion indicates a bug in "
+         "how the contextual profile is managed by IPO transforms");
+  auto TotalCount = BBInfo.getCount();
+  TrueCount = PImpl->Counters[Index];
+  FalseCount = (TotalCount > TrueCount ? TotalCount - TrueCount : 0U);
+  return true;
+}
+
+bool ProfileAnnotator::getOutgoingBranchWeights(
+    BasicBlock &BB, SmallVectorImpl<uint64_t> &Profile,
+    uint64_t &MaxCount) const {
+  Profile.clear();
+
+  if (succ_size(&BB) < 2)
+    return false;
+
+  auto *Term = BB.getTerminator();
+  Profile.resize(Term->getNumSuccessors());
+
+  const auto &BBInfo = PImpl->getBBInfo(BB);
+  MaxCount = 0;
+  for (unsigned SuccIdx = 0, Size = BBInfo.getNumOutEdges(); SuccIdx < Size;
+       ++SuccIdx) {
+    uint64_t EdgeCount = BBInfo.getEdgeCount(SuccIdx);
+    if (EdgeCount > MaxCount)
+      MaxCount = EdgeCount;
+    Profile[SuccIdx] = EdgeCount;
+  }
+  return MaxCount > 0;
+}
+
 PreservedAnalyses AssignGUIDPass::run(Module &M, ModuleAnalysisManager &MAM) {
   for (auto &F : M.functions()) {
     if (F.isDeclaration())
diff --git a/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp b/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp
index 508a41684ed20..e47c9ab75ffe1 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOCtxProfFlattening.cpp
@@ -45,358 +45,33 @@ using namespace llvm;
 
 namespace {
 
-class ProfileAnnotator final {
-  class BBInfo;
-  struct EdgeInfo {
-    BBInfo *const Src;
-    BBInfo *const Dest;
-    std::optional<uint64_t> Count;
+/// Assign branch weights and function entry count. Also update the PSI
+/// builder.
+void assignProfileData(Function &F, ArrayRef<uint64_t> RawCounters) {
+  assert(!RawCounters.empty());
+  ProfileAnnotator PA(F, RawCounters);
 
-    explicit EdgeInfo(BBInfo &Src, BBInfo &Dest) : Src(&Src), Dest(&Dest) {}
-  };
+  F.setEntryCount(RawCounters[0]);
+  SmallVector<uint64_t, 2> ProfileHolder;
 
-  class BBInfo {
-    std::optional<uint64_t> Count;
-    // OutEdges is dimensioned to match the number of terminator operands.
-    // Entries in the vector match the index in the terminator operand list. In
-    // some cases - see `shouldExcludeEdge` and its implementation - an entry
-    // will be nullptr.
-    // InEdges doesn't have the above constraint.
-    SmallVector<EdgeInfo *> OutEdges;
-    SmallVector<EdgeInfo *> InEdges;
-    size_t UnknownCountOutEdges = 0;
-    size_t UnknownCountInEdges = 0;
-
-    // Pass AssumeAllKnown when we try to propagate counts from edges to BBs -
-    // because all the edge counters must be known.
-    // Return std::nullopt if there were no edges to sum. The user can decide
-    // how to interpret that.
-    std::optional<uint64_t> getEdgeSum(const SmallVector<EdgeInfo *> &Edges,
-                                       bool AssumeAllKnown) const {
-      std::optional<uint64_t> Sum;
-      for (const auto *E : Edges) {
-        // `Edges` may be `OutEdges`, case in which `E` could be nullptr.
-        if (E) {
-          if (!Sum.has_value())
-            Sum = 0;
-          *Sum += (AssumeAllKnown ? *E->Count : E->Count.value_or(0U));
-        }
-      }
-      return Sum;
-    }
-
-    bool computeCountFrom(const SmallVector<EdgeInfo *> &Edges) {
-      assert(!Count.has_value());
-      Count = getEdgeSum(Edges, true);
-      return Count.has_value();
-    }
-
-    void setSingleUnknownEdgeCount(SmallVector<EdgeInfo *> &Edges) {
-      uint64_t KnownSum = getEdgeSum(Edges, false).value_or(0U);
-      uint64_t EdgeVal = *Count > KnownSum ? *Count - KnownSum : 0U;
-      EdgeInfo *E = nullptr;
-      for (auto *I : Edges)
-        if (I && !I->Count.has_value()) {
-          E = I;
-#ifdef NDEBUG
-          break;
-#else
-          assert((!E || E == I) &&
-                 "Expected exactly one edge to have an unknown count, "
-                 "found a second one");
-          continue;
-#endif
-        }
-      assert(E && "Expected exactly one edge to have an unknown count");
-      assert(!E->Count.has_value());
-      E->Count = EdgeVal;
-      assert(E->Src->UnknownCountOutEdges > 0);
-      assert(E->Dest->UnknownCountInEdges > 0);
-      --E->Src->UnknownCountOutEdges;
-      --E->Dest->UnknownCountInEdges;
-    }
-
-  public:
-    BBInfo(size_t NumInEdges, size_t NumOutEdges, std::optional<uint64_t> Count)
-        : Count(Count) {
-      // For in edges, we just want to pre-allocate enough space, since we know
-      // it at this stage. For out edges, we will insert edges at the indices
-      // corresponding to positions in this BB's terminator instruction, so we
-      // construct a default (nullptr values)-initialized vector. A nullptr edge
-      // corresponds to those that are excluded (see shouldExcludeEdge).
-      InEdges.reserve(NumInEdges);
-      OutEdges.resize(NumOutEdges);
-    }
-
-    bool tryTakeCountFromKnownOutEdges(const BasicBlock &BB) {
-      if (!UnknownCountOutEdges) {
-        return computeCountFrom(OutEdges);
-      }
-      return false;
-    }
-
-    bool tryTakeCountFromKnownInEdges(const BasicBlock &BB) {
-      if (!UnknownCountInEdges) {
-        return computeCountFrom(InEdges);
-      }
-      return false;
-    }
-
-    void addInEdge(EdgeInfo &Info) {
-      InEdges.push_back(&Info);
-      ++UnknownCountInEdges;
-    }
-
-    // For the out edges, we care about the position we place them in, which is
-    // the positio...
[truncated]

@mtrofin mtrofin force-pushed the users/mtrofin/04-15-_ctxprof_nfc_move_profile_annotator_to_analysis branch from 037c7fb to babbffb Compare April 16, 2025 15:09
Copy link
Contributor

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mtrofin mtrofin force-pushed the users/mtrofin/04-15-_ctxprof_nfc_move_profile_annotator_to_analysis branch from babbffb to 91a8afc Compare April 16, 2025 17:51
Copy link
Member Author

mtrofin commented Apr 16, 2025

Merge activity

  • Apr 16, 3:08 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Apr 16, 3:10 PM EDT: A user merged this pull request with Graphite.

@mtrofin mtrofin merged commit 4903a7b into main Apr 16, 2025
9 of 11 checks passed
@mtrofin mtrofin deleted the users/mtrofin/04-15-_ctxprof_nfc_move_profile_annotator_to_analysis branch April 16, 2025 19:10
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 16, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-ubuntu running on linaro-lldb-aarch64-ubuntu while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/59/builds/16148

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteAttach.py (1200 of 2124)
UNSUPPORTED: lldb-api :: tools/lldb-server/TestGdbRemoteFork.py (1201 of 2124)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteCompletion.py (1202 of 2124)
UNSUPPORTED: lldb-api :: tools/lldb-server/TestGdbRemoteForkNonStop.py (1203 of 2124)
UNSUPPORTED: lldb-api :: tools/lldb-server/TestGdbRemoteForkResume.py (1204 of 2124)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteExitCode.py (1205 of 2124)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteHostInfo.py (1206 of 2124)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteModuleInfo.py (1207 of 2124)
PASS: lldb-api :: tools/lldb-server/TestGdbRemoteAuxvSupport.py (1208 of 2124)
UNRESOLVED: lldb-api :: tools/lldb-dap/variables/TestDAP_variables.py (1209 of 2124)
******************** TEST 'lldb-api :: tools/lldb-dap/variables/TestDAP_variables.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --arch aarch64 --build-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/tools/lldb-dap/variables -p TestDAP_variables.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 4903a7b77b56c7d9a650205b6e7dca46581c7134)
  clang revision 4903a7b77b56c7d9a650205b6e7dca46581c7134
  llvm revision 4903a7b77b56c7d9a650205b6e7dca46581c7134
Skipping the following test categories: ['libc++', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_darwin_dwarf_missing_obj (TestDAP_variables.TestDAP_variables) (requires one of macosx, darwin, ios, tvos, watchos, bridgeos, iphonesimulator, watchsimulator, appletvsimulator) 
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_darwin_dwarf_missing_obj_with_symbol_ondemand_enabled (TestDAP_variables.TestDAP_variables) (requires one of macosx, darwin, ios, tvos, watchos, bridgeos, iphonesimulator, watchsimulator, appletvsimulator) 
========= DEBUG ADAPTER PROTOCOL LOGS =========
1744831309.012517929 --> (stdin/stdout) {"command":"initialize","type":"request","arguments":{"adapterID":"lldb-native","clientID":"vscode","columnsStartAt1":true,"linesStartAt1":true,"locale":"en-us","pathFormat":"path","supportsRunInTerminalRequest":true,"supportsVariablePaging":true,"supportsVariableType":true,"supportsStartDebuggingRequest":true,"supportsProgressReporting":true,"$__lldb_sourceInitFile":false},"seq":1}
1744831309.014628410 <-- (stdin/stdout) {"body":{"$__lldb_version":"lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 4903a7b77b56c7d9a650205b6e7dca46581c7134)\n  clang revision 4903a7b77b56c7d9a650205b6e7dca46581c7134\n  llvm revision 4903a7b77b56c7d9a650205b6e7dca46581c7134","completionTriggerCharacters":["."," ","\t"],"exceptionBreakpointFilters":[{"default":false,"filter":"cpp_catch","label":"C++ Catch"},{"default":false,"filter":"cpp_throw","label":"C++ Throw"},{"default":false,"filter":"objc_catch","label":"Objective-C Catch"},{"default":false,"filter":"objc_throw","label":"Objective-C Throw"}],"supportTerminateDebuggee":true,"supportsBreakpointLocationsRequest":true,"supportsCancelRequest":true,"supportsCompletionsRequest":true,"supportsConditionalBreakpoints":true,"supportsConfigurationDoneRequest":true,"supportsDataBreakpoints":true,"supportsDelayedStackTraceLoading":true,"supportsDisassembleRequest":true,"supportsEvaluateForHovers":true,"supportsExceptionInfoRequest":true,"supportsExceptionOptions":true,"supportsFunctionBreakpoints":true,"supportsHitConditionalBreakpoints":true,"supportsInstructionBreakpoints":true,"supportsLogPoints":true,"supportsModulesRequest":true,"supportsReadMemoryRequest":true,"supportsRestartRequest":true,"supportsSetVariable":true,"supportsStepInTargetsRequest":true,"supportsSteppingGranularity":true,"supportsValueFormattingOptions":true},"command":"initialize","request_seq":1,"seq":0,"success":true,"type":"response"}
1744831309.014860868 --> (stdin/stdout) {"command":"launch","type":"request","arguments":{"program":"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/tools/lldb-dap/variables/TestDAP_variables.test_indexedVariables/a.out","initCommands":["settings clear -all","settings set symbols.enable-external-lookup false","settings set target.inherit-tcc true","settings set target.disable-aslr false","settings set target.detach-on-error false","settings set target.auto-apply-fixits false","settings set plugin.process.gdb-remote.packet-timeout 60","settings set symbols.clang-modules-cache-path \"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api\"","settings set use-color false","settings set show-statusline false"],"disableASLR":false,"enableAutoVariableSummaries":false,"enableSyntheticChildDebugging":false,"displayExtendedBacktrace":false,"commandEscapePrefix":null},"seq":2}
1744831309.015057325 <-- (stdin/stdout) {"body":{"category":"console","output":"Running initCommands:\n"},"event":"output","seq":0,"type":"event"}
1744831309.015079498 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings clear -all\n"},"event":"output","seq":0,"type":"event"}
1744831309.015089750 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set symbols.enable-external-lookup false\n"},"event":"output","seq":0,"type":"event"}
1744831309.015098333 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.inherit-tcc true\n"},"event":"output","seq":0,"type":"event"}
1744831309.015106678 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.disable-aslr false\n"},"event":"output","seq":0,"type":"event"}
1744831309.015114784 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.detach-on-error false\n"},"event":"output","seq":0,"type":"event"}
1744831309.015122414 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set target.auto-apply-fixits false\n"},"event":"output","seq":0,"type":"event"}
1744831309.015130281 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set plugin.process.gdb-remote.packet-timeout 60\n"},"event":"output","seq":0,"type":"event"}
1744831309.015150309 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set symbols.clang-modules-cache-path \"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api\"\n"},"event":"output","seq":0,"type":"event"}
1744831309.015158415 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set use-color false\n"},"event":"output","seq":0,"type":"event"}
1744831309.015166759 <-- (stdin/stdout) {"body":{"category":"console","output":"(lldb) settings set show-statusline false\n"},"event":"output","seq":0,"type":"event"}
1744831309.091242790 <-- (stdin/stdout) {"command":"launch","request_seq":2,"seq":0,"success":true,"type":"response"}
1744831309.091302872 <-- (stdin/stdout) {"body":{"isLocalProcess":true,"name":"/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/tools/lldb-dap/variables/TestDAP_variables.test_indexedVariables/a.out","startMethod":"launch","systemProcessId":2167135},"event":"process","seq":0,"type":"event"}
1744831309.091313124 <-- (stdin/stdout) {"event":"initialized","seq":0,"type":"event"}
1744831309.091600657 --> (stdin/stdout) {"command":"setBreakpoints","type":"request","arguments":{"source":{"name":"main.cpp","path":"main.cpp"},"sourceModified":false,"lines":[40],"breakpoints":[{"line":40}]},"seq":3}
1744831309.092995644 <-- (stdin/stdout) {"body":{"breakpoints":[{"column":1,"id":1,"instructionReference":"0xAAAAC0380C54","line":41,"source":{"name":"main.cpp","path":"main.cpp"},"verified":true}]},"command":"setBreakpoints","request_seq":3,"seq":0,"success":true,"type":"response"}

var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
This moves the utility that propagates counter values such that we can reuse it elsewhere. Specifically, in a subsequent patch, it'll be used to guide ICP: we need to prioritize promoting indirect calls that dominate larger portions of the dynamic instruction count. We can compare them based on the dynamic count of IR instructions, and we can get that early with this counter propagation logic.

The patch is mostly a move of the existing logic, with a pimpl - style implementation to hide all the current complexity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants