public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-10728] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164]
@ 2023-05-02 20:16 Jakub Jelinek
0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2023-05-02 20:16 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:6a4942ac236ce46499073daed7ebb8fa272aa7fa
commit r11-10728-g6a4942ac236ce46499073daed7ebb8fa272aa7fa
Author: Jakub Jelinek <jakub@redhat.com>
Date: Mon Mar 20 20:29:47 2023 +0100
c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164]
The following two testcases are miscompiled, because we keep TREE_READONLY
on the vars even when they are (possibly) dynamically initialized by a TLS
wrapper function. Normally cp_finish_decl drops TREE_READONLY from vars
which need dynamic initialization, but for TLS we do this kind of
initialization upon every access to those variables. Keeping them
TREE_READONLY means e.g. PRE can hoist loads from those before loops
which contain the TLS wrapper calls, so we can access the TLS variables
before they are initialized.
2023-03-20 Jakub Jelinek <jakub@redhat.com>
PR c++/109164
* cp-tree.h (var_needs_tls_wrapper): Declare.
* decl2.c (var_needs_tls_wrapper): No longer static.
* decl.c (cp_finish_decl): Clear TREE_READONLY on TLS variables
for which a TLS wrapper will be needed.
* g++.dg/tls/thread_local13.C: New test.
* g++.dg/tls/thread_local13-aux.cc: New file.
* g++.dg/tls/thread_local14.C: New test.
* g++.dg/tls/thread_local14-aux.cc: New file.
(cherry picked from commit 0a846340b99675d57fc2f2923a0412134eed09d3)
Diff:
---
gcc/cp/cp-tree.h | 1 +
gcc/cp/decl.c | 12 +++++++++
gcc/cp/decl2.c | 2 +-
gcc/testsuite/g++.dg/tls/thread_local13-aux.cc | 35 ++++++++++++++++++++++++++
gcc/testsuite/g++.dg/tls/thread_local13.C | 21 ++++++++++++++++
gcc/testsuite/g++.dg/tls/thread_local14-aux.cc | 26 +++++++++++++++++++
gcc/testsuite/g++.dg/tls/thread_local14.C | 19 ++++++++++++++
7 files changed, 115 insertions(+), 1 deletion(-)
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index fded2527e5e..620da867a6b 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6789,6 +6789,7 @@ extern void copy_linkage (tree, tree);
extern tree get_guard (tree);
extern tree get_guard_cond (tree, bool);
extern tree set_guard (tree);
+extern bool var_needs_tls_wrapper (tree);
extern tree maybe_get_tls_wrapper_call (tree);
extern void mark_needed (tree);
extern bool decl_needed_p (tree);
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 4c7c1374be3..2110e3142b4 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -8268,6 +8268,18 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
if (!decl_maybe_constant_destruction (decl, type))
TREE_READONLY (decl) = 0;
}
+ else if (VAR_P (decl)
+ && CP_DECL_THREAD_LOCAL_P (decl)
+ && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)
+ && (was_readonly || TREE_READONLY (decl))
+ && var_needs_tls_wrapper (decl))
+ {
+ /* TLS variables need dynamic initialization by the TLS wrapper
+ function, we don't want to hoist accesses to it before the
+ wrapper. */
+ was_readonly = 0;
+ TREE_READONLY (decl) = 0;
+ }
make_rtl_for_nonlocal_decl (decl, init, asmspec);
diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index c00508103e9..3ea37e0831f 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -3449,7 +3449,7 @@ var_defined_without_dynamic_init (tree var)
/* Returns true iff VAR is a variable that needs uses to be
wrapped for possible dynamic initialization. */
-static bool
+bool
var_needs_tls_wrapper (tree var)
{
return (!error_operand_p (var)
diff --git a/gcc/testsuite/g++.dg/tls/thread_local13-aux.cc b/gcc/testsuite/g++.dg/tls/thread_local13-aux.cc
new file mode 100644
index 00000000000..691f308cff1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tls/thread_local13-aux.cc
@@ -0,0 +1,35 @@
+// PR c++/109164
+
+struct S { virtual void foo (); int s; };
+extern bool baz ();
+
+void
+S::foo ()
+{
+ if (s != 42)
+ __builtin_abort ();
+}
+
+S s;
+
+S &
+qux ()
+{
+ s.s = 42;
+ return s;
+}
+
+thread_local S &t = qux ();
+
+bool
+bar ()
+{
+ return false;
+}
+
+int
+main ()
+{
+ if (baz ())
+ __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/tls/thread_local13.C b/gcc/testsuite/g++.dg/tls/thread_local13.C
new file mode 100644
index 00000000000..b424b9f8acf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tls/thread_local13.C
@@ -0,0 +1,21 @@
+// PR c++/109164
+// { dg-do run { target c++11 } }
+// { dg-options "-O2" }
+// { dg-add-options tls }
+// { dg-require-effective-target tls_runtime }
+// { dg-additional-sources "thread_local13-aux.cc" }
+
+struct S { virtual void foo (); int s; };
+extern thread_local S &t;
+bool bar ();
+
+bool
+baz ()
+{
+ while (1)
+ {
+ t.foo ();
+ if (!bar ())
+ return false;
+ }
+}
diff --git a/gcc/testsuite/g++.dg/tls/thread_local14-aux.cc b/gcc/testsuite/g++.dg/tls/thread_local14-aux.cc
new file mode 100644
index 00000000000..e8f2a243fc1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tls/thread_local14-aux.cc
@@ -0,0 +1,26 @@
+// PR c++/109164
+
+extern bool baz ();
+
+int
+qux ()
+{
+ return 42;
+}
+
+extern thread_local const int t = qux ();
+
+bool
+bar (int x)
+{
+ if (x != 42)
+ __builtin_abort ();
+ return false;
+}
+
+int
+main ()
+{
+ if (baz ())
+ __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/tls/thread_local14.C b/gcc/testsuite/g++.dg/tls/thread_local14.C
new file mode 100644
index 00000000000..a7402d0d631
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tls/thread_local14.C
@@ -0,0 +1,19 @@
+// PR c++/109164
+// { dg-do run { target c++11 } }
+// { dg-options "-O2" }
+// { dg-add-options tls }
+// { dg-require-effective-target tls_runtime }
+// { dg-additional-sources "thread_local14-aux.cc" }
+
+extern thread_local const int t;
+bool bar (int);
+
+bool
+baz ()
+{
+ while (1)
+ {
+ if (!bar (t))
+ return false;
+ }
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-05-02 20:16 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-02 20:16 [gcc r11-10728] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 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).