This is an archive of the discontinued LLVM Phabricator instance.

[LSR] Fold terminating condition not only for eq and ne
ClosedPublic

Authored by MarkGoncharovAl on Mar 13 2023, 5:58 AM.

Details

Reviewers
eopXD
reames
nikic
Summary

Add opportunity to fold any icmp instruction.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 5:58 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
MarkGoncharovAl requested review of this revision.Mar 13 2023, 5:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 5:58 AM
MarkGoncharovAl retitled this revision from LSR]: Fold terminating condition not only for eq and ne. to [LSR]: Fold terminating condition not only for eq and ne..Mar 13 2023, 6:03 AM

Thank you for looking into this! Some comments and I think we are good to go.

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

I think this is change un-related to the topic of this patch.

6307

Ditto.

6711

Please add test coverage for this, thank you.

6721

Ditto, test coverage :)

I think the patch description should also include the added poison check. (and also remove the extra semicolon)

CC-ing @reames, since he is the author of the FIXME.

MarkGoncharovAl edited the summary of this revision. (Show Details)

@eopXD , please, review this change.

File formatted correctly, tests for icmp and poison value have been added

MarkGoncharovAl marked 4 inline comments as done.Mar 14 2023, 1:56 AM
eopXD accepted this revision.Mar 14 2023, 1:56 AM

LGTM, please wait for another day so others can also check the patch.

This revision is now accepted and ready to land.Mar 14 2023, 1:56 AM
eopXD retitled this revision from [LSR]: Fold terminating condition not only for eq and ne. to [LSR] Fold terminating condition not only for eq and ne.Mar 14 2023, 1:57 AM
reames added inline comments.Mar 14 2023, 1:42 PM
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6935

This change *does not* address this FIXME. Please restore the comment.

No comment on the review otherwise.

nikic requested changes to this revision.Mar 14 2023, 2:02 PM
nikic added a subscriber: nikic.
nikic added inline comments.
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6729

Drop this code, it doesn't make any sense.

6939

This is very likely incorrect -- the predicate is decided by which of the branch targets is loop exiting. Please add test variants where the predicate and the branch targets are inverted.

This revision now requires changes to proceed.Mar 14 2023, 2:02 PM
MarkGoncharovAl updated this revision to Diff 505390.EditedMar 15 2023, 1:22 AM

Thank you for review!

  1. I disabled checking for poison value.

*dyn_cast to poison* / use *isGuaranteedNotToBeUndefOrPoison* / use *containsUndefOrPoisonElement* are not correct way to check.

Please, offer your solution how to process it - so, I will not add it to this patch.

  1. @nikic , thank you for finding my mistake.

I added 4 tests. The solution is to process using one scheme:

%cmp = icmp eq ptr %iv, %iv_end
br i1 %cmp, label %for.end, label %for.body

We create *iv_end* using SE.getBackedgeTakenCount, thus when we get *iv* equals to *iv_end* then we have to jump to if.end. Finally, we have to be confident that the second successors is equal to LoopLatch.

MarkGoncharovAl marked 3 inline comments as done.Mar 15 2023, 1:23 AM
nikic requested changes to this revision.Mar 15 2023, 1:27 AM
nikic added inline comments.
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
6941

I don't think comparing with loop latch is the right condition here: You need to check for the loop header. In test current test cases these just happen to be the same.

Can you add a test where the loop has more than one block? Probably even just inserting a dummy unconditional br to split the block would be enough.

llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold.ll
1

Can you please rerun this script? The check line formatting looks a bit off.

This revision now requires changes to proceed.Mar 15 2023, 1:27 AM
  1. Fix checking branch swap successors.
  2. Tests are formatted accroding to clang-format and passed.
MarkGoncharovAl marked 2 inline comments as done.Mar 15 2023, 2:35 AM
nikic added a comment.Mar 15 2023, 5:00 AM
  1. Tests are formatted accroding to clang-format and passed.

For tests, the relevant script is llvm/utils/update_test_checks.py. You can run it as llvm/utils/update_test_checks.py --opt-bin build/bin/opt llvm/test/Transforms/LoopStrengthReduce/lsr-term-fold.ll.

Update tests using update_test_checks.py

nikic accepted this revision.Mar 15 2023, 5:07 AM

LGTM, thanks!

This revision is now accepted and ready to land.Mar 15 2023, 5:07 AM
fhahn added a subscriber: fhahn.Mar 15 2023, 10:40 AM

Please make sure to update the commit message; it looks like the latest version doesn't address the poison issue IIUC

MarkGoncharovAl edited the summary of this revision. (Show Details)Mar 20 2023, 12:39 AM

Last week, someone rewrote lit-tests style.

I use ./llvm/utils/update_test_checks.py to make correct style.

After passing build, will push changes.