> -----Original Message-----
> From: H.J. Lu <hjl.to...@gmail.com>
> Sent: Friday, August 22, 2025 8:57 PM
> To: gcc-patches@gcc.gnu.org
> Cc: ubiz...@gmail.com; Liu, Hongtao <hongtao....@intel.com>;
> sly...@gcc.gnu.org
> Subject: [PATCH] Emit the TLS call after NOTE_INSN_FUNCTION_BEG
>
> For the beginning basic block:
>
> (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK) (note 2 4 26 2
> NOTE_INSN_FUNCTION_BEG)
>
> emit the TLS call after NOTE_INSN_FUNCTION_BEG.
>
Ok.
> gcc/
>
> PR target/121635
> * config/i386/i386-features.cc (ix86_emit_tls_call): Emit the
> TLS call after NOTE_INSN_FUNCTION_BEG.
>
> gcc/testsuite/
>
> PR target/121635
> * gcc.target/i386/pr121635-1a.c: New test.
> * gcc.target/i386/pr121635-1b.c: Likewise.
>
> Signed-off-by: H.J. Lu <hjl.to...@gmail.com>
> ---
> gcc/config/i386/i386-features.cc | 20 ++++++++++++++++----
> gcc/testsuite/gcc.target/i386/pr121635-1a.c | 21
> +++++++++++++++++++++ gcc/testsuite/gcc.target/i386/pr121635-1b.c | 7
> +++++++
> 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644
> gcc/testsuite/gcc.target/i386/pr121635-1a.c
> create mode 100644 gcc/testsuite/gcc.target/i386/pr121635-1b.c
>
> diff --git a/gcc/config/i386/i386-features.cc b/gcc/config/i386/i386-
> features.cc
> index 514d2a5d378..cdb2a0b34b2 100644
> --- a/gcc/config/i386/i386-features.cc
> +++ b/gcc/config/i386/i386-features.cc
> @@ -3796,14 +3796,20 @@ ix86_emit_tls_call (rtx tls_set, x86_cse_kind
> kind, basic_block bb,
> {
> if (insn == BB_END (bb))
> {
> - /* This must be a basic block with only a label:
> + /* This must be the beginning basic block:
> +
> + (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
> + (note 2 4 26 2 NOTE_INSN_FUNCTION_BEG)
> +
> + or a basic block with only a label:
>
> (code_label 78 11 77 3 14 (nil) [1 uses])
> (note 77 78 54 3 [bb 3] NOTE_INSN_BASIC_BLOCK)
>
> */
> gcc_assert (NOTE_P (insn)
> - && NOTE_KIND (insn) ==
> NOTE_INSN_BASIC_BLOCK);
> + && (NOTE_KIND (insn) ==
> NOTE_INSN_FUNCTION_BEG
> + || NOTE_KIND (insn) ==
> NOTE_INSN_BASIC_BLOCK));
> insn = NULL;
> break;
> }
> @@ -3842,8 +3848,14 @@ ix86_emit_tls_call (rtx tls_set, x86_cse_kind kind,
> basic_block bb,
> }
> else
> {
> - /* Emit the TLS call after NOTE_INSN_BASIC_BLOCK in a
> - basic block with only a label:
> + /* Emit the TLS call after NOTE_INSN_FUNCTION_BEG in the
> + beginning basic block:
> +
> + (note 4 0 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
> + (note 2 4 26 2 NOTE_INSN_FUNCTION_BEG)
> +
> + or after NOTE_INSN_BASIC_BLOCK a basic block with only
> + a label:
>
> (code_label 78 11 77 3 14 (nil) [1 uses])
> (note 77 78 54 3 [bb 3] NOTE_INSN_BASIC_BLOCK) diff --git
> a/gcc/testsuite/gcc.target/i386/pr121635-1a.c
> b/gcc/testsuite/gcc.target/i386/pr121635-1a.c
> new file mode 100644
> index 00000000000..4db7deff69a
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr121635-1a.c
> @@ -0,0 +1,21 @@
> +/* { dg-do compile { target *-*-linux* } } */
> +/* { dg-options "-O2 -fpic -fplt -mtls-dialect=gnu" } */
> +
> +extern int get_cordz_mean_interval ();
> +extern thread_local long cordz_next_sample, kIntervalIfDisabled; extern
> +bool cordz_should_profile_slow (void); inline bool cordz_should_profile
> +(void) {
> + return cordz_should_profile_slow ();
> +}
> +bool
> +cordz_should_profile_slow (void)
> +{
> + int mean_interval = get_cordz_mean_interval ();
> + if (mean_interval)
> + cordz_next_sample = kIntervalIfDisabled;
> + return cordz_next_sample || cordz_should_profile (); }
> +
> +/* { dg-final { scan-assembler-times "call\[ \t\]__tls_get_addr@PLT" 2
> +{ target { ! ia32 } } } } */
> diff --git a/gcc/testsuite/gcc.target/i386/pr121635-1b.c
> b/gcc/testsuite/gcc.target/i386/pr121635-1b.c
> new file mode 100644
> index 00000000000..4095fb556ae
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr121635-1b.c
> @@ -0,0 +1,7 @@
> +/* { dg-do compile { target *-*-linux* } } */
> +/* { dg-options "-O2 -fpic -fplt -mtls-dialect=gnu2" } */
> +
> +#include "pr121635-1a.c"
> +
> +/* { dg-final { scan-assembler-times "call\[
> +\t\]\\*cordz_next_sample@TLSCALL\\(%(?:r|e)ax\\)" 1 { target { ! ia32 }
> +} } } */
> +/* { dg-final { scan-assembler-times "call\[
> +\t\]\\*kIntervalIfDisabled@TLSCALL\\(%(?:r|e)ax\\)" 1 { target { ! ia32
> +} } } } */
> --
> 2.50.1