public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/107294] Missed optimization: multiplying real with complex number in Fortran (only)
Date: Mon, 17 Oct 2022 12:58:25 +0000	[thread overview]
Message-ID: <bug-107294-4-SvKw7Z09D2@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107294-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107294

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-10-17

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
The issue is that the fortran frontend produces

  complex(kind=4) __result_csmul;

  __result_csmul = COMPLEX_EXPR <a, 0.0> * b;
  return __result_csmul;

while the C frontend does

  return COMPLEX_EXPR <SAVE_EXPR <a> * REALPART_EXPR <SAVE_EXPR <b>>, SAVE_EXPR
<a> * IMAGPART_EXPR <SAVE_EXPR <b>>>;

the fortran IL then gets optimized to

  <bb 2> [local count: 1073741824]:
  b$real_6 = REALPART_EXPR <b_4(D)>;
  b$imag_7 = IMAGPART_EXPR <b_4(D)>;
  _8 = a_3(D) * b$real_6;
  _9 = b$imag_7 * 0.0;
  _10 = a_3(D) * b$imag_7;
  _11 = b$real_6 * 0.0;
  _12 = _8 - _9;
  _13 = _10 + _11;
  _2 = COMPLEX_EXPR <_12, _13>;
  return _2;

and here we have to stop because with singed zeros the multiply by zero
is ambiguous and the resulting sign important(?).  With -fno-signed-zeros
the optimization result is the same but really the C frontend
interprets scalar * complex multiplication differently here.

Not sure who is correct, or if both are and we can optimize the multiply
by zero here (maybe in the context of a - b * 0.0 and a + b * 0.0).

It looks like the C17 standard in the G.5.1 annex gives us leeway to
implement multiplication in the pre-simplified form we do.  Somebody
would have to check the Fortran standard how to treat this case.

  reply	other threads:[~2022-10-17 12:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-17 12:39 [Bug fortran/107294] New: " bartoldeman at users dot sourceforge.net
2022-10-17 12:58 ` rguenth at gcc dot gnu.org [this message]
2022-10-17 13:02 ` [Bug fortran/107294] " rguenth at gcc dot gnu.org
2022-10-17 15:14 ` kargl at gcc dot gnu.org
2022-10-17 16:13 ` bartoldeman at users dot sourceforge.net
2022-10-17 16:14 ` bartoldeman at users dot sourceforge.net
2024-06-17 10:03 ` mjr19 at cam dot ac.uk
2024-06-17 14:38 ` kargls at comcast dot net

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=bug-107294-4-SvKw7Z09D2@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).