[llvm] [ctx_prof] Profile flatterner (PR #104539)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 22:02:22 PDT 2024


================
@@ -193,3 +201,35 @@ InstrProfCallsite *CtxProfAnalysis::getCallsiteInstrumentation(CallBase &CB) {
       return IPC;
   return nullptr;
 }
+
+static void
+preorderVisit(const PGOCtxProfContext::CallTargetMapTy &Profiles,
+              function_ref<void(const PGOCtxProfContext &)> Visitor) {
+  std::function<void(const PGOCtxProfContext &)> Traverser =
+      [&](const auto &Ctx) {
+        Visitor(Ctx);
+        for (const auto &[_, SubCtxSet] : Ctx.callsites())
+          for (const auto &[__, Subctx] : SubCtxSet)
+            Traverser(Subctx);
+      };
+  for (const auto &[_, P] : Profiles)
+    Traverser(P);
+}
+
+const CtxProfFlatProfile PGOContextualProfile::flatten() const {
+  assert(Profiles.has_value());
+  CtxProfFlatProfile Flat;
+  preorderVisit(*Profiles, [&](const PGOCtxProfContext &Ctx) {
+    auto [It, Ins] = Flat.insert({Ctx.guid(), {}});
+    if (Ins) {
+      llvm::append_range(It->second, Ctx.counters());
+    } else {
+      assert(It->second.size() == Ctx.counters().size() &&
+             "All contexts corresponding to a function should have the exact "
+             "same nr of counters.");
----------------
boomanaiden154 wrote:

nit: `s/nr/number`

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


More information about the llvm-commits mailing list