c++optimizationvalarray

Result of summing std::valarray differs when changing optimization levels


I have this piece of code summing std::valarray<int>'s:

#include <iostream>
#include <valarray>
#include <vector>

int main()
{
    std::vector<std::valarray<int>> vectorOfValarrays{{1, 1}, {2, 2}, {3, 3}};
    std::valarray<int> sumOfValarrays(2);
    for (const auto& i : vectorOfValarrays)
      sumOfValarrays = sumOfValarrays + i;

    std::cout << sumOfValarrays[0] << ' ' << sumOfValarrays[1];
}

Compiling with x86-64 gcc 12.2 using -O0 and -O1, it prints the expect result:

6 6

But when compiling with -O2 and -O3, it prints:

3 3

What could be the reason for this? Is my code undefined behaviour or is this a gcc bug?


Solution

  • I'm pretty sure that this is a gcc bug. Clang gives the correct behaviour for all optimization levels (-O0, -O1, -02, -O3). I also have a look at std::valarray constructors, operator + and operator = and it seems like my code doesn't any undefined behaviour.

    I found this bug report on gcc Bugzilla, and the problem seems like gcc has the wrong implementation for copying std::valarray, so in the question the line sumOfValarrays = sumOfValarrays + i; makes gcc tripped up.