From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 86C8F3858C36; Fri, 23 Jun 2023 18:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 86C8F3858C36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687543593; bh=j4MiEwr28TypDUwNKeBLgrvf79zYDxTRoObFWIVAJOw=; h=From:To:Subject:Date:From; b=BkucUhKA7CkAAXT/tXPXTAN0KMm1/tui0ntz65pn+qUokbIVNaIMiCntUSDaZvOk4 nkQD7MQoUuakqHlojAaXcU0q04zRxnGbwGcSoJr1OslYW+iGnoArKW6+9XYJMpMrJB rFbJiy3ntEcvlVsaghJcqQmopvoiPh2AusahLGyc= From: "lennox.ho at intel dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/110381] New: Incorrect loop unrolling for structs of floating point types Date: Fri, 23 Jun 2023 18:06:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lennox.ho at intel dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110381 Bug ID: 110381 Summary: Incorrect loop unrolling for structs of floating point types Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lennox.ho at intel dot com Target Milestone: --- We believe gcc is incorrectly unrolling loops while performing summation of structs with floating point members: Here's a minimal example: ``` #include using value_type =3D double; struct FOO { value_type a =3D 0; value_type b =3D 0; value_type c =3D 0; }; value_type sum_8_foos(const FOO* foos) { value_type sum =3D 0; for (int i =3D 0; i < 8; ++i) { auto foo =3D foos[i]; sum +=3D foo.c; sum +=3D foo.b; sum +=3D foo.a; } return sum; } int main() { FOO foos[8]; foos[0].b =3D 5; std::cout << sum_8_foos(foos) << '\n'; return 0; } ``` With -O1, we get 5. With -O2, we get 10. godbolt link: https://godbolt.org/z/7cxeb3Gsv Slightly reorganising the assembly output for the loop, ``` .L2 add rdi, 48 addsd sum, QWORD PTR [rdi-48] // c addsd sum, QWORD PTR [rdi-40] // b addsd sum, QWORD PTR [rdi-32] // a addsd sum, QWORD PTR [rdi-24] // c addsd sum, QWORD PTR [rdi-16] // b addsd sum, QWORD PTR [rdi-8] // a add rax, 24 addsd sum, QWORD PTR [rax-16] // b addsd sum, QWORD PTR [rax-24] // c cmp rdi, end jne .L2 ``` There appears to be duplicate additions for the members b and c. This behaviour appears on gcc 12.1 and is still present in gcc 13.1.=