public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Palmer Dabbelt <palmer@dabbelt.com>
To: hubicka@ucw.cz
Cc: richard.guenther@gmail.com, kito.cheng@sifive.com,
	gcc-patches@gcc.gnu.org, Kito Cheng <kito.cheng@gmail.com>,
	msebor@gcc.gnu.org, jakub@gcc.gnu.org, rguenth@gcc.gnu.org,
	koen.zandberg@inria.fr, andy.chiu@sifive.com, guoren@kernel.org,
	greentime.hu@sifive.com, monk.chiang@sifive.com
Subject: Re: [PATCH] PR middle-end/88345: Honor -falign-functions=N even optimized for size.
Date: Fri, 07 Oct 2022 06:50:19 -0700 (PDT)	[thread overview]
Message-ID: <mhng-2700a562-3ddd-4845-ae29-150745ae3e89@palmer-ri-x1c9> (raw)
In-Reply-To: <Y0AiBz2OUjyX4uY0@kam.mff.cuni.cz>

On Fri, 07 Oct 2022 05:56:39 PDT (-0700), hubicka@ucw.cz wrote:
>> On Fri, Oct 7, 2022 at 6:04 AM Kito Cheng <kito.cheng@sifive.com> wrote:
>> >
>> > From: Monk Chiang <monk.chiang@sifive.com>
>> >
>> > Currnetly setting of -falign-functions=N will be ignored if the function
>> > is optimized for size or marked as cold function.
>> >
>> > However function alignment requirement is needed even optimized for
>> > size in some situations, RISC-V target is an example, RISC-V kernel implement
>> > patchable function that require function must be align to at least 4 bytes for
>> > atomicly patch the function prologue.
>> >
>> > Here is 4 way to fix that:
>> > 1. Fix -falign-functions=N, let it work as expect on -Os and all cold
>> > functions, which is this patch.
>> > 2. Force align to 4 byte if -fpatchable-function-entry is given by adjust
>> > RISC-V's FUNCTION_BOUNDARY.
>> > 3. Adjust RISC-V's FUNCTION_BOUNDARY to let it honor to -falign-functions=N.
>> > 4. Adding a -malign-functions=N for RISC-V...which x86 already deprecated that.
>> >
>> > And this patch is the first approach.
>>
>> The behavior changed with r0-42853-g194734e9e5501f but documentation
>> wasn't changed to reflect that -falign-functions=N is now only a hint.
>>
>> I'm not sure in what circumstances users are expected to use -falign-functions
>> and whether if it is for ABI reasons (as in this case) we should instead have
>> a -malign-functions or other magic.
>>
>> Honza - any comment on your change?
>
> This was done a long time ago and -falign-functions was/is about
> performance.
>
> The basic idea of the patch was to use minimal required alignment for
> -Os and cold functions while use -falign-functions for functions
> optimized for speed.  This is not different what we do for other
> optimization options.

I'd also noticed last night that -falign-functions doesn't override 
__attribute__((align(N))), but got too tired to write up the patch.  I 
wasn't sure it was the desired behavior at the time, but sounds like it 
is?  I just send a doc patch to mention that.

> i386 targets define quite large alignments (especially for older ones
> that needed function entry to be separated by given number of
> instructions from cache page boundary) so enabling it for all functions
> unconditionally is expensive (in code size).
>
> We have FUNCTION_BOUNDARY to specify minimal function alignment required
> by the ABI.  So I think if live patches requires bigger alignment, I
> would go with adjusting FUNCTION_BOUNDARY with -flive-patching.
> Alternatively we could introduce -malign-all-function or
> -falign-all-functions to adjust minimal alignment if this is specific to
> a particular implementation of live patching in the kernel.
>
> Honza
>>
>> Thanks,
>> Richard.
>>
>> > gcc/ChangeLog:
>> >
>> >         PR middle-end/88345
>> >         * varasm.cc (assemble_start_function): Adjust function align
>> >         even optimized for size.
>> >         * doc/invoke.texi (Os): Remove -falign-functions= from the exclusion
>> >         list of -Os.
>> >
>> > gcc/testsuite/ChangeLog:
>> >
>> >         PR middle-end/88345
>> >         * gcc.target/i386/pr88345-1.c: New.
>> >         * gcc.target/i386/pr88345-2.c: Ditto.
>> >         * gcc.target/riscv/pr88345-1.c: Ditto.
>> >         * gcc.target/riscv/pr88345-2.c: Ditto.
>> > ---
>> >  gcc/doc/invoke.texi                        | 2 +-
>> >  gcc/testsuite/gcc.target/i386/pr88345-1.c  | 5 +++++
>> >  gcc/testsuite/gcc.target/i386/pr88345-2.c  | 5 +++++
>> >  gcc/testsuite/gcc.target/riscv/pr88345-1.c | 5 +++++
>> >  gcc/testsuite/gcc.target/riscv/pr88345-2.c | 5 +++++
>> >  gcc/varasm.cc                              | 3 +--
>> >  6 files changed, 22 insertions(+), 3 deletions(-)
>> >  create mode 100644 gcc/testsuite/gcc.target/i386/pr88345-1.c
>> >  create mode 100644 gcc/testsuite/gcc.target/i386/pr88345-2.c
>> >  create mode 100644 gcc/testsuite/gcc.target/riscv/pr88345-1.c
>> >  create mode 100644 gcc/testsuite/gcc.target/riscv/pr88345-2.c
>> >
>> > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
>> > index a2b0b9636f0..acf98c68825 100644
>> > --- a/gcc/doc/invoke.texi
>> > +++ b/gcc/doc/invoke.texi
>> > @@ -11381,7 +11381,7 @@ results.  This is the default.
>> >  Optimize for size.  @option{-Os} enables all @option{-O2} optimizations
>> >  except those that often increase code size:
>> >
>> > -@gccoptlist{-falign-functions  -falign-jumps @gol
>> > +@gccoptlist{-falign-jumps @gol
>> >  -falign-labels  -falign-loops @gol
>> >  -fprefetch-loop-arrays  -freorder-blocks-algorithm=stc}
>> >
>> > diff --git a/gcc/testsuite/gcc.target/i386/pr88345-1.c b/gcc/testsuite/gcc.target/i386/pr88345-1.c
>> > new file mode 100644
>> > index 00000000000..226bb9ffc82
>> > --- /dev/null
>> > +++ b/gcc/testsuite/gcc.target/i386/pr88345-1.c
>> > @@ -0,0 +1,5 @@
>> > +/* { dg-do compile } */
>> > +/* { dg-options "-falign-functions=128" } */
>> > +/* { dg-final { scan-assembler-times "\.p2align\t7" 1 } } */
>> > +
>> > +__attribute__((__cold__)) void a() {}
>> > diff --git a/gcc/testsuite/gcc.target/i386/pr88345-2.c b/gcc/testsuite/gcc.target/i386/pr88345-2.c
>> > new file mode 100644
>> > index 00000000000..a7fc3b162dd
>> > --- /dev/null
>> > +++ b/gcc/testsuite/gcc.target/i386/pr88345-2.c
>> > @@ -0,0 +1,5 @@
>> > +/* { dg-do compile } */
>> > +/* { dg-options "-falign-functions=128 -Os" } */
>> > +/* { dg-final { scan-assembler-times "\.p2align\t7" 1 } } */
>> > +
>> > +void a() {}
>> > diff --git a/gcc/testsuite/gcc.target/riscv/pr88345-1.c b/gcc/testsuite/gcc.target/riscv/pr88345-1.c
>> > new file mode 100644
>> > index 00000000000..7d5afe683eb
>> > --- /dev/null
>> > +++ b/gcc/testsuite/gcc.target/riscv/pr88345-1.c
>> > @@ -0,0 +1,5 @@
>> > +/* { dg-do compile } */
>> > +/* { dg-options "-falign-functions=128" } */
>> > +/* { dg-final { scan-assembler-times "\.align\t7" 1 } } */
>> > +
>> > +__attribute__((__cold__)) void a() {}
>> > diff --git a/gcc/testsuite/gcc.target/riscv/pr88345-2.c b/gcc/testsuite/gcc.target/riscv/pr88345-2.c
>> > new file mode 100644
>> > index 00000000000..d4fc89d86d8
>> > --- /dev/null
>> > +++ b/gcc/testsuite/gcc.target/riscv/pr88345-2.c
>> > @@ -0,0 +1,5 @@
>> > +/* { dg-do compile } */
>> > +/* { dg-options "-falign-functions=128 -Os" } */
>> > +/* { dg-final { scan-assembler-times "\.align\t7" 1 } } */
>> > +
>> > +void a() {}
>> > diff --git a/gcc/varasm.cc b/gcc/varasm.cc
>> > index 423f3f91af8..4001648b214 100644
>> > --- a/gcc/varasm.cc
>> > +++ b/gcc/varasm.cc
>> > @@ -1923,8 +1923,7 @@ assemble_start_function (tree decl, const char *fnname)
>> >       Note that we still need to align to DECL_ALIGN, as above,
>> >       because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all.  */
>> >    if (! DECL_USER_ALIGN (decl)
>> > -      && align_functions.levels[0].log > align
>> > -      && optimize_function_for_speed_p (cfun))
>> > +      && align_functions.levels[0].log > align)
>> >      {
>> >  #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
>> >        int align_log = align_functions.levels[0].log;
>> > --
>> > 2.37.2
>> >

      reply	other threads:[~2022-10-07 13:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-07  4:03 Kito Cheng
2022-10-07  4:52 ` Palmer Dabbelt
2022-10-07  6:32 ` Richard Biener
2022-10-07 12:56   ` Jan Hubicka
2022-10-07 13:50     ` Palmer Dabbelt [this message]

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=mhng-2700a562-3ddd-4845-ae29-150745ae3e89@palmer-ri-x1c9 \
    --to=palmer@dabbelt.com \
    --cc=andy.chiu@sifive.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=greentime.hu@sifive.com \
    --cc=guoren@kernel.org \
    --cc=hubicka@ucw.cz \
    --cc=jakub@gcc.gnu.org \
    --cc=kito.cheng@gmail.com \
    --cc=kito.cheng@sifive.com \
    --cc=koen.zandberg@inria.fr \
    --cc=monk.chiang@sifive.com \
    --cc=msebor@gcc.gnu.org \
    --cc=rguenth@gcc.gnu.org \
    --cc=richard.guenther@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).