Query on Loop Vectorization in std::reduce

Jonathan Wakely jwakely.gcc@gmail.com
Fri Jan 31 08:00:53 GMT 2025


On Thu, 30 Jan 2025, 20:28 Abhishek Kaushik, <abhishek.kaushik@intel.com>
wrote:

> Hello libstdc++ maintainers,
> I am encountering an issue with the following piece of code in std::reduce
> :
>
> while ((__last - __first) >= 4)
> {
>   _Tp __v1 = __binary_op(__first[0], __first[1]);
>   _Tp __v2 = __binary_op(__first[2], __first[3]);
>   _Tp __v3 = __binary_op(__v1, __v2);
>   __init = __binary_op(__init, __v3);
>   __first += 4;
> }
>
> The Intel compiler is not able to vectorize this loop. However, if I
> change the loop to a for loop like this:
>
> for (; __first <= __last - 4; __first += 4)
> {
>   _Tp __v1 = __binary_op(__first[0], __first[1]);
>   _Tp __v2 = __binary_op(__first[2], __first[3]);
>   _Tp __v3 = __binary_op(__v1, __v2);
>   __init = __binary_op(__init, __v3);
> }
>
> to follow the OpenMP canonical form, vectorization can occur, resulting in
> improved performance.
> If I submit a patch to change the loop header, will it be accepted?
>

That seems reasonable, yes.


Thank you for your assistance!
> Best regards,
> Abhishek Kaushik
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20250131/5fe93706/attachment.htm>


More information about the Libstdc++ mailing list