This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/70444] New: Optimizer removes expression template


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70444

            Bug ID: 70444
           Summary: Optimizer removes expression template
           Product: gcc
           Version: 5.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: g++bug at oxyware dot com
  Target Milestone: ---

The code below crashes with a SIGSEGV when compiled with g++ 5.3.1 and
optimization turned on.  Gdb shows that the variable v4 has been optimized out
and is a null pointer.  The code runs correctly with no optimization, -O, -Os
or -O1, but crashes with -O2 or -O3.

The bug appears to be present in previous versions of g++ back to 4.7.0 (the
earliest I can test as the code uses C++11).

The code runs correctly with clang++, with or without optimisation.

Correct functionality
$ g++ -std=c++11 % && ./a.out
$ echo $?
33

Incorrect
$ g++ -std=c++11 -O2 % && ./a.out
Segmentation fault (core dumped)

Correct
$ clang++ -std=c++11 -O3 exprtmpl.cpp && ./a.out
$ echo $?
33

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-linker-hash-style=gnu --enable-plugin --enable-initfini-array
--disable-libgcj --with-default-libstdcxx-abi=gcc4-compatible --with-isl
--enable-libmpx --enable-gnu-indirect-function --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.3.1 20151207 (Red Hat 5.3.1-2) (GCC)

$ clang++ -v
clang version 3.5.0 (tags/RELEASE_350/final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/5.3.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/5.3.1
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/5.3.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64


(Platform is 64-bit Fedora 22 on x86_64, using the distribution's version of
g++)


//-------------
#include <vector>

template <typename Left, typename Op, typename Right>
struct Expression {
    Expression(const Left & l, const Right & r) : left(l), right(r) {}
    float operator[](unsigned int index) const {
        return Op::apply(left[index], right[index]);
    }
    const Left & left;
    const Right & right;
};

struct plus {
    static float apply(float l, float r) { return l + r; }
};

template <typename Left, typename Right>
Expression<Left, plus, Right> operator+(const Left & l, const Right & r)
{
    return Expression<Left, plus, Right>(l, r);
}

int main()
{
    std::vector<float> v1{2, 3.4, 5};
    std::vector<float> v2{3, 5.0, 4};
    std::vector<float> v3{4, 6.0, 1};
    auto v4 = v1 + v2 + v3;
    float total = 0;
    for (auto i = 0u; i != v1.size(); ++i)
        total += v4[i];
    return total;
}
//-------------

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]