public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jpegqs at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-101786-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101786

            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 declarations
> of thread_local variables:
> 
> extern thread_local constinit x;
> int f() { return x; }
> 
> 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: 

_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.

             reply	other threads:[~2021-08-05  3:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-05  3:47 jpegqs at gmail dot com [this message]
2021-08-05  3:52 ` [Bug c++/101786] " pinskia at gcc dot gnu.org
2021-08-05  3:54 ` pinskia at gcc dot gnu.org
2021-08-05 14:19 ` jakub at gcc dot gnu.org
2021-08-05 14:24 ` jakub at gcc dot gnu.org
2021-08-11 20:01 ` cvs-commit at gcc dot gnu.org
2021-08-11 20:02 ` jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-101786-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).