From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164]
Date: Fri, 17 Mar 2023 18:51:36 +0100 [thread overview]
Message-ID: <ZBSoqOLhNMhm4YTo@tucnak> (raw)
Hi!
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.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2023-03-17 Jakub Jelinek <jakub@redhat.com>
PR c++/109164
* decl2.cc (get_tls_wrapper_fn): Clear TREE_READONLY on variables for
which a TLS wrapper is added.
* 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.
--- gcc/cp/decl2.cc.jj 2023-03-07 21:20:31.800491531 +0100
+++ gcc/cp/decl2.cc 2023-03-17 12:20:11.960678291 +0100
@@ -3773,6 +3773,12 @@ get_tls_wrapper_fn (tree var)
DECL_BEFRIENDING_CLASSES (fn) = var;
set_global_binding (fn);
+
+ /* The variable now needs dynamic initialization by the wrapper
+ function, we don't want to hoist accesses to it before the
+ wrapper. */
+ if (TREE_READONLY (var))
+ TREE_READONLY (var) = 0;
}
return fn;
}
--- gcc/testsuite/g++.dg/tls/thread_local13.C.jj 2023-03-17 12:28:24.692427351 +0100
+++ gcc/testsuite/g++.dg/tls/thread_local13.C 2023-03-17 12:30:34.505519746 +0100
@@ -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;
+ }
+}
--- gcc/testsuite/g++.dg/tls/thread_local13-aux.cc.jj 2023-03-17 12:28:28.721368058 +0100
+++ gcc/testsuite/g++.dg/tls/thread_local13-aux.cc 2023-03-17 12:37:53.952070861 +0100
@@ -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 ();
+}
--- gcc/testsuite/g++.dg/tls/thread_local14.C.jj 2023-03-17 12:35:48.951905245 +0100
+++ gcc/testsuite/g++.dg/tls/thread_local14.C 2023-03-17 12:49:03.456249628 +0100
@@ -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;
+ }
+}
--- gcc/testsuite/g++.dg/tls/thread_local14-aux.cc.jj 2023-03-17 12:36:58.724881322 +0100
+++ gcc/testsuite/g++.dg/tls/thread_local14-aux.cc 2023-03-17 12:48:53.914389421 +0100
@@ -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 ();
+}
Jakub
next reply other threads:[~2023-03-17 17:51 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-17 17:51 Jakub Jelinek [this message]
2023-03-18 12:39 ` Jason Merrill
2023-03-18 12:54 ` Jakub Jelinek
2023-03-18 15:09 ` [PATCH] c++, v2: " Jakub Jelinek
2023-03-20 15:45 ` Jakub Jelinek
2023-03-20 19:15 ` Jason Merrill
2023-03-20 19:26 ` Jakub Jelinek
2023-03-20 19:28 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZBSoqOLhNMhm4YTo@tucnak \
--to=jakub@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).