|
|
44ce1d |
c++: Optimize constinit thread_local vars [PR101786]
|
|
|
44ce1d |
|
|
|
44ce1d |
The paper that introduced constinit mentioned in rationale that constinit
|
|
|
44ce1d |
can be used on externs as well and that it can be used to avoid the
|
|
|
44ce1d |
thread_local initialization wrappers, because the standard requires that
|
|
|
44ce1d |
if constinit is present on any declaration, it is also present on the
|
|
|
44ce1d |
initialization declaration, even if it is in some other TU etc.
|
|
|
44ce1d |
|
|
|
44ce1d |
There is a small problem though, we use the tls wrappers not just if
|
|
|
44ce1d |
the thread_local variable needs dynamic initialization, but also when
|
|
|
44ce1d |
it has static initialization, but non-trivial destructor, as the
|
|
|
44ce1d |
"dynamic initialization" in that case needs to register the destructor.
|
|
|
44ce1d |
|
|
|
44ce1d |
So, the following patch optimizes constinit thread_local vars only
|
|
|
44ce1d |
if we can prove they will not have non-trivial destructors. That includes
|
|
|
44ce1d |
the case where we have incomplete type where we don't know and need to
|
|
|
44ce1d |
conservatively assume the type will have non-trivial destructor at the
|
|
|
44ce1d |
initializing declaration side.
|
|
|
44ce1d |
|
|
|
44ce1d |
2021-08-11 Jakub Jelinek <jakub@redhat.com>
|
|
|
44ce1d |
|
|
|
44ce1d |
PR c++/101786
|
|
|
44ce1d |
* decl2.c (var_defined_without_dynamic_init): Return true for
|
|
|
44ce1d |
DECL_DECLARED_CONSTINIT_P with complete type and trivial destructor.
|
|
|
44ce1d |
|
|
|
44ce1d |
* g++.dg/cpp2a/constinit16.C: New test.
|
|
|
44ce1d |
|
|
|
44ce1d |
--- gcc/cp/decl2.c
|
|
|
44ce1d |
+++ gcc/cp/decl2.c
|
|
|
44ce1d |
@@ -3447,6 +3447,12 @@ set_guard (tree guard)
|
|
|
44ce1d |
static bool
|
|
|
44ce1d |
var_defined_without_dynamic_init (tree var)
|
|
|
44ce1d |
{
|
|
|
44ce1d |
+ /* constinit vars are guaranteed to not have dynamic initializer,
|
|
|
44ce1d |
+ but still registering the destructor counts as dynamic initialization. */
|
|
|
44ce1d |
+ if (DECL_DECLARED_CONSTINIT_P (var)
|
|
|
44ce1d |
+ && COMPLETE_TYPE_P (TREE_TYPE (var))
|
|
|
44ce1d |
+ && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
|
|
|
44ce1d |
+ return true;
|
|
|
44ce1d |
/* If it's defined in another TU, we can't tell. */
|
|
|
44ce1d |
if (DECL_EXTERNAL (var))
|
|
|
44ce1d |
return false;
|
|
|
44ce1d |
--- gcc/testsuite/g++.dg/cpp2a/constinit16.C
|
|
|
44ce1d |
+++ gcc/testsuite/g++.dg/cpp2a/constinit16.C
|
|
|
44ce1d |
@@ -0,0 +1,21 @@
|
|
|
44ce1d |
+// PR c++/101786
|
|
|
44ce1d |
+// { dg-do compile { target c++20 } }
|
|
|
44ce1d |
+// { dg-add-options tls }
|
|
|
44ce1d |
+// { dg-require-alias "" }
|
|
|
44ce1d |
+// { dg-require-effective-target tls_runtime }
|
|
|
44ce1d |
+// { dg-final { scan-assembler-not "_ZTH17mythreadlocalvar1" } }
|
|
|
44ce1d |
+// { dg-final { scan-assembler "_ZTH17mythreadlocalvar2" } }
|
|
|
44ce1d |
+// { dg-final { scan-assembler-not "_ZTH17mythreadlocalvar3" } }
|
|
|
44ce1d |
+// { dg-final { scan-assembler "_ZTH17mythreadlocalvar4" } }
|
|
|
44ce1d |
+
|
|
|
44ce1d |
+extern thread_local constinit int mythreadlocalvar1;
|
|
|
44ce1d |
+struct S;
|
|
|
44ce1d |
+extern thread_local constinit S mythreadlocalvar2;
|
|
|
44ce1d |
+struct T { int t; };
|
|
|
44ce1d |
+extern thread_local constinit T mythreadlocalvar3;
|
|
|
44ce1d |
+struct U { int u; ~U (); };
|
|
|
44ce1d |
+extern thread_local constinit U mythreadlocalvar4;
|
|
|
44ce1d |
+int foo () { return mythreadlocalvar1; }
|
|
|
44ce1d |
+S *bar () { return &mythreadlocalvar2; }
|
|
|
44ce1d |
+T *baz () { return &mythreadlocalvar3; }
|
|
|
44ce1d |
+U *qux () { return &mythreadlocalvar4; }
|