c++: Optimize constinit thread_local vars [PR101786]
authorJakub Jelinek <jakub@redhat.com>
Wed, 11 Aug 2021 20:00:29 +0000 (22:00 +0200)
committerJakub Jelinek <jakub@redhat.com>
Wed, 11 Aug 2021 20:00:29 +0000 (22:00 +0200)
commitee8f9ff00d79998274c967ad0c23692be9dd3ada
tree892c90afdb28cb344dd176464d8c8b0a64dfad9e
parent7e39d1a15f5276f72ee478a692445569bb646e65
c++: Optimize constinit thread_local vars [PR101786]

The paper that introduced constinit mentioned in rationale that constinit
can be used on externs as well and that it can be used to avoid the
thread_local initialization wrappers, because the standard requires that
if constinit is present on any declaration, it is also present on the
initialization declaration, even if it is in some other TU etc.

There is a small problem though, we use the tls wrappers not just if
the thread_local variable needs dynamic initialization, but also when
it has static initialization, but non-trivial destructor, as the
"dynamic initialization" in that case needs to register the destructor.

So, the following patch optimizes constinit thread_local vars only
if we can prove they will not have non-trivial destructors.  That includes
the case where we have incomplete type where we don't know and need to
conservatively assume the type will have non-trivial destructor at the
initializing declaration side.

2021-08-11  Jakub Jelinek  <jakub@redhat.com>

PR c++/101786
* decl2.c (var_defined_without_dynamic_init): Return true for
DECL_DECLARED_CONSTINIT_P with complete type and trivial destructor.

* g++.dg/cpp2a/constinit16.C: New test.
gcc/cp/decl2.c
gcc/testsuite/g++.dg/cpp2a/constinit16.C [new file with mode: 0644]