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

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 16 18:58:18 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Fangrui Song (MaskRay)

<details>
<summary>Changes</summary>

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


---
Full diff: https://github.com/llvm/llvm-project/pull/69276.diff


1 Files Affected:

- (modified) llvm/lib/Transforms/Utils/CodeLayout.cpp (+4-4) 


``````````diff
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};

``````````

</details>


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


More information about the llvm-commits mailing list