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

Mingjie Xu via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 18:58:42 PDT 2026


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

>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 1/5] [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)

>From 9a9053865027459c0a7bd9f378d18f379b75e51e Mon Sep 17 00:00:00 2001
From: Enna1 <xumingjie.enna1 at bytedance.com>
Date: Tue, 28 Apr 2026 23:27:50 +0800
Subject: [PATCH 2/5] fixup! [CodeGen] Use SmallMapVector for
 SpillPlacement::Node::Links

---
 llvm/lib/CodeGen/SpillPlacement.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp
index 4ac63cb2a6de2..620928a2a9a36 100644
--- a/llvm/lib/CodeGen/SpillPlacement.cpp
+++ b/llvm/lib/CodeGen/SpillPlacement.cpp
@@ -28,6 +28,7 @@
 
 #include "llvm/CodeGen/SpillPlacement.h"
 #include "llvm/ADT/BitVector.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/CodeGen/EdgeBundles.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"

>From afd55e876b58d88ab033b3f212acc15e93f7a4ea Mon Sep 17 00:00:00 2001
From: Enna1 <xumingjie.enna1 at bytedance.com>
Date: Tue, 28 Apr 2026 23:39:28 +0800
Subject: [PATCH 3/5] address review comments

---
 llvm/lib/CodeGen/SpillPlacement.cpp | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp
index 620928a2a9a36..8884024df5954 100644
--- a/llvm/lib/CodeGen/SpillPlacement.cpp
+++ b/llvm/lib/CodeGen/SpillPlacement.cpp
@@ -119,13 +119,9 @@ struct SpillPlacement::Node {
     SumLinkWeights += w;
 
     // There can be multiple links to the same bundle, add them up.
-    auto It = Links.find(b);
-    if (It != Links.end()) {
+    auto [It, Inserted] = Links.try_emplace(b, w);
+    if (!Inserted)
       It->second += w;
-      return;
-    }
-    // This must be the first link to b.
-    Links.insert(std::make_pair(b, w));
   }
 
   /// addBias - Bias this node.

>From 019c739fa96e5bafcaff9e0fa66e27539a9584df Mon Sep 17 00:00:00 2001
From: Enna1 <xumingjie.enna1 at bytedance.com>
Date: Wed, 29 Apr 2026 09:45:44 +0800
Subject: [PATCH 4/5] auto [BundleNo, Weight]

---
 llvm/lib/CodeGen/SpillPlacement.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp
index 8884024df5954..837ebbf62c5e8 100644
--- a/llvm/lib/CodeGen/SpillPlacement.cpp
+++ b/llvm/lib/CodeGen/SpillPlacement.cpp
@@ -147,11 +147,11 @@ struct SpillPlacement::Node {
     // Compute the weighted sum of inputs.
     BlockFrequency SumN = BiasN;
     BlockFrequency SumP = BiasP;
-    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;
+    for (auto [BundleNo, Weight] : Links) {
+      if (nodes[BundleNo].Value == -1)
+        SumN += Weight;
+      else if (nodes[BundleNo].Value == 1)
+        SumP += Weight;
     }
 
     // Each weighted sum is going to be less than the total frequency of the

>From da472384665ad643ccf6d9da44bdda037c27ac18 Mon Sep 17 00:00:00 2001
From: Enna1 <xumingjie.enna1 at bytedance.com>
Date: Wed, 29 Apr 2026 09:57:48 +0800
Subject: [PATCH 5/5] getDissentingNeighbors

---
 llvm/lib/CodeGen/SpillPlacement.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp
index 837ebbf62c5e8..1b3cd9ccbf08b 100644
--- a/llvm/lib/CodeGen/SpillPlacement.cpp
+++ b/llvm/lib/CodeGen/SpillPlacement.cpp
@@ -174,12 +174,11 @@ struct SpillPlacement::Node {
 
   void getDissentingNeighbors(SparseSet<unsigned> &List,
                               const Node nodes[]) const {
-    for (const auto &Elt : Links) {
-      unsigned n = Elt.first;
+    for (auto [BundleNo, _] : Links) {
       // Neighbors that already have the same value are not going to
       // change because of this node changing.
-      if (Value != nodes[n].Value)
-        List.insert(n);
+      if (Value != nodes[BundleNo].Value)
+        List.insert(BundleNo);
     }
   }
 };



More information about the llvm-commits mailing list