https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101786
--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>: https://gcc.gnu.org/g:ee8f9ff00d79998274c967ad0c23692be9dd3ada commit r12-2861-gee8f9ff00d79998274c967ad0c23692be9dd3ada Author: Jakub Jelinek <ja...@redhat.com> Date: Wed Aug 11 22:00:29 2021 +0200 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 <ja...@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.