flang-21 produces NaN and Inf when it should not, randomly
With flang-21, the code at the end of the post generates NaN and Inf unexpectedly and randomly.
The behavior can be reproduced by the following bash script.
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/Inf 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 un-mathematical results. However, the resutls should be deterministic.
-
Keeping
-ffast-math, different combinations of the compilation flags will lead to different results. -
Here are the results in my tests:
**The results are not always the same across the runs. Look carefully :) **
-
The above results can be reproduced by forking https://github.com/zequipe/flang_ranom_nan and running the GitHub Actions.
Thank you for taking a look.
Code:
! 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
Still a bug with llvm-project/main today, using -ffast-math and -O2 or greater. Works with -O1 and less.
The abs() calls are not needed, either for correctness with the supplied data or for demonstrating the bug.
I suspect the trouble starts with a/maxval(a), which generates all NaNs, but shouldn't. a/c is valid, and c=maxval(a). Perhaps the use of -ffast-math is causing a division to be replaced with a multiplication by a reciprocal, since c and maxval(a) are denormal values and their reciprocals can't be normal.
If the assignment to a is replaced with a static initializer with the same elements, the results become deterministic with -ffast-math -O2, though suspicious -- although a is correct,maxval(a) becomes 0. and all the divisions then yield NaNs.
Here's a minimal reproducer:
real :: a(1) = [7e-45]
print *, a, maxval(a)
end
Since -ffast-math is likely to treat denormal operands s as zeroes, this may be what's expected. But I can't explain the nun-to-run nondeterminacy of the original case -- even UB, which this probably is, probably shouldn't vary.
Thank you for the investigation @klausler .
I observed that printing might change the results. The following part of the code was intended to demonstrate this behavior.
print *, a/c, '|', b
print *, (a/c)**2, '|', b**2
print * , '------------------'
print *, a, '|', a/c, '|', b
print *, a, '|', (a/c)**2, '|', b**2
For example, see lines 20--31 of https://github.com/user-attachments/files/25238070/ubuntu_x86.log .
Based on my limited experience, this sometimes indicates that something is corrupted in the memory.
The behavior of the code is indeed quite complex, varying across different combinations of the dimension, printing, compilation flags, and operating system. This is why I did not provide a minimal example --- actually I was not able to because the pattern was unclear to me.
Did you observe the same on your side?
Similar trouble with AMD/aocc-compiler-5.1.0/bin/flang but I had to change -std=f2018 to -std=f2008 because the former is not supported by that flang version. Without any options Gfortran and lfortran gave no Inf or Nan but ifx gave some NaN and no Inf with this command:
ifx test_div_flang.f90 && ./a.out ; rm -f a.out
As an aside, you don't need to use -std= with flang-new. All features are always available. If you want conformance warnings, use -pedantic.
As an aside, you don't need to use
-std=with flang-new. All features are always available. If you want conformance warnings, use-pedantic.
Indeed, each and every compilation flag in my example plays some role. As mentioned in the original post, different combinations of compilation flags will yield different results. See below. (Another possibility: it is not the flags that change the results, it is the intrinsic randomness of this bug.)
$ flang -fstack-arrays -std=f2018 -O3 -ffast-math -g test_div_flang.f90 && ./a.out
>>> Dimension = 14
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0.
.8 .8 0. 0. 0. 0. .8
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0.
.64000004 0. .64000004 .64000004 0. 0. 0. 0. .64000004
------------------
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0. .8 .8
0. 0. 0. 0. .8
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .64000004 0.
.64000004 .64000004 0. 0. 0. 0. .64000004
==================
1.E-45 0. 0. 0. -4.1455934E-24 4.5916E-41 -4.145587E-24 4.5916E-41
-4.145445E-24 4.5916E-41 4.573722E-19 3.6292E-41 1.4908033E+38 4.3778E-41 | 0.
0. 1. 1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.3061767E+38 4.3778E-41 NaN NaN | 0. 0. 1. 1.
0. .64000004 0. .64000004 .64000004 0. 0. 0. 0. .64000004
------------------
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 | NaN NaN
NaN 0. 0. 0. NaN 0. 0. 0. -5423. -1.76576E-28 -4.145464E-24 4.5916E-41 | 0. 0.
1. 1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
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 | 2.76E-43
4.E-45 1.E-45 0. 8.081523E-39 0. 9.E-44 0. 9.2902E-41 0. 5.E-45 0. 3.E-45 0. |
0. 0. 1. 1. 0. .64000004 0. .64000004 .64000004 0. 0. 0. 0. .64000004
$ flang -fstack-arrays -O3 -ffast-math -g test_div_flang.f90 && ./a.out
>>> Dimension = 14
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0.
.8 .8 0. 0. 0. 0. .8
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0.
.64000004 0. .64000004 .64000004 0. 0. 0. 0. .64000004
------------------
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0. .8 .8
0. 0. 0. 0. .8
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .64000004 0.
.64000004 .64000004 0. 0. 0. 0. .64000004
==================
2.76E-43 4.E-45 1.E-45 0. 8.081523E-39 0. 9.E-44 0. 3.E-45 0. 8.E-45 -0. 0. 0.
| 0. 0. 1. 1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
1.E-45 0. 0. 0. -3.6707067E+20 4.5913E-41 -3.670701E+20 4.5913E-41
-3.6705744E+20 4.5913E-41 -24.241243 3.3957E-41 -1.453062E+25 4.4497E-41 | 0.
0. 1. 1. 0. .64000004 0. .64000004 .64000004 0. 0. 0. 0. .64000004
------------------
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 | 0. 0. 0.
0. 0. 0. 0. 0. 4.7019774E-38 0. -1.2431659E+25 4.4497E-41 NaN NaN | 0. 0. 1.
1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
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 | NaN NaN
NaN 0. 0. 0. NaN 0. 0. 0. 1.1879275E+12 -1.0943273E+17 -3.6705913E+20
4.5913E-41 | 0. 0. 1. 1. 0. .64000004 0. .64000004 .64000004 0. 0. 0. 0.
.64000004
Does running your executable via "setarch uname -m -R ./a.out" remove the apparent indeterminacy in your environment?
Does running your executable via "setarch
uname -m-R ./a.out" remove the apparent indeterminacy in your environment?
Not completely.
$ setarch $(uname -m) -R ./a.out
>>> Dimension = 14
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0.
.8 .8 0. 0. 0. 0. .8
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0.
.64000004 0. .64000004 .64000004 0. 0. 0. 0. .64000004
------------------
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0. .8 .8
0. 0. 0. 0. .8
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .64000004 0.
.64000004 .64000004 0. 0. 0. 0. .64000004
==================
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -7.97227E+33 4.5916E-41 NaN NaN | 0. 0. 1. 1. 0.
.8 0. .8 .8 0. 0. 0. 0. .8
2.76E-43 4.E-45 1.E-45 0. 8.081523E-39 0. 9.E-44 0. 9.2902E-41 0. 5.E-45 0.
3.E-45 0. | 0. 0. 1. 1. 0. .64000004 0. .64000004 .64000004 0. 0. 0. 0.
.64000004
------------------
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 | NaN NaN
NaN 0. 0. 0. NaN 0. 0. 0. -2.1861646E+38 -5.6907393E-14 NaN 4.5916E-41 | 0. 0.
1. 1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
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 | 1.E-45 0.
0. 0. NaN 4.5916E-41 NaN 4.5916E-41 NaN 4.5916E-41 1.5220845E+13 3.0611E-41
-9.0991415E+33 4.5916E-41 | 0. 0. 1. 1. 0. .64000004 0. .64000004 .64000004 0.
0. 0. 0. .64000004
$ setarch $(uname -m) -R ./a.out
>>> Dimension = 14
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0.
.8 .8 0. 0. 0. 0. .8
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0.
.64000004 0. .64000004 .64000004 0. 0. 0. 0. .64000004
------------------
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .8 0. .8 .8
0. 0. 0. 0. .8
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 | NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN | 0. 0. 1. 1. 0. .64000004 0.
.64000004 .64000004 0. 0. 0. 0. .64000004
==================
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -7.97227E+33 4.5916E-41 NaN NaN | 0. 0. 1. 1. 0.
.8 0. .8 .8 0. 0. 0. 0. .8
2.76E-43 4.E-45 1.E-45 0. 8.081523E-39 0. 9.E-44 0. 9.2902E-41 0. 5.E-45 0.
3.E-45 0. | 0. 0. 1. 1. 0. .64000004 0. .64000004 .64000004 0. 0. 0. 0.
.64000004
------------------
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 | NaN NaN
NaN 0. 0. 0. NaN 0. 0. 0. 5.4389E-27 1.0328078E-19 NaN 4.5916E-41 | 0. 0. 1.
1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
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 | 1.E-45 0.
0. 0. NaN 4.5916E-41 NaN 4.5916E-41 NaN 4.5916E-41 1.5220845E+13 3.0611E-41
-9.0991415E+33 4.5916E-41 | 0. 0. 1. 1. 0. .64000004 0. .64000004 .64000004 0.
0. 0. 0. .64000004
Are those two runs distinct? My old eyes can't immediately spot a difference. I didn't see one in my own runs.
What I am nearly certain is happening here is that with high optimization levels and -ffastmath, LLVM's optimizer is seeing the values being assigned to a and computed from them, perceiving that some of those computations are undefined behavior, and deleting some stores into stack memory that is also being written to by the runtime library's I/O code. The values left on the stack by the runtime include addresses that, by default, vary from run to run, so it leads to garbage output that varies as well.
When I moved the initialization of a into another source file and compiled it separately, the problem vanished for me, even with -ffast-math and high optimization levels, and without setarch -R.
We've run into this kind of behavior from LLVM optimization in the past. I don't think there's an actual bug here to be fixed, although I'm not happy about it.
Are those two runs distinct? My old eyes can't immediately spot a difference. I didn't see one in my own runs.
Sorry, the distinctions are indeed not easy to spot.
As far as I can see, the results produced by the following line were different.
print *, a, '|', a/c, '|', b
I copy the results of the two runs below. Note the large value -2.1861646E+38 in the first run.
------------------
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 | NaN NaN
NaN 0. 0. 0. NaN 0. 0. 0. -2.1861646E+38 -5.6907393E-14 NaN 4.5916E-41 | 0. 0.
1. 1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
...
------------------
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 | NaN NaN
NaN 0. 0. 0. NaN 0. 0. 0. 5.4389E-27 1.0328078E-19 NaN 4.5916E-41 | 0. 0. 1.
1. 0. .8 0. .8 .8 0. 0. 0. 0. .8
The only thing that I have thought of that I can do in the compiler to help you here is to emit a warning for the presence of denormal constants or folded expression results in a compilation with -ffast-math enabled, and that seems so weak that it probably wouldn't have helped you in the first place even if you had compiled with verbose warnings enabled. I'll need to think about this further.
FWIW, with -ffast-math -fno-reciprocal-math, we still generate "fast" divisions:
%77 = fir.load %76 : !fir.ref<f32>
%78 = arith.divf %77, %15 fastmath<fast> : f32
hlfir.yield_element %78 : f32
The -fno-reciprocal-math seems to be ignored.
I think the issue is an uninitialised variable in the Fortran runtime that implements I/O, the reproducer below does not involve denormal numbers (only NaN) and all user variables are properly defined when referenced.
! -O2 -ffast-math -g
use iso_fortran_env, only : RP => REAL32
real(RP) :: a(14), b(14), c
a = 0.
b = a / maxval(a)
c = maxval(a)
print *, a/maxval(a), '|', b
print *, a/c, '|', b
end
==4012165== Conditional jump or move depends on uninitialised value(s)
==4012165== at 0x1654E7: Fortran::decimal::ConversionToDecimalResult Fortran::decimal::ConvertToDecimal<24>(char*, unsigned long, Fortran::decimal::DecimalConversionFlags, int, Fortran::decimal::FortranRounding, Fortran::decimal::BinaryFloatingPointNumber<24>) (in a.out)
==4012165== by 0x1551D7: Fortran::runtime::io::RealOutputEditing<4>::EditListDirectedOutput(Fortran::runtime::io::DataEdit const&) (in a.out)
==4012165== by 0x15548A: Fortran::runtime::io::RealOutputEditing<4>::Edit(Fortran::runtime::io::DataEdit const&) (in a.out)
==4012165== by 0x135D85: bool Fortran::runtime::io::descr::FormattedRealIO<4, (Fortran::runtime::io::Direction)0>(Fortran::runtime::io::IoStatementState&, Fortran::runtime::Descriptor const&) (in a.out)
==4012165== by 0x13BF98: Fortran::runtime::io::descr::DescriptorIoTicket<(Fortran::runtime::io::Direction)0>::Begin(Fortran::runtime::WorkQueue&) (in a.out)
==4012165== by 0x1445DE: bool Fortran::runtime::io::descr::DescriptorIO<(Fortran::runtime::io::Direction)0>(Fortran::runtime::io::IoStatementState&, Fortran::runtime::Descriptor const&, Fortran::runtime::io::NonTbpDefinedIoTable const*) (in a.out)
==4012165== by 0x12A574: _QQmain (1.f90:9)
==4012165== by 0x12A5CC: main (1.f90:10)
==4012165== Uninitialised value was created by a heap allocation
==4012165== at 0x4C39185: malloc (vg_replace_malloc.c:442)
==4012165== by 0x12A530: _QQmain (1.f90:9)
==4012165== by 0x12A5CC: main (1.f90:10)
I think the issue is an uninitialised variable in the Fortran runtime that implements I/O, the reproducer below does not involve denormal numbers (only NaN) and all user variables are properly defined when referenced.
0./0. under -ffast-math is undefined behavior in LLVM's eyes, so higher optimization levels that detect the bad divisions at compilation time delete the code that computes them. Lowering is still allocating buffers for passing expression results o the I/O runtime, but they don't get filled with anything. Look more carefully at your valgrind output -- the binary->decimal formatting routine is reading uninitialized memory allocated from the heap.