Skip to content

Commit

Permalink
[SCEV] Invalidate user SCEVs along with operand SCEVs to avoid cache …
Browse files Browse the repository at this point in the history
…corruption

Following discussion in D110390, it seems that we are suffering from unability
to traverse users of a SCEV being invalidated. The result of that is that ScalarEvolution's
inner caches may store obsolete data about SCEVs even if their operands are
forgotten. It creates problems when we try to verify the contents of those caches.

It's also a frequent situation when messing with cache causes very sneaky and
hard-to-analyze bugs related to corruption of memory when dealing with cached
data. They are lurking there because ScalarEvolution's veirfication is not powerful
enough and misses many problematic cases. I plan to make SCEV's verification
much stricter in follow-ups, and this requires dangling-pointers-free caches.

This patch makes sure that, whenever we forget cached information for a SCEV,
we also forget it for all SCEVs that (transitively) use it.

This may have negative compile time impact. It's a sacrifice we are more
than willing to make to enforce correctness. We can also save some time by
reworking invokers of forgetMemoizedResults (maybe we can forget multiple
SCEVs with single query).

Differential Revision: https://reviews.llvm.org/D111533
Reviewed By: reames
  • Loading branch information
xortator committed Oct 28, 2021
1 parent 1387483 commit 513914e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12737,9 +12737,21 @@ bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const {
}

void ScalarEvolution::forgetMemoizedResults(ArrayRef<const SCEV *> SCEVs) {
for (auto *S : SCEVs)
forgetMemoizedResultsImpl(S);
SmallPtrSet<const SCEV *, 8> ToForget(SCEVs.begin(), SCEVs.end());
SmallVector<const SCEV *, 8> Worklist(ToForget.begin(), ToForget.end());

while (!Worklist.empty()) {
const SCEV *Curr = Worklist.pop_back_val();
auto Users = SCEVUsers.find(Curr);
if (Users != SCEVUsers.end())
for (auto *User : Users->second)
if (ToForget.insert(User).second)
Worklist.push_back(User);
}

for (auto *S : ToForget)
forgetMemoizedResultsImpl(S);

for (auto I = PredicatedSCEVRewrites.begin();
I != PredicatedSCEVRewrites.end();) {
std::pair<const SCEV *, const Loop *> Entry = I->first;
Expand Down

0 comments on commit 513914e

Please sign in to comment.