[llvm] [CodeLayout] CDSortImpl: remove linear-time erase_value from mergeChains (PR #69276)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 16 18:57:10 PDT 2023


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/69276

After mergeChainPairs initializes a priority queue, HotChains is unused
except a HotChains.size() use in LLVM_DEBUG. Replace it with a variable.


>From 6f0afe84913bd118e27ed30ed26ecf135d83a8ab Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Mon, 16 Oct 2023 18:54:12 -0700
Subject: [PATCH] [CodeLayout] CDSortImpl: remove linear-time erase_value from
 mergeChains

After mergeChainPairs initializes a priority queue, HotChains is unused
except a HotChains.size() use in LLVM_DEBUG. Replace it with a variable.
---
 llvm/lib/Transforms/Utils/CodeLayout.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/CodeLayout.cpp b/llvm/lib/Transforms/Utils/CodeLayout.cpp
index d9e302d8b4fa54d..d2f39b0284c6799 100644
--- a/llvm/lib/Transforms/Utils/CodeLayout.cpp
+++ b/llvm/lib/Transforms/Utils/CodeLayout.cpp
@@ -1027,7 +1027,7 @@ class CDSortImpl {
 
     LLVM_DEBUG(dbgs() << "Cache-directed function sorting reduced the number"
                       << " of chains from " << NumNodes << " to "
-                      << HotChains.size() << "\n");
+                      << NumHotChains << "\n");
 
     // Collect nodes from all the chains.
     return concatChains();
@@ -1085,6 +1085,7 @@ class CDSortImpl {
       if (Node.ExecutionCount > 0)
         HotChains.push_back(&AllChains.back());
     }
+    NumHotChains = HotChains.size();
 
     // Initialize chain edges.
     AllEdges.reserve(AllJumps.size());
@@ -1152,6 +1153,7 @@ class CDSortImpl {
       MergeGainT BestGain = BestEdge->getMergeGain();
       mergeChains(BestSrcChain, BestDstChain, BestGain.mergeOffset(),
                   BestGain.mergeType());
+      --NumHotChains;
 
       // Insert newly created edges into the queue.
       for (const auto &[_, Edge] : BestSrcChain->Edges) {
@@ -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.
@@ -1372,6 +1371,7 @@ class CDSortImpl {
 
   /// Active chains. The vector gets updated at runtime when chains are merged.
   std::vector<ChainT *> HotChains;
+  size_t NumHotChains = 0;
 
   /// The total number of samples in the graph.
   uint64_t TotalSamples{0};



More information about the llvm-commits mailing list