[llvm] [ADT][Reassociate] Invalidate stale UniformityInfo entries on instruction erase (PR #214719)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 05:49:02 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/214719

`GenericUniformityAnalysisImpl::UniformValues` never removes an entry when its value is erased. Reassociate keeps one `UniformityInfo` for the whole pass while creating and deleting instructions, so a new instruction can reuse a freed one's address and inherit its stale "uniform" verdict instead of the documented divergent default. This makes rank-boosting depend on heap addresses

Add `forgetValue()` (following the same idea as `ScalarEvolution::forgetValue`) and call it at Reassociate two erase sites

As a result of this issue we have a side effect that is reproducible on https://github.com/llvm/llvm-project/pull/214624: nondeterministic operand ordering in Reassociate output, because a newly-created instruction can spuriously be classified "uniform" instead of the documented default of divergent depending on whether the allocator happened to reuse a freed instruction address

That leads to nondeterministic optimized IR, which is what made __clang_hip_math.hip in the mentioned patch FileCheck output flaky (mul operands reordered from run to run for the SPIR-V target)

>From 84fabb63678576479b4eff3a11f55ec03081126f Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 7 Aug 2026 14:43:05 +0200
Subject: [PATCH] [ADT][Reassociate] Invalidate stale UniformityInfo entries on
 instruction erase

GenericUniformityAnalysisImpl::UniformValues never removes an entry when its value is erased. Reassociate keeps one UniformityInfo for the whole pass while creating and deleting instructions, so a new instruction can reuse a freed one's address and inherit its stale "uniform" verdict instead of the documented divergent default. This makes rank-boosting depend on heap addresses

Add forgetValue() (same idea as ScalarEvolution::forgetValue) and call it at Reassociate's two erase sites
---
 llvm/include/llvm/ADT/GenericUniformityImpl.h | 10 ++++++++++
 llvm/include/llvm/ADT/GenericUniformityInfo.h |  4 ++++
 llvm/lib/Transforms/Scalar/Reassociate.cpp    |  4 ++++
 3 files changed, 18 insertions(+)

diff --git a/llvm/include/llvm/ADT/GenericUniformityImpl.h b/llvm/include/llvm/ADT/GenericUniformityImpl.h
index bc8f546d798cc..d066e11c117df 100644
--- a/llvm/include/llvm/ADT/GenericUniformityImpl.h
+++ b/llvm/include/llvm/ADT/GenericUniformityImpl.h
@@ -404,6 +404,10 @@ template <typename ContextT> class GenericUniformityAnalysisImpl {
     return DivergentTermBlocks.contains(&B);
   }
 
+  /// Call before erasing \p V, or a later instruction reusing its address
+  /// may be misclassified as uniform.
+  void forgetValue(ConstValueRefT V) { UniformValues.erase(V); }
+
   void print(raw_ostream &Out) const;
 
   /// Print divergent arguments and return true if any were found.
@@ -1317,6 +1321,12 @@ bool GenericUniformityInfo<ContextT>::hasDivergentTerminator(const BlockT &B) {
   return DA && DA->hasDivergentTerminator(B);
 }
 
+template <typename ContextT>
+void GenericUniformityInfo<ContextT>::forgetValue(ConstValueRefT V) {
+  if (DA)
+    DA->forgetValue(V);
+}
+
 /// \brief T helper function for printing.
 template <typename ContextT>
 void GenericUniformityInfo<ContextT>::print(raw_ostream &Out) const {
diff --git a/llvm/include/llvm/ADT/GenericUniformityInfo.h b/llvm/include/llvm/ADT/GenericUniformityInfo.h
index 594d6f8bd1f60..682718b66f049 100644
--- a/llvm/include/llvm/ADT/GenericUniformityInfo.h
+++ b/llvm/include/llvm/ADT/GenericUniformityInfo.h
@@ -87,6 +87,10 @@ template <typename ContextT> class GenericUniformityInfo {
 
   bool hasDivergentTerminator(const BlockT &B);
 
+  /// Call before erasing \p V, or a later instruction reusing its address
+  /// may be misclassified as uniform.
+  void forgetValue(ConstValueRefT V);
+
   void print(raw_ostream &Out) const;
 
   iterator_range<TemporalDivergenceTuple *> getTemporalDivergenceList() const;
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index eb3d3b0218c74..c8a27e6509ec3 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -2061,6 +2061,8 @@ void ReassociatePass::RecursivelyEraseDeadInsts(Instruction *I,
   ValueRankMap.erase(I);
   Insts.remove(I);
   RedoInsts.remove(I);
+  if (UA)
+    UA->forgetValue(I);
   llvm::salvageDebugInfo(*I);
   I->eraseFromParent();
   for (auto *Op : Ops)
@@ -2078,6 +2080,8 @@ void ReassociatePass::EraseInst(Instruction *I) {
   // Erase the dead instruction.
   ValueRankMap.erase(I);
   RedoInsts.remove(I);
+  if (UA)
+    UA->forgetValue(I);
   llvm::salvageDebugInfo(*I);
   I->eraseFromParent();
   // Optimize its operands.



More information about the llvm-commits mailing list