[llvm] [AssumptionCache] Fix removeAffectedValues() when value is repeated in AssumeInst (PR #205275)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 23 00:20:45 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Thurston Dang (thurstond)

<details>
<summary>Changes</summary>

If a value appears more than once in an AssumeInst (e.g., `ptr %arg1` in `call void @<!-- -->llvm.assume(i1 true) [ "dereferenceable"(ptr %arg1, i64 1), "align"(ptr %arg1, i64 8) ]`) it will appear multiple times in the result of findAffectedValues(). removeAffectedValues() may (depending on how the results in AffectedValues.find_as(AV.Assume) are ordered), nullify multiple values from AffectedValues.find_as(AV.Assume) in one iteration of an outer for loop. The next iteration of that outer for loop might then find a match only to a different AssumeInst, resulting in an assertion failure.

This patch fixes the issue by counting the number of matching ResultElems we expect to find.

This was a latent bug that was revealed by https://github.com/llvm/llvm-project/pull/204432; the latter is not itself buggy, but relies on AssumptionCache::removeAffectedValues().

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


2 Files Affected:

- (modified) llvm/lib/Analysis/AssumptionCache.cpp (+39-1) 
- (modified) llvm/test/Transforms/DropUnnecessaryAssumes/dereferenceable.ll (+45) 


``````````diff
diff --git a/llvm/lib/Analysis/AssumptionCache.cpp b/llvm/lib/Analysis/AssumptionCache.cpp
index 1f5fea2fa5d1a..c020c2cbd01ff 100644
--- a/llvm/lib/Analysis/AssumptionCache.cpp
+++ b/llvm/lib/Analysis/AssumptionCache.cpp
@@ -119,22 +119,60 @@ void AssumptionCache::removeAffectedValues(AssumeInst *CI) {
   SmallVector<AssumptionCache::ResultElem, 16> Affected;
   findAffectedValues(CI, TTI, Affected);
 
+  int ExpectedMatches = 0;
+  DenseMap<Value*, bool> VisitedAffected;
+
+  for (auto &AV : Affected) {
+    auto AVI = AffectedValues.find_as(AV.Assume);
+    if (AVI == AffectedValues.end())
+      continue;
+
+    if (VisitedAffected[AV.Assume])
+      continue;
+    VisitedAffected[AV.Assume] = true;
+
+    // If a value appears more than once in an AssumeInst e.g., 'ptr %arg1' in:
+    //     call void @llvm.assume(i1 true)
+    //                   [ "dereferenceable"(ptr %arg1, i64 1),
+    //                     "align"(ptr %arg1, i64 8) ]
+    // it will appear multiple times in Affected, but we may (depending on
+    // how the results in AffectedValues.find_as(AV.Assume) are ordered)
+    // nullify multiple instances of Elem.Assume during one iteration of the
+    // for loop below. The next iteration of the for loop may then find only a
+    // match to a different AssumeInst, resulting in an assertion failure.
+    for (ResultElem &Elem : AVI->second) {
+      if (Elem.Assume == CI)
+        ExpectedMatches++;
+    }
+  }
+
   for (auto &AV : Affected) {
     auto AVI = AffectedValues.find_as(AV.Assume);
     if (AVI == AffectedValues.end())
       continue;
+
     bool Found = false;
     bool HasNonnull = false;
     for (ResultElem &Elem : AVI->second) {
       if (Elem.Assume == CI) {
         Found = true;
         Elem.Assume = nullptr;
+
+        ExpectedMatches--;
+        assert(ExpectedMatches >= 0);
+        // After ExpectedMatches == 0, we still need to iterate through this
+        // loop to determine the value of HasNonnull, to prevent prematurely
+        // AffectedValues.erase(AVI).
       }
       HasNonnull |= !!Elem.Assume;
       if (HasNonnull && Found)
         break;
     }
-    assert(Found && "already unregistered or incorrect cache state");
+
+    if (ExpectedMatches > 0) {
+      assert(Found && "already unregistered or incorrect cache state");
+    }
+
     if (!HasNonnull)
       AffectedValues.erase(AVI);
   }
diff --git a/llvm/test/Transforms/DropUnnecessaryAssumes/dereferenceable.ll b/llvm/test/Transforms/DropUnnecessaryAssumes/dereferenceable.ll
index 079c0403227f3..39bee9c3dd4b6 100644
--- a/llvm/test/Transforms/DropUnnecessaryAssumes/dereferenceable.ll
+++ b/llvm/test/Transforms/DropUnnecessaryAssumes/dereferenceable.ll
@@ -53,6 +53,51 @@ define i8 @test_dereferenceable_with_align_ptr_used(ptr %p, i64 %size) {
   ret i8 %val
 }
 
+; Regression test for AssumptionCache::removeAffectedValues() crash revealed at
+; (but not caused by) def1355cf14cec28f71b8ca947b7723641c1580d
+;
+; Check that when the first AssumeInst has two op bundles that rely on %arg1,
+; removeAffectedValues() is not confused by the presence of an unrelated
+; AssumeInst that also has an %arg1 op bundle.
+define ptr @test_dup_ptr_used_elsewhere(i1 %arg, ptr %arg1) {
+; CHECK-LABEL: define ptr @test_dup_ptr_used_elsewhere(
+; CHECK-SAME: i1 [[ARG:%.*]], ptr [[ARG1:%.*]]) {
+; CHECK-NEXT:  [[BB:.*:]]
+; CHECK-NEXT:    br i1 [[ARG]], label %[[BB3:.*]], label %[[BB4:.*]]
+; CHECK:       [[BB2:.*]]:
+; CHECK-NEXT:    ret ptr null
+; CHECK:       [[BB3]]:
+; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "dereferenceable"(ptr [[ARG1]], i64 1) ]
+; CHECK-NEXT:    br label %[[BB2]]
+; CHECK:       [[BB4]]:
+; CHECK-NEXT:    br label %[[BB2]]
+;
+; DROP-DEREF-LABEL: define ptr @test_dup_ptr_used_elsewhere(
+; DROP-DEREF-SAME: i1 [[ARG:%.*]], ptr [[ARG1:%.*]]) {
+; DROP-DEREF-NEXT:  [[BB:.*:]]
+; DROP-DEREF-NEXT:    br i1 [[ARG]], label %[[BB3:.*]], label %[[BB4:.*]]
+; DROP-DEREF:       [[BB2:.*]]:
+; DROP-DEREF-NEXT:    ret ptr null
+; DROP-DEREF:       [[BB3]]:
+; DROP-DEREF-NEXT:    br label %[[BB2]]
+; DROP-DEREF:       [[BB4]]:
+; DROP-DEREF-NEXT:    br label %[[BB2]]
+;
+bbl:
+  br i1 %arg, label %bbl3, label %bbl4
+
+bbl2:
+  ret ptr null
+
+bbl3:
+  call void @llvm.assume(i1 true) [ "dereferenceable"(ptr %arg1, i64 1), "align"(ptr %arg1, i64 8) ]
+  br label %bbl2
+
+bbl4:
+  call void @llvm.assume(i1 true) [ "align"(ptr %arg1, i64 4) ]
+  br label %bbl2
+}
+
 ; Make sure newly created assumes are handled properly.
 define i8 @test_dereferenceable_with_align_cache_realloc(ptr %p, ptr %q, i1 %c) {
 ; CHECK-LABEL: define i8 @test_dereferenceable_with_align_cache_realloc(

``````````

</details>


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


More information about the llvm-commits mailing list