public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: "H.J. Lu" <hjl.tools@gmail.com>
Cc: Martin Sebor <msebor@gmail.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>,
	 Uros Bizjak <ubizjak@gmail.com>,
	Jakub Jelinek <jakub@redhat.com>,
	 Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Subject: Re: [PATCH v4 2/2] x86: Add general_regs_only function attribute
Date: Thu, 22 Apr 2021 10:27:11 +0200	[thread overview]
Message-ID: <CAFiYyc1N=BHcLYwuxDyGKcH-JQMcrBpY0uLYbo_Fn9rZ5JCNJQ@mail.gmail.com> (raw)
In-Reply-To: <CAMe9rOr2nzQw3GbJ2VMWOgOqcrkTy-gE5iymMCyOoGqXEyXF_g@mail.gmail.com>

On Thu, Apr 22, 2021 at 3:01 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Wed, Apr 21, 2021 at 4:24 PM Martin Sebor <msebor@gmail.com> wrote:
> >
> > On 4/21/21 2:58 PM, H.J. Lu wrote:
> > > On Wed, Apr 21, 2021 at 10:09 AM Martin Sebor <msebor@gmail.com> wrote:
> > >>
> > >> On 4/14/21 4:39 PM, H.J. Lu wrote:
> > >>> commit 87c753ac241f25d222d46ba1ac66ceba89d6a200
> > >>> Author: H.J. Lu <hjl.tools@gmail.com>
> > >>> Date:   Fri Aug 21 09:42:49 2020 -0700
> > >>>
> > >>>       x86: Add target("general-regs-only") function attribute
> > >>>
> > >>> is incomplete since it is impossible to call integer intrinsics from
> > >>> a function with general-regs-only target attribute.
> > >>>
> > >>> 1. Add general_regs_only function attribute to inform the compiler that
> > >>> functions use only general purpose registers.  When making inlining
> > >>> decisions on such functions, non-GPR compiler options are excluded.
> > >>> 2. Add general_regs_only attribute to x86 intrinsics which use only
> > >>> general purpose registers.
> > >>>
> > >> ...
> > >>> --- a/gcc/doc/extend.texi
> > >>> +++ b/gcc/doc/extend.texi
> > >>> @@ -7066,6 +7066,11 @@ On x86 targets, the @code{fentry_section} attribute sets the name
> > >>>    of the section to record function entry instrumentation calls in when
> > >>>    enabled with @option{-pg -mrecord-mcount}
> > >>>
> > >>> +@item general_regs_only
> > >>> +@cindex @code{general_regs_only} function attribute, x86
> > >>> +The @code{general_regs_only} attribute on functions is used to
> > >>> +inform the compiler that functions use only general purpose registers.
> > >>
> > >> I'll just reiterate basically the same comment as before: it's not
> > >> clear from the very brief description above what the requirements
> > >> are for using the attribute.  I'm guessing it can be applied to
> > >> any function (inline or otherwise) but only has any effect when
> > >> the function is actually inlined and otherwise doesn't constrain
> > >> what the function can do.  (Whatever the constraints are, I think
> > >> the manual should spell them out, and likewise for its effects.)
> > >
> > > That is correct.
> > >
> > >> Similarly it's not clear what should be expected when the function
> > >> does use some other register.  Ideally, I think GCC would check and
> > >> issue a nice error message whether or not the function is inlined
> > >> or called.  I suspect that might only be possible for inline
> > >> functions that are actually called and for which the back end must
> > >> emit code.
> > >
> > > This is what GCC does today:
> > >
> > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99744
> >
> > Yes, that's the rather obscure error I think I commented on before
> > and suggested should be improved.  Based on r99744-3.c I don't think
> > this has changed in the improved patch.
>
> My goal is to fix the inline failures, not to improve the compiler error
> message.
>
> > >
> > >> Other than that, I'd suggest to improve the phrasing a bit:
> > >>
> > >>     The @code{general_regs_only} function attribute indicates that
> > >>     the function uses only general purpose registers... [text
> > >>     explaining constraints and errors follows].
> > >>
> > >> Martin
> > >
> > > How about this
> > >
> > > @item general_regs_only
> > > @cindex @code{general_regs_only} function attribute, x86
> > > The @code{general_regs_only} attribute on functions is used to inform
> > > the compiler that functions use only general purpose registers.  It
> > > can be used together with the @code{always_inline} attribute to avoid
> > > inlining failure when there is a mismatch in compiler vector options.
> >
> > Without an article the part "that functions use only general purpose
> > registers" is unclear and/or grammatically incorrect.  What functions?
> > If the function the attribute is applied to, it needs an article, e.g.,
> > "the function" or "a function", and singular.  (Otherwise it could be
> > read as talking about the functions called from the one with
> > the attribute, or some other functions altogether).
> >
> > I tried to correct that above but, if you prefer, the following would
> > be closer to your phrasing but more correct/accurate:
> >
> >    The @code{general_regs_only} function attribute informs
> >    the compiler that the function uses only general purpose
> >    registers.
> >
> > I don't understand what the second sentence is trying to say, and
> > without a better error message for the problem in r99744, I suspect
> > few users will either.  I am suggesting to explain in the text you
> > are adding, under what conditions inlining might fail without
> > the attribute, and what effect the attribute has on the function
> > that prevents the inlining failure.
>
> How about this?
>
> @item general_regs_only
> @cindex @code{general_regs_only} function attribute, x86
> The @code{general_regs_only} function attribute informs the compiler
> that the function uses only general purpose registers.  When the
> compiler inlines a function with the @code{always_inline} attribute,
> target-specific compilation options may lead to inline failures.
> The @code{general_regs_only} attribute, if applicable, can be used
> together with the @code{always_inline} attribute to reduce inlining
> failure.
>
> > (If we can't explain what the effect is then I wonder why
> > the attribute is being added at all instead of teaching GCC to
> > always behave as if the attribute were there when its absence
> > would otherwise lead to an error.)
> >
>
> Inlining an always_inline function into a function, which doesn't
> support the ISA needed by the always_inline function,  should fail.
> But when inlining the always_inline function, the compiler doesn't
> know if GPR instructions are sufficient for the always_inline function.
> The general_regs_only informs the compiler that the function uses
> only general purpose registers.   There are no other ways for the
> compiler to deduce such info at this stage.

Is placing this attribute on a function definition which ends up
using non-GPR regs invoking undefined behavior?  That is,
can IRA/LRA assume xmm regs need no saving around calls
to such functions?  Do we need to diagnose non-GPR uses
(late, of course)?

I'm not sure we're going to solve the always-inline issues with
this new attribute - for example BMI intrinsics won't use
non-GPR regs but then a function using those and marked
as general-regs-only should still not be inlined into a
function doing the BMI cpuid check.

You document the attribute only affects always_inline
decisions, this makes it even more special.

I still think we should do the same for target attributes
as we do for optimize attributes - always_inline should
ignore any option differences and do what it is documented
(inline):

"@item always_inline
@cindex @code{always_inline} function attribute
Generally, functions are not inlined unless optimization is specified.
For functions declared inline, this attribute inlines the function
independent of any restrictions that otherwise apply to inlining.
Failure to inline such a function is diagnosed as an error.
Note that if such a function is called indirectly the compiler may
or may not inline it depending on optimization level and a failure
to inline an indirect call may or may not be diagnosed."

nowhere does this suggest that inlining is not done if
the caller is AVX2 but the callee is not.

Richard.

> --
> H.J.

  reply	other threads:[~2021-04-22  8:27 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-14 22:39 [PATCH v4 0/2] " H.J. Lu
2021-04-14 22:39 ` [PATCH v4 1/2] x86: Move OPTION_MASK_* to i386-common.h H.J. Lu
2021-04-14 22:39 ` [PATCH v4 2/2] x86: Add general_regs_only function attribute H.J. Lu
2021-04-21  7:30   ` Uros Bizjak
2021-04-21 13:47     ` H.J. Lu
2021-04-21 16:54     ` Martin Sebor
2021-04-21 17:09   ` Martin Sebor
2021-04-21 20:58     ` H.J. Lu
2021-04-21 23:23       ` Martin Sebor
2021-04-22  1:01         ` H.J. Lu
2021-04-22  8:27           ` Richard Biener [this message]
2021-04-22  9:02           ` Jakub Jelinek
2021-04-22 11:23             ` Richard Biener
2021-04-22 11:57               ` H.J. Lu
2021-04-22 12:16                 ` Richard Biener
2021-04-22 12:22               ` Jakub Jelinek
2021-04-22 12:52                 ` Richard Biener
2021-04-22 12:55                   ` Richard Biener
2021-07-18  1:45                     ` [PATCH v5] <x86gprintrin.h>: Add pragma GCC target("general-regs-only") H.J. Lu
2021-07-31 15:35                       ` PING^1 " H.J. Lu
2021-08-03 11:47                       ` Richard Biener
2021-08-03 14:45                         ` [PATCH v6] " 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='CAFiYyc1N=BHcLYwuxDyGKcH-JQMcrBpY0uLYbo_Fn9rZ5JCNJQ@mail.gmail.com' \
    --to=richard.guenther@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hjl.tools@gmail.com \
    --cc=jakub@redhat.com \
    --cc=msebor@gmail.com \
    --cc=rep.dot.nop@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).