[llvm] [nfc][ctx_prof] Efficient profile traversal and update (PR #110052)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 26 15:29:58 PDT 2024
================
@@ -279,16 +278,44 @@ static void preorderVisit(ProfilesTy &Profiles,
Traverser(P);
}
-void PGOContextualProfile::update(Visitor V, const Function *F) {
- GlobalValue::GUID G = F ? getDefinedFunctionGUID(*F) : 0U;
+void PGOContextualProfile::initIndex() {
+ // Initialize the head of the index list for each function. We don't need it
+ // after this point.
+ DenseMap<GlobalValue::GUID, PGOCtxProfContext *> InsertionPoints;
+ for (auto &FI : FuncInfo)
+ InsertionPoints[FI.first] = &FI.second.Index;
preorderVisit<PGOCtxProfContext::CallTargetMapTy, PGOCtxProfContext>(
- *Profiles, V, G);
+ *Profiles, [&](PGOCtxProfContext &Ctx) {
+ auto InsertIt = InsertionPoints.find(Ctx.guid());
+ if (InsertIt == InsertionPoints.end())
+ return;
+ // Insert at the end of the list. Since we traverse in preorder, it
+ // means that when we iterate the list from the beginning, we'd
+ // encounter the contexts in the order we would have, should we have
+ // performed a full preorder traversal.
+ InsertIt->second->Next = &Ctx;
+ Ctx.Previous = InsertIt->second;
+ InsertIt->second = &Ctx;
+ });
+}
+
+void PGOContextualProfile::update(Visitor V, const Function &F) {
+ assert(isFunctionKnown(F));
+ GlobalValue::GUID G = getDefinedFunctionGUID(F);
+ for (auto *Node = FuncInfo.find(G)->second.Index.Next; Node;
+ Node = Node->Next)
+ V(*Node);
}
void PGOContextualProfile::visit(ConstVisitor V, const Function *F) const {
- GlobalValue::GUID G = F ? getDefinedFunctionGUID(*F) : 0U;
- preorderVisit<const PGOCtxProfContext::CallTargetMapTy,
- const PGOCtxProfContext>(*Profiles, V, G);
+ if (!F)
+ preorderVisit<const PGOCtxProfContext::CallTargetMapTy,
+ const PGOCtxProfContext>(*Profiles, V);
----------------
kazutakahirata wrote:
Do you mean to return when `F == nullptr`? Otherwise, `*F` below would segfault. I'm guessing that you either traverse the whole tree or visit those nodes that correspond to `F`, but not both.
```suggestion
if (!F) {
preorderVisit<const PGOCtxProfContext::CallTargetMapTy,
const PGOCtxProfContext>(*Profiles, V);
return;
}
```
https://github.com/llvm/llvm-project/pull/110052
More information about the llvm-commits
mailing list