On Tue, 9 Sep 2025, H.J. Lu wrote:
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 2922d9e9839..75b7b865902 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -7559,11 +7559,14 @@ information.
> @cindex @code{tls_model} variable attribute
> @item tls_model ("@var{tls_model}")
> The @code{tls_model} attribute sets thread-local storage model
> -(@pxref{Thread-Local}) of a particular @code{__thread} variable,
> -overriding @option{-ftls-model=} command-line switch on a per-variable
> -basis.
> -The @var{tls_model} argument should be one of @code{global-dynamic},
> -@code{local-dynamic}, @code{initial-exec} or @code{local-exec}.
> +(@pxref{Thread-Local}) of a particular @code{__thread} variable on a
> +per-variable basis. The @var{tls_model} argument should be one of
> +@code{global-dynamic}, @code{local-dynamic}, @code{initial-exec} or
> +@code{local-exec}. The @code{tls_model} attribute specifies the
> +weakest @acronym{TLS} model. It overrides @option{-ftls-model=}
> +command-line switch if it is stronger than the @acronym{TLS} model
> +specified by the command-line switch. GCC may optimize @acronym{TLS}
> +access to a stronger @acronym{TLS} model.
Perhaps add (see @option{-fno-ipa-tls-access}) at the end here?
>
> Not all targets support this attribute.
>
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index d0c13d4a24e..9dd94f8c42c 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -14216,6 +14216,11 @@ option is not enabled by default otherwise.
> Reduce stack alignment on call sites if possible.
> Enabled by default.
>
> +@opindex fipa-tls-access
> +@item -fipa-tls-access
> +Perform TLS access optimization as the part of IPA whole-program
> +visibility optimization. Enabled by default.
Since it is enabled by default, it should be documented in the negative form,
-fno-ipa-tls-access (and then "Do not upgrade TLS [access/model] as part ...").
I'm a bit worried that searching for 'tls model' will skip over this paragraph.
Would suggest to change "TLS access" to "TLS model" in the description.
> +
> @opindex fipa-pta
> @item -fipa-pta
> Perform interprocedural pointer analysis and interprocedural modification
> diff --git a/gcc/ipa-visibility.cc b/gcc/ipa-visibility.cc
> index 8097a03e240..8e33ef3e24c 100644
> --- a/gcc/ipa-visibility.cc
> +++ b/gcc/ipa-visibility.cc
> @@ -883,16 +883,23 @@ function_and_variable_visibility (bool whole_program)
> tree decl = vnode->decl;
>
> /* Upgrade TLS access model based on optimized visibility status,
> - unless it was specified explicitly or no references remain. */
> + unless TLS access optimization is disabled, it was specified
> + explicitly or no references remain. */
The modified comment does not match the code (the comment says that the
attribute would prevail).
> if (DECL_THREAD_LOCAL_P (decl)
> - && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl))
> + && (flag_ipa_tls_access
> + || !lookup_attribute ("tls_model",
> + decl_attributes (decl)))
Since the option is mainly intended for toolchain debugging, please change this
to just
&& flag_ipa_tls_access
that is, do not scan the attribute list and make the option a simple on-off
switch: disabling the option disables the optimization for all TLS variables.
> && vnode->ref_list.referring.length ())
> {
> enum tls_model new_model = decl_default_tls_model (decl);
> STATIC_ASSERT (TLS_MODEL_GLOBAL_DYNAMIC <
> TLS_MODEL_LOCAL_DYNAMIC);
> STATIC_ASSERT (TLS_MODEL_INITIAL_EXEC < TLS_MODEL_LOCAL_EXEC);
> - /* We'd prefer to assert that recomputed model is not weaker than
> - what the front-end assigned, but cannot: see PR 107353. */
> + /* The recomputed model of an undefined TLS variable with
> + LOCAL_EXEC attribute is INITIAL_EXEC. Linker will
> + issue an error if the TLS model isn't supported in
> + shared library. But compiler doesn't know where the
> + TLS variable is defined nor if the generated code will
> + be linked into executable or shared library. */
Sorry, I think this comment is more confusing than helpful. As mentioned before,
decl_default_tls_model does not scan the attribute list, and may return a weaker
model than the attribute. If there was no attribute, we can expect that
recomputed model is never weaker. Can you put it in code like this, please:
old_model = decl_tls_model (decl);
if (CHECKING_P
&& !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl)))
gcc_assert (new_model >= old_model);
Thanks.
Alexander
> if (new_model >= decl_tls_model (decl))
> set_decl_tls_model (decl, new_model);
> }