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

LCSSAPass corrupts SCEV

Open DaniilSuchkov opened this issue 7 years ago • 3 comments

Bugzilla Link 44058
Resolution FIXED
Resolved on Dec 08, 2019 21:12
Version trunk
OS Linux
CC @fhahn,@preames

Extended Description

Consider following IR: define void @​foo(i32* %arg, i32* %arg1, i1 %arg2) { bb: br label %bb3

bb3: ; preds = %bb13, %bb %tmp = load i32, i32* %arg %tmp4 = load i32, i32* %arg1 %tmp5 = add i32 %tmp4, %tmp %tmp6 = icmp sgt i32 %tmp5, %tmp br i1 %tmp6, label %bb7, label %bb11

bb7: ; preds = %bb3 br i1 %arg2, label %bb10, label %bb8

bb8: ; preds = %bb7 %tmp9 = add nsw i32 %tmp, 1 ret void

bb10: ; preds = %bb7 br label %bb11

bb11: ; preds = %bb10, %bb3 %tmp12 = phi i32 [ 0, %bb3 ], [ %tmp4, %bb10 ] br label %bb13

bb13: ; preds = %bb13, %bb11 %tmp14 = phi i32 [ %tmp15, %bb13 ], [ 0, %bb11 ] %tmp15 = add nuw nsw i32 %tmp14, 1 %tmp16 = icmp slt i32 %tmp15, %tmp12 br i1 %tmp16, label %bb13, label %bb3 }

SCEV of trip count for bb13 loop is (-1 + (1 smax ((-1 * %tmp) + ((%tmp + %tmp4) smax %tmp))))<nsw> LCSSA inserts following phi into bb8: %tmp.lcssa = phi i32 [ %tmp, %bb7 ] When the phi is inserted, LCSSA calls ValueHandleBase::ValueIsRAUWd(%tmp, %tmp.lcssa). Since %tmp is SCEVUnknown, SCEVUnknown::allUsesReplacedWith(%tmp.lcssa) is called. After that point SCEV of trip count for bb13 loop is (-1 + (1 smax ((-1 * %tmp.lcssa) + ((%tmp4 + %tmp.lcssa) smax %tmp.lcssa))))<nsw> Please note that %bb13 isn't reachable from %bb8, so this SCEV is incorrect.

With this patch applied: https://reviews.llvm.org/D70423 Following command: opt -passes="verify,lcssa,verify" -verify-scev-strict -S repro.ll fails with following message: Trip Count for Loop at depth 2 containing: %bb13

Changed! Old: (-1 + (1 smax ((-1 * %tmp.lcssa) + ((%tmp4 + %tmp.lcssa) smax %tmp.lcssa)))) New: (-1 + (1 smax ((-1 * %tmp) + ((%tmp + %tmp4) smax %tmp)))) Delta: ((-1 * (1 smax ((-1 * %tmp) + ((%tmp + %tmp4) smax %tmp)))) + (1 smax ((-1 * %tmp.lcssa) + ((%tmp4 + %tmp.lcssa) smax %tmp.lcssa))))

It is possible to reproduce the problem even without that patch, but it is less convenient. With this command: opt -passes="print,lcssa,print" -S repro.ll

The same problem is visible, but it requires manual checking (or parsing) of the output.

Important note: both commands contain an extra pass before LCSSA (verify or print). In both cases they are required to query SCEV so that when LCSSA runs it has some cached values to corrupt.

In my opinion behavior of SCEVUnknown::allUsesReplacedWith is reasonable for the case when all uses of the value are replaced, so the problem is that LCSSA calls ValueHandleBase::ValueIsRAUWd when only some uses of the value are replaced (hence the solution is to call something like SE->forgetValue instead). But I'd like to hear others' opinions on this matter.

DaniilSuchkov avatar Nov 19 '19 11:11 DaniilSuchkov

Fixed by https://reviews.llvm.org/rGc4d8c6319f576a7540017168db2f0440691914f4

DaniilSuchkov avatar Dec 09 '19 05:12 DaniilSuchkov

Here's an attempt to fix it: https://reviews.llvm.org/D70593

DaniilSuchkov avatar Nov 22 '19 11:11 DaniilSuchkov

assigned to @DaniilSuchkov

DaniilSuchkov avatar Nov 19 '19 11:11 DaniilSuchkov