Skip to content

Commit

Permalink
[MemProf] Consolidate increments in callee matching code (#99385)
Browse files Browse the repository at this point in the history
To facilitate some follow on changes, consolidate the incrementing of
the edge iterator used during callee matching to the for loop statement.
This requires an additional adjustment in the case of tail call
handling.
  • Loading branch information
teresajohnson authored Jul 18, 2024
1 parent ad15428 commit edfe250
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,8 @@ class CallsiteContextGraph {
/// we were able to identify the call chain through intermediate tail calls.
/// In the latter case new context nodes are added to the graph for the
/// identified tail calls, and their synthesized nodes are added to
/// TailCallToContextNodeMap. The EdgeIter is updated in either case to the
/// next element after the input position (either incremented or updated after
/// removing the old edge).
/// TailCallToContextNodeMap. The EdgeIter is updated in the latter case for
/// the updated edges and to prepare it for an increment in the caller.
bool
calleesMatch(CallTy Call, EdgeIter &EI,
MapVector<CallInfo, ContextNode *> &TailCallToContextNodeMap);
Expand Down Expand Up @@ -1835,12 +1834,11 @@ void CallsiteContextGraph<DerivedCCG, FuncTy,
assert(Node->Clones.empty());
// Check all node callees and see if in the same function.
auto Call = Node->Call.call();
for (auto EI = Node->CalleeEdges.begin(); EI != Node->CalleeEdges.end();) {
for (auto EI = Node->CalleeEdges.begin(); EI != Node->CalleeEdges.end();
++EI) {
auto Edge = *EI;
if (!Edge->Callee->hasCall()) {
++EI;
if (!Edge->Callee->hasCall())
continue;
}
assert(NodeToCallingFunc.count(Edge->Callee));
// Check if the called function matches that of the callee node.
if (calleesMatch(Call, EI, TailCallToContextNodeMap))
Expand Down Expand Up @@ -1889,16 +1887,12 @@ bool CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::calleesMatch(
// calls between the profiled caller and callee.
std::vector<std::pair<CallTy, FuncTy *>> FoundCalleeChain;
if (!calleeMatchesFunc(Call, ProfiledCalleeFunc, CallerFunc,
FoundCalleeChain)) {
++EI;
FoundCalleeChain))
return false;
}

// The usual case where the profiled callee matches that of the IR/summary.
if (FoundCalleeChain.empty()) {
++EI;
if (FoundCalleeChain.empty())
return true;
}

auto AddEdge = [Edge, &EI](ContextNode *Caller, ContextNode *Callee) {
auto *CurEdge = Callee->findEdgeFromCaller(Caller);
Expand Down Expand Up @@ -1960,6 +1954,13 @@ bool CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::calleesMatch(
Edge->Callee->eraseCallerEdge(Edge.get());
EI = Edge->Caller->CalleeEdges.erase(EI);

// To simplify the increment of EI in the caller, subtract one from EI.
// In the final AddEdge call we would have either added a new callee edge,
// to Edge->Caller, or found an existing one. Either way we are guaranteed
// that there is at least one callee edge.
assert(!Edge->Caller->CalleeEdges.empty());
--EI;

return true;
}

Expand Down

0 comments on commit edfe250

Please sign in to comment.