Blame SOURCES/gcc11-pr101786.patch

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