From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id ADD603985031; Thu, 5 Aug 2021 03:47:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ADD603985031 From: "jpegqs at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/101786] New: P1143R2 constinit implementation is incomplete (joining with thread_local) Date: Thu, 05 Aug 2021 03:47:41 +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.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jpegqs 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 cc 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: Thu, 05 Aug 2021 03:47:41 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101786 Bug ID: 101786 Summary: P1143R2 constinit implementation is incomplete (joining with thread_local) Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jpegqs at gmail dot com CC: mpolacek at gcc dot gnu.org Target Milestone: --- The paper says: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1143r2.html > constinit can also be useful to compilers for non-initializing declaratio= ns > of thread_local variables: >=20 > extern thread_local constinit x; > int f() { return x; } >=20 > Without constinit, runtime code must be executed to perform a check of a > guard variable and conditionally initialize x each time it is used. (Other > techniques exist, but this approach is common.) If the variable is known = to > have constant initialization, this can be avoided. Let's fix the missing type for x and try: extern thread_local constinit int x; int f() { return x; } In case of compilation, GCC does not remove the TLS wrapper function as it should according to this paper: _ZTW1x: push rbp mov rbp, rsp mov eax, OFFSET FLAT:_ZTH1x test rax, rax je .L2 call _ZTH1x .L2: mov rdx, QWORD PTR fs:0 mov rax, QWORD PTR x@gottpoff[rip] add rax, rdx pop rbp ret _Z1fv: push rbp mov rbp, rsp call _ZTW1x mov eax, DWORD PTR [rax] pop rbp ret The code it should produce should look like this:=20 _Z1fv: push rbp mov rbp, rsp mov rax, QWORD PTR x@gottpoff[rip] mov eax, DWORD PTR fs:[rax] pop rbp ret What I can get now is only by replacing "thread_local constinit" with "__thread". Clang implements this feature.=