feltor-dev/feltor

gcc-7.3 -O3 produces wrong results

mwiesenberger opened this issue · 6 comments

The runge_kutta_t code produces wrong results when compiled with -O3
When compiled with -O2 everything is fine

msvc 15.8 reports fatal error C1001: An internal error has occurred in the compiler.
when compiling with /O2, everything fine with /O1

The error also goes away if no fma are used in the Axpby subroutine

Error there in g++-8.0 and g++-6.4
Error NOT there in g++-5.5 and in g++-8.1

Submitted a gcc bug report
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87458
Minimal program compile with: g++ -O3 -mfma

#include <iostream>
#include <array>
#include <cmath>


std::array<double, 2> create(double x)
{
    std::cout << "Need to output x to reproduce error ";
    std::cout << x << std::endl;
    return {x,x};
}

void axpby( double alpha, const std::array<double,2>& x, double beta, std::array<double,2>& y)
{
    double tmp = beta*y[0];
    y[0] = std::fma( alpha,x[0],tmp);
    tmp = beta*y[1];
    y[1] = std::fma( alpha,x[1],tmp);
}
double dot( const std::array<double,2>& u)
{
    //we also need this output to reproduce error
    std::cout << "In dot u is "<<u[0] << " (Correct value is 93)\n";
    return u[0]*u[0]+u[1]*u[1];
}   
    
int main()
{
    std::array<double,2> u = {{7,7}};
    axpby( 1., create(100), -1., u); //100 - 7 , 100 - 7
    std::cout << dot( u)<<" (Correct value is 17298)" << std::endl;

    return 0;
}

Apparently they know of this bug in gcc and fixed it in a later release. Question is if we should change the default optimization to -O2 instead of -O3?

change it to -O2 in upcoming release