Issue 234335
Summary [flang] Wrong result with !$omp simd for large loop body with outer loop vectorization
Labels flang
Assignees
Reporter ivan-pi
    flang miscompiles a valid `!$omp simd` loop:

```fortran
! repro_simd_linear.f90
! flang -O2 -fopenmp-simd repro_simd_linear.F90: v(101:128, e) wrong for all but the last SIMD lane
#define S1(i) v(i, e) = a(i, e) + b(i, e)
#define S4(i) S1(i); S1(i+1); S1(i+2); S1(i+3)
#define S16(i) S4(i); S4(i+4); S4(i+8); S4(i+12)
#define S64(i) S16(i); S16(i+16); S16(i+32); S16(i+48)

subroutine k(n, a, b, v)
  integer, intent(in) :: n
  real, intent(in) :: a(128, n), b(128, n)
  real, intent(out) :: v(128, n)
  integer :: e, eb
  do eb = 1, n, 8
     !$omp simd
     do e = eb, min(n, eb + 7)
        S64(1); S64(65)
     end do
  end do
end subroutine k

program p
  implicit none
  integer, parameter :: n = 8
 real :: a(128, n), b(128, n), v(128, n)
  integer :: i, e
  do e = 1, n
 do i = 1, 128
        a(i, e) = e
        b(i, e) = i
     end do
  end do
  v = 0
  call k(n, a, b, v)
  print '(a,i0,a,i0)', 'wrong values: ', count(v /= a + b), ' of ', size(v)
  print '(a,8f6.0)', 'v(128, 1:8) = ', v(128, :)
end program p
```

```
$ flang -O2 -fopenmp-simd repro_simd_linear.F90 && ./a.out
wrong values: 112 of 1024
v(128, 1:8) = 0.  130.    0.  132.    0.  134.    0.  136.
```

The wrong results occurs under `-O1`, `-O2`, and `-O3`, but is correct at `-O0` (no vectorization): 

```
$ flang -O0 -fopenmp-simd repro_simd_linear.F90 && ./a.out
wrong values: 0 of 1024
v(128, 1:8) =   129.  130.  131.  132. 133.  134.  135.  136.
```

gfortran results for reference: 

```
$ gfortran -O2 -fopenmp-simd repro_simd_linear.F90 && ./a.out
wrong values: 0 of 1024
v(128, 1:8) =   129.  130.  131.  132.  133.  134.  135. 136.
```

Seen with flang 23.1.1 (Homebrew, arm64-apple-darwin25.6.0). 

--- 

Using Claude Opus I also isolated a smaller reproducer for the issue: 

```fortran
! repro_simd_linear_small.f90
subroutine k(n, u, v, w)
  integer, intent(in) :: n
  real, intent(in) :: u(n)
  real, intent(out) :: v(n), w(n)
  integer :: e, eb
  do eb = 1, n, 8
     !$omp simd
     do e = eb, min(n, eb + 7)
        v(e) = u(e)
        w(e) = u(e)
     end do
  end do
end subroutine k
 
program p
  implicit none
  integer, parameter :: n = 16
  real :: u(n), v(n), w(n)
  external :: k
  integer :: i
  u = [(i, i = 1, n)]
  v = 0
  w = 0
  call k(n, u, v, w)
  print '(a,16f4.0)', 'v =', v
  print '(a,16f4.0)', 'w =', w
end program p
```

```
$ flang -O1 -fopenmp-simd -mllvm -earlycse-mssa-optimization-cap=0 -mllvm -disable-licm-promotion repro_simd_linear_small.f90 && ./a.out
v =  1.  2.  3.  4.  5.  6.  7.  8. 9. 10. 11. 12. 13. 14. 15. 16.
w =  0.  0.  0.  4.  0.  0.  0.  8.  0.  0. 0. 12.  0.  0.  0. 16.
```

```
$ gfortran -O1 -fopenmp-simd repro_simd_linear_small.f90 && ./a.out
v =  1.  2.  3.  4.  5.  6.  7.  8. 9. 10. 11. 12. 13. 14. 15. 16.
w =  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15. 16.
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to