This is an archive of the discontinued LLVM Phabricator instance.

[MemDep] Don't use cached results from a previous larger query
ClosedPublic

Authored by sunfish on Dec 4 2017, 1:07 PM.

Details

Summary

When a memdep query finds a cached result from a previous query that had a larger size, don't reuse the cached results, because aliasing queries with a greater size aren't always more conservative. For example, BasicAA knows that accesses larger than the size of underlying objects don't alias those objects.

This could increase compile time in the case of many queries to the same pointer with successively smaller sizes, though this is uncommon.

Fixes PR35519.

Diff Detail

Repository
rL LLVM

Event Timeline

sunfish created this revision.Dec 4 2017, 1:07 PM
sanjoy requested changes to this revision.Dec 5 2017, 1:38 PM
sanjoy added inline comments.
lib/Analysis/MemoryDependenceAnalysis.cpp
1129 ↗(On Diff #125405)

You can still use the result if CacheInfo->Size is UnknownSize, right?

And, secondly, any reason to not add the size to the cache key as well?

This revision now requires changes to proceed.Dec 5 2017, 1:38 PM
sunfish updated this revision to Diff 125731.Dec 6 2017, 8:03 AM
sunfish edited edge metadata.

This adds a fix for the testcase reported in PR35519; see the change in MemCpyOptimizer.cpp. The code for merging stores into a memset wasn't properly updating memdep's query cache.

This isn't ready for final review yet:

  • I plan to add a reduced testcase. (It's complex to write as the bug requires a specific sequence of events -- one optimization happens in a way that leaves the cache in an incorrect state, then a second optimization relies on the cache).
  • I plan to address @sanjoy's review feedback.
sunfish updated this revision to Diff 125780.Dec 6 2017, 12:21 PM
  • Fixes debug info updating.
  • Adds a proper testcase for the memset merging caching bug.
sunfish marked an inline comment as done.Dec 6 2017, 12:30 PM
sunfish added inline comments.
lib/Analysis/MemoryDependenceAnalysis.cpp
1129 ↗(On Diff #125405)

Yes, I think reusing the result from an UnknownSize query would be conservatively correct, though it would be less precise. Also, yes, we could add the size, and the AATags too for that matter, to the key, however it would make the DenseMap use more memory.

My expectation, which seems supported by some ad-hoc experiments, is that repeated queries with the same pointer and differing sizes aren't very common. Assuming this is true in general, it seems best to avoid pessimizing common cases to optimize it.

sunfish marked an inline comment as done.Dec 7 2017, 5:58 AM

The issue reported in PR35519 is now fixed, there's a testcase now, and I responded to @sanjoy's comment above.

hfinkel accepted this revision.Dec 14 2017, 3:26 PM
hfinkel added a subscriber: hfinkel.

LGTM

This is a pretty awful implicit contract. Hopefully, we'll update this pass to use MemorySSA soon.

lib/Transforms/Scalar/MemCpyOptimizer.cpp
495 ↗(On Diff #125780)

Please include the fact that you're talking about MemDep here, such as:

... on the removed instructions in MemoryDependenceAnalysis, the...

This revision was not accepted when it landed; it landed in state Needs Review.Dec 19 2017, 5:37 PM
This revision was automatically updated to reflect the committed changes.
sunfish marked an inline comment as done.Dec 19 2017, 5:38 PM
sunfish added inline comments.
lib/Transforms/Scalar/MemCpyOptimizer.cpp
495 ↗(On Diff #125780)

Done.