Skip to content

[MemProf] Refactor context node creation into a new helper (NFC) #108408

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,16 @@ class CallsiteContextGraph {
return static_cast<const DerivedCCG *>(this)->getLabel(Func, Call, CloneNo);
}

// Create and return a new ContextNode.
ContextNode *createNewNode(bool IsAllocation, const FuncTy *F = nullptr,
CallInfo C = CallInfo()) {
NodeOwner.push_back(std::make_unique<ContextNode>(IsAllocation, C));
auto *NewNode = NodeOwner.back().get();
if (F)
NodeToCallingFunc[NewNode] = F;
return NewNode;
}

/// Helpers to find the node corresponding to the given call or stackid.
ContextNode *getNodeForInst(const CallInfo &C);
ContextNode *getNodeForAlloc(const CallInfo &C);
Expand Down Expand Up @@ -1023,11 +1033,8 @@ typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode *
CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::addAllocNode(
CallInfo Call, const FuncTy *F) {
assert(!getNodeForAlloc(Call));
NodeOwner.push_back(
std::make_unique<ContextNode>(/*IsAllocation=*/true, Call));
ContextNode *AllocNode = NodeOwner.back().get();
ContextNode *AllocNode = createNewNode(/*IsAllocation=*/true, F, Call);
AllocationCallToContextNodeMap[Call] = AllocNode;
NodeToCallingFunc[AllocNode] = F;
// Use LastContextId as a uniq id for MIB allocation nodes.
AllocNode->OrigStackOrAllocId = LastContextId;
// Alloc type should be updated as we add in the MIBs. We should assert
Expand Down Expand Up @@ -1084,9 +1091,7 @@ void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::addStackNodesForMIB(
auto StackId = getStackId(*ContextIter);
ContextNode *StackNode = getNodeForStackId(StackId);
if (!StackNode) {
NodeOwner.push_back(
std::make_unique<ContextNode>(/*IsAllocation=*/false));
StackNode = NodeOwner.back().get();
StackNode = createNewNode(/*IsAllocation=*/false);
StackEntryIdToContextNodeMap[StackId] = StackNode;
StackNode->OrigStackOrAllocId = StackId;
}
Expand Down Expand Up @@ -1371,10 +1376,7 @@ void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::
continue;

// Create new context node.
NodeOwner.push_back(
std::make_unique<ContextNode>(/*IsAllocation=*/false, Call));
ContextNode *NewNode = NodeOwner.back().get();
NodeToCallingFunc[NewNode] = Func;
ContextNode *NewNode = createNewNode(/*IsAllocation=*/false, Func, Call);
NonAllocationCallToContextNodeMap[Call] = NewNode;
NewNode->AllocTypes = computeAllocType(SavedContextIds);

Expand Down Expand Up @@ -2083,10 +2085,7 @@ bool CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::calleesMatch(
} else {
FuncToCallsWithMetadata[Func].push_back({NewCall});
// Create Node and record node info.
NodeOwner.push_back(
std::make_unique<ContextNode>(/*IsAllocation=*/false, NewCall));
NewNode = NodeOwner.back().get();
NodeToCallingFunc[NewNode] = Func;
NewNode = createNewNode(/*IsAllocation=*/false, Func, NewCall);
TailCallToContextNodeMap[NewCall] = NewNode;
NewNode->AllocTypes = Edge->AllocTypes;
}
Expand Down Expand Up @@ -2655,13 +2654,11 @@ CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::moveEdgeToNewCalleeClone(
const std::shared_ptr<ContextEdge> &Edge, EdgeIter *CallerEdgeI,
DenseSet<uint32_t> ContextIdsToMove) {
ContextNode *Node = Edge->Callee;
NodeOwner.push_back(
std::make_unique<ContextNode>(Node->IsAllocation, Node->Call));
ContextNode *Clone = NodeOwner.back().get();
assert(NodeToCallingFunc.count(Node));
ContextNode *Clone =
createNewNode(Node->IsAllocation, NodeToCallingFunc[Node], Node->Call);
Node->addClone(Clone);
Clone->MatchingCalls = Node->MatchingCalls;
assert(NodeToCallingFunc.count(Node));
NodeToCallingFunc[Clone] = NodeToCallingFunc[Node];
moveEdgeToExistingCalleeClone(Edge, Clone, CallerEdgeI, /*NewClone=*/true,
ContextIdsToMove);
return Clone;
Expand Down
Loading