public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Should -ffp-contract=off the default on GCC?
@ 2023-03-16 16:24 Qing Zhao
  2023-03-16 16:31 ` Andrew Pinski
  0 siblings, 1 reply; 32+ messages in thread
From: Qing Zhao @ 2023-03-16 16:24 UTC (permalink / raw)
  To: richard.earnshaw; +Cc: gcc Patches, Richard Sandiford

Hi,

Recently, we discovered some floating point precision diffs when using GCC8 to build our 
application on arm64: After some investigation, it turns out that this is due to the 
-ffp-contract=fast option that is on by default. Therefore, we have to explicitly add 
-ffp-contract=off and do a full-rebuild.  

GCC by default turns -ffp-contract=fast on.
https://gcc.gnu.org/onlinedocs/gcc-8.5.0/gcc/Optimize-Options.html#Optimize-Options
https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Optimize-Options.html#Optimize-Options

"
-ffp-contract=style
-ffp-contract=off disables floating-point expression contraction. -ffp-contract=fast enables
floating-point expression contraction such as forming of fused multiply-add operations if 
the target has native support for them. -ffp-contract=on enables floating-point expression
contraction if allowed by the language standard. This is currently not implemented and 
treated equal to -ffp-contract=off.

The default is -ffp-contract=fast.
"

This can be shown by a small example for arm64 with gcc8.5 in https://godbolt.org/z/MxYfnG8TE. 
Only when adding -std=c89 explicitly, this transformaton is off.

another exmaple also shows that Clang and MSVC only allow this transformation when speifiying
ffast-math and fp:fast:  https://godbolt.org/z/o54bYfPbP 

When searching online, we found that there were similar discussions recently on the exact same issue:
https://github.com/dotnet/runtime/issues/64604
https://github.com/dotnet/runtime/issues/64591

a summary of these discussions is:

1. "fmadd" is a fused operation and will return a different result for many inputs;  
2. therefore, -ffp-contract=fast is not a safe optimization to be on by default;
3. Clang and MSVC only allow this when specifying ffast-math and fp:fast since this is not an
  IEEE754 compliant optimization;
4. The reasons why GCC turns on this option by default are:
  A. GNU C language spec allows such transformation. 
  B. this did not expose real problem for most X86/X64 apps previously since FMA instructions
     didn't exist until 2013 when the FMA3 instruction set was added, and also these instructions
     were not always available.. 
5. Arm64 has fused multiply-add instructions as "baseline" and are always available. therefore
  -ffp-contract=fast exposed more serious problems on Arm64 platforms.  

our major question:

Should GCC turn off -ffp-contract=fast by default since it's not IEEE754 compliant and more
  modern processors have the FMA instructions available by default?

Thanks.

Qing

^ permalink raw reply	[flat|nested] 32+ messages in thread
* Re: Should -ffp-contract=off the default on GCC?
@ 2023-03-27  9:03 Zeson
  0 siblings, 0 replies; 32+ messages in thread
From: Zeson @ 2023-03-27  9:03 UTC (permalink / raw)
  To: gcc-patches

Any update on this thread discussion? And the thread was straying to the document of option and user-friendly stuff.
So does the default value of -ffp-contract=fast obey the C/C++ language standard? But why does clang not obey? Or is it just compiler implement-dependent which is not specified by standard?


Regards,
Zeson

^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2023-03-31 16:31 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-16 16:24 Should -ffp-contract=off the default on GCC? Qing Zhao
2023-03-16 16:31 ` Andrew Pinski
2023-03-16 16:38   ` Qing Zhao
2023-03-16 16:53     ` Jakub Jelinek
2023-03-16 18:40       ` Qing Zhao
2023-03-20 22:05       ` Qing Zhao
2023-03-20 22:25         ` Jakub Jelinek
2023-03-21 12:49           ` Qing Zhao
2023-03-21 15:01           ` Qing Zhao
2023-03-21 16:56             ` Paul Koning
2023-03-21 17:00               ` Qing Zhao
2023-03-21 17:59                 ` Jeff Law
2023-03-21 18:03                   ` Paul Koning
2023-03-21 18:55                     ` Toon Moene
2023-03-21 18:12                   ` Alexander Monakov
2023-03-21 18:18                     ` Jeff Law
2023-03-22 10:13                       ` Richard Biener
2023-03-22 12:26                         ` Alexander Monakov
2023-03-22 13:57                           ` Richard Biener
2023-03-22 15:52                             ` Qing Zhao
2023-03-24  7:12                               ` Fangrui Song
2023-03-24 19:42                                 ` Andrew Pinski
2023-03-31 16:31                                   ` Qing Zhao
2023-03-21 19:01                   ` Qing Zhao
2023-03-21 19:12                     ` Jakub Jelinek
2023-03-21 23:28                       ` Jeff Law
2023-03-21 23:31                         ` Jakub Jelinek
2023-03-21 19:51                     ` Jeff Law
2023-03-21 21:08                       ` Qing Zhao
2023-03-22 12:33           ` Alexander Monakov
2023-03-22 14:33             ` Qing Zhao
2023-03-27  9:03 Zeson

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).