Blame SOURCES/gcc11-pr101786.patch

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