public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Hongtao Liu <crazylht@gmail.com>
To: Uros Bizjak <ubizjak@gmail.com>
Cc: "H.J. Lu" <hjl.tools@gmail.com>, GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: PING^1 [PATCH] x86: Skip ENDBR when emitting direct call/jmp to local function
Date: Mon, 27 Jun 2022 13:50:24 +0800	[thread overview]
Message-ID: <CAMZc-bxCw3mDJEs14JWM5vOHvXrHWWc4SxsdxoAraHbG0MZCyw@mail.gmail.com> (raw)
In-Reply-To: <CAFULd4ax24SEe4uZWx42QUqOihEcbNwayHEW7ah9O=moGyoqtQ@mail.gmail.com>

On Tue, Jun 21, 2022 at 3:50 AM Uros Bizjak via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Mon, Jun 20, 2022 at 8:14 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Tue, May 10, 2022 at 9:25 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >
> > > Mark a function with SYMBOL_FLAG_FUNCTION_ENDBR when inserting ENDBR at
> > > function entry.  Skip the 4-byte ENDBR when emitting a direct call/jmp
> > > to a local function with ENDBR at function entry.
> > >
> > > This has been tested on Linux kernel.
> > >
> > > gcc/
> > >
> > >         PR target/102953
> > >         * config/i386/i386-features.cc
> > >         (rest_of_insert_endbr_and_patchable_area): Set
> > >         SYMBOL_FLAG_FUNCTION_ENDBR when inserting ENDBR.
> > >         * config/i386/i386.cc (ix86_print_operand): Skip the 4-byte ENDBR
> > >         when calling the local function with ENDBR at function entry.
> > >         * config/i386/i386.h (SYMBOL_FLAG_FUNCTION_ENDBR): New.
> > >         (SYMBOL_FLAG_FUNCTION_ENDBR_P): Likewise.
> > >
> > > gcc/testsuite/
> > >
> > >         PR target/102953
> > >         * gcc.target/i386/pr102953-1.c: New test.
> > >         * gcc.target/i386/pr102953-2.c: Likewise.
The patch looks good to me.
For direct call, endbr64 should not be used as a marker, right?
> > > ---
> > >  gcc/config/i386/i386-features.cc           |  2 ++
> > >  gcc/config/i386/i386.cc                    | 11 +++++++-
> > >  gcc/config/i386/i386.h                     |  5 ++++
> > >  gcc/testsuite/gcc.target/i386/pr102953-1.c | 25 ++++++++++++++++++
> > >  gcc/testsuite/gcc.target/i386/pr102953-2.c | 30 ++++++++++++++++++++++
> > >  5 files changed, 72 insertions(+), 1 deletion(-)
> > >  create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-1.c
> > >  create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-2.c
> > >
> > > diff --git a/gcc/config/i386/i386-features.cc b/gcc/config/i386/i386-features.cc
> > > index 6fe41c3c24f..3ca1131ed59 100644
> > > --- a/gcc/config/i386/i386-features.cc
> > > +++ b/gcc/config/i386/i386-features.cc
> > > @@ -1979,6 +1979,8 @@ rest_of_insert_endbr_and_patchable_area (bool need_endbr,
> > >               || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
> > >                   && DECL_DLLIMPORT_P (cfun->decl))))
> > >         {
> > > +         rtx symbol = XEXP (DECL_RTL (cfun->decl), 0);
> > > +         SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_FUNCTION_ENDBR;
> > >           if (crtl->profile && flag_fentry)
> > >             {
> > >               /* Queue ENDBR insertion to x86_function_profiler.
> > > diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
> > > index 86752a6516a..ad1de239bef 100644
> > > --- a/gcc/config/i386/i386.cc
> > > +++ b/gcc/config/i386/i386.cc
> > > @@ -13787,7 +13787,16 @@ ix86_print_operand (FILE *file, rtx x, int code)
> > >        else if (flag_pic || MACHOPIC_INDIRECT)
> > >         output_pic_addr_const (file, x, code);
> > >        else
> > > -       output_addr_const (file, x);
> > > +       {
> > > +         /* Skip ENDBR when emitting a direct call/jmp to a local
> > > +            function with ENDBR at function entry.  */
> > > +         if (code == 'P'
> > > +             && GET_CODE (x) == SYMBOL_REF
> > > +             && SYMBOL_REF_LOCAL_P (x)
> > > +             && SYMBOL_FLAG_FUNCTION_ENDBR_P (x))
> > > +           x = gen_rtx_PLUS (Pmode, x, GEN_INT (4));
> > > +         output_addr_const (file, x);
> > > +       }
> > >      }
> > >  }
> > >
> > > diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h
> > > index 363082ba47b..7a6317fea57 100644
> > > --- a/gcc/config/i386/i386.h
> > > +++ b/gcc/config/i386/i386.h
> > > @@ -2792,6 +2792,11 @@ extern GTY(()) tree ms_va_list_type_node;
> > >  #define SYMBOL_REF_STUBVAR_P(X) \
> > >         ((SYMBOL_REF_FLAGS (X) & SYMBOL_FLAG_STUBVAR) != 0)
> > >
> > > +/* Flag to mark a function with ENDBR at entry.  */
> > > +#define SYMBOL_FLAG_FUNCTION_ENDBR     (SYMBOL_FLAG_MACH_DEP << 5)
> > > +#define SYMBOL_FLAG_FUNCTION_ENDBR_P(X) \
> > > +       ((SYMBOL_REF_FLAGS (X) & SYMBOL_FLAG_FUNCTION_ENDBR) != 0)
> > > +
> > >  extern void debug_ready_dispatch (void);
> > >  extern void debug_dispatch_window (int);
> > >
> > > diff --git a/gcc/testsuite/gcc.target/i386/pr102953-1.c b/gcc/testsuite/gcc.target/i386/pr102953-1.c
> > > new file mode 100644
> > > index 00000000000..2afad391baf
> > > --- /dev/null
> > > +++ b/gcc/testsuite/gcc.target/i386/pr102953-1.c
> > > @@ -0,0 +1,25 @@
> > > +/* { dg-do compile { target { ! *-*-darwin* } } } */
> > > +/* { dg-options "-O2 -fno-pic -fplt -fcf-protection" } */
> > > +
> > > +extern int func (int);
> > > +
> > > +extern int i;
> > > +
> > > +__attribute__ ((noclone, noinline, noipa))
> > > +static int
> > > +bar (int x)
> > > +{
> > > +  if (x == 0)
> > > +    return x;
> > > +  return bar (x - 1) + func (x);
> > > +}
> > > +
> > > +void *
> > > +foo (void)
> > > +{
> > > +  i = bar (2);
> > > +  return bar;
> > > +}
> > > +
> > > +/* { dg-final { scan-assembler-times {call\t_?bar\+4\M} 2 } } */
> > > +/* { dg-final { scan-assembler-times {call\t_?func\M} 1 } } */
> > > diff --git a/gcc/testsuite/gcc.target/i386/pr102953-2.c b/gcc/testsuite/gcc.target/i386/pr102953-2.c
> > > new file mode 100644
> > > index 00000000000..5b8d517f4f2
> > > --- /dev/null
> > > +++ b/gcc/testsuite/gcc.target/i386/pr102953-2.c
> > > @@ -0,0 +1,30 @@
> > > +/* { dg-do compile { target { ! *-*-darwin* } } } */
> > > +/* { dg-options "-O2 -fno-pic -fplt -fcf-protection" } */
> > > +
> > > +static int bar (int x);
> > > +extern int func (int);
> > > +
> > > +int
> > > +foo (int i)
> > > +{
> > > +  return bar (i);
> > > +}
> > > +
> > > +void *
> > > +bar_p (void)
> > > +{
> > > +  return bar;
> > > +}
> > > +
> > > +__attribute__ ((noclone, noinline, noipa))
> > > +static int
> > > +bar (int x)
> > > +{
> > > +  if (x == 0)
> > > +    return x;
> > > +  return bar (x - 1) + func (x);
> > > +}
> > > +
> > > +/* { dg-final { scan-assembler-times {call\t_?bar\+4\M} 1 } } */
> > > +/* { dg-final { scan-assembler-times {jmp\t_?bar\+4\M} 1 } } */
> > > +/* { dg-final { scan-assembler-times {call\t_?func\M} 1 } } */
> > > --
> > > 2.35.1
> > >
> >
> > PING.
>
> CET stuff will have to be reviewed by someone else.
>
> Uros.



-- 
BR,
Hongtao

  reply	other threads:[~2022-06-27  5:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-10 16:25 H.J. Lu
2022-06-20 18:14 ` PING^1 " H.J. Lu
2022-06-20 19:49   ` Uros Bizjak
2022-06-27  5:50     ` Hongtao Liu [this message]
2022-06-27 13:03       ` H.J. Lu

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=CAMZc-bxCw3mDJEs14JWM5vOHvXrHWWc4SxsdxoAraHbG0MZCyw@mail.gmail.com \
    --to=crazylht@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hjl.tools@gmail.com \
    --cc=ubizjak@gmail.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).