llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

SSAUpdater corrupts SCEV

Open DaniilSuchkov opened this issue 6 years ago • 6 comments

Bugzilla Link 44320
Resolution FIXED
Resolved on Jan 23, 2020 01:14
Version trunk
OS Linux
Attachments CFG after loop unswitch
CC @alinas,@efriedma-quic,@Theodor,@fhahn,@preames

Extended Description

Consider following IR: define void @​foo(i32* %arg, i32* %arg1, i1 %cond1, i1 %cond2, i1 %cond3, i1 %cond4, i1 %cond5) { bb: br label %bb3

bb2: ; preds = %bb20 br label %bb3

bb3: ; preds = %bb2, %bb br i1 %cond1, label %bb4, label %bb24

bb4: ; preds = %bb3 %tmp = load i32, i32* %arg %tmp5 = load i32, i32* %arg1 %tmp6 = add i32 %tmp5, %tmp %tmp7 = icmp sgt i32 %tmp6, %tmp br i1 %tmp7, label %bb8, label %bb17

bb8: ; preds = %bb4 %tmp9 = and i1 %cond2, %cond3 br label %bb10

bb10: ; preds = %bb15, %bb8 %tmp11 = phi i32 [ %tmp12, %bb15 ], [ %tmp, %bb8 ] %tmp12 = add nsw i32 %tmp11, 1 br i1 %tmp9, label %bb15, label %bb13

bb13: ; preds = %bb10 %tmp14 = phi i32 [ %tmp12, %bb10 ] ret void

bb15: ; preds = %bb10 br i1 %cond4, label %bb10, label %bb16

bb16: ; preds = %bb15 br label %bb17

bb17: ; preds = %bb16, %bb4 %tmp18 = phi i32 [ 0, %bb4 ], [ %tmp5, %bb16 ] br i1 %cond5, label %bb25, label %bb19

bb19: ; preds = %bb17 br label %bb20

bb20: ; preds = %bb20, %bb19 %tmp21 = phi i32 [ %tmp22, %bb20 ], [ 0, %bb19 ] %tmp22 = add nuw nsw i32 %tmp21, 1 %tmp23 = icmp slt i32 %tmp22, %tmp18 br i1 %tmp23, label %bb20, label %bb2

bb24: ; preds = %bb3 ret void

bb25: ; preds = %bb17 ret void }

Following command: opt -passes="verify,loop(unswitch),verify" -S -o /dev/null -verify-scev-strict input.ll

completes with following error message: Trip Count for Loop at depth 2 containing: %bb20

Changed! Old: (-1 + (1 smax ((-1 * %.us-phi) + ((%tmp5 + %.us-phi) smax %.us-phi)))) New: (-1 + (1 smax ((-1 * %tmp) + ((%tmp + %tmp5) smax %tmp)))) Delta: ((-1 * (1 smax ((-1 * %tmp) + ((%tmp + %tmp5) smax %tmp)))) + (1 smax ((-1 * %.us-phi) + ((%tmp5 + %.us-phi) smax %.us-phi))))

(comment: %.us-phi is an alias of %tmp that doesn't dominate %bb20, %bb20 isn't even reachable from the point where %.us-phi is defined; if that's unclear see attached .svg CFG of the final IR)

Here's why: To preserve LCSSA SimpleLoopUnswitch calls formLCSSA, which in some cases uses SSAUpdater::RewriteUse. Even though SSAUpdater::RewriteUse rewrites only one use, it calls ValueHandleBase::ValueIsRAUWd which in turn calls CallbackVH::allUsesReplacedWith. SCEVUnknown::allUsesReplacedWith acts the way that is correct if and only if all uses of the value are replaced (and given the name of the method it makes complete sense), so it looks like ValueHandleBase::ValueIsRAUWd shouldn't be called by SSAUpdater::RewriteUse at all. Actually if I just remove the call to ValueHandleBase::ValueIsRAUWd the bug goes away and make check completes successfully. Here's the catch: that call was added by the following commit: commit 8d804520766b6772277fcc1d9b31da02f320bc12 Author: Nadav Rotem [email protected] Date: Mon Aug 13 23:06:54 2012 +0000

    LICM uses AliasSet information to hoist and sink instructions. However, other passes, such as LoopRotate
    may invalidate its AliasSet because SSAUpdater does not update the AliasSet properly.
    This patch teaches SSAUpdater to notify AliasSet that it made changes.
    The testcase in llvm/llvm-project#13273  is too big to be useful and I could not reduce it to a normal size.
    
    rdar://11872059 llvm/llvm-project#13273 
    
    llvm-svn: 161803

I.e. AliasSetTracker relies on SSAUpdater to (incorrectly) call ValueHandleBase::ValueIsRAUWd for every new instruction it adds, so we can't simply remove that call since it will (probably) cause new bugs.

One way to fix it is to remove that call and invalidate AliasSet everywhere where SSAUpdater is used, either directly or indirectly, but it goes against what many efforts were aimed for (compile time improvements).

Another way is to introduce a new method, say CallbackVH::UseIsReplacedWith, and its counterpart ValueHandleBase::UsesOfValueReplaced and call it in such cases instead of ValueHandleBase::ValueIsRAUWd. This option looks OK to me but the scope of the work leaves me wonder if there is an easier solution.

I'd like to hear others opinions on how this issue can be resolved.

DaniilSuchkov avatar Dec 17 '19 12:12 DaniilSuchkov

Fixed by: commit 4a8dbc617d0b28ce2ce3e6856376b3802409554e Author: Daniil Suchkov [email protected] Date: Tue Jan 21 12:21:53 2020 +0700

[SSAUpdater] Don't call ValueIsRAUWd upon single use replacement

It is incorrect to call ValueHandleBase::ValueIsRAUWd when only one use is replaced since it simply violates semantics of the callback and leads to bugs like llvm/llvm-project#43665 .

Previously this call was used specifically to keep LICM's cache of AliasSetTrackers up to date across passes (as llvm/llvm-project#36149 showed, even for that purpose it didn't work properly), but since LICM doesn't have that cache anymore, we can safely remove this incorrect call with no repercussions.

This patch fixes llvm/llvm-project#43665

Reviewers: asbirlea, fhahn, efriedma, reames

Reviewed-By: asbirlea

Differential Revision: https://reviews.llvm.org/D73089

DaniilSuchkov avatar Jan 23 '20 09:01 DaniilSuchkov

I'm sorry, I only saw this now.

We already switched LICM to use MemorySSA, and only build an AliasSetTracker for the promoteToScalars step, and do not rely on cached AliasSetTrackers.

I strongly suggest removing AliasSetTracker caching altogether. There is at least another bug that needs this caching removed, and there is no longer a compile-time reason to kept it. (see explanation regarding SSAUpdater::RewriteUse in https://reviews.llvm.org/D58560)

Thanks a lot for your comment! I will remove AliasSetTracker caching and then get rid of ValueIsRAUWd call from SSAUpdater::RewriteUse, since that call is clearly completely incorrect for all purposes (not just for SCEV as I thought before reading your comment from D58560).

DaniilSuchkov avatar Jan 21 '20 05:01 DaniilSuchkov

I'm sorry, I only saw this now.

We already switched LICM to use MemorySSA, and only build an AliasSetTracker for the promoteToScalars step, and do not rely on cached AliasSetTrackers.

I strongly suggest removing AliasSetTracker caching altogether. There is at least another bug that needs this caching removed, and there is no longer a compile-time reason to kept it. (see explanation regarding SSAUpdater::RewriteUse in https://reviews.llvm.org/D58560)

alinas avatar Jan 17 '20 18:01 alinas

An attempt to fix is up for review: https://reviews.llvm.org/D72905

DaniilSuchkov avatar Jan 17 '20 09:01 DaniilSuchkov

I think you've basically covered the possibilities... except for one more possibility: we could require that passes explicitly preserve AliasSetTracker across changes. This probably wasn't feasible before, but by default, LICM doesn't hold onto an AliasSetTracker across passes anymore (and I don't think any other pass ever did).

efriedma-quic avatar Dec 17 '19 18:12 efriedma-quic

assigned to @DaniilSuchkov

DaniilSuchkov avatar Dec 17 '19 12:12 DaniilSuchkov