This is an archive of the discontinued LLVM Phabricator instance.

[SCEVExpander] Make CanonicalMode handing in isSafeToExpand() more robust (PR50506)
ClosedPublic

Authored by nikic on Jul 13 2022, 3:29 AM.

Details

Summary

isSafeToExpand() for addrecs depends on whether the SCEVExpander will be used in CanonicalMode. At least one caller currently gets this wrong, resulting in PR50506.

Fix this by a) making the CanonicalMode argument on the freestanding functions required and b) adding member functions on SCEVExpander that automatically take the SCEVExpander mode into account. We can use the latter variant nearly everywhere, and thus make sure that there is no chance of CanonicalMode mismatch.

Fixes https://github.com/llvm/llvm-project/issues/50506.

Diff Detail

Event Timeline

nikic created this revision.Jul 13 2022, 3:29 AM
nikic requested review of this revision.Jul 13 2022, 3:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 13 2022, 3:29 AM
fhahn added inline comments.Jul 13 2022, 12:06 PM
llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
41

It looks like only 2 uses of those versions remain. I think it would be good to replace them with the member variants, to reduce the chance of mis-use. Although the use in LSR might require a bit of extra work :(

llvm/lib/Transforms/Scalar/LoopPredication.cpp
526–527

Could we just pass the used Expander and then use Expander.isSafeToExpandAt here?

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
3339

It might be worth creating the SCEVExpander up-front and then use the member version?

reames accepted this revision.Jul 13 2022, 5:50 PM

LGTM to this patch as is. I agree with @fhahn's comments, but those are reasonable follow ups and don't need to be done in this patch. This is already is a significant improvement.

As a follow up thought, have you considered whether the member function variant of isSafeToExpand could be implemented in terms of isSafeToExpandAt with the internal insertion point? Not sure if that's a good idea or not...

This revision is now accepted and ready to land.Jul 13 2022, 5:50 PM
This revision was landed with ongoing or failed builds.Jul 14 2022, 5:42 AM
This revision was automatically updated to reflect the committed changes.