[PATCH] D149251: [SCEV] Don't expect BackedgeTakenCounts cache to contain an entry for loop in getBackedgeTakenInfo

Dmitry Makogon via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 26 04:48:25 PDT 2023


dmakogon created this revision.
dmakogon added reviewers: mkazantsev, skatkov, nikic, fhahn.
Herald added subscribers: StephenFan, javed.absar, hiraditya.
Herald added a project: All.
dmakogon requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This fixes assertion crash in https://github.com/llvm/llvm-project/issues/62380.

In the beginning of `ScalarEvolution::getBackedgeTakenInfo` we make sure that `BackedgeTakenCounts` contains an entry for the given loop.
Then we call `computeBackedgeTakenCount` which computes the result, and in the end we insert it in the map like so:

  return BackedgeTakenCounts.find(L)->second = std::move(Result);

So we expect that the entry for L still exists in the cache.
However, it can get deleted. When it has computed the result, `getBackedgeTakenInfo` clears all the cached SCEVs that use the AddRecs in the loop.
In the crashing example, `getBackedgeTakenInfo` gets called on parent loop during the initial call from user code, and this recursion happens after the call to `computeBackedgeTakenCount`. So when we compute BTI for a loop, we happen to call `getBackedgeTakenInfo` for its parent loop. And it happens so that some SCEV from the BTI of the child loop uses an AddRec of the parent loop. So when we successfully compute BTI for the parent loop, we erase already computed result for the child one.

This patch gets rid of this assumption. Now we would insert a new entry in the cache in case the old one got erased. This also fixes the way we update another cache associated with BackedgeTakenInfo - `BECountUsers`. Previously, it got updated in the end of `computeBackedgeTakenCount`. Due to the fact that we clear cached SCEVs after the call to that function, we must also update the `BECountUsers` cache after that call.


https://reviews.llvm.org/D149251

Files:
  llvm/include/llvm/Analysis/ScalarEvolution.h
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/pr62380.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149251.517123.patch
Type: text/x-patch
Size: 7385 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230426/97fedb03/attachment.bin>


More information about the llvm-commits mailing list