This is an archive of the discontinued LLVM Phabricator instance.

[IndVars] Use symbolic max block exit count to handle the last iter
ClosedPublic

Authored by mkazantsev on Dec 8 2022, 10:55 PM.

Details

Summary

Old logic: when loop symbolic max exit count matched *exact* block exit count,
assume that all subsequent blocks will do 1 iteration less.

New logic: when loop symbolic max exit count matched *symbolic max* block exit count,
assume that all subsequent blocks will do 1 iteration less.

The new logic is still legal and is more permissive in situations when exact
block exit count is not known.

Diff Detail

Event Timeline

mkazantsev created this revision.Dec 8 2022, 10:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 10:55 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
mkazantsev requested review of this revision.Dec 8 2022, 10:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 10:55 PM
mkazantsev retitled this revision from [IndVars] Use symbolic block exit count to handle the last iter to [IndVars] Use symbolic max block exit count to handle the last iter.
nikic added a comment.Dec 8 2022, 11:34 PM

The change looks fine, but I think the naming here is now a mess. We have ExitCount (the exact exit count of this exit), SymbolicMaxExitCount (the max exit count of this exit) and MaxExitCount (the max backedge taken count). I'd suggest to rename these variables as follows:

MaxExitCount -> MaxBECount
ExitCount -> ExactExitCount
SymbolicMaxExitCount -> MaxExitCount

Or keep including Symbolic if you think it adds value. (Preferably precommit the rename.)

mkazantsev updated this revision to Diff 481574.Dec 9 2022, 1:54 AM

Rebased/renamed.

nikic accepted this revision.Dec 9 2022, 1:58 AM

LG

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
1705

As a followup, we should be able to use MaxExitCount here as well.

This revision is now accepted and ready to land.Dec 9 2022, 1:58 AM
mkazantsev added inline comments.Dec 9 2022, 2:04 AM
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
1705

Good catch, need to find a test on it.

This revision was landed with ongoing or failed builds.Dec 9 2022, 2:06 AM
This revision was automatically updated to reflect the committed changes.
mkazantsev added inline comments.Dec 9 2022, 2:19 AM
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
1705

Thinking more of it, I don't think it's right. Imagine case:
exit 1: exact (unknown) is 2, symbolic max is x
exit 2: exact (unknown) is 0, symbolic max is x + 1<nuw>

Loop's symbolic max is x.

In this case we cannot eliminate the 2nd exit basing on fact that its maximum is more than some other maximum.

Unilke this, in my patch exit (in given block) earlier than proven max doesn't matter; logic with last iteration is only in the game when loop actually exits on max possible proven iteration (and therefore all subsequent exits don't execute).