From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6EED23842414; Tue, 16 Feb 2021 11:01:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6EED23842414 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/99117] [9/10/11 Regression] cannot accumulate std::valarray Date: Tue, 16 Feb 2021 11:01:47 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 10.1.0 X-Bugzilla-Keywords: needs-reduction, wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 9.4 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Feb 2021 11:01:47 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99117 --- Comment #4 from Jonathan Wakely --- The problem is that sum + e returns an expression template that holds references to its operands, so that the sum is done lazily. When that expression template result is assigned back to sum it evaluates t= he sum of each element, which is basically: struct valarray { int* __restrict__ _M_data; size_t _M_size; }; valarray sum{ new int[2]{1,1}, 2 }; valarray rhs{ new int[2]{2,2}, 2 }; int* p =3D sum._M_data; int* e1 =3D sum._M_data; int* e2 =3D rhs._M_data; for (size_t i =3D 0; i < sum._M_size; ++i, ++p) *p =3D e1[i] + e2[i]; Where p and e1 alias, but are marked with __restrict__. Maybe we need to check for such aliasing in __valarray_copy.=