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

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 21 02:01:25 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Ömer Sinan Ağacan (osa1)

<details>
<summary>Changes</summary>

The changed values type `SmallDenseMap<PointerIntPair<Value *, 2, IPOGrouping>, ...>` is iterated non-deterministically because of the pointer key type, causing non-deterministic debug outputs (`-debug-only=sparseprop`) and compile times (when counting instructions).

Compile times of this PR relative to parent PR: https://llvm-compile-time-tracker.com/compare.php?from=60e18840f4dfe77735bc66baa9ea01dd11c08ba8&to=ec6a5c94ba4ceb647a216678a17a34f15495dfa0&stat=instructions:u

Compile times of the PR stack: https://llvm-compile-time-tracker.com/compare.php?from=ef9e67ed44778620f157911570e2f9707ec3e8b4&to=ec6a5c94ba4ceb647a216678a17a34f15495dfa0&stat=instructions:u

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


3 Files Affected:

- (modified) llvm/include/llvm/Analysis/SparsePropagation.h (+4-3) 
- (modified) llvm/lib/Transforms/IPO/CalledValuePropagation.cpp (+13-11) 
- (modified) llvm/unittests/Analysis/SparsePropagation.cpp (+17-13) 


``````````diff
diff --git a/llvm/include/llvm/Analysis/SparsePropagation.h b/llvm/include/llvm/Analysis/SparsePropagation.h
index f8a940ae357a1..e1810b4c32c0e 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 fd5a1507b2284..6edee935418bb 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 872620fa42145..b56624a44da8c 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();
   }

``````````

</details>


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


More information about the llvm-branch-commits mailing list