[llvm] [LLVM][Transforms][Attributor] - Optimize AAIsDeadFunction::isAssumedDead (PR #189467)

Pranav Bhandarkar via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 13:07:40 PDT 2026


================
@@ -4885,6 +4931,11 @@ ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
     ToBeExploredFrom = std::move(NewToBeExploredFrom);
   }
 
+  // If the state changed (KnownDeadEnds or ToBeExploredFrom), the cache is
+  // invalid.
+  if (Change == ChangeStatus::CHANGED)
+    FirstDeadInstCache.clear();
----------------
bhandarkar-pranav wrote:

I need to think about this some more or can you elaborate on how you think we can avoid clearing everything here especially if we move to eager population of barrier inst cache in `updateImpl` (vs lazy population of barrier inst cache in `isAssumedDead`). 

With eager population, the cache would be updated at every `KnownDeadEnds.insert(I)` and `NewToBeExploredFrom.insert(I)`. This is good — it eliminates the forward BB scan from  `isAssumedDead`. But it also means the cache is more fully populated, which makes the `ToBeExploredFrom` swap more dangerous.
  Consider:

```
  BB:
    %x = add ...
    call @foo()       ;; call1
    %y = mul ...
    call @bar()       ;; call2
    br label %next
```
Suppose in iteration N, both call1 and call2 end up in `ToBeExploredFrom` (their `noreturn` status hasn't converged). With eager population, the cache maps `BB` → `call1`.

In iteration N+1, the AA for `@foo` converges — it's known to return. So `call1` has alive successors and goes into neither `NewToBeExploredFrom` nor `KnownDeadEnds`. `call2` still uses assumed info and goes into `NewToBeExploredFrom`. After the swap, `call1` is no longer a barrier, but the cache still points to it. If `isAssumedDead` is queried for `%y = mul..`, it would see the cached `call1` comes before `%y` and incorrectly declare `%y` dead — a miscompile.
The bulk `clear()` after the swap prevents this. Eliminating it would require computing the set difference (old `ToBeExploredFrom` minus new), checking each removed instruction against the
cache, and rescanning affected blocks. That's doable but adds complexity for unclear benefit. 

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


More information about the llvm-commits mailing list