public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Odd optimizer behavior with FP arithmetic
@ 2019-09-26 16:53 Konstantin Shegunov
  2019-09-26 17:11 ` Alexander Monakov
  0 siblings, 1 reply; 3+ messages in thread
From: Konstantin Shegunov @ 2019-09-26 16:53 UTC (permalink / raw)
  To: gcc-help

Hello,
I have a piece of floating point EFT arithmetic in my code, but I get a
rather strange code generation for it. Here's the actual source code and
flags I've experimented with: https://godbolt.org/z/oJEs4U

The first pane is what I expect to get as output, that is, all the
arithmetic operations in the assembly. However the strange thing is I have
to compile with -funsafe-math-optimizations to actually produce that.
The second pane is the same thing but with -fno-unsafe-math-optimizations
(default) and surprisingly the optimizer strips the relevant instructions.
I would've thought the -fno-associative-math enough to prevent that from
happening but for some reason it's not.
The third pane I added for good measure. It illustrates the same flags as
the first one (the one that generates the correct assembly), but the flags
are applied through an attribute directly to the symbol. Even more
surprisingly this generates yet another completely different piece of
output, which doesn't even seem to do what's supposed to (unsafe means
unsafe in this case, I gather).

Has anyone any insight into this?
Why isn't -fno-associative-math enough in stopping the optimizer here?
Why would -funsafe-math-optimizations actually trigger the correct code
being generated?
And as a curiosity, why would it matter if the flag is applied to the
symbol in contrast to the pragma?

Thanks in advance!
Konstantin.

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

* Re: Odd optimizer behavior with FP arithmetic
  2019-09-26 16:53 Odd optimizer behavior with FP arithmetic Konstantin Shegunov
@ 2019-09-26 17:11 ` Alexander Monakov
  2019-09-26 17:38   ` Konstantin Shegunov
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Monakov @ 2019-09-26 17:11 UTC (permalink / raw)
  To: Konstantin Shegunov; +Cc: gcc-help

On Thu, 26 Sep 2019, Konstantin Shegunov wrote:

> Hello,
> I have a piece of floating point EFT arithmetic in my code, but I get a
> rather strange code generation for it. Here's the actual source code and
> flags I've experimented with: https://godbolt.org/z/oJEs4U
> 
> The first pane is what I expect to get as output, that is, all the
> arithmetic operations in the assembly. However the strange thing is I have
> to compile with -funsafe-math-optimizations to actually produce that.
> The second pane is the same thing but with -fno-unsafe-math-optimizations
> (default) and surprisingly the optimizer strips the relevant instructions.

The 'add' function is inlined into main, and floating-point operations that
would compute result.c are eliminated as dead code because result.c is never
used.

(and because the compiler doesn't attempt to preserve floating-point exceptions
that those eliminated instructions might raise)

> I would've thought the -fno-associative-math enough to prevent that from
> happening but for some reason it's not.
> The third pane I added for good measure. It illustrates the same flags as
> the first one (the one that generates the correct assembly), but the flags
> are applied through an attribute directly to the symbol. Even more
> surprisingly this generates yet another completely different piece of
> output, which doesn't even seem to do what's supposed to (unsafe means
> unsafe in this case, I gather).

In this case inlining does not happen due to flags being different between
'main' and 'add'.

In the first case inlining also does not happen for the same reason.

Alexander

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

* Re: Odd optimizer behavior with FP arithmetic
  2019-09-26 17:11 ` Alexander Monakov
@ 2019-09-26 17:38   ` Konstantin Shegunov
  0 siblings, 0 replies; 3+ messages in thread
From: Konstantin Shegunov @ 2019-09-26 17:38 UTC (permalink / raw)
  To: Alexander Monakov; +Cc: gcc-help

On Thu, Sep 26, 2019 at 8:11 PM Alexander Monakov <amonakov@ispras.ru>
wrote:

> The 'add' function is inlined into main, and floating-point operations that
> would compute result.c are eliminated as dead code because result.c is
> never
> used.
>
> (and because the compiler doesn't attempt to preserve floating-point
> exceptions
> that those eliminated instructions might raise)

...

In this case inlining does not happen due to flags being different between
> 'main' and 'add'.
>
> In the first case inlining also does not happen for the same reason.
>

Well, ain't I a dummy.

Thanks a lot!
Konstantin.

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

end of thread, other threads:[~2019-09-26 17:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-26 16:53 Odd optimizer behavior with FP arithmetic Konstantin Shegunov
2019-09-26 17:11 ` Alexander Monakov
2019-09-26 17:38   ` Konstantin Shegunov

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