Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Wrong answer after loop vectorization #47552

Open Quuxplusone opened 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR48583
Status NEW
Importance P enhancement
Reported by guopeilin (guopeilin1@huawei.com)
Reported on 2020-12-23 05:15:37 -0800
Last modified on 2020-12-23 05:16:46 -0800
Version trunk
Hardware PC Windows NT
CC arnaud.degrandmaison@arm.com, lebedev.ri@gmail.com, llvm-bugs@lists.llvm.org, smithp352@googlemail.com, Ties.Stuij@arm.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
This test case get the wrong answer after loop vectorization
====== commands =======
opt -loop-vectorize before-loop-vec.ll -o after-loop-vec.ll -S
llc after-loop-vec.ll -filetype=obj -o tmp.o
clang tmp.o -o tmp.out

========================
actually the test case's main job is doing a "361804 >> 18"
However the result shows that we finally get result 0 rather than 1, which is
a totally wrong answer

===== result =========
tmp.out: test-case.cpp:16: int main(): Assertion `a!=0 && "a should be 1"'
failed.
Aborted (core dumped)

===== before-loop-vec.ll ==========
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"

@a = dso_local local_unnamed_addr global i8 0, align 4
@b = dso_local local_unnamed_addr global [384 x i64] zeroinitializer, align 8
@c = dso_local local_unnamed_addr global i32 0, align 4
@.str.1 = private unnamed_addr constant [24 x i8] c"a!=0 && \22a should be
1\22\00", align 1
@.str.2 = private unnamed_addr constant [14 x i8] c"test-case.cpp\00", align 1
@__PRETTY_FUNCTION__.main = private unnamed_addr constant [11 x i8] c"int
main()\00", align 1

; Function Attrs: nofree norecurse nounwind
define dso_local void @_Z1dii(i32 %e, i32 %f) local_unnamed_addr {
entry:
  br label %for.cond

for.cond:                                         ; preds = %for.cond.cleanup5,
%entry
  %g.0 = phi i32 [ 0, %entry ], [ %add11, %for.cond.cleanup5 ]
  %cmp = icmp ult i32 %g.0, 11
  br i1 %cmp, label %for.cond1.preheader, label %for.cond.cleanup

for.cond1.preheader:                              ; preds = %for.cond
  %conv3 = and i32 %e, 255
  %cmp417 = icmp ugt i32 %conv3, 0
  br i1 %cmp417, label %for.body6.lr.ph, label %for.cond.cleanup5

for.body6.lr.ph:                                  ; preds = %for.cond1.preheader
  br label %for.body6

for.cond.cleanup:                                 ; preds = %for.cond
  ret void

for.cond1.for.cond.cleanup5_crit_edge:            ; preds = %for.inc
  br label %for.cond.cleanup5

for.cond.cleanup5:                                ; preds =
%for.cond1.for.cond.cleanup5_crit_edge, %for.cond1.preheader
  %add11 = add nuw nsw i32 %g.0, 2
  br label %for.cond

for.body6:                                        ; preds = %for.body6.lr.ph,
%for.inc
  %h.018 = phi i8 [ 0, %for.body6.lr.ph ], [ %add, %for.inc ]
  %0 = load i32, i32* @c, align 4
  %tobool = icmp eq i32 %0, -1
  br i1 %tobool, label %for.inc, label %if.then

if.then:                                          ; preds = %for.body6
  %1 = lshr i32 %f, 18
  %conv7 = trunc i32 %1 to i8
  store i8 %conv7, i8* @a, align 4
  %idxprom = zext i8 %h.018 to i64
  %arrayidx = getelementptr inbounds [384 x i64], [384 x i64]* @b, i64 0, i64 %idxprom
  store i64 0, i64* %arrayidx, align 8
  br label %for.inc

for.inc:                                          ; preds = %for.body6, %if.then
  %add = add i8 %h.018, 3
  %conv = zext i8 %add to i32
  %cmp4 = icmp ugt i32 %conv3, %conv
  br i1 %cmp4, label %for.body6, label %for.cond1.for.cond.cleanup5_crit_edge, !llvm.loop !9
}

; Function Attrs: norecurse nounwind
define dso_local i32 @main() local_unnamed_addr {
entry:
  call void @_Z1dii(i32 78, i32 361804)
  %0 = load i8, i8* @a, align 4
  %cmp = icmp eq i8 %0, 0
  br i1 %cmp, label %cond.false, label %cond.end

cond.false:                                       ; preds = %entry
  call void @__assert_fail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__PRETTY_FUNCTION__.main, i64 0, i64 0))
  unreachable

cond.end:                                         ; preds = %entry
  ret i32 0
}

; Function Attrs: noreturn nounwind
declare dso_local void @__assert_fail(i8*, i8*, i32, i8*) local_unnamed_addr

!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.vectorize.predicate.enable", i1 true}
!11 = !{!"llvm.loop.vectorize.enable", i1 true}
Quuxplusone commented 3 years ago

https://reviews.llvm.org/D93617

Quuxplusone commented 3 years ago

Can you also show the original C code?