From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3D7BB387087A; Sat, 13 Feb 2021 21:55:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D7BB387087A From: "barry.revzin at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99091] New: constexpr variable evaluated at runtime Date: Sat, 13 Feb 2021 21:55:26 +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: 10.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: barry.revzin at gmail 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 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: Sat, 13 Feb 2021 21:55:26 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99091 Bug ID: 99091 Summary: constexpr variable evaluated at runtime Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: barry.revzin at gmail dot com Target Milestone: --- Consider the following (link to compiler explorer: https://godbolt.org/z/jWsxsK) #include static constexpr auto doy =3D []{ std::array month_offset{}; for (int m=3D1; m<=3D12;++m) { month_offset[m] =3D (153*(m > 2 ? m-3 : m+9) + 2)/5; } return month_offset; }(); auto foo(int m) -> int { #ifdef LOCAL constexpr auto doy =3D []{ std::array month_offset{}; for (int m=3D1; m<=3D12;++m) { month_offset[m] =3D (153*(m > 2 ? m-3 : m+9) + 2)/5; } return month_offset; }(); #endif return doy[m]; } This is a fragment of code that does some calculation to figure out the dat= e, which isn't super important. If LOCAL is not defined (i.e. we declare the a= rray as a namespace-scope constexpr), the -O3 codegen of 'foo' is: foo(int): movsx rdi, edi mov eax, DWORD PTR doy[0+rdi*4] ret However, if we want to move the declaration of doy to be more local to the calculation and compile with -DLOCAL, the codegen instead is (on -O3): foo(int): pxor xmm0, xmm0 mov rax, QWORD PTR .LC0[rip] movsx rdi, edi mov DWORD PTR [rsp-24], 275 movaps XMMWORD PTR [rsp-72], xmm0 movdqa xmm0, XMMWORD PTR .LC1[rip] mov QWORD PTR [rsp-68], rax movaps XMMWORD PTR [rsp-56], xmm0 movdqa xmm0, XMMWORD PTR .LC2[rip] movaps XMMWORD PTR [rsp-40], xmm0 mov eax, DWORD PTR [rsp-72+rdi*4] ret This can be alleviated by marked the local variable doy as being static constexpr. Except that this prevents 'foo' from being a constexpr function (can't have static variables).=20 This just seems like bad codegen, I'm not sure there's any reason that the compiler needs to do work here. It would be nice if the codegen with a local constexpr variable were the same as if it were a non-local constexpr variab= le.=