Package: clang-6.0 Version: 1:6.0.1-5 Severity: normal Dear Maintainer,
When compiling with -O2 the code below, resulting loop is wrongly optimized leading to plainly wrong results (not an approximation), it seems that elements in std::vector are not accessed in the right order. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #include <iostream> #include <vector> class Foo { private: std::vector<double> _a; std::vector<double> _d; public: Foo(const std::vector<double> &x, const std::vector<double> &y) : _a(x.size()), _d(x.size()) { for (unsigned int i = 0; i < x.size() - 1; i++) { _a[i] = x[i + 1] - x[i]; _d[i] = (y[i + 1] - y[i]) / _a[i]; } } const std::vector<double> &a() const noexcept { return _a; } }; int main() { // Read input file std::vector<double> x, y; while (std::cin) { double xi, yi; if (std::cin >> xi >> yi) { x.push_back(xi); y.push_back(yi); } } // Create Foo instance Foo foo(x, y); // Print computed data for (auto a : foo.a()) std::cout << a << '\n'; return 0; } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% When executing the following commands (assuming that file bug.cpp contains code above) $> clang++-6.0 -std=c++11 -O2 -o bug bug.cpp $> paste <(seq 1 5) <(seq 1 5) | ./bug the expected result is: 1 1 1 1 0 But one gets: 1 2 2 0 0 Nevertheless, the expected result is obtained with -O1. Best regards, Juan -- System Information: Debian Release: buster/sid APT prefers testing APT policy: (990, 'testing'), (500, 'stable-updates'), (500, 'unstable'), (500, 'stable'), (500, 'oldstable') Architecture: amd64 (x86_64) Kernel: Linux 4.17.0-3-amd64 (SMP w/4 CPU cores) Locale: LANG=en_GB.utf8, LC_CTYPE=en_GB.UTF-8 (charmap=UTF-8), LANGUAGE=en_GB:en (charmap=UTF-8) Shell: /bin/sh linked to /bin/dash Init: systemd (via /run/systemd/system) LSM: AppArmor: enabled Versions of packages clang-6.0 depends on: ii binutils 2.31.1-4 ii libc6 2.27-5 ii libc6-dev 2.27-5 ii libclang-common-6.0-dev 1:6.0.1-5 ii libclang1-6.0 1:6.0.1-5 ii libgcc-8-dev 8.2.0-4 ii libgcc1 1:8.2.0-4 ii libjsoncpp1 1.7.4-3 ii libllvm6.0 1:6.0.1-5 ii libobjc-8-dev 8.2.0-4 ii libstdc++-8-dev 8.2.0-4 ii libstdc++6 8.2.0-4 Versions of packages clang-6.0 recommends: ii libomp-dev 6.0.1-1 ii llvm-6.0-dev 1:6.0.1-5 ii python 2.7.15-3 Versions of packages clang-6.0 suggests: pn clang-6.0-doc <none> pn gnustep <none> pn gnustep-devel <none> -- no debconf information