This is an archive of the discontinued LLVM Phabricator instance.

[LSR] Filter out zero factors. PR50765
ClosedPublic

Authored by mkazantsev on Jun 22 2021, 4:39 AM.

Details

Summary

Zero factor leads to division by zero and failure of corresponding
assert as shown in PR50765. We should filter out such factors.

Diff Detail

Event Timeline

mkazantsev created this revision.Jun 22 2021, 4:39 AM
mkazantsev requested review of this revision.Jun 22 2021, 4:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 22 2021, 4:39 AM

Not really familiar with this code, but sounds reasonable.

huihuiz accepted this revision.Jun 22 2021, 10:10 AM

Ideally we should reject early when stride is zero, but in this case we won't be able to detect this early since stride zero is in a form of add expression.

(((3 + (-1 * (74 smax (1 + %tmp3)))<nsw>)<nuw><nsw> * {(-2 + (-2 * (trunc i64 undef to i32))),+,-2}<%bb1>) + ((6 + (-2 * (74 smax (1 + %tmp3))))<nuw> * {(1 + (trunc i64 undef to i32)),+,1}<%bb1>))  // Zero stride as the LHS

After getExactSDiv pull out common factor 2 , then we get an add expression that turn out to be zero.

This revision is now accepted and ready to land.Jun 22 2021, 10:10 AM
reames added a subscriber: reames.Jun 22 2021, 4:01 PM

LGTM to me as well.

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

Not a problem with this patch, but dang this code is suspicious. It appears to be wanting to check whether one factor divides another, but a) it's *asserting* the divide is exact, and b) it's discarding any remainder. That probably wasn't what was actually wanted here...

Ideally we should reject early when stride is zero, but in this case we won't be able to detect this early since stride zero is in a form of add expression.

(((3 + (-1 * (74 smax (1 + %tmp3)))<nsw>)<nuw><nsw> * {(-2 + (-2 * (trunc i64 undef to i32))),+,-2}<%bb1>) + ((6 + (-2 * (74 smax (1 + %tmp3))))<nuw> * {(1 + (trunc i64 undef to i32)),+,1}<%bb1>))  // Zero stride as the LHS

After getExactSDiv pull out common factor 2 , then we get an add expression that turn out to be zero.

That's a point where we could try to improve SCEV. But generally, it's not correct to assume that SCEV will or will not fully simplify something. It has a lot of limitations, one of which is depth threshold which simply cuts the simplification process for sake of saving CT.

This revision was automatically updated to reflect the committed changes.