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

SLPVectorizer finds incorrect reduction value

Open mveriksson opened this issue 10 years ago • 12 comments

Bugzilla Link 25787
Resolution FIXED
Resolved on Dec 17, 2015 02:52
Version trunk
OS Linux
Attachments reduced input file
CC @hfinkel

Extended Description

The function getReductionValue can return a Value that is not dominated by the phi-node. This can cause miscompiles.

I found the problem originally in an out-of-tree target with a huge test case. I have added an assert in the code like this:

--- a/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -3937,7 +3937,8 @@ static bool PhiTypeSorterFunc(Value *V, Value *V2) { /// /// \returns A candidate reduction value if possible, or \code nullptr \endcode /// if not possible. -static Value *getReductionValue(PHINode *P, BasicBlock *ParentBB, +static Value *getReductionValue(const DominatorTree *DT,

  •                            PHINode *P, BasicBlock *ParentBB,
                               LoopInfo *LI) {
    
    Value *Rdx = nullptr;

@@ -3967,6 +3968,12 @@ static Value *getReductionValue(PHINode *P, BasicBlock *ParentBB, Rdx = P->getIncomingValue(1); }

  • assert(!Rdx || !dyn_cast(Rdx) ||
  •     DT->dominates(P->getParent(), dyn_cast<Instruction>(Rdx)->getParent()));
    
  • // if (Rdx && dyn_cast(Rdx) &&
  • // !DT->dominates(P->getParent(), dyn_cast(Rdx)->getParent()))
  • // return nullptr;
  • return Rdx; }

@@ -4062,7 +4069,7 @@ bool SLPVectorizer::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) { if (P->getNumIncomingValues() != 2) return Changed;

  •  Value *Rdx = getReductionValue(P, BB, LI);
    
  •  Value *Rdx = getReductionValue(DT, P, BB, LI);
    
     // Check if this is a Binary Operator.
     BinaryOperator *BI = dyn_cast_or_null<BinaryOperator>(Rdx);
    

And then reduced the failing test to something that still trigger the assert (but it no longer miscompiles).

Look at the first phi-node in the dump below. The incoming value from the loop latch is actually defined in bb2. Is this a valid phi-node? If it is, then there must be a bug when SLP Vectorizer finds the reduction value "%_tmp279 = load volatile i16, i16* null".

*** IR Dump Before SLP Vectorizer *** define i16 @​test_spill_stress(i16 %tnr.8.par) #​0 { bb2: %_tmp279 = load volatile i16, i16* null, align 32768 br label %bb_usw4.outer.outer

bb_usw4.outer.outer.loopexit: ; preds = %bb_usw4 br label %bb_usw4.outer.outer

bb_usw4.outer.outer: ; preds = %bb_usw4.outer.outer.loopexit, %bb2 %f3.351.0.ph.ph = phi i16 [ 0, %bb2 ], [ %_tmp279, %bb_usw4.outer.outer.loopexit ] br label %bb_usw4

bb_usw4: ; preds = %bb_usw4.backedge, %bb_usw4.outer.outer %i__0.358.0 = phi i16 [ 4, %bb_usw4.outer.outer ], [ %i__0.358.0.be, %bb_usw4.backedge ] switch i16 %i__0.358.0, label %bb15 [ i16 3, label %bb_usw4.outer.outer.loopexit i16 5, label %bb_usw4.backedge ]

bb15: ; preds = %bb_usw4 %_tmp1143 = add i16 %i__0.358.0, 1 %_tmp1145 = icmp slt i16 %_tmp1143, 8 br i1 %_tmp1145, label %bb_usw4.backedge, label %bb16

bb_usw4.backedge: ; preds = %bb15, %bb_usw4 %i__0.358.0.be = phi i16 [ %_tmp1143, %bb15 ], [ 6, %bb_usw4 ] br label %bb_usw4

bb16: ; preds = %bb15 %_tmp1161 = icmp eq i16 %f3.351.0.ph.ph, 11584 %_tmp1162 = zext i1 %_tmp1161 to i16 %_tmp1163 = tail call i16 @​check_i(i16 998, i16 %_tmp1162, i16 1) ret i16 2 }

With the added assert, the following command crashes for the attached file: opt -S -mcpu=swift -mtriple=thumbv7-apple-ios -slp-vectorizer -O3 bugpoint-reduced-simplified.ll

mveriksson avatar Dec 09 '15 13:12 mveriksson

Fantastic. Resolving fixed.

llvmbot avatar Dec 17 '15 10:12 llvmbot

I have verified that the fix does indeed solve my original problem.

Thank you!

mveriksson avatar Dec 17 '15 10:12 mveriksson

I've landed this change in r255792.

Mattias, can you let me know if this resolves your problem?

llvmbot avatar Dec 16 '15 20:12 llvmbot

It's less expensive for X86 because it efficiently supports operations on smaller vectors than AArch64 (A threshold of -10 is good enough to hit this fault on X86, -25 for AArch64). Maybe your architecture efficiently supports small vector types?

You can get a glimpse at the costs the SLP vectorizer found by passing -debug-only=SLP and looking for lines starting with "SLP: Adding cost ...". If this pattern occurred in larger trees, then I would have seen it on AArch64, but I didn't have any test-cases exercising this. It looks like this has been a latent bug in horizontal reduction vectorization that has never been caught (I only turned it on by default fairly recently), so thanks very much for this test case.

I have a patch ready to go. I'd like to do a bootstrap and a conformance check over a large suit of benchmarks to make sure I've not missed anything here.

Will get this sent to the list tomorrow. Hope that's OK!

llvmbot avatar Dec 16 '15 17:12 llvmbot

Great!

I did not think about changing the threshold.

Maybe I should look into why my target is much more willing to vectorize this example compared to other targets.

mveriksson avatar Dec 16 '15 15:12 mveriksson

Ahhh! Thanks Mattias.

I took this as input:

efine void @​test_spill_stress() #​0 { bb4: %_tmp473 = load volatile i16, i16* undef, align 1 %_tmp503 = load volatile i16, i16* undef, align 1 %_tmp505 = load volatile i16, i16* undef, align 1 %_tmp519 = load volatile i16, i16* undef, align 1 %_tmp521 = load volatile i16, i16* undef, align 1 %_tmp527 = load volatile i16, i16* undef, align 1 %_tmp529 = load volatile i16, i16* undef, align 1 %_tmp535 = load volatile i16, i16* undef, align 1 %_tmp976 = sub i16 %_tmp527, %_tmp521 %_tmp983 = sub i16 %_tmp535, %_tmp529 %_tmp1088 = sub i16 %_tmp503, %_tmp473 %_tmp1091 = add i16 %_tmp983, %_tmp976 %_tmp1094 = sub i16 %_tmp519, %_tmp505 %_tmp1095 = add i16 %_tmp1091, %_tmp1094 %_tmp1119 = add i16 %_tmp1095, %_tmp1088 br label %bb15

bb15: ; preds = %bb4, %bb15 %f7.355.0 = phi i16 [ 0, %bb4 ], [ %_tmp1119, %bb15 ] br label %bb15 }

And then ran it through AArch64 (most backends would do) with -slp-threshold=-1000 (a ridiculously large threshold). I get a broken function at the end of it as expected:

Instruction does not dominate all uses! %f7.355.0 = phi i16 [ 0, %bb4 ], [ %_tmp1119, %bb15 ] %_tmp1119 = add i16 %9, %f7.355.0 LLVM ERROR: Broken function found, compilation aborted!

This will help me with the testcase.

Thanks so much for your time!

Charlie.

llvmbot avatar Dec 16 '15 14:12 llvmbot

Hi, Charlie!

I tried to reduce the test in a better way by putting the assert where the miscompile happens.

I still can't make it miscompile for an in-tree target.

This excerpt from a debugging session shows when it goes wrong for me: *** IR Dump Before SLP Vectorizer *** define void @​test_spill_stress() #​0 { bb4: %_tmp473 = load volatile i16, i16* undef, align 1 %_tmp503 = load volatile i16, i16* undef, align 1 %_tmp505 = load volatile i16, i16* undef, align 1 %_tmp519 = load volatile i16, i16* undef, align 1 %_tmp521 = load volatile i16, i16* undef, align 1 %_tmp527 = load volatile i16, i16* undef, align 1 %_tmp529 = load volatile i16, i16* undef, align 1 %_tmp535 = load volatile i16, i16* undef, align 1 %_tmp976 = sub i16 %_tmp527, %_tmp521 %_tmp983 = sub i16 %_tmp535, %_tmp529 %_tmp1088 = sub i16 %_tmp503, %_tmp473 %_tmp1091 = add i16 %_tmp983, %_tmp976 %_tmp1094 = sub i16 %_tmp519, %_tmp505 %_tmp1095 = add i16 %_tmp1091, %_tmp1094 %_tmp1119 = add i16 %_tmp1095, %_tmp1088 br label %bb15

bb15: ; preds = %bb4, %bb15 %f7.355.0 = phi i16 [ 0, %bb4 ], [ %_tmp1119, %bb15 ] br label %bb15 }

Breakpoint 2, (anonymous namespace)::SLPVectorizer::runOnFunction (this=0x4194610, F=...) at ../lib/Transforms/Vectorize/SLPVectorizer.cpp:3163 (gdb) c Continuing. *** IR Dump After SLP Vectorizer *** define void @​test_spill_stress() #​0 { bb4: %_tmp473 = load volatile i16, i16* undef, align 1 %_tmp503 = load volatile i16, i16* undef, align 1 %_tmp505 = load volatile i16, i16* undef, align 1 %_tmp519 = load volatile i16, i16* undef, align 1 %_tmp521 = load volatile i16, i16* undef, align 1 %_tmp527 = load volatile i16, i16* undef, align 1 %_tmp529 = load volatile i16, i16* undef, align 1 %_tmp535 = load volatile i16, i16* undef, align 1 %0 = insertelement <4 x i16> undef, i16 %_tmp535, i32 0 %1 = insertelement <4 x i16> %0, i16 %_tmp527, i32 1 %2 = insertelement <4 x i16> %1, i16 %_tmp519, i32 2 %3 = insertelement <4 x i16> %2, i16 %_tmp503, i32 3 %4 = insertelement <4 x i16> undef, i16 %_tmp529, i32 0 %5 = insertelement <4 x i16> %4, i16 %_tmp521, i32 1 %6 = insertelement <4 x i16> %5, i16 %_tmp505, i32 2 %7 = insertelement <4 x i16> %6, i16 %_tmp473, i32 3 %8 = sub <4 x i16> %3, %7 %_tmp1091 = add i16 undef, undef %_tmp1095 = add i16 %_tmp1091, undef %rdx.shuf = shufflevector <4 x i16> %8, <4 x i16> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef> %bin.rdx = add <4 x i16> %8, %rdx.shuf %rdx.shuf1 = shufflevector <4 x i16> %bin.rdx, <4 x i16> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef> %bin.rdx2 = add <4 x i16> %bin.rdx, %rdx.shuf1 %9 = extractelement <4 x i16> %bin.rdx2, i32 0 %_tmp1119 = add i16 %9, %f7.355.0 br label %bb15

bb15: ; preds = %bb4, %bb15 %f7.355.0 = phi i16 [ 0, %bb4 ], [ %_tmp1119, %bb15 ] br label %bb15 }

You can see that %f7.355.0 is used before it is defined.

I have attached the input file that I used. But as I said, I can't make it fail for any other target than mine.

mveriksson avatar Dec 16 '15 14:12 mveriksson

Hi Mattias,

I'm clearly not creative enough today, I'm struggling to concoct a testcase to demonstrate the codgen issue here. I'm finding it hard to get the SLP vectorizer to vectorize anything with such a pattern. (I still agree such reduction values are dodgy, but I'd like to see what the SLP vectorizer does with one)

I'm going to launch a few benchmarks runs to see if I have an example of such patterns in the tests I have access to, maybe that will help me figure out a decent testcase.

How big was your testcase before it stopped producing a codegen fault? It might be useful as a starting point for creating a testcase that does give a codegen fault.

Kind regards, Charlie.

llvmbot avatar Dec 15 '15 18:12 llvmbot

Thank you for looking at my report!

If you could submit a patch for this I would appreciate it.

mveriksson avatar Dec 15 '15 09:12 mveriksson

Missed a sentence above.

I think this should be submitted for review... I can do that for your you of course, unless you'd prefer to submit yourself.

:-)

llvmbot avatar Dec 14 '15 18:12 llvmbot

Hi Matthias,

Thanks very much for reducing your test case down and analysing the problem. Sorry this mistake cost you time :-(

Your analysis sounds reasonable to me. The fix you propose does not affect any of the transformations I rely on with this patch; I had no ambition of catching non-dominated reuction values like this -- it shouldn't be considered a legal phi under my initial conditions, so the extra check seems like a good idea.

I can do that for your you of course, unless you'd prefer to submit yourself.

Further apologies for the delay in responding, I've been on leave.

Thanks again! Charlie.

llvmbot avatar Dec 14 '15 18:12 llvmbot