Issue 180957
Summary flang-22 produces NaN and Inf when it should not, randomly
Labels new issue
Assignees
Reporter zaikunzhang
    With flang-22, the following code generates NaN unexpectedly and randomly. 

It can be reproduced by the following bash script. 
```bash
          rm -f a.out
          uname -a
          flang --version
          flang -fstack-arrays -std=f2018 -O3 -ffast-math -g test_div_flang.f90
          # Run a.out 5 times to demonstrate the random NaN generation behavior
          for i in {1..5}; do
            echo "Run $i:"
            ./a.out
          done
```

N.B.: 
0. The `-ffast-math` flag is being used. I know, this is not recommended in practice and it may give rise to non-mathematical results. However, the resutls should be deterministic. 
1. Keeping `-ffast-math`, different combinations of the compilation flags will lead to different results. 
2. Here are the results in my tests:
    - [ubuntu_x86.log](https://github.com/user-attachments/files/25238070/ubuntu_x86.log)
 - [ubuntu_arm.log](https://github.com/user-attachments/files/25238072/ubuntu_arm.log)
 - [macos_x86.log](https://github.com/user-attachments/files/25238071/macos_x86.log)
 - [macos_arm.log](https://github.com/user-attachments/files/25238073/macos_arm.log)
3. The above results can be reproduced by forking https://github.com/zequipe/flang_ranom_nan and run the GitHub Actions. 

Thank you for taking a look.   

Code: 

```fortran
! test_div_flang.f90
program test_div_flang
use iso_fortran_env, only : RP => REAL32

implicit none

! The code may behave differently depending on the dimension of the array. Try them.
real(RP) :: a(14), b(14), c
a =  [0., 0., 7.E-45, 7.E-45, 0., 5.E-45, 0., 5.E-45, 5.E-45, 0., 0., 0., 0., 5.E-45]

!real(RP) :: a(9), b(9), c
!a =  [5.E-45, 0., 5.E-45, 5.E-45, 0., 0., 0., 0., 5.E-45]
!
!real(RP) :: a(8), b(8), c
!a =  [0., 5.E-45, 5.E-45, 0., 0., 0., 0., 5.E-45]

b =  a / maxval(abs(a))
c = maxval(abs(a))

print *,  '>>> Dimension = ', size(a)

print *, a/maxval(abs(a)), '|', b
print *, (a/maxval(abs(a)))**2, '|', b**2
print * , '------------------'
print *, a, '|', a/maxval(abs(a)), '|', b
print *, a, '|', (a/maxval(abs(a)))**2, '|', b**2

print * , '=================='

print *, a/c, '|', b
print *, (a/c)**2, '|', b**2
print * , '------------------'
print *, a, '|', a/c, '|', b
print *, a, '|', (a/c)**2, '|', b**2

end program test_div_flang
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to