This is an archive of the discontinued LLVM Phabricator instance.

[LoopReroll] Fix rerolling loop with use outside the loop
ClosedPublic

Authored by kawashima-fj on May 6 2020, 9:59 PM.

Details

Summary

Fixes PR41696

The loop-reroll pass generates an invalid IR (or its assertion
fails in debug build) if values of the base instruction and
other root instructions (terms used in the loop-reroll pass)
are used outside the loop block. See IRs written in PR41696
as examples.

The current implementation of the loop-reroll pass can reroll
only loops that don't have values that are used outside the
loop, except reduced values (the last values of reduction chains).
This is described in the comment of the LoopReroll::reroll
function.
https://github.com/llvm/llvm-project/blob/llvmorg-10.0.0/llvm/lib/Transforms/Scalar/LoopRerollPass.cpp#L1600

This is checked in the LoopReroll::DAGRootTracker::validate
function.
https://github.com/llvm/llvm-project/blob/llvmorg-10.0.0/llvm/lib/Transforms/Scalar/LoopRerollPass.cpp#L1393

However, the base instruction and other root instructions skip
this check in the validation loop.
https://github.com/llvm/llvm-project/blob/llvmorg-10.0.0/llvm/lib/Transforms/Scalar/LoopRerollPass.cpp#L1229

Moving the check in front of the skip is the logically simplest
fix. However, inserting the check in an earlier stage is better
in terms of compilation time of unrerollable loops. This fix
inserts the check for the base instruction into the function
to validate possible base/root instructions. Check for other
root instructions is unnecessary because they don't match any
base instructions if they have uses outside the loop.

Diff Detail

Event Timeline

kawashima-fj created this revision.May 6 2020, 9:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2020, 9:59 PM

I think there are four loop-reroll issues currently open in Bugzilla:

https://bugs.llvm.org/show_bug.cgi?id=34029
https://bugs.llvm.org/show_bug.cgi?id=42267
https://bugs.llvm.org/show_bug.cgi?id=34760
https://bugs.llvm.org/show_bug.cgi?id=41696

Could you check which ones this patch fixes?

llvm/lib/Transforms/Scalar/LoopRerollPass.cpp
882

Probably worth putting the "Check for other root instructions is unnecessary because they don't match any base instructions if they have uses outside the loop." explanation into a comment here.

Comment is added.

Could you take another look at the testcase for 34760 in particular? That seems similar enough that it should get fixed at the same time.

kawashima-fj marked an inline comment as done.EditedMay 8 2020, 10:47 PM

Could you take another look at the testcase for 34760 in particular? That seems similar enough that it should get fixed at the same time.

My change does not fix PR34760.
I tried the original reporter's precedure. LLVM bofore this change and after this change both give the same incorrect value (8).
I also tried opt -S -loop-reroll for the bitcode attached in the Comment 2. LLVM bofore this change and after this change both give same IR.
I read the bitcode attached in the Comment 2. It is not the pattern which I fixed.

In a quick glance, the problem of PR34760 is in the root finding process. I'll put a comment to PR34760 later.

This revision is now accepted and ready to land.May 11 2020, 11:24 AM
This revision was automatically updated to reflect the committed changes.