This is an archive of the discontinued LLVM Phabricator instance.

[SCEVExpander] Stop hoisting IR when reusing phis
ClosedPublic

Authored by reames on Jul 16 2021, 12:24 PM.

Details

Summary

This is a fix for PR43678, and is an alternate patch to D105723.

The basic issue we're running into is that LSR + SCEVExpander are moving the very instruction whose operand we're in the process of expanding. This breaks the subtle and ill-documented invariant which let LSR work. (Full story can be found here: https://reviews.llvm.org/D105723#2878473)

Rather than attempting a fix, this change just removes the optimization entirely. The code is entirely untested, and removing it appears to have no impact I can find.

From a philosophical standpoint, it also seems very strange to have the expander implementing optimizations which should live in a dedicated transform pass.

This code was added back in 2014 by 1e12f8563d4b7 with a single test which does not seem to actually test the hoisting logic.

Diff Detail

Event Timeline

reames created this revision.Jul 16 2021, 12:24 PM
reames requested review of this revision.Jul 16 2021, 12:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2021, 12:24 PM

I have no objection. AFAIK SCEVExpander is generally just trying to avoid introducing new phis that didn't exist in the original loop. I was never a fan of the whole SCEVExpander approach.

But it seems pretty clear why this was done:

commit 26f567d8a4ea440c6182c793d6946aad5587cfe7
Date: Sun Feb 16 15:49:50 2014 +0000

SCEVExpander: Try hard not to create derived induction variables in other loops

During LSR of one loop we can run into a situation where we have to expand the
start of a recurrence of a loop induction variable in this loop. This start
value is a value derived of the induction variable of a preceeding loop. SCEV
has cannonicalized this value to a different recurrence than the recurrence of
the preceeding loop's induction variable (the type and/or step direction) has
changed). When we come to instantiate this SCEV we created a second induction
variable in this preceeding loop.  This patch tries to base such derived
induction variables of the preceeding loop's induction variable.

This helps twolf on arm and seems to help scimark2 on x86.

Reapply with a fix for the case of a value derived from a pointer.

radar://15970709
qcolombet accepted this revision.Jul 28 2021, 11:07 AM

Following the conversation in https://reviews.llvm.org/D105723#2894209, approving this patch.

This revision is now accepted and ready to land.Jul 28 2021, 11:07 AM
mkazantsev accepted this revision.Aug 1 2021, 9:49 PM
This revision was landed with ongoing or failed builds.Aug 17 2021, 9:38 AM
This revision was automatically updated to reflect the committed changes.