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