From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp1-g21.free.fr (smtp1-g21.free.fr [IPv6:2a01:e0c:1:1599::10]) by sourceware.org (Postfix) with ESMTPS id 8FE75383F871 for ; Tue, 16 Jun 2020 11:56:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8FE75383F871 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=math.univ-lyon1.fr Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=denis@math.univ-lyon1.fr Received: from [IPv6:2a01:e34:ec15:6750:64ec:ac75:a3bb:9f78] (unknown [IPv6:2a01:e34:ec15:6750:64ec:ac75:a3bb:9f78]) (Authenticated sender: denis.roland) by smtp1-g21.free.fr (Postfix) with ESMTPSA id B9F61B005A3 for ; Tue, 16 Jun 2020 13:56:55 +0200 (CEST) From: Roland Denis Subject: Kind of undefined behavior (or bug?) with GCC 10.1 To: gcc-help@gcc.gnu.org Message-ID: Date: Tue, 16 Jun 2020 13:57:10 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------D76733CDEA751BF852FC4576" Content-Language: en-US X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, KAM_NUMSUBJECT, SPF_HELO_NONE, SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Jun 2020 11:57:00 -0000 This is a multi-part message in MIME format. --------------D76733CDEA751BF852FC4576 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi, I encounter a strange bug while compiling a project with GCC 10.1 and -O3 that looks like an undefined behavior but I don't found where. Result is correct with previous version of GCC, with LLVM & Intel. I have attached a snippet (also on godbolt https://gcc.godbolt.org/z/EC4WYZ) that I tried to minimize but remaining lines seems to be important to reproduce the erroneous result. Uncommenting any of the indicated lines solve the issue. To explain the code a bit: from a 3D Point with integer coordinates, I convert it into another coordinate system (using uSpel that returns a Cell). I use a dimension iterator q (DirIterator, that doesn't look like an iterator anymore) and the uIncident function to visit neighbors of the cell. From the point (0, 0, 0), I should get the cell c=(1, 1, 1) and the incident cell f1=(0, 1, 1). I additionally copy f1 into f2 (to increase the weirdness of the bug). Here are the result with and without -O3 flag: $ g++ bug_minimal.cpp && ./a.out q.dir = 0 ; f1 = 0 1 1  ; f2 = 0 1 1 $ g++ -O3 bug_minimal.cpp && ./a.out q.dir = 0 ; f1 = 1 1 1  ; f2 = 0 1 1 The bug is in the first coordinate of f1 that should be 0. As I explain above, slightly modifying the code implies the right result, e.g.: - declaring default copy or move constructor for Point - using std::array in place of Point - using Point in place of Cell - passing Cell by reference instead of copy in uIncident - constructing Cell without uSpel - specifying the incidence direction by hand Am I missing something important or does that look like a GCC bug ? Regards, PS: result is correct with trunk version of GCC in godbolt, does that mean it is an already solved bug? -- Roland DENIS --------------D76733CDEA751BF852FC4576 Content-Type: text/x-c++src; charset=UTF-8; name="bug_minimal.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="bug_minimal.cpp" #include #include struct Point { std::array array; Point(int x, int y, int z) : array{x, y, z} {} Point(const Point & other) : array{other.array} {} // OK if commented //Point(const Point &) = default; // OK //Point(Point && other) = default; // OK int operator[] (std::size_t i) const { return array[i]; } int& operator[] (std::size_t i) { return array[i]; } }; //using Point = std::array; // OK struct Cell { Point point; Cell(Point const& pt) : point(pt) {} int operator[] (std::size_t i) const { return point[i]; } int& operator[] (std::size_t i) { return point[i]; } }; //using Cell = Point; // OK std::ostream & operator<< (std::ostream & out, Cell const& object) //std::ostream & operator<< (std::ostream & out, Cell object) // Fails with f2 too { for ( std::size_t i = 0; i < 3; ++i ) out << object[ i ] << " "; return out; } struct DirIterator { std::size_t dir; Cell cell; DirIterator(Cell c) : dir(0), cell(c) { find(); // OK if commented } void find() { //while (dir < 3) // Fails with f2 too while (dir < 3 && (cell[dir] % 2) == 0) ++dir; } }; Cell uIncident(Cell c, std::size_t k) //Cell uIncident(Cell& c, std::size_t k) // OK { --c[k]; return c; } Cell uSpel(Point p) { for (std::size_t i = 0; i < 3; ++i) p[i] += p[i] + 1; return Cell(p); } int main () { Cell c = uSpel(Point{0, 0, 0}); // Fails //Cell c( Point(1, 1, 1) ); // OK auto q = DirIterator( c ); Cell f1 = uIncident( c, q.dir ); // Fails //Cell f1 = uIncident( c, 0 ); // OK Cell f2 = f1; // f2 is the right cell that f1 should be std::cout << "q.dir = " << q.dir << " ; f1 = " << f1 << " ; f2 = " << f2 << std::endl; //std::cout << "q.dir = " << q.dir << " ; f1 = " << f1[0] << " " << f1[1] << " " << f1[2] << " ; f2 = " << f2[0] << " " << f2[1] << " " << f2[2] << std::endl; // OK return 0; } --------------D76733CDEA751BF852FC4576--