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

[LV] Miscompile caused due to incorrect index for extracting vector result

Open annamthomas opened this issue 9 years ago • 8 comments

Bugzilla Link 32396
Resolution FIXED
Resolved on May 23, 2017 14:30
Version trunk
OS All
Attachments initial IR: test.ll, IR after loop vectorization: test_after_vectorize.ll
CC @annamthomas,@topperc

Extended Description

Loop vectorizer extracts incorrect result from the vector. Source code snippet: int byres = 52, i=1; for (i1 = 141; i1 < 170; i1++) { i2 = 193; while (--i2 > 0) {

            byRes = (((i) * i)); 
            i = i1 + i2; 
        }   
    }   

printf("%d", byRes);

Here byRes should be (169 + 2) ^ 2, i.e. i2 = 2 and i1 = 169

After vectorization of inner loop, byRes becomes (169 + 1) ^ 2, i.e. i2 = 1.

In the vectorized code, I've verified that the incorrect result comes about by extracting from the incorrect index. vector.ph: ; preds = %min.iters.checked %broadcast.splatinsert = insertelement <16 x i32> undef, i32 %local_4_, i32 0 %broadcast.splat = shufflevector <16 x i32> %broadcast.splatinsert, <16 x i32> undef, <16 x i32> zeroinitializer ... br label %vector.body

vector.body: %vec.ind = phi <16 x i32> [ <i32 192, i32 191, i32 190, i32 189, i32 188, i32 187, i32 186, i32 185, i32 184, i32 183, i32 182, i32 181, i32 180, i32 179, i32 178, i32 177>, %vector.ph ], [ %vec.ind.next, %vector.body ] .... %16 = add nuw nsw <16 x i32> %vec.ind, %broadcast.splat %vec.ind.next = add <16 x i32> %vec.ind, <i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16, i32 -16> %18 = icmp eq i32 %index.next, 192 br i1 %18, label %middle.block, label %vector.body, !llvm.loop !​0

middle.block: ; preds = %vector.body %cmp.n = icmp eq i32 192, 192 %vector.recur.extract = extractelement <16 x i32> %16, i32 15

At index 15 from vector extract, we have i2 + i1 = 170, which is the incorrect result to extract. The correct result is at index 14 (i2 + i1 = 171).

I'll attach the IR before vectorization and after vectorization.

Reproduce problem: lli test.ll 29241

opt -loop-vectorize test.ll -S > test_after_vectorize.ll 2>&1 lli test_after_vectorize.ll 28900

annamthomas avatar Mar 23 '17 15:03 annamthomas

Fixed by change: https://reviews.llvm.org/rL300238

annamthomas avatar May 23 '17 21:05 annamthomas

*** Bug llvm/llvm-project#31483 has been marked as a duplicate of this bug. ***

annamthomas avatar May 23 '17 21:05 annamthomas

*** Bug llvm/llvm-project#31664 has been marked as a duplicate of this bug. ***

annamthomas avatar Apr 11 '17 15:04 annamthomas

Fix up for review: https://reviews.llvm.org/D31910

annamthomas avatar Apr 11 '17 12:04 annamthomas

Easier test case below. opt -loop-vectorize test.ll -S >test_vect.ll lli test_vect.ll 95

It should be 94.

test.ll: @.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1 define i32 @​test1(i32* %cval, i32 %x) { entry: br label %for.body

for.body: %inc.phi = phi i32 [ 0, %entry ], [ %inc, %for.body ] %val.phi = phi i32 [ 0, %entry ], [ %addx, %for.body ] %inc = add nsw i32 %inc.phi, 1 %addx = add nuw nsw i32 %inc.phi, %x %cmp = icmp eq i32 %inc.phi, 95 br i1 %cmp, label %for.end, label %for.body

for.end: ret i32 %val.phi }

define i32 @​main() #​1 { %addr = call i8* @​malloc(i32 40) %addrbc = bitcast i8* %addr to i32* %result = call i32 @​test1(i32* %addrbc, i32 0) %re = call i32 (i8*, ...) @​printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %result) #​2 call void @​free(i8* %addr) ret i32 0 }

declare void @​free(i8*) declare i8* @​malloc(i32) declare i32 @​printf(i8*, ...)

annamthomas avatar Apr 10 '17 22:04 annamthomas

llvm/llvm-project#31483 is likely related.

llvmbot avatar Apr 07 '17 18:04 llvmbot

We are vectorizing past the actual number of elements. The fix is to vectorize to TripCount - Step, and rely on the scalar loop to handle the remaining iterations correctly.

annamthomas avatar Apr 07 '17 16:04 annamthomas

assigned to @annamthomas

annamthomas avatar Mar 23 '17 15:03 annamthomas