[llvm] f69661d - [BFI] Use MapVector in combineWeightsByHashing. (#227027)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 02:48:25 PDT 2026


Author: Florian Hahn
Date: 2026-09-29T09:48:18Z
New Revision: f69661de1c6fd034afabc2acf79a888cf7b58c5c

URL: https://github.com/llvm/llvm-project/commit/f69661de1c6fd034afabc2acf79a888cf7b58c5c
DIFF: https://github.com/llvm/llvm-project/commit/f69661de1c6fd034afabc2acf79a888cf7b58c5c.diff

LOG: [BFI] Use MapVector in combineWeightsByHashing. (#227027)

combineWeightsByHashing iterated over a DenseMap, which depends on the
hash. Use MapVector to get consistent results, independent of index
type/values.

This is mainly to ensure consistency between users that use different
index numbers, i.e. used for IR BFI and VPlan's use.

PR: https://github.com/llvm/llvm-project/pull/227027

Added: 
    llvm/test/Analysis/BlockFrequencyInfo/many-successors-order.ll

Modified: 
    llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp b/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp
index 27c52afc3baab..66c36f36d7d92 100644
--- a/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp
+++ b/llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp
@@ -12,7 +12,7 @@
 
 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
 #include "llvm/ADT/APInt.h"
-#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SCCIterator.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Config/llvm-config.h"
@@ -197,10 +197,8 @@ static void combineWeightsBySorting(WeightList &Weights) {
 }
 
 static void combineWeightsByHashing(WeightList &Weights) {
-  // Collect weights into a DenseMap.
-  using HashTable = DenseMap<BlockNode::IndexType, Weight>;
-
-  HashTable Combined(NextPowerOf2(2 * Weights.size()));
+  MapVector<BlockNode::IndexType, Weight> Combined;
+  Combined.reserve(Weights.size());
   for (const Weight &W : Weights)
     combineWeight(Combined[W.TargetNode.Index], W);
 
@@ -210,7 +208,6 @@ static void combineWeightsByHashing(WeightList &Weights) {
 
   // Fill in the new weights.
   Weights.clear();
-  Weights.reserve(Combined.size());
   for (const auto &I : Combined)
     Weights.push_back(I.second);
 }

diff  --git a/llvm/test/Analysis/BlockFrequencyInfo/many-successors-order.ll b/llvm/test/Analysis/BlockFrequencyInfo/many-successors-order.ll
new file mode 100644
index 0000000000000..1479d175d83d7
--- /dev/null
+++ b/llvm/test/Analysis/BlockFrequencyInfo/many-successors-order.ll
@@ -0,0 +1,156 @@
+; RUN: opt < %s -passes='print<block-freq>' -disable-output 2>&1 | FileCheck %s
+
+; Blocks with more than 128 successor edges combine duplicate edges via a map.
+; Make sure that distributing the mass does not depend on the iteration order.
+
+; CHECK-LABEL: block-frequency-info: f
+; CHECK-NEXT:  - entry: float = 1.0, int = [[ENTRY:[0-9]+]]
+; CHECK-NEXT:  - default: float = 0.0077519, int = {{[0-9]+}}
+; CHECK-NEXT:  - b0: float = 0.49612, int = [[B:[0-9]+]]
+; CHECK-NEXT:  - b1: float = 0.49612, int = [[B]]
+; CHECK-NEXT:  - exit: float = 1.0, int = [[ENTRY]]
+define void @f(i32 %x) {
+entry:
+  switch i32 %x, label %default [
+    i32 0, label %b0
+    i32 1, label %b1
+    i32 2, label %b0
+    i32 3, label %b1
+    i32 4, label %b0
+    i32 5, label %b1
+    i32 6, label %b0
+    i32 7, label %b1
+    i32 8, label %b0
+    i32 9, label %b1
+    i32 10, label %b0
+    i32 11, label %b1
+    i32 12, label %b0
+    i32 13, label %b1
+    i32 14, label %b0
+    i32 15, label %b1
+    i32 16, label %b0
+    i32 17, label %b1
+    i32 18, label %b0
+    i32 19, label %b1
+    i32 20, label %b0
+    i32 21, label %b1
+    i32 22, label %b0
+    i32 23, label %b1
+    i32 24, label %b0
+    i32 25, label %b1
+    i32 26, label %b0
+    i32 27, label %b1
+    i32 28, label %b0
+    i32 29, label %b1
+    i32 30, label %b0
+    i32 31, label %b1
+    i32 32, label %b0
+    i32 33, label %b1
+    i32 34, label %b0
+    i32 35, label %b1
+    i32 36, label %b0
+    i32 37, label %b1
+    i32 38, label %b0
+    i32 39, label %b1
+    i32 40, label %b0
+    i32 41, label %b1
+    i32 42, label %b0
+    i32 43, label %b1
+    i32 44, label %b0
+    i32 45, label %b1
+    i32 46, label %b0
+    i32 47, label %b1
+    i32 48, label %b0
+    i32 49, label %b1
+    i32 50, label %b0
+    i32 51, label %b1
+    i32 52, label %b0
+    i32 53, label %b1
+    i32 54, label %b0
+    i32 55, label %b1
+    i32 56, label %b0
+    i32 57, label %b1
+    i32 58, label %b0
+    i32 59, label %b1
+    i32 60, label %b0
+    i32 61, label %b1
+    i32 62, label %b0
+    i32 63, label %b1
+    i32 64, label %b0
+    i32 65, label %b1
+    i32 66, label %b0
+    i32 67, label %b1
+    i32 68, label %b0
+    i32 69, label %b1
+    i32 70, label %b0
+    i32 71, label %b1
+    i32 72, label %b0
+    i32 73, label %b1
+    i32 74, label %b0
+    i32 75, label %b1
+    i32 76, label %b0
+    i32 77, label %b1
+    i32 78, label %b0
+    i32 79, label %b1
+    i32 80, label %b0
+    i32 81, label %b1
+    i32 82, label %b0
+    i32 83, label %b1
+    i32 84, label %b0
+    i32 85, label %b1
+    i32 86, label %b0
+    i32 87, label %b1
+    i32 88, label %b0
+    i32 89, label %b1
+    i32 90, label %b0
+    i32 91, label %b1
+    i32 92, label %b0
+    i32 93, label %b1
+    i32 94, label %b0
+    i32 95, label %b1
+    i32 96, label %b0
+    i32 97, label %b1
+    i32 98, label %b0
+    i32 99, label %b1
+    i32 100, label %b0
+    i32 101, label %b1
+    i32 102, label %b0
+    i32 103, label %b1
+    i32 104, label %b0
+    i32 105, label %b1
+    i32 106, label %b0
+    i32 107, label %b1
+    i32 108, label %b0
+    i32 109, label %b1
+    i32 110, label %b0
+    i32 111, label %b1
+    i32 112, label %b0
+    i32 113, label %b1
+    i32 114, label %b0
+    i32 115, label %b1
+    i32 116, label %b0
+    i32 117, label %b1
+    i32 118, label %b0
+    i32 119, label %b1
+    i32 120, label %b0
+    i32 121, label %b1
+    i32 122, label %b0
+    i32 123, label %b1
+    i32 124, label %b0
+    i32 125, label %b1
+    i32 126, label %b0
+    i32 127, label %b1
+  ]
+
+default:
+  br label %exit
+
+b0:
+  br label %exit
+
+b1:
+  br label %exit
+
+exit:
+  ret void
+}


        


More information about the llvm-commits mailing list