public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-2861] c++: Optimize constinit thread_local vars [PR101786]
@ 2021-08-11 20:01 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2021-08-11 20:01 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ee8f9ff00d79998274c967ad0c23692be9dd3ada

commit r12-2861-gee8f9ff00d79998274c967ad0c23692be9dd3ada
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Aug 11 22:00:29 2021 +0200

    c++: Optimize constinit thread_local vars [PR101786]
    
    The paper that introduced constinit mentioned in rationale that constinit
    can be used on externs as well and that it can be used to avoid the
    thread_local initialization wrappers, because the standard requires that
    if constinit is present on any declaration, it is also present on the
    initialization declaration, even if it is in some other TU etc.
    
    There is a small problem though, we use the tls wrappers not just if
    the thread_local variable needs dynamic initialization, but also when
    it has static initialization, but non-trivial destructor, as the
    "dynamic initialization" in that case needs to register the destructor.
    
    So, the following patch optimizes constinit thread_local vars only
    if we can prove they will not have non-trivial destructors.  That includes
    the case where we have incomplete type where we don't know and need to
    conservatively assume the type will have non-trivial destructor at the
    initializing declaration side.
    
    2021-08-11  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/101786
            * decl2.c (var_defined_without_dynamic_init): Return true for
            DECL_DECLARED_CONSTINIT_P with complete type and trivial destructor.
    
            * g++.dg/cpp2a/constinit16.C: New test.

Diff:
---
 gcc/cp/decl2.c                           |  6 ++++++
 gcc/testsuite/g++.dg/cpp2a/constinit16.C | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index 9564b0d619f..ba27388df5d 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -3447,6 +3447,12 @@ set_guard (tree guard)
 static bool
 var_defined_without_dynamic_init (tree var)
 {
+  /* constinit vars are guaranteed to not have dynamic initializer,
+     but still registering the destructor counts as dynamic initialization.  */
+  if (DECL_DECLARED_CONSTINIT_P (var)
+      && COMPLETE_TYPE_P (TREE_TYPE (var))
+      && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
+    return true;
   /* If it's defined in another TU, we can't tell.  */
   if (DECL_EXTERNAL (var))
     return false;
diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit16.C b/gcc/testsuite/g++.dg/cpp2a/constinit16.C
new file mode 100644
index 00000000000..dda81d50619
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constinit16.C
@@ -0,0 +1,21 @@
+// PR c++/101786
+// { dg-do compile { target c++20 } }
+// { dg-add-options tls }
+// { dg-require-alias "" }
+// { dg-require-effective-target tls_runtime }
+// { dg-final { scan-assembler-not "_ZTH17mythreadlocalvar1" } }
+// { dg-final { scan-assembler "_ZTH17mythreadlocalvar2" } }
+// { dg-final { scan-assembler-not "_ZTH17mythreadlocalvar3" } }
+// { dg-final { scan-assembler "_ZTH17mythreadlocalvar4" } }
+
+extern thread_local constinit int mythreadlocalvar1;
+struct S;
+extern thread_local constinit S mythreadlocalvar2;
+struct T { int t; };
+extern thread_local constinit T mythreadlocalvar3;
+struct U { int u; ~U (); };
+extern thread_local constinit U mythreadlocalvar4;
+int foo () { return mythreadlocalvar1; }
+S *bar () { return &mythreadlocalvar2; }
+T *baz () { return &mythreadlocalvar3; }
+U *qux () { return &mythreadlocalvar4; }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-08-11 20:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-11 20:01 [gcc r12-2861] c++: Optimize constinit thread_local vars [PR101786] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).