Issue 79258
Summary [Flang] TSVC s118: not vectorized because LICM doesn't work
Labels flang:ir, vectorization
Assignees
Reporter yus3710-fj
    Flang can't vectorize the loop in `s118` of [TSVC](https://www.netlib.org/benchmark/vectors) while Clang can vectorize the loop written in C.

```fortran
! Fortran version
      module mod
      integer ld, nloops
      parameter (ld=1000,nloops=135)
 real a(ld), b(ld), c(ld), d(ld), e(ld)
      real aa(ld,ld), bb(ld,ld), cc(ld,ld)
      interface
      subroutine dummy(ld,n,a,b,c,d,e,aa,bb,cc,x)
         integer ld, n
         real a(ld), b(ld), c(ld), d(ld), e(ld)
         real aa(ld,ld), bb(ld,ld), cc(ld,ld)
         real, value :: x
      end subroutine
      end interface
      end module

      subroutine s118 (n)
      use mod
      integer n, i, j

      call init(ld,n,a,b,c,d,e,aa,bb,cc,'s118 ')
      do 10 i = 2,n
        do 20 j = 1,i-1
          a(i) = a(i) + bb(i,j) * a(i-j)
  20    continue
 10  continue
      call dummy(ld,n,a,b,c,d,e,aa,bb,cc,1.)
 end
```

```c
// C version
#define LEN 32000
#define LEN2 256
float a[LEN], b[LEN], c[LEN], d[LEN], e[LEN];
float aa[LEN2][LEN2], bb[LEN2][LEN2], cc[LEN2][LEN2];

int s118() {
  init( "s118 ");
  for (int i = 1; i < LEN2; i++) {
    for (int j = 0; j <= i - 1; j++) {
      a[i] += bb[j][i] * a[i-j-1];
    }
  }
  dummy(a, b, c, d, e, aa, bb, cc, 0.);
  return 0;
}
```

```console
$ flang-new -v -Ofast s118.f -S -Rpass=vector
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git 2759e47067ea286f6302adcfe93b653cfaf6f2eb)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple x86_64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -ffast-math -target-cpu x86-64 -fstack-arrays -fversion-loops-for-stride -mframe-pointer=none -O3 -o /tmp/s118-5868cd.o -x f95-cpp-input s118.f
$ clang -Ofast s118.c -S -Rpass=vector
/path/to/s118.c:16:4: remark: vectorized loop (vectorization width: 4, interleaved count: 2) [-Rpass=loop-vectorize]
   16 | for (int j = 0; j <= i - 1; j++) {
      | ^
```

Hoisting the store outside the loop is necessary for vectorization, but it doesn't work because BasicAA says `a(i)` and `a(i-j)` may alias each other.
It's similar to #74262 but BasicAA won't do complicated analyses, so I suspect it's difficult to fix BasicAA.

```llvm
25: ; preds = %.lr.ph, %25
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next, %25 ], !dbg !30
  %26 = phi float [ %.promoted, %.lr.ph ], [ %32, %25 ], !dbg !30
  %27 = mul nuw nsw i64 %indvars.iv, 1000, !dbg !30
  %gep13 = getelementptr float, ptr %invariant.gep, i64 %27, !dbg !30
  %28 = load float, ptr %gep13, align 4, !dbg !30, !tbaa !31
  %29 = sub nuw nsw i64 %indvars.iv21, %indvars.iv, !dbg !30
  %gep12 = getelementptr float, ptr getelementptr ([1000 x float], ptr @_QMmodEa, i64 -1, i64 999), i64 %29, !dbg !30
  %30 = load float, ptr %gep12, align 4, !dbg !30, !tbaa !27
  %31 = fmul fast float %30, %28, !dbg !30
  %32 = fadd fast float %31, %26, !dbg !30
  store float %32, ptr %24, align 4, !dbg !30, !tbaa !27 ;; this can be hoisted
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1, !dbg !33
  %exitcond.not = icmp eq i64 %indvars.iv.next, %indvars.iv21, !dbg !26
  br i1 %exitcond.not, label %._crit_edge, label %25, !dbg !26
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to