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

Not vectorized trivial case since LAA (SCEV) fails to figure out exit counts

Open BalaRishi-AMD opened this issue 8 years ago • 3 comments

Bugzilla Link 38853
Version trunk
OS Linux
Attachments Testcase to reproduce the issue
CC @davidbolvansky,@fhahn,@hfinkel,@sanjoy

Extended Description

BUG DETAILS:

LLVM is not able to vectorize below trivial case:


extern int len; extern int *ptr_x, *ptr_y, *ptr_z; void foo() { for (int i = 0; i < len; i++) ptr_x[i] = ptr_y[i] * ptr_z[i]; }

The reason for this is LAA (SCEV) is not able to figure out the exit counts.

LV: Can't vectorize due to memory conflicts LAA: SCEV could not compute the loop exit count.

This test looks trivial and LAA is not able to figure out the loop exit.

This issue is not seen when arrays are used instead of pointers.

========================================== STEPS to REPRODUCE:

  • Download the attached vect.c file
  • Run the below command: clang -O3 -S -emit-llvm -o vect.ll vect.c

==========================================

BalaRishi-AMD avatar Sep 06 '18 07:09 BalaRishi-AMD

as

I believe this is because LLVM can't prove that &len does not alias ptr_x and so has to assume len is a loop varying value. If I change the code to

extern int len; extern int *ptr_x, *ptr_y, *ptr_z; void foo() { int len2 = len; for (int i = 0; i < len2; i++) ptr_x[i] = ptr_y[i] * ptr_z[i]; }

then it vectorizes.

I guess we maybe could try generate a runtime check ensuring &len is not in [ptr_x, ptr_x+len] ?

fhahn avatar Jul 27 '20 10:07 fhahn

Anyway

Clang: movsxd rdi, dword ptr [rip + len] cmp rsi, rdi

GCC: cmp DWORD PTR len[rip], eax jg .L3

I feel like movsxd is useless here (we do not use value in rdi after cmp)

davidbolvansky avatar Sep 12 '18 10:09 davidbolvansky

I believe this is because LLVM can't prove that &len does not alias ptr_x and so has to assume len is a loop varying value. If I change the code to

extern int len; extern int *ptr_x, *ptr_y, *ptr_z; void foo() { int len2 = len; for (int i = 0; i < len2; i++) ptr_x[i] = ptr_y[i] * ptr_z[i]; }

then it vectorizes.

sanjoy avatar Sep 06 '18 16:09 sanjoy