Blame SOURCES/gcc11-pr101786.patch

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