llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.34k stars 12.13k forks source link

An optimization error about loop unrolling #41727

Open llvmbot opened 5 years ago

llvmbot commented 5 years ago
Bugzilla Link 42382
Version 7.0
OS Linux
Reporter LLVM Bugzilla Contributor
CC @TNorthover

Extended Description

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.

TNorthover 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.