[llvm-branch-commits] [llvm] [CalledValuePropagation] Use SmallMapVector instead of SmallDenseMap for deterministic iteration in the fixpoint loop (PR #244851)

Ömer Sinan Ağacan via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 21 00:49:42 PST 2026


https://github.com/osa1 created https://github.com/llvm/llvm-project/pull/244851

None

>From ec6a5c94ba4ceb647a216678a17a34f15495dfa0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= <omer at osa1.net>
Date: Sat, 21 Nov 2026 08:40:35 +0000
Subject: [PATCH] [CalledValuePropagation] Use SmallMapVector instead of
 SmallDenseMap for deterministic iteration in the fixpoint loop

---
 .../include/llvm/Analysis/SparsePropagation.h |  7 +++--
 .../Transforms/IPO/CalledValuePropagation.cpp | 24 ++++++++-------
 llvm/unittests/Analysis/SparsePropagation.cpp | 30 +++++++++++--------
 3 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/Analysis/SparsePropagation.h b/llvm/include/llvm/Analysis/SparsePropagation.h
index f8a940ae357a1b..e1810b4c32c0e0 100644
--- a/llvm/include/llvm/Analysis/SparsePropagation.h
+++ b/llvm/include/llvm/Analysis/SparsePropagation.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_ANALYSIS_SPARSEPROPAGATION_H
 #define LLVM_ANALYSIS_SPARSEPROPAGATION_H
 
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Instructions.h"
@@ -88,7 +89,7 @@ template <class LatticeKey, class LatticeVal> class AbstractLatticeFunction {
   /// of executing instruction \p I. Their associated LatticeVals are store in
   /// \p ChangedValues.
   virtual void ComputeInstructionState(
-      Instruction &I, SmallDenseMap<LatticeKey, LatticeVal, 16> &ChangedValues,
+      Instruction &I, SmallMapVector<LatticeKey, LatticeVal, 16> &ChangedValues,
       SparseSolver<LatticeKey, LatticeVal> &SS) = 0;
 
   /// PrintLatticeVal - Render the given LatticeVal to the specified stream.
@@ -400,7 +401,7 @@ void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::visitPHINode(PHINode &PN) {
   // computed from its incoming values.  For example, SSI form stores its sigma
   // functions as PHINodes with a single incoming value.
   if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
-    SmallDenseMap<LatticeKey, LatticeVal, 16> ChangedValues;
+    SmallMapVector<LatticeKey, LatticeVal, 16> ChangedValues;
     LatticeFunc->ComputeInstructionState(PN, ChangedValues, *this);
     for (auto &ChangedValue : ChangedValues)
       if (ChangedValue.second != LatticeFunc->getUntrackedVal())
@@ -455,7 +456,7 @@ void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::visitInst(Instruction &I) {
 
   // Otherwise, ask the transfer function what the result is.  If this is
   // something that we care about, remember it.
-  SmallDenseMap<LatticeKey, LatticeVal, 16> ChangedValues;
+  SmallMapVector<LatticeKey, LatticeVal, 16> ChangedValues;
   LatticeFunc->ComputeInstructionState(I, ChangedValues, *this);
   for (auto &ChangedValue : ChangedValues)
     if (ChangedValue.second != LatticeFunc->getUntrackedVal())
diff --git a/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp b/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp
index fd5a1507b22846..6edee935418bb2 100644
--- a/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp
+++ b/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp
@@ -169,7 +169,7 @@ class CVPLatticeFunc
   /// can be called.
   void ComputeInstructionState(
       Instruction &I,
-      SmallDenseMap<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
+      SmallMapVector<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
       SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) override {
     switch (I.getOpcode()) {
     case Instruction::Call:
@@ -240,7 +240,7 @@ class CVPLatticeFunc
   /// the returned value state and the function's return state.
   void
   visitReturn(ReturnInst &I,
-              SmallDenseMap<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
+              SmallMapVector<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
               SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
     Function *F = I.getParent()->getParent();
     if (F->getReturnType()->isVoidTy())
@@ -257,7 +257,7 @@ class CVPLatticeFunc
   /// with the returned value state of the called function.
   void
   visitCallBase(CallBase &CB,
-                SmallDenseMap<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
+                SmallMapVector<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
                 SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
     Function *F = CB.getCalledFunction();
     auto RegI = CVPLatticeKey(&CB, IPOGrouping::Register);
@@ -302,7 +302,7 @@ class CVPLatticeFunc
   /// true and false value states.
   void
   visitSelect(SelectInst &I,
-              SmallDenseMap<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
+              SmallMapVector<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
               SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
     auto RegI = CVPLatticeKey(&I, IPOGrouping::Register);
     auto RegT = CVPLatticeKey(I.getTrueValue(), IPOGrouping::Register);
@@ -314,9 +314,10 @@ class CVPLatticeFunc
   /// Handle load instructions. If the pointer operand of the load is a global
   /// variable, we attempt to track the value. The loaded value state is the
   /// merge of the loaded value state with the global variable state.
-  void visitLoad(LoadInst &I,
-                 SmallDenseMap<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
-                 SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
+  void
+  visitLoad(LoadInst &I,
+            SmallMapVector<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
+            SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
     auto RegI = CVPLatticeKey(&I, IPOGrouping::Register);
     if (auto *GV = dyn_cast<GlobalVariable>(I.getPointerOperand())) {
       auto MemGV = CVPLatticeKey(GV, IPOGrouping::Memory);
@@ -332,7 +333,7 @@ class CVPLatticeFunc
   /// is the merge of the stored value state with the global variable state.
   void
   visitStore(StoreInst &I,
-             SmallDenseMap<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
+             SmallMapVector<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
              SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
     auto *GV = dyn_cast<GlobalVariable>(I.getPointerOperand());
     if (!GV)
@@ -345,9 +346,10 @@ class CVPLatticeFunc
 
   /// Handle all other instructions. All other instructions are marked
   /// overdefined.
-  void visitInst(Instruction &I,
-                 SmallDenseMap<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
-                 SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
+  void
+  visitInst(Instruction &I,
+            SmallMapVector<CVPLatticeKey, CVPLatticeVal, 16> &ChangedValues,
+            SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) {
     // Simply bail if this instruction has no user.
     if (I.use_empty())
       return;
diff --git a/llvm/unittests/Analysis/SparsePropagation.cpp b/llvm/unittests/Analysis/SparsePropagation.cpp
index 872620fa42145a..b56624a44da8ce 100644
--- a/llvm/unittests/Analysis/SparsePropagation.cpp
+++ b/llvm/unittests/Analysis/SparsePropagation.cpp
@@ -140,7 +140,7 @@ class TestLatticeFunc
   /// instruction. We only handle the few instructions needed for the tests.
   void ComputeInstructionState(
       Instruction &I,
-      SmallDenseMap<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
+      SmallMapVector<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
       SparseSolver<TestLatticeKey, TestLatticeVal> &SS) override {
     switch (I.getOpcode()) {
     case Instruction::Call:
@@ -159,9 +159,10 @@ class TestLatticeFunc
   /// of the current formal argument state with the call site's corresponding
   /// actual argument state. The call site state is the merge of the call site
   /// state with the returned value state of the called function.
-  void visitCallBase(CallBase &I,
-                     SmallDenseMap<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
-                     SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
+  void visitCallBase(
+      CallBase &I,
+      SmallMapVector<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
+      SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
     Function *F = I.getCalledFunction();
     auto RegI = TestLatticeKey(&I, IPOGrouping::Register);
     if (!F) {
@@ -183,9 +184,10 @@ class TestLatticeFunc
 
   /// Handle return instructions. The function's return state is the merge of
   /// the returned value state and the function's current return state.
-  void visitReturn(ReturnInst &I,
-                   SmallDenseMap<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
-                   SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
+  void
+  visitReturn(ReturnInst &I,
+              SmallMapVector<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
+              SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
     Function *F = I.getParent()->getParent();
     if (F->getReturnType()->isVoidTy())
       return;
@@ -199,9 +201,10 @@ class TestLatticeFunc
   /// global variable, we attempt to track the value. The global variable state
   /// is the merge of the stored value state with the current global variable
   /// state.
-  void visitStore(StoreInst &I,
-                  SmallDenseMap<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
-                  SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
+  void
+  visitStore(StoreInst &I,
+             SmallMapVector<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
+             SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
     auto *GV = dyn_cast<GlobalVariable>(I.getPointerOperand());
     if (!GV)
       return;
@@ -213,9 +216,10 @@ class TestLatticeFunc
 
   /// Handle all other instructions. All other instructions are marked
   /// overdefined.
-  void visitInst(Instruction &I,
-                 SmallDenseMap<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
-                 SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
+  void
+  visitInst(Instruction &I,
+            SmallMapVector<TestLatticeKey, TestLatticeVal, 16> &ChangedValues,
+            SparseSolver<TestLatticeKey, TestLatticeVal> &SS) {
     auto RegI = TestLatticeKey(&I, IPOGrouping::Register);
     ChangedValues[RegI] = getOverdefinedVal();
   }



More information about the llvm-branch-commits mailing list