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