Skip to content

[CodeLayout] CDSortImpl: remove HotChains and remove linear-time erase_value from mergeChains #69276

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 3 commits into from
Oct 18, 2023
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
26 changes: 11 additions & 15 deletions llvm/lib/Transforms/Utils/CodeLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,10 +1025,6 @@ class CDSortImpl {
// Merge pairs of chains while improving the objective.
mergeChainPairs();

LLVM_DEBUG(dbgs() << "Cache-directed function sorting reduced the number"
<< " of chains from " << NumNodes << " to "
<< HotChains.size() << "\n");

// Collect nodes from all the chains.
return concatChains();
}
Expand Down Expand Up @@ -1074,16 +1070,13 @@ class CDSortImpl {

// Initialize chains.
AllChains.reserve(NumNodes);
HotChains.reserve(NumNodes);
for (NodeT &Node : AllNodes) {
// Adjust execution counts.
Node.ExecutionCount = std::max(Node.ExecutionCount, Node.inCount());
Node.ExecutionCount = std::max(Node.ExecutionCount, Node.outCount());
// Create chain.
AllChains.emplace_back(Node.Index, &Node);
Node.CurChain = &AllChains.back();
if (Node.ExecutionCount > 0)
HotChains.push_back(&AllChains.back());
}

// Initialize chain edges.
Expand Down Expand Up @@ -1116,8 +1109,12 @@ class CDSortImpl {
std::set<ChainEdge *, decltype(GainComparator)> Queue(GainComparator);

// Insert the edges into the queue.
for (ChainT *ChainPred : HotChains) {
for (const auto &[_, Edge] : ChainPred->Edges) {
[[maybe_unused]] size_t NumActiveChains = 0;
for (NodeT &Node : AllNodes) {
if (Node.ExecutionCount == 0)
continue;
++NumActiveChains;
for (const auto &[_, Edge] : Node.CurChain->Edges) {
// Ignore self-edges.
if (Edge->isSelfEdge())
continue;
Expand Down Expand Up @@ -1152,6 +1149,7 @@ class CDSortImpl {
MergeGainT BestGain = BestEdge->getMergeGain();
mergeChains(BestSrcChain, BestDstChain, BestGain.mergeOffset(),
BestGain.mergeType());
--NumActiveChains;

// Insert newly created edges into the queue.
for (const auto &[_, Edge] : BestSrcChain->Edges) {
Expand All @@ -1167,6 +1165,10 @@ class CDSortImpl {
Queue.insert(Edge);
}
}

LLVM_DEBUG(dbgs() << "Cache-directed function sorting reduced the number"
<< " of chains from " << NumNodes << " to "
<< NumActiveChains << "\n");
}

/// Compute the gain of merging two chains.
Expand Down Expand Up @@ -1301,9 +1303,6 @@ class CDSortImpl {
// Merge the edges.
Into->mergeEdges(From);
From->clear();

// Remove the chain from the list of active chains.
llvm::erase_value(HotChains, From);
}

/// Concatenate all chains into the final order.
Expand Down Expand Up @@ -1370,9 +1369,6 @@ class CDSortImpl {
/// All edges between the chains.
std::vector<ChainEdge> AllEdges;

/// Active chains. The vector gets updated at runtime when chains are merged.
std::vector<ChainT *> HotChains;

/// The total number of samples in the graph.
uint64_t TotalSamples{0};

Expand Down