[llvm] [CodeGen] Use SmallMapVector for SpillPlacement::Node::Links (PR #194653)

Mingjie Xu via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 08:04:07 PDT 2026


https://github.com/Enna1 created https://github.com/llvm/llvm-project/pull/194653

Previously, `SpillPlacement::Node::Links` was implemented as a `SmallVector` of `(Weight, BundleNo)` pairs.

This patch replaces the `SmallVector` with a `SmallMapVector<unsigned, BlockFrequency, 4>`, which stores `(BundleNo, Weight)` pairs. This allows for more efficient lookups and weight accumulations when multiple links to the same bundle are added.

>From 540c40fc5ac9d0bd32d10f193fdc4afa94ec274c Mon Sep 17 00:00:00 2001
From: Enna1 <xumingjie.enna1 at bytedance.com>
Date: Tue, 28 Apr 2026 22:30:54 +0800
Subject: [PATCH] [CodeGen] Use SmallMapVector for SpillPlacement::Node::Links

Previously, `SpillPlacement::Node::Links` was implemented as a `SmallVector`
of `(Weight, BundleNo)` pairs.

This patch replaces the `SmallVector` with a `SmallMapVector<unsigned, BlockFrequency, 4>`,
which stores `(BundleNo, Weight)` pairs. This allows for more efficient
lookups and weight accumulations when multiple links to the same bundle are
added.
---
 llvm/lib/CodeGen/SpillPlacement.cpp | 30 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp
index 55a96a22a00ec..4ac63cb2a6de2 100644
--- a/llvm/lib/CodeGen/SpillPlacement.cpp
+++ b/llvm/lib/CodeGen/SpillPlacement.cpp
@@ -81,11 +81,9 @@ struct SpillPlacement::Node {
   /// variable should go in a register through this bundle.
   int Value;
 
-  using LinkVector = SmallVector<std::pair<BlockFrequency, unsigned>, 4>;
-
-  /// Links - (Weight, BundleNo) for all transparent blocks connecting to other
+  /// Links - (BundleNo, Weight) for all transparent blocks connecting to other
   /// bundles. The weights are all positive block frequencies.
-  LinkVector Links;
+  SmallMapVector<unsigned, BlockFrequency, 4> Links;
 
   /// SumLinkWeights - Cached sum of the weights of all links + ThresHold.
   BlockFrequency SumLinkWeights;
@@ -120,13 +118,13 @@ struct SpillPlacement::Node {
     SumLinkWeights += w;
 
     // There can be multiple links to the same bundle, add them up.
-    for (std::pair<BlockFrequency, unsigned> &L : Links)
-      if (L.second == b) {
-        L.first += w;
-        return;
-      }
+    auto It = Links.find(b);
+    if (It != Links.end()) {
+      It->second += w;
+      return;
+    }
     // This must be the first link to b.
-    Links.push_back(std::make_pair(w, b));
+    Links.insert(std::make_pair(b, w));
   }
 
   /// addBias - Bias this node.
@@ -152,11 +150,11 @@ struct SpillPlacement::Node {
     // Compute the weighted sum of inputs.
     BlockFrequency SumN = BiasN;
     BlockFrequency SumP = BiasP;
-    for (std::pair<BlockFrequency, unsigned> &L : Links) {
-      if (nodes[L.second].Value == -1)
-        SumN += L.first;
-      else if (nodes[L.second].Value == 1)
-        SumP += L.first;
+    for (std::pair<unsigned int, llvm::BlockFrequency> &L : Links) {
+      if (nodes[L.first].Value == -1)
+        SumN += L.second;
+      else if (nodes[L.first].Value == 1)
+        SumP += L.second;
     }
 
     // Each weighted sum is going to be less than the total frequency of the
@@ -180,7 +178,7 @@ struct SpillPlacement::Node {
   void getDissentingNeighbors(SparseSet<unsigned> &List,
                               const Node nodes[]) const {
     for (const auto &Elt : Links) {
-      unsigned n = Elt.second;
+      unsigned n = Elt.first;
       // Neighbors that already have the same value are not going to
       // change because of this node changing.
       if (Value != nodes[n].Value)



More information about the llvm-commits mailing list