[llvm] [flang-rt] Consider the signedness of zero for PRODUCT (PR #226918)
Yusuke MINATO via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 29 00:15:57 PDT 2026
yus3710-fj wrote:
I forgot to mention the performance impact.
This patch forces the accumulation of all array elements, which can cause performance regressions when the result is zero.
I measured the performance in the following cases:
1. The first element is zero; all subsequent elements are -1.
2. No zeros in the array; the resulting product is non-zero.
3. Same as case 1, but the `DIM` argument is not constant (i.e., the runtime call is never inlined).
4. Same as case 2, but the `DIM` argument is not constant.
| | Before [s] | After [s] | Speedup |
| --- | --- | --- | --- |
| Case 1 | 0.00 | 15.28 | 0.00x |
| Case 2 | 17.279 | 15.279 | 1.13x |
| Case 3 | 0.108 | 34.695 | 0.00x |
| Case 4 | 37.518 | 33.721 | 1.11x |
*(Note: "Speedup" values less than 1.0 indicate a performance slowdown.)*
* Test code
```fortran
subroutine measure(a)
implicit none
integer :: i, time_start, time_end, count_rate, count_max
integer, parameter :: iter = 10000
real, intent(in) :: a(:,:)
real :: d
call system_clock(time_start, count_rate, count_max)
do i=1,iter
d = product(a)
end do
call system_clock(time_end)
print *, d
print *, 'Time elapsed: ', real(time_end - time_start) / real(count_rate)
end subroutine measure
```
Based on these results, the performance regression in the worst-case scenario is significant, whereas the impact on typical cases is negligible. I believe that a result of zero from `PRODUCT` is rare in practical applications.
https://github.com/llvm/llvm-project/pull/226918
More information about the llvm-commits
mailing list