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

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Wed Apr 23 11:09:02 PDT 2025


vzakhari wrote:

> Naïve question: why is a first element needed for numerical min/max val/loc?
> 
> Isn't there a comparison + iteration order that could be used so that the type representation min/max can be used as a starting point instead of some value from the input array?
> 
> For instance, assuming MAXLOC with BACK being absent, we could start with zero coordinates and a temporary max being the numerical min for that type, then the array could be iterated backwards (maybe that is a bad idea from a memory access perspective though) always replacing the result when something bigger or equal is found. Then there is no need to deal with the array size and mask.
> 
> Again, this is a naïve question, it may be a terrible idea from a performance perspective and just be incorrect with some corner cases (nans probably? although I think `fcmp uge` would work in my example to select the first NaN in array order as being the result location (assuming that is what we want)).
> 
> Otherwise, the implementation of your solution looks good to me.

Thank you for the review, Jean!

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.

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


More information about the flang-commits mailing list