[flang-commits] [flang] [flang][CodeGen] add nsw to address calculations (PR #74709)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Dec 8 11:26:01 PST 2023


vzakhari wrote:

Tom, I understand your concern.  I think adding no signed wrap on the do-variable increment may be a problem depending on how LLVM would optimize the loop ivs.

For example:
```
program main
  real :: x(10)
  call test(x, -127_1, 127_1, 10_1)
contains
  subroutine test(x,b,e,n)
    integer(1) :: i, b, e, n
    real :: x(:)
    do i=b,e
       x(MOD(i,n)) = 1.0
    end do
    print *, i
  end subroutine test
end program main
```

There is signed wrap on the final increment of `i`.  At the same time, Flang uses a separate loop iteration control of an extended type, so the loop control will not trigger the signed wrap and the loop execution will complete.

I guess setting `nsw` on the `i` increment may lead LLVM to erroneously reason about the range of iteration space.  I am not sure if such a deduction may lead to incorrect optimization of the compiler-generated loop iteration control, though.

We can try to add `nsw` under an option and investigate further.  At least, there will be an option for conformant programs.

P.S. btw, gfortran compiles the above code into an infinite loop.

https://github.com/llvm/llvm-project/pull/74709


More information about the flang-commits mailing list