Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

An optimization error about loop unrolling #41352

Open Quuxplusone opened 5 years ago

Quuxplusone commented 5 years ago
Bugzilla Link PR42382
Status NEW
Importance P enhancement
Reported by witstorm (witstorm@163.com)
Reported on 2019-06-25 04:18:49 -0700
Last modified on 2019-06-25 05:33:15 -0700
Version 7.0
Hardware PC Linux
CC htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, t.p.northover@gmail.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
In the experiment, when clang uses O2 and O3 optimization options, the pass of
Loop unrolling will cause an result error.The procedure is as follows:
typedef int A __attribute__((vector _size(64),aligned(64)));
A Aa,Ab,Ai;
static __inline A __attribute__((__always_inline__))
SET_A (int __A,int __B,int __C,int __D,int __E,int __F,int __G,int __H,int
__I,int __J,int __K,int __L,int __M,int __N,int __O,int __P){
  union{
   int __a[16] __attribute__((aligned(64)));
   A __v;
}__u;
  __u.__a[0]=__A;
  __u.__a[1]=__B;
  __u.__a[2]=__C;
  __u.__a[3]=__D;
  __u.__a[4]=__E;
  __u.__a[5]=__F;
  __u.__a[6]=__G;
  __u.__a[7]=__H;
  __u.__a[8]=__I;
  __u.__a[9]=__J;
  __u.__a[10]=__K;
  __u.__a[11]=__L;
  __u.__a[12]=__M;
  __u.__a[13]=__N;
  __u.__a[14]=__O;
  __u.__a[15]=__P;
return __u.__v;
}
int main(){
   int r[16]={2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047};
   int i;
   for(i=1024;i>=1;i>>=1){
   Ai=SET_A(i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i);
   Aa^=Aa>>i;
   Ab+=Aa+Ai;
}
}
You can reproduce the error with the following command:
clang -arch=x86 -static -O2 -emit-llvm -S a.c
Loop unrolling optimization produces incorrect values in IR.
Quuxplusone commented 5 years ago

UBSan reports a shift of 1024 (which is out of range and so undefined behaviour). Does the issue still happen if you fix that?

It would also be useful to describe why the IR is wrong, or put some diagnostic printf in to demonstrate the issue.