[PATCH] D109845: [SCEV] Correctly propagate nowrap flags across scopes when folding invariant add through addrec

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 15 12:26:06 PDT 2021


reames created this revision.
reames added reviewers: nikic, fhahn, mkazantsev, efriedma.
Herald added subscribers: javed.absar, bollu, hiraditya, nemanjai, mcrosier.
reames requested review of this revision.
Herald added a project: LLVM.

This fixes a violation of the wrap flag rules introduced in c4048d8f <https://reviews.llvm.org/rGc4048d8f50aaf2c4c13b8d3e138abc34a22da754>.  This is an alternate fix to D106852 <https://reviews.llvm.org/D106852>.

The basic problem being fixed is that we infer a set of flags which is valid at some inner scope S1 (usually by correctly propagating them from IR), and then (incorrectly) extend them to a SCEV in scope S2 where S1 != S2.  This is not in general safe per the wrap flags semantics recently defined.

In this patch, I include a simple inference step to handle the case where we can prove that S2 is the preheader of the loop S1, and that entry into S2 implies execution of S1.  See the code for a more detailed explanation.  I'd welcome input on how to reword the comment to make it easier to follow.  I believe the reasoning is sound, but its super hard to follow.

One worry I have with this patch is that I might be over-fitting what shows up in tests - and thus hiding negative impact we'd see in the real world.  My best defense is that the rule used here very closely follows the one used to propagate the flags from IR to the inner add to start with, and thus if one is reasonable, so probably is the other.  Curious what others think about that piece.

The test diffs are roughly as expected.  Mostly analysis only, with two transform changes.  Oddly, the result looks better in the loop-idiom test, and I don't understand the PPC output enough to have tell.  Nothing terrible looking though.  (For context, without the scope inference peephole, the test delta includes a couple of vectorization tests.  Again, not super concerning, but slightly more so.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109845

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/flags-from-poison.ll
  llvm/test/Analysis/ScalarEvolution/incorrect-exit-count.ll
  llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
  llvm/test/Analysis/ScalarEvolution/no-wrap-symbolic-becount.ll
  llvm/test/Analysis/ScalarEvolution/nsw-offset-assume.ll
  llvm/test/Analysis/ScalarEvolution/nsw-offset.ll
  llvm/test/Analysis/ScalarEvolution/nsw.ll
  llvm/test/Analysis/ScalarEvolution/ptrtoint.ll
  llvm/test/CodeGen/PowerPC/lsr-profitable-chain.ll
  llvm/test/Transforms/LoopIdiom/basic.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109845.372769.patch
Type: text/x-patch
Size: 33943 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210915/7c08da50/attachment.bin>


More information about the llvm-commits mailing list