This is an archive of the discontinued LLVM Phabricator instance.

[LoopUnroll] Fix bug in computeUnrollCount causing it to not honor MaxCount
ClosedPublic

Authored by gberry on Jun 22 2017, 2:22 PM.

Event Timeline

gberry created this revision.Jun 22 2017, 2:22 PM
anna added inline comments.Jun 26 2017, 7:17 AM
lib/Transforms/Scalar/LoopUnrollPass.cpp
813

This check should actually be present along all paths from this function?

839

What happens if we early return from this function with result as true, but UP.Count > UP.MaxCount?

Perhaps we should be setting the max value at the caller of this function :

tryToUnrollLoop() {
....
 // computeUnrollCount() decides whether it is beneficial to use upper bound to fully 
 // unroll the loop.
  bool UseUpperBound = false;
  bool IsCountSetExplicitly =
      computeUnrollCount(L, TTI, DT, LI, SE, &ORE, TripCount, MaxTripCount,
                         TripMultiple, LoopSize, UP, UseUpperBound);
  if (!UP.Count)
    return false;
  // Unroll factor (Count) must be less or equal to TripCount.
  if (TripCount && UP.Count > TripCount)
    UP.Count = TripCount;
  // Add the check after this?

}
gberry added inline comments.Jun 26 2017, 10:40 AM
lib/Transforms/Scalar/LoopUnrollPass.cpp
839

I think this is the only place that MaxCount wasn't being honored where it should be. The earlier checks that return early are:

  • explicit -unroll-count settings and pragma unroll counts, which I don't think should be affected by MaxCount
  • full unroll count, which has a separate max: FullUnrollMaxCount which seems to be honored
  • loop peeling: sets Count to 1, so MaxCount should not have any effect
anna edited edge metadata.Jun 26 2017, 6:14 PM

LGTM.
Just clarifying: this just honours the user defined max count right (if user hasn't defined the value, it would be UINT_MAX).

anna accepted this revision.Jun 26 2017, 6:14 PM
This revision is now accepted and ready to land.Jun 26 2017, 6:14 PM
In D34532#791464, @anna wrote:

LGTM.
Just clarifying: this just honours the user defined max count right (if user hasn't defined the value, it would be UINT_MAX).

That, or the value set by the target hook getUnrollingPreferences(), which is how I originally ran into the bug, when implementing D34533

This revision was automatically updated to reflect the committed changes.