[flang-commits] [flang] [flang] Fetch the initial reduction value from the input array. (PR #136790)

via flang-commits flang-commits at lists.llvm.org
Thu Apr 24 05:02:27 PDT 2025


jeanPerier wrote:


> As you pointed out, I think we have to do it this way to properly handle NaNs. I believe your approach with `uge` will not work in this case:
> 
> ```
> input = (/NaN, 1.0/)
> result = smallest;
> result = (1.0 .uge. result) ? 1.0 : result; // yields 1.0
> result (NaN .uge. result) ? NaN : result; // yields NaN
> ```
> 
> The actual result should be 1.0.

I see, thanks, I expected NaN should be the result here, but that is indeed not what other compilers are doing (I am not quite sure what is the requirement with regards to NaN in the Fortran spec though...)... That indeed makes it impossible to find a comparison function+comparison order here because NaNs should be selected if this is this is the first element (to cover the all NaNs case), but not otherwise...

BTW, I see that IFX/Ifort and nvfortran/classic flang are a bit different in behavior and return `-Inf` for maxval on an all NaN inputs, so `they probably have an easier time getting optimal code here which could explain better perf (to get this result, it is possible to just init to -Inf and never select NaNs).

Are we sure about the requirement that all NaNs should be NaN and not -Inf for MAXVAL?

```
subroutine foo(x, y)
real :: x(10000), y
y = maxval(x)
end


real :: x(10000), y, zero
zero = 0.
x = 0./zero
call foo(x, y)
print *, y
end
```

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


More information about the flang-commits mailing list