[llvm] f02b481 - [AssumptionCache] Deduplicate affected values in removeAffectedValues (#205441)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 01:44:02 PDT 2026


Author: Krisitan Erik Olsen
Date: 2026-07-08T08:43:46Z
New Revision: f02b48182be73c531fb83312da15c85031fadde3

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

LOG: [AssumptionCache] Deduplicate affected values in removeAffectedValues (#205441)

removeAffectedValues can encounter duplicate entries in the Affected
list when a separate_storage bundle uses the same pointer for both
arguments (e.g. "separate_storage"(ptr %p, ptr %p)). The duplicate
entries share both the same value and the same bundle index. The first
iteration handles the value completely and may erase the AffectedValues
entry, causing the second iteration to fail the assertion.

This patch deduplicates entries in removeAffectedValues by (Value *,
Index) pair, matching the deduplication already done in
updateAffectedValues. This also fixes the ExpectedMatches counting in
the debug logic added by #205275, which otherwise miscounts when
duplicates are present.

Fixes #205378.

Added: 
    llvm/test/Transforms/DropUnnecessaryAssumes/duplicate-affected-value.ll

Modified: 
    llvm/lib/Analysis/AssumptionCache.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/AssumptionCache.cpp b/llvm/lib/Analysis/AssumptionCache.cpp
index d7db161b05381..0794d83a844e2 100644
--- a/llvm/lib/Analysis/AssumptionCache.cpp
+++ b/llvm/lib/Analysis/AssumptionCache.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
@@ -132,9 +133,11 @@ void AssumptionCache::removeAffectedValues(AssumeInst *CI) {
   // an assertion failure. Avoid this by counting the number of expected
   // matches.
 #ifndef NDEBUG
+  SmallDenseSet<std::pair<Value *, unsigned>, 16> Seen;
   DenseMap<Value *, int> ExpectedMatches;
   for (auto &AV : Affected)
-    if (AffectedValues.find_as(AV.Assume) != AffectedValues.end())
+    if (Seen.insert({AV.Assume, AV.Index}).second &&
+        AffectedValues.find_as(AV.Assume) != AffectedValues.end())
       ExpectedMatches[AV.Assume]++;
 #endif
 

diff  --git a/llvm/test/Transforms/DropUnnecessaryAssumes/duplicate-affected-value.ll b/llvm/test/Transforms/DropUnnecessaryAssumes/duplicate-affected-value.ll
new file mode 100644
index 0000000000000..2c1475f48a6d4
--- /dev/null
+++ b/llvm/test/Transforms/DropUnnecessaryAssumes/duplicate-affected-value.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=drop-unnecessary-assumes -S < %s | FileCheck %s
+; This test verifies that drop-unnecessary-assumes does not crash when
+; a separate_storage bundle has the same pointer for both arguments.
+
+declare void @llvm.assume(i1)
+
+define void @f(ptr %p) {
+; CHECK-LABEL: define void @f(
+; CHECK-SAME: ptr [[P:%.*]]) {
+; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "dereferenceable"(ptr null, i64 0) ]
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.assume(i1 true) [ "separate_storage"(ptr %p, ptr %p), "dereferenceable"(ptr null, i64 0) ]
+  call void @llvm.assume(i1 true) [ "align"(ptr %p, i64 0) ]
+  ret void
+}


        


More information about the llvm-commits mailing list