Query on Loop Vectorization in std::reduce

Abhishek Kaushik abhishek.kaushik@intel.com
Tue Feb 4 05:00:43 GMT 2025


I wanted to ask one more question regarding this, can we just disable the while loop on newer targets? It looks like the manual unroll that this is trying to perform is not very helpful and g++ doesn't vectorize it unless I'm missing some options (https://godbolt.org/z/WhWs7frP9).


________________________________
From: Abhishek Kaushik <abhishek.kaushik@intel.com>
Sent: Friday, January 31, 2025 1:58 AM
To: libstdc++@gcc.gnu.org <libstdc++@gcc.gnu.org>
Subject: Query on Loop Vectorization in std::reduce

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?
Thank you for your assistance!
Best regards,
Abhishek Kaushik

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20250204/df6984ec/attachment.htm>


More information about the Libstdc++ mailing list