* [PATCH] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] @ 2023-03-17 17:51 Jakub Jelinek 2023-03-18 12:39 ` Jason Merrill 0 siblings, 1 reply; 8+ messages in thread From: Jakub Jelinek @ 2023-03-17 17:51 UTC (permalink / raw) To: Jason Merrill; +Cc: gcc-patches 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 2023-03-17 17:51 [PATCH] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] Jakub Jelinek @ 2023-03-18 12:39 ` Jason Merrill 2023-03-18 12:54 ` Jakub Jelinek 0 siblings, 1 reply; 8+ messages in thread From: Jason Merrill @ 2023-03-18 12:39 UTC (permalink / raw) To: Jakub Jelinek; +Cc: gcc-patches On 3/17/23 13:51, Jakub Jelinek wrote: > 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. Why not handle this case in cp_finish_decl, too? That is, add DECL_THREAD_LOCAL_P to the TREE_STATIC check in > if (var_definition_p > /* With -fmerge-all-constants, gimplify_init_constructor > might add TREE_STATIC to aggregate variables. */ > && (TREE_STATIC (decl) > || (flag_merge_constants >= 2 > && AGGREGATE_TYPE_P (type)))) > { > /* If a TREE_READONLY variable needs initialization > at runtime, it is no longer readonly and we need to > avoid MEM_READONLY_P being set on RTL created for it. */ ? > 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 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 2023-03-18 12:39 ` Jason Merrill @ 2023-03-18 12:54 ` Jakub Jelinek 2023-03-18 15:09 ` [PATCH] c++, v2: " Jakub Jelinek 0 siblings, 1 reply; 8+ messages in thread From: Jakub Jelinek @ 2023-03-18 12:54 UTC (permalink / raw) To: Jason Merrill; +Cc: gcc-patches On Sat, Mar 18, 2023 at 08:39:45AM -0400, Jason Merrill wrote: > On 3/17/23 13:51, Jakub Jelinek wrote: > > 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. > > Why not handle this case in cp_finish_decl, too? That is, add > DECL_THREAD_LOCAL_P to the TREE_STATIC check in The patch is mostly about DECL_EXTERNAL cases, the others are supposedly handled by the var_definition_p code there (or at least I assumed; testcases certainly test only DECL_EXTERNAL). I guess it could be done in cp_finish_decl, maybe better next to the /* A reference will be modified here, as it is initialized. */ if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl) && TYPE_REF_P (type)) { was_readonly = 1; TREE_READONLY (decl) = 0; } spot, but we'd need to export the decl2.cc helpers for it, because not all DECL_THREAD_LOCAL_P vars need to be treated that way. if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl) && var_needs_tls_wrapper (decl) && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)) TREE_READONLY (decl) = 0; where var_needs_tls_wrapper would need to be exported from decl2.cc. Though, var_needs_tls_wrapper -> var_defined_without_dynamic_init needs DECL_NONTRIVIALLY_INITIALIZED_P/DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P, so perhaps that is accurate only closer to the end of cp_finish_decl? Jakub ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] c++, v2: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 2023-03-18 12:54 ` Jakub Jelinek @ 2023-03-18 15:09 ` Jakub Jelinek 2023-03-20 15:45 ` Jakub Jelinek 2023-03-20 19:15 ` Jason Merrill 0 siblings, 2 replies; 8+ messages in thread From: Jakub Jelinek @ 2023-03-18 15:09 UTC (permalink / raw) To: Jason Merrill; +Cc: gcc-patches On Sat, Mar 18, 2023 at 01:54:58PM +0100, Jakub Jelinek via Gcc-patches wrote: > The patch is mostly about DECL_EXTERNAL cases, the others are supposedly > handled by the var_definition_p code there (or at least I assumed; > testcases certainly test only DECL_EXTERNAL). > I guess it could be done in cp_finish_decl, maybe better next to the > /* A reference will be modified here, as it is initialized. */ > if (! DECL_EXTERNAL (decl) > && TREE_READONLY (decl) > && TYPE_REF_P (type)) > { > was_readonly = 1; > TREE_READONLY (decl) = 0; > } > spot, but we'd need to export the decl2.cc helpers for it, > because not all DECL_THREAD_LOCAL_P vars need to be treated that way. > if (VAR_P (decl) > && CP_DECL_THREAD_LOCAL_P (decl) > && var_needs_tls_wrapper (decl) > && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)) > TREE_READONLY (decl) = 0; > where var_needs_tls_wrapper would need to be exported from decl2.cc. > Though, var_needs_tls_wrapper -> var_defined_without_dynamic_init > needs > DECL_NONTRIVIALLY_INITIALIZED_P/DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P, > so perhaps that is accurate only closer to the end of cp_finish_decl? Here it is in patch form, tested so far on tls.exp: 2023-03-18 Jakub Jelinek <jakub@redhat.com> PR c++/109164 * cp-tree.h (var_needs_tls_wrapper): Declare. * decl2.cc (var_needs_tls_wrapper): No longer static. * decl.cc (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. --- gcc/cp/cp-tree.h.jj 2023-03-17 08:37:07.542937058 +0100 +++ gcc/cp/cp-tree.h 2023-03-18 16:02:46.771230806 +0100 @@ -6989,6 +6989,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); --- gcc/cp/decl2.cc.jj 2023-03-17 16:09:01.749244271 +0100 +++ gcc/cp/decl2.cc 2023-03-18 15:48:31.340642324 +0100 @@ -3623,7 +3623,7 @@ var_defined_without_dynamic_init (tree v /* 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) --- gcc/cp/decl.cc.jj 2023-03-18 15:47:32.198500421 +0100 +++ gcc/cp/decl.cc 2023-03-18 16:00:04.565584266 +0100 @@ -8706,6 +8706,18 @@ cp_finish_decl (tree decl, tree init, bo 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); --- gcc/testsuite/g++.dg/tls/thread_local13.C.jj 2023-03-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local13.C 2023-03-18 15:47:50.934228583 +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-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local13-aux.cc 2023-03-18 15:47:50.934228583 +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-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local14.C 2023-03-18 15:47:50.934228583 +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-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local14-aux.cc 2023-03-18 15:47:50.934228583 +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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] c++, v2: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 2023-03-18 15:09 ` [PATCH] c++, v2: " Jakub Jelinek @ 2023-03-20 15:45 ` Jakub Jelinek 2023-03-20 19:15 ` Jason Merrill 1 sibling, 0 replies; 8+ messages in thread From: Jakub Jelinek @ 2023-03-20 15:45 UTC (permalink / raw) To: Jason Merrill, gcc-patches On Sat, Mar 18, 2023 at 04:09:55PM +0100, Jakub Jelinek via Gcc-patches wrote: > On Sat, Mar 18, 2023 at 01:54:58PM +0100, Jakub Jelinek via Gcc-patches wrote: > > The patch is mostly about DECL_EXTERNAL cases, the others are supposedly > > handled by the var_definition_p code there (or at least I assumed; > > testcases certainly test only DECL_EXTERNAL). > > I guess it could be done in cp_finish_decl, maybe better next to the > > /* A reference will be modified here, as it is initialized. */ > > if (! DECL_EXTERNAL (decl) > > && TREE_READONLY (decl) > > && TYPE_REF_P (type)) > > { > > was_readonly = 1; > > TREE_READONLY (decl) = 0; > > } > > spot, but we'd need to export the decl2.cc helpers for it, > > because not all DECL_THREAD_LOCAL_P vars need to be treated that way. > > if (VAR_P (decl) > > && CP_DECL_THREAD_LOCAL_P (decl) > > && var_needs_tls_wrapper (decl) > > && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)) > > TREE_READONLY (decl) = 0; > > where var_needs_tls_wrapper would need to be exported from decl2.cc. > > Though, var_needs_tls_wrapper -> var_defined_without_dynamic_init > > needs > > DECL_NONTRIVIALLY_INITIALIZED_P/DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P, > > so perhaps that is accurate only closer to the end of cp_finish_decl? > > Here it is in patch form, tested so far on tls.exp: And successfully bootstrapped/regtested on x86_64-linux and i686-linux over the weekend. > 2023-03-18 Jakub Jelinek <jakub@redhat.com> > > PR c++/109164 > * cp-tree.h (var_needs_tls_wrapper): Declare. > * decl2.cc (var_needs_tls_wrapper): No longer static. > * decl.cc (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. Jakub ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] c++, v2: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 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 1 sibling, 1 reply; 8+ messages in thread From: Jason Merrill @ 2023-03-20 19:15 UTC (permalink / raw) To: Jakub Jelinek; +Cc: gcc-patches On 3/18/23 11:09, Jakub Jelinek wrote: > On Sat, Mar 18, 2023 at 01:54:58PM +0100, Jakub Jelinek via Gcc-patches wrote: >> The patch is mostly about DECL_EXTERNAL cases, the others are supposedly >> handled by the var_definition_p code there (or at least I assumed; >> testcases certainly test only DECL_EXTERNAL). >> I guess it could be done in cp_finish_decl, maybe better next to the >> /* A reference will be modified here, as it is initialized. */ >> if (! DECL_EXTERNAL (decl) >> && TREE_READONLY (decl) >> && TYPE_REF_P (type)) >> { >> was_readonly = 1; >> TREE_READONLY (decl) = 0; >> } >> spot, but we'd need to export the decl2.cc helpers for it, >> because not all DECL_THREAD_LOCAL_P vars need to be treated that way. >> if (VAR_P (decl) >> && CP_DECL_THREAD_LOCAL_P (decl) >> && var_needs_tls_wrapper (decl) >> && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)) >> TREE_READONLY (decl) = 0; >> where var_needs_tls_wrapper would need to be exported from decl2.cc. >> Though, var_needs_tls_wrapper -> var_defined_without_dynamic_init >> needs >> DECL_NONTRIVIALLY_INITIALIZED_P/DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P, >> so perhaps that is accurate only closer to the end of cp_finish_decl? > > Here it is in patch form, tested so far on tls.exp: > > 2023-03-18 Jakub Jelinek <jakub@redhat.com> > > PR c++/109164 > * cp-tree.h (var_needs_tls_wrapper): Declare. > * decl2.cc (var_needs_tls_wrapper): No longer static. > * decl.cc (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. > > --- gcc/cp/cp-tree.h.jj 2023-03-17 08:37:07.542937058 +0100 > +++ gcc/cp/cp-tree.h 2023-03-18 16:02:46.771230806 +0100 > @@ -6989,6 +6989,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); > --- gcc/cp/decl2.cc.jj 2023-03-17 16:09:01.749244271 +0100 > +++ gcc/cp/decl2.cc 2023-03-18 15:48:31.340642324 +0100 > @@ -3623,7 +3623,7 @@ var_defined_without_dynamic_init (tree v > /* 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) > --- gcc/cp/decl.cc.jj 2023-03-18 15:47:32.198500421 +0100 > +++ gcc/cp/decl.cc 2023-03-18 16:00:04.565584266 +0100 > @@ -8706,6 +8706,18 @@ cp_finish_decl (tree decl, tree init, bo > 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) Hmm, I wonder why we don't check the above line in var_needs_tls_wrapper? But the patch is OK. > + && (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); > > --- gcc/testsuite/g++.dg/tls/thread_local13.C.jj 2023-03-18 15:47:50.934228583 +0100 > +++ gcc/testsuite/g++.dg/tls/thread_local13.C 2023-03-18 15:47:50.934228583 +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-18 15:47:50.934228583 +0100 > +++ gcc/testsuite/g++.dg/tls/thread_local13-aux.cc 2023-03-18 15:47:50.934228583 +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-18 15:47:50.934228583 +0100 > +++ gcc/testsuite/g++.dg/tls/thread_local14.C 2023-03-18 15:47:50.934228583 +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-18 15:47:50.934228583 +0100 > +++ gcc/testsuite/g++.dg/tls/thread_local14-aux.cc 2023-03-18 15:47:50.934228583 +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 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] c++, v2: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 2023-03-20 19:15 ` Jason Merrill @ 2023-03-20 19:26 ` Jakub Jelinek 2023-03-20 19:28 ` Jason Merrill 0 siblings, 1 reply; 8+ messages in thread From: Jakub Jelinek @ 2023-03-20 19:26 UTC (permalink / raw) To: Jason Merrill; +Cc: gcc-patches On Mon, Mar 20, 2023 at 03:15:32PM -0400, Jason Merrill wrote: > > + else if (VAR_P (decl) > > + && CP_DECL_THREAD_LOCAL_P (decl) > > + && (!DECL_EXTERNAL (decl) || flag_extern_tls_init) > > Hmm, I wonder why we don't check the above line in var_needs_tls_wrapper? It is tested in get_tls_init_fn (one of the 2 previous callers of var_needs_tls_wrapper). No idea why it isn't in get_tls_wrapper_fn (the other caller of it). Jakub ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] c++, v2: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] 2023-03-20 19:26 ` Jakub Jelinek @ 2023-03-20 19:28 ` Jason Merrill 0 siblings, 0 replies; 8+ messages in thread From: Jason Merrill @ 2023-03-20 19:28 UTC (permalink / raw) To: Jakub Jelinek; +Cc: gcc-patches On 3/20/23 15:26, Jakub Jelinek wrote: > On Mon, Mar 20, 2023 at 03:15:32PM -0400, Jason Merrill wrote: >>> + else if (VAR_P (decl) >>> + && CP_DECL_THREAD_LOCAL_P (decl) >>> + && (!DECL_EXTERNAL (decl) || flag_extern_tls_init) >> >> Hmm, I wonder why we don't check the above line in var_needs_tls_wrapper? > > It is tested in get_tls_init_fn (one of the 2 previous callers of > var_needs_tls_wrapper). No idea why it isn't in get_tls_wrapper_fn (the > other caller of it). I suppose because we might see a definition of the variable later on, and we don't want to have previously decided to omit the wrapper because we hadn't seen it yet. Jason ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2023-03-20 19:28 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-03-17 17:51 [PATCH] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] Jakub Jelinek 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
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).