Skip to content

Commit beffc82

Browse files
authored
[CodeLayout] CDSortImpl: remove HotChains and remove linear-time erase_value from mergeChains (#69276)
After mergeChainPairs initializes a priority queue, HotChains is unused except a HotChains.size() use in LLVM_DEBUG. Optimize it out.
1 parent ae3ba72 commit beffc82

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

llvm/lib/Transforms/Utils/CodeLayout.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,10 +1025,6 @@ class CDSortImpl {
10251025
// Merge pairs of chains while improving the objective.
10261026
mergeChainPairs();
10271027

1028-
LLVM_DEBUG(dbgs() << "Cache-directed function sorting reduced the number"
1029-
<< " of chains from " << NumNodes << " to "
1030-
<< HotChains.size() << "\n");
1031-
10321028
// Collect nodes from all the chains.
10331029
return concatChains();
10341030
}
@@ -1074,16 +1070,13 @@ class CDSortImpl {
10741070

10751071
// Initialize chains.
10761072
AllChains.reserve(NumNodes);
1077-
HotChains.reserve(NumNodes);
10781073
for (NodeT &Node : AllNodes) {
10791074
// Adjust execution counts.
10801075
Node.ExecutionCount = std::max(Node.ExecutionCount, Node.inCount());
10811076
Node.ExecutionCount = std::max(Node.ExecutionCount, Node.outCount());
10821077
// Create chain.
10831078
AllChains.emplace_back(Node.Index, &Node);
10841079
Node.CurChain = &AllChains.back();
1085-
if (Node.ExecutionCount > 0)
1086-
HotChains.push_back(&AllChains.back());
10871080
}
10881081

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

11181111
// Insert the edges into the queue.
1119-
for (ChainT *ChainPred : HotChains) {
1120-
for (const auto &[_, Edge] : ChainPred->Edges) {
1112+
[[maybe_unused]] size_t NumActiveChains = 0;
1113+
for (NodeT &Node : AllNodes) {
1114+
if (Node.ExecutionCount == 0)
1115+
continue;
1116+
++NumActiveChains;
1117+
for (const auto &[_, Edge] : Node.CurChain->Edges) {
11211118
// Ignore self-edges.
11221119
if (Edge->isSelfEdge())
11231120
continue;
@@ -1152,6 +1149,7 @@ class CDSortImpl {
11521149
MergeGainT BestGain = BestEdge->getMergeGain();
11531150
mergeChains(BestSrcChain, BestDstChain, BestGain.mergeOffset(),
11541151
BestGain.mergeType());
1152+
--NumActiveChains;
11551153

11561154
// Insert newly created edges into the queue.
11571155
for (const auto &[_, Edge] : BestSrcChain->Edges) {
@@ -1167,6 +1165,10 @@ class CDSortImpl {
11671165
Queue.insert(Edge);
11681166
}
11691167
}
1168+
1169+
LLVM_DEBUG(dbgs() << "Cache-directed function sorting reduced the number"
1170+
<< " of chains from " << NumNodes << " to "
1171+
<< NumActiveChains << "\n");
11701172
}
11711173

11721174
/// Compute the gain of merging two chains.
@@ -1301,9 +1303,6 @@ class CDSortImpl {
13011303
// Merge the edges.
13021304
Into->mergeEdges(From);
13031305
From->clear();
1304-
1305-
// Remove the chain from the list of active chains.
1306-
llvm::erase_value(HotChains, From);
13071306
}
13081307

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

1373-
/// Active chains. The vector gets updated at runtime when chains are merged.
1374-
std::vector<ChainT *> HotChains;
1375-
13761372
/// The total number of samples in the graph.
13771373
uint64_t TotalSamples{0};
13781374

0 commit comments

Comments
 (0)