[x86] x86: Don't add crtfastmath.o for -shared and add a new option -mdaz-ftz to enable FTZ and DAZ flags in MXCSR.
Checks
Commit Message
Don't add crtfastmath.o for -shared to avoid changing the MXCSR
register when loading a shared library. crtfastmath.o will be used
only when building executables.
Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
Ok for trunk?
gcc/ChangeLog:
PR target/55522
PR target/36821
* config/i386/gnu-user-common.h (GNU_USER_TARGET_MATHFILE_SPEC):
Link crtfastmath.o when -mdaz-ftz is specified, not link it
when -shared is specified.
* config/i386/i386.opt (mdaz-ftz): New option.
* doc/invoke.texi (x86 options): Document mftz-daz.
---
gcc/config/i386/gnu-user-common.h | 2 +-
gcc/config/i386/i386.opt | 4 ++++
gcc/doc/invoke.texi | 10 +++++++++-
3 files changed, 14 insertions(+), 2 deletions(-)
Comments
On Wed, Dec 14, 2022 at 3:21 AM liuhongt via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Don't add crtfastmath.o for -shared to avoid changing the MXCSR
> register when loading a shared library. crtfastmath.o will be used
> only when building executables.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> Ok for trunk?
You reject negative -mdaz-ftz but wouldn't that be useful with
-Ofast -mno-daz-ftz since there's otherwise no way to avoid that?
Richard.
> gcc/ChangeLog:
>
> PR target/55522
> PR target/36821
> * config/i386/gnu-user-common.h (GNU_USER_TARGET_MATHFILE_SPEC):
> Link crtfastmath.o when -mdaz-ftz is specified, not link it
> when -shared is specified.
> * config/i386/i386.opt (mdaz-ftz): New option.
> * doc/invoke.texi (x86 options): Document mftz-daz.
> ---
> gcc/config/i386/gnu-user-common.h | 2 +-
> gcc/config/i386/i386.opt | 4 ++++
> gcc/doc/invoke.texi | 10 +++++++++-
> 3 files changed, 14 insertions(+), 2 deletions(-)
>
> diff --git a/gcc/config/i386/gnu-user-common.h b/gcc/config/i386/gnu-user-common.h
> index cab9be2bfb7..02e4a2192a4 100644
> --- a/gcc/config/i386/gnu-user-common.h
> +++ b/gcc/config/i386/gnu-user-common.h
> @@ -47,7 +47,7 @@ along with GCC; see the file COPYING3. If not see
>
> /* Similar to standard GNU userspace, but adding -ffast-math support. */
> #define GNU_USER_TARGET_MATHFILE_SPEC \
> - "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
> + "%{Ofast|ffast-math|funsafe-math-optimizations|mdaz-ftz:%{!shared:crtfastmath.o%s}} \
> %{mpc32:crtprec32.o%s} \
> %{mpc64:crtprec64.o%s} \
> %{mpc80:crtprec80.o%s}"
> diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
> index fb4e57ada7c..8fd222db857 100644
> --- a/gcc/config/i386/i386.opt
> +++ b/gcc/config/i386/i386.opt
> @@ -420,6 +420,10 @@ mpc80
> Target RejectNegative
> Set 80387 floating-point precision to 80-bit.
>
> +mdaz-ftz
> +Target RejectNegative
> +Set the FTZ and DAZ Flags.
> +
> mpreferred-stack-boundary=
> Target RejectNegative Joined UInteger Var(ix86_preferred_stack_boundary_arg)
> Attempt to keep stack aligned to this power of 2.
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index cb40b38b73a..670e3767fbd 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -1433,7 +1433,7 @@ See RS/6000 and PowerPC Options.
> -m96bit-long-double -mlong-double-64 -mlong-double-80 -mlong-double-128 @gol
> -mregparm=@var{num} -msseregparm @gol
> -mveclibabi=@var{type} -mvect8-ret-in-mem @gol
> --mpc32 -mpc64 -mpc80 -mstackrealign @gol
> +-mpc32 -mpc64 -mpc80 -mdaz-ftz -mstackrealign @gol
> -momit-leaf-frame-pointer -mno-red-zone -mno-tls-direct-seg-refs @gol
> -mcmodel=@var{code-model} -mabi=@var{name} -maddress-mode=@var{mode} @gol
> -m32 -m64 -mx32 -m16 -miamcu -mlarge-data-threshold=@var{num} @gol
> @@ -32752,6 +32752,14 @@ are enabled by default; routines in such libraries could suffer significant
> loss of accuracy, typically through so-called ``catastrophic cancellation'',
> when this option is used to set the precision to less than extended precision.
>
> +@item -mdaz-ftz
> +@opindex mdaz-ftz
> +
> +the flush-to-zero (FTZ) and denormals-are-zero (DAZ) flags in the MXCSR register
> +are used to control floating-point calculations.SSE and AVX instructions
> +including scalar and vector instructions could benefit from enabling the FTZ
> +and DAZ flags when @option{-mdaz-ftz} is specified.
Maybe say that the MXCSR register is set at program start to achieve
this when the
flag is specified at _link_ time and say this switch is ignored when
-shared is specified?
> +
> @item -mstackrealign
> @opindex mstackrealign
> Realign the stack at entry. On the x86, the @option{-mstackrealign}
> --
> 2.27.0
>
On Wed, Dec 14, 2022 at 09:08:02AM +0100, Richard Biener via Gcc-patches wrote:
> On Wed, Dec 14, 2022 at 3:21 AM liuhongt via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > Don't add crtfastmath.o for -shared to avoid changing the MXCSR
> > register when loading a shared library. crtfastmath.o will be used
> > only when building executables.
> >
> > Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> > Ok for trunk?
>
> You reject negative -mdaz-ftz but wouldn't that be useful with
> -Ofast -mno-daz-ftz since there's otherwise no way to avoid that?
Agreed.
I even wonder if the best wouldn't be to make the option effectively
three state, default, no and yes, where if the option isn't specified
at all, then crtfastmath.o* is linked as is now except for -shared,
if it is -mno-daz-ftz, then it is never linked in regardless of other
options and if it is -mdaz-ftz, then it is linked even for -shared.
> > --- a/gcc/config/i386/i386.opt
> > +++ b/gcc/config/i386/i386.opt
> > @@ -420,6 +420,10 @@ mpc80
> > Target RejectNegative
> > Set 80387 floating-point precision to 80-bit.
> >
> > +mdaz-ftz
> > +Target RejectNegative
> > +Set the FTZ and DAZ Flags.
As the option is only used in the driver, shouldn't it be marked Driver
and not Target? It doesn't need to be saved/restored on every cfun switch
etc.
> > +@item -mdaz-ftz
> > +@opindex mdaz-ftz
> > +
> > +the flush-to-zero (FTZ) and denormals-are-zero (DAZ) flags in the MXCSR register
Shouldn't description start with capital letter?
> > +are used to control floating-point calculations.SSE and AVX instructions
> > +including scalar and vector instructions could benefit from enabling the FTZ
> > +and DAZ flags when @option{-mdaz-ftz} is specified.
>
> Maybe say that the MXCSR register is set at program start to achieve
> this when the
> flag is specified at _link_ time and say this switch is ignored when
> -shared is specified?
Jakub
On Wed, Dec 14, 2022 at 9:16 AM Jakub Jelinek <jakub@redhat.com> wrote:
>
> On Wed, Dec 14, 2022 at 09:08:02AM +0100, Richard Biener via Gcc-patches wrote:
> > On Wed, Dec 14, 2022 at 3:21 AM liuhongt via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> > >
> > > Don't add crtfastmath.o for -shared to avoid changing the MXCSR
> > > register when loading a shared library. crtfastmath.o will be used
> > > only when building executables.
> > >
> > > Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> > > Ok for trunk?
> >
> > You reject negative -mdaz-ftz but wouldn't that be useful with
> > -Ofast -mno-daz-ftz since there's otherwise no way to avoid that?
>
> Agreed.
> I even wonder if the best wouldn't be to make the option effectively
> three state, default, no and yes, where if the option isn't specified
> at all, then crtfastmath.o* is linked as is now except for -shared,
> if it is -mno-daz-ftz, then it is never linked in regardless of other
> options and if it is -mdaz-ftz, then it is linked even for -shared.
Possibly. I'd also suggest to split the changed -shared handling to
a separate patch since people may want to backport this and it
should be applicable to all other targets with similar handling.
> > > --- a/gcc/config/i386/i386.opt
> > > +++ b/gcc/config/i386/i386.opt
> > > @@ -420,6 +420,10 @@ mpc80
> > > Target RejectNegative
> > > Set 80387 floating-point precision to 80-bit.
> > >
> > > +mdaz-ftz
> > > +Target RejectNegative
> > > +Set the FTZ and DAZ Flags.
>
> As the option is only used in the driver, shouldn't it be marked Driver
> and not Target? It doesn't need to be saved/restored on every cfun switch
> etc.
>
> > > +@item -mdaz-ftz
> > > +@opindex mdaz-ftz
> > > +
> > > +the flush-to-zero (FTZ) and denormals-are-zero (DAZ) flags in the MXCSR register
>
> Shouldn't description start with capital letter?
>
> > > +are used to control floating-point calculations.SSE and AVX instructions
> > > +including scalar and vector instructions could benefit from enabling the FTZ
> > > +and DAZ flags when @option{-mdaz-ftz} is specified.
> >
> > Maybe say that the MXCSR register is set at program start to achieve
> > this when the
> > flag is specified at _link_ time and say this switch is ignored when
> > -shared is specified?
>
> Jakub
>
> -----Original Message-----
> From: Richard Biener <richard.guenther@gmail.com>
> Sent: Wednesday, December 14, 2022 4:23 PM
> To: Jakub Jelinek <jakub@redhat.com>
> Cc: Liu, Hongtao <hongtao.liu@intel.com>; gcc-patches@gcc.gnu.org;
> crazylht@gmail.com; hjl.tools@gmail.com; ubizjak@gmail.com
> Subject: Re: [PATCH] [x86] x86: Don't add crtfastmath.o for -shared and add a
> new option -mdaz-ftz to enable FTZ and DAZ flags in MXCSR.
>
> On Wed, Dec 14, 2022 at 9:16 AM Jakub Jelinek <jakub@redhat.com> wrote:
> >
> > On Wed, Dec 14, 2022 at 09:08:02AM +0100, Richard Biener via Gcc-patches
> wrote:
> > > On Wed, Dec 14, 2022 at 3:21 AM liuhongt via Gcc-patches
> > > <gcc-patches@gcc.gnu.org> wrote:
> > > >
> > > > Don't add crtfastmath.o for -shared to avoid changing the MXCSR
> > > > register when loading a shared library. crtfastmath.o will be
> > > > used only when building executables.
> > > >
> > > > Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> > > > Ok for trunk?
> > >
> > > You reject negative -mdaz-ftz but wouldn't that be useful with
> > > -Ofast -mno-daz-ftz since there's otherwise no way to avoid that?
> >
> > Agreed.
> > I even wonder if the best wouldn't be to make the option effectively
> > three state, default, no and yes, where if the option isn't specified
> > at all, then crtfastmath.o* is linked as is now except for -shared, if
> > it is -mno-daz-ftz, then it is never linked in regardless of other
> > options and if it is -mdaz-ftz, then it is linked even for -shared.
>
> Possibly. I'd also suggest to split the changed -shared handling to a separate
> patch since people may want to backport this and it should be applicable to
> all other targets with similar handling.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55522#c26
So patch in the upper link is ok for trunk?
I'll change -mdaz-ftz part as a separate patch.
>
> > > > --- a/gcc/config/i386/i386.opt
> > > > +++ b/gcc/config/i386/i386.opt
> > > > @@ -420,6 +420,10 @@ mpc80
> > > > Target RejectNegative
> > > > Set 80387 floating-point precision to 80-bit.
> > > >
> > > > +mdaz-ftz
> > > > +Target RejectNegative
> > > > +Set the FTZ and DAZ Flags.
> >
> > As the option is only used in the driver, shouldn't it be marked
> > Driver and not Target? It doesn't need to be saved/restored on every
> > cfun switch etc.
> >
> > > > +@item -mdaz-ftz
> > > > +@opindex mdaz-ftz
> > > > +
> > > > +the flush-to-zero (FTZ) and denormals-are-zero (DAZ) flags in the
> > > > +MXCSR register
> >
> > Shouldn't description start with capital letter?
> >
> > > > +are used to control floating-point calculations.SSE and AVX
> > > > +instructions including scalar and vector instructions could
> > > > +benefit from enabling the FTZ and DAZ flags when @option{-mdaz-ftz}
> is specified.
> > >
> > > Maybe say that the MXCSR register is set at program start to achieve
> > > this when the flag is specified at _link_ time and say this switch
> > > is ignored when -shared is specified?
> >
> > Jakub
> >
On Wed, Dec 14, 2022 at 9:34 AM Liu, Hongtao <hongtao.liu@intel.com> wrote:
>
>
>
> > -----Original Message-----
> > From: Richard Biener <richard.guenther@gmail.com>
> > Sent: Wednesday, December 14, 2022 4:23 PM
> > To: Jakub Jelinek <jakub@redhat.com>
> > Cc: Liu, Hongtao <hongtao.liu@intel.com>; gcc-patches@gcc.gnu.org;
> > crazylht@gmail.com; hjl.tools@gmail.com; ubizjak@gmail.com
> > Subject: Re: [PATCH] [x86] x86: Don't add crtfastmath.o for -shared and add a
> > new option -mdaz-ftz to enable FTZ and DAZ flags in MXCSR.
> >
> > On Wed, Dec 14, 2022 at 9:16 AM Jakub Jelinek <jakub@redhat.com> wrote:
> > >
> > > On Wed, Dec 14, 2022 at 09:08:02AM +0100, Richard Biener via Gcc-patches
> > wrote:
> > > > On Wed, Dec 14, 2022 at 3:21 AM liuhongt via Gcc-patches
> > > > <gcc-patches@gcc.gnu.org> wrote:
> > > > >
> > > > > Don't add crtfastmath.o for -shared to avoid changing the MXCSR
> > > > > register when loading a shared library. crtfastmath.o will be
> > > > > used only when building executables.
> > > > >
> > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> > > > > Ok for trunk?
> > > >
> > > > You reject negative -mdaz-ftz but wouldn't that be useful with
> > > > -Ofast -mno-daz-ftz since there's otherwise no way to avoid that?
> > >
> > > Agreed.
> > > I even wonder if the best wouldn't be to make the option effectively
> > > three state, default, no and yes, where if the option isn't specified
> > > at all, then crtfastmath.o* is linked as is now except for -shared, if
> > > it is -mno-daz-ftz, then it is never linked in regardless of other
> > > options and if it is -mdaz-ftz, then it is linked even for -shared.
> >
> > Possibly. I'd also suggest to split the changed -shared handling to a separate
> > patch since people may want to backport this and it should be applicable to
> > all other targets with similar handling.
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55522#c26
> So patch in the upper link is ok for trunk?
It needs target maintainer approval but yes, I think that's what we want.
Richard.
> I'll change -mdaz-ftz part as a separate patch.
> >
> > > > > --- a/gcc/config/i386/i386.opt
> > > > > +++ b/gcc/config/i386/i386.opt
> > > > > @@ -420,6 +420,10 @@ mpc80
> > > > > Target RejectNegative
> > > > > Set 80387 floating-point precision to 80-bit.
> > > > >
> > > > > +mdaz-ftz
> > > > > +Target RejectNegative
> > > > > +Set the FTZ and DAZ Flags.
> > >
> > > As the option is only used in the driver, shouldn't it be marked
> > > Driver and not Target? It doesn't need to be saved/restored on every
> > > cfun switch etc.
> > >
> > > > > +@item -mdaz-ftz
> > > > > +@opindex mdaz-ftz
> > > > > +
> > > > > +the flush-to-zero (FTZ) and denormals-are-zero (DAZ) flags in the
> > > > > +MXCSR register
> > >
> > > Shouldn't description start with capital letter?
> > >
> > > > > +are used to control floating-point calculations.SSE and AVX
> > > > > +instructions including scalar and vector instructions could
> > > > > +benefit from enabling the FTZ and DAZ flags when @option{-mdaz-ftz}
> > is specified.
> > > >
> > > > Maybe say that the MXCSR register is set at program start to achieve
> > > > this when the flag is specified at _link_ time and say this switch
> > > > is ignored when -shared is specified?
> > >
> > > Jakub
> > >
On Wed, Dec 14, 2022 at 9:52 AM Richard Biener
<richard.guenther@gmail.com> wrote:
>
> On Wed, Dec 14, 2022 at 9:34 AM Liu, Hongtao <hongtao.liu@intel.com> wrote:
> >
> >
> >
> > > -----Original Message-----
> > > From: Richard Biener <richard.guenther@gmail.com>
> > > Sent: Wednesday, December 14, 2022 4:23 PM
> > > To: Jakub Jelinek <jakub@redhat.com>
> > > Cc: Liu, Hongtao <hongtao.liu@intel.com>; gcc-patches@gcc.gnu.org;
> > > crazylht@gmail.com; hjl.tools@gmail.com; ubizjak@gmail.com
> > > Subject: Re: [PATCH] [x86] x86: Don't add crtfastmath.o for -shared and add a
> > > new option -mdaz-ftz to enable FTZ and DAZ flags in MXCSR.
> > >
> > > On Wed, Dec 14, 2022 at 9:16 AM Jakub Jelinek <jakub@redhat.com> wrote:
> > > >
> > > > On Wed, Dec 14, 2022 at 09:08:02AM +0100, Richard Biener via Gcc-patches
> > > wrote:
> > > > > On Wed, Dec 14, 2022 at 3:21 AM liuhongt via Gcc-patches
> > > > > <gcc-patches@gcc.gnu.org> wrote:
> > > > > >
> > > > > > Don't add crtfastmath.o for -shared to avoid changing the MXCSR
> > > > > > register when loading a shared library. crtfastmath.o will be
> > > > > > used only when building executables.
> > > > > >
> > > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> > > > > > Ok for trunk?
> > > > >
> > > > > You reject negative -mdaz-ftz but wouldn't that be useful with
> > > > > -Ofast -mno-daz-ftz since there's otherwise no way to avoid that?
> > > >
> > > > Agreed.
> > > > I even wonder if the best wouldn't be to make the option effectively
> > > > three state, default, no and yes, where if the option isn't specified
> > > > at all, then crtfastmath.o* is linked as is now except for -shared, if
> > > > it is -mno-daz-ftz, then it is never linked in regardless of other
> > > > options and if it is -mdaz-ftz, then it is linked even for -shared.
> > >
> > > Possibly. I'd also suggest to split the changed -shared handling to a separate
> > > patch since people may want to backport this and it should be applicable to
> > > all other targets with similar handling.
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55522#c26
> > So patch in the upper link is ok for trunk?
>
> It needs target maintainer approval but yes, I think that's what we want.
Yes, the one-liner is OK. Perhaps it also needs a corresponding
documentation change (haven't checked the documentation).
Uros.
>
> Richard.
>
> > I'll change -mdaz-ftz part as a separate patch.
> > >
> > > > > > --- a/gcc/config/i386/i386.opt
> > > > > > +++ b/gcc/config/i386/i386.opt
> > > > > > @@ -420,6 +420,10 @@ mpc80
> > > > > > Target RejectNegative
> > > > > > Set 80387 floating-point precision to 80-bit.
> > > > > >
> > > > > > +mdaz-ftz
> > > > > > +Target RejectNegative
> > > > > > +Set the FTZ and DAZ Flags.
> > > >
> > > > As the option is only used in the driver, shouldn't it be marked
> > > > Driver and not Target? It doesn't need to be saved/restored on every
> > > > cfun switch etc.
> > > >
> > > > > > +@item -mdaz-ftz
> > > > > > +@opindex mdaz-ftz
> > > > > > +
> > > > > > +the flush-to-zero (FTZ) and denormals-are-zero (DAZ) flags in the
> > > > > > +MXCSR register
> > > >
> > > > Shouldn't description start with capital letter?
> > > >
> > > > > > +are used to control floating-point calculations.SSE and AVX
> > > > > > +instructions including scalar and vector instructions could
> > > > > > +benefit from enabling the FTZ and DAZ flags when @option{-mdaz-ftz}
> > > is specified.
> > > > >
> > > > > Maybe say that the MXCSR register is set at program start to achieve
> > > > > this when the flag is specified at _link_ time and say this switch
> > > > > is ignored when -shared is specified?
> > > >
> > > > Jakub
> > > >
@@ -47,7 +47,7 @@ along with GCC; see the file COPYING3. If not see
/* Similar to standard GNU userspace, but adding -ffast-math support. */
#define GNU_USER_TARGET_MATHFILE_SPEC \
- "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
+ "%{Ofast|ffast-math|funsafe-math-optimizations|mdaz-ftz:%{!shared:crtfastmath.o%s}} \
%{mpc32:crtprec32.o%s} \
%{mpc64:crtprec64.o%s} \
%{mpc80:crtprec80.o%s}"
@@ -420,6 +420,10 @@ mpc80
Target RejectNegative
Set 80387 floating-point precision to 80-bit.
+mdaz-ftz
+Target RejectNegative
+Set the FTZ and DAZ Flags.
+
mpreferred-stack-boundary=
Target RejectNegative Joined UInteger Var(ix86_preferred_stack_boundary_arg)
Attempt to keep stack aligned to this power of 2.
@@ -1433,7 +1433,7 @@ See RS/6000 and PowerPC Options.
-m96bit-long-double -mlong-double-64 -mlong-double-80 -mlong-double-128 @gol
-mregparm=@var{num} -msseregparm @gol
-mveclibabi=@var{type} -mvect8-ret-in-mem @gol
--mpc32 -mpc64 -mpc80 -mstackrealign @gol
+-mpc32 -mpc64 -mpc80 -mdaz-ftz -mstackrealign @gol
-momit-leaf-frame-pointer -mno-red-zone -mno-tls-direct-seg-refs @gol
-mcmodel=@var{code-model} -mabi=@var{name} -maddress-mode=@var{mode} @gol
-m32 -m64 -mx32 -m16 -miamcu -mlarge-data-threshold=@var{num} @gol
@@ -32752,6 +32752,14 @@ are enabled by default; routines in such libraries could suffer significant
loss of accuracy, typically through so-called ``catastrophic cancellation'',
when this option is used to set the precision to less than extended precision.
+@item -mdaz-ftz
+@opindex mdaz-ftz
+
+the flush-to-zero (FTZ) and denormals-are-zero (DAZ) flags in the MXCSR register
+are used to control floating-point calculations.SSE and AVX instructions
+including scalar and vector instructions could benefit from enabling the FTZ
+and DAZ flags when @option{-mdaz-ftz} is specified.
+
@item -mstackrealign
@opindex mstackrealign
Realign the stack at entry. On the x86, the @option{-mstackrealign}