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

Loop vectorizer introduces bad "undef"

Open mveriksson opened this issue 11 years ago • 14 comments

Bugzilla Link 24738
Resolution FIXED
Resolved on May 13, 2017 08:00
Version trunk
OS Linux
Attachments Reduced reproducer
CC @filcab,@hfinkel,@RKSimon,@sanjoy,@zhendongsu

Extended Description

I think this is a bug in the fix to Bug 14725.

With the attached file if we run: opt -S -licm -loop-vectorize -O1 -o /dev/null bugpoint-reduced-simplified.ll -print-after-all -print-before-all

The loop vectorizer does this transformation:

*** IR Dump Before Loop Vectorization *** define void @​TEST__MAIN() { %_tmp52 = call i16 @​foo(i16 0) %_tmp53 = load i16*, i16** undef br label %bb1

bb1: ; preds = %bb1, %0 %i.22.0 = phi i16 [ undef, %0 ], [ %_tmp55, %bb1 ] %_tmp55 = add i16 %i.22.0, 1 %_tmp59 = icmp slt i16 %_tmp55, 40 br i1 %_tmp59, label %bb1, label %bb4.preheader

bb4.preheader: ; preds = %bb1 %_tmp52.lcssa = phi i16 [ %_tmp52, %bb1 ] store i16 %_tmp52, i16* %_tmp53 %_tmp67 = load i16*, i16** undef br label %bb4

bb4: ; preds = %bb4.preheader, %bb4 br i1 false, label %bb4, label %bb6

bb6: ; preds = %bb4 store i16 %_tmp52.lcssa, i16* %_tmp67 ret void } *** IR Dump After Loop Vectorization *** define void @​TEST__MAIN() { %_tmp52 = call i16 @​foo(i16 0) %_tmp53 = load i16*, i16** undef %min.iters.check = icmp ult i32 1, 4 br i1 %min.iters.check, label %scalar.ph, label %min.iters.checked

min.iters.checked: ; preds = %0 br i1 true, label %middle.block, label %vector.ph

vector.ph: ; preds = %min.iters.checked br label %vector.body

vector.body: ; preds = %vector.body, %vector.ph %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ] %1 = trunc i32 %index to i16 %broadcast.splatinsert = insertelement <4 x i16> undef, i16 %1, i32 0 %broadcast.splat = shufflevector <4 x i16> %broadcast.splatinsert, <4 x i16> undef, <4 x i32> zeroinitializer %induction = add <4 x i16> %broadcast.splat, <i16 0, i16 1, i16 2, i16 3> %2 = add <4 x i16> %induction, <i16 1, i16 1, i16 1, i16 1> %3 = icmp slt <4 x i16> %2, <i16 40, i16 40, i16 40, i16 40> %index.next = add i32 %index, 4 %4 = icmp eq i32 %index.next, 0 br i1 %4, label %middle.block, label %vector.body, !llvm.loop !​0

middle.block: ; preds = %vector.body, %min.iters.checked %resume.val = phi i32 [ 0, %min.iters.checked ], [ 0, %vector.body ] %trunc.resume.val = phi i16 [ undef, %min.iters.checked ], [ 0, %vector.body ] %cmp.n = icmp eq i32 1, %resume.val br i1 %cmp.n, label %bb4.preheader, label %scalar.ph

scalar.ph: ; preds = %middle.block, %0 %bc.resume.val = phi i32 [ %resume.val, %middle.block ], [ 0, %0 ] %bc.trunc.resume.val = phi i16 [ %trunc.resume.val, %middle.block ], [ undef, %0 ] br label %bb1

bb1: ; preds = %bb1, %scalar.ph %i.22.0 = phi i16 [ %bc.trunc.resume.val, %scalar.ph ], [ %_tmp55, %bb1 ] %_tmp55 = add i16 %i.22.0, 1 %_tmp59 = icmp slt i16 %_tmp55, 40 br i1 %_tmp59, label %bb1, label %bb4.preheader, !llvm.loop !​3

bb4.preheader: ; preds = %middle.block, %bb1 %_tmp52.lcssa = phi i16 [ %_tmp52, %bb1 ], [ undef, %middle.block ]

The problem is at the last line. The phi-node has a new incoming value that is undef, but in this case it should be the same as the first incoming value.

The new "undef" is added in fixLCSSAPHIs() in LoopVectorize.cpp.

mveriksson avatar Sep 07 '15 15:09 mveriksson

mentioned in issue llvm/llvm-bugzilla-archive#32935

llvmbot avatar Nov 26 '21 23:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#32859

llvmbot avatar Nov 26 '21 23:11 llvmbot

rL302988

RKSimon avatar May 13 '17 15:05 RKSimon

*** Bug llvm/llvm-bugzilla-archive#32935 has been marked as a duplicate of this bug. ***

llvmbot avatar May 09 '17 22:05 llvmbot

Adding some people owning (and with interest in) the vectorizer. See the dupe #​32859 where we add the undef in the vectorizer and the NewGVN is smart enough to figure out and zaps some code away.

fixLCSSAPHIs() code seems a little dubious to me, but I'm not a vectorizer expert :)

llvmbot avatar May 02 '17 23:05 llvmbot

NewGVN is smart enough to break another instance of this bug that PR 32859 gives an example for :(

llvmbot avatar May 02 '17 23:05 llvmbot

*** Bug llvm/llvm-bugzilla-archive#32859 has been marked as a duplicate of this bug. ***

llvmbot avatar May 02 '17 23:05 llvmbot

Ah, I see now, I thought opt was failing. Do you by any chance have less simplified example? I can see that this might be an issue, but I suspect that the rootcause might be somewhere else and to verify it it would be super helpful to have a test that really exposes a bug (in the current reproducer the undef value doesn't matter as we never take the corresponding edge).

Unfortunately I do not have saved the original problem. I remember that I found it in randomized testing and that it was rare. I also remember that it was a real wrong-code error that I had to spend a day or so to track down.

I will try to make an effort in finding a "real" error again by running some more randomized testing during the weekend.

mveriksson avatar Apr 29 '16 11:04 mveriksson

Ah, I see now, I thought opt was failing. Do you by any chance have less simplified example? I can see that this might be an issue, but I suspect that the rootcause might be somewhere else and to verify it it would be super helpful to have a test that really exposes a bug (in the current reproducer the undef value doesn't matter as we never take the corresponding edge).

llvmbot avatar Apr 28 '16 22:04 llvmbot

Looks like it's fixed in trunk. Could you please verify?

I tried to verify, but the problem is still there I think. The reproducer that I uploaded still shows the bad behavior.

In our out-of-tree target we have used the fix I proposed in Comment 3 and it has worked fine so far. (I have not tried to push it upstream because I am not an LLVM committer.)

mveriksson avatar Apr 28 '16 11:04 mveriksson

Looks like it's fixed in trunk. Could you please verify?

llvmbot avatar Apr 28 '16 01:04 llvmbot

As an experiment I tried modifying the code so that it would duplicate the first incoming value of the phi instead of always creating undef:

@@ -3401,7 +3401,7 @@ void InnerLoopVectorizer::fixLCSSAPHIs() { PHINode *LCSSAPhi = dyn_cast(LEI); if (!LCSSAPhi) break; if (LCSSAPhi->getNumIncomingValues() == 1)

  •  LCSSAPhi->addIncoming(UndefValue::get(LCSSAPhi->getType()),
    
  •  LCSSAPhi->addIncoming(LCSSAPhi->getIncomingValue(0),
                           LoopMiddleBlock);
    
    } }

This seems to work for both the original Bug 14725 and for this new bug. But I am not familiar enough with the loop vectorizer to say that this is a good solution, I do not even understand why there is a phi-node here with only one incoming value to begin with.

mveriksson avatar Sep 08 '15 09:09 mveriksson

I don't know the exact commit but I would guess it is r171251 that is mentioned in Bug 14725. It is possible that this example was broken also before this, but in another way.

I found the bug with a test on my out-of-tree target, and I don't know a good way to bisect this far back in the history. Just looking at the code I think this is a good place to start looking:

commit db2367512e87dbd7b93c3250ef30c9df5e40cb43 Author: Nadav Rotem [email protected] Date: Sun Dec 30 07:47:00 2012 +0000

 LoopVectorizer: Fix a bug in the code that updates the loop exiting block.
 LCSSA PHIs may have undef values. The vectorizer updates values that are used by outside users such as PHIs.
 The bug happened because undefs are not loop values. This patch handles these PHIs.
 
 llvm/llvm-project#15097 
 
 
 
 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171251 91177308-0d34-0410-b5e6-96231b3b80d8

mveriksson avatar Sep 08 '15 09:09 mveriksson

This looks pretty bad. Do you know which specific commit introduced the bug?

llvmbot avatar Sep 07 '15 17:09 llvmbot