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.