public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/114012] New: overloaded unary operator called twice
@ 2024-02-20 14:14 alexandre.poux at coria dot fr
  2024-02-20 20:32 ` [Bug fortran/114012] " anlauf at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: alexandre.poux at coria dot fr @ 2024-02-20 14:14 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114012
           Summary: overloaded unary operator called twice
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexandre.poux at coria dot fr
  Target Milestone: ---

Created attachment 57470
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57470&action=edit
a program triggering the duplicate call on unary operator

In the attached code, I overloaded assignment (assign) and the unary operator
'-' (neg) for a custom class

The assign subroutine receive a polymorphic argument and the function neg
returns a polymorphic result.

When a simple `i = -i` is supposed to call `neg` and the `assign`, it
surprisingly call `neg` twice and then `assign`. 

Both time `neg` is called with the correct argument (the old value of `i`) so
the result is good anyway.

As far as I know, `neg` and `assign` are supposed to be pure (which they are
not, due to the print) so this should only induce a performance hit.

I've observed this on an up to date Arch linux with core/gcc-fortran 13.2.1-5
and extra/gcc12-fortran 12.3.0-3.

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

* [Bug fortran/114012] overloaded unary operator called twice
  2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
@ 2024-02-20 20:32 ` anlauf at gcc dot gnu.org
  2024-02-25 19:44 ` anlauf at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-02-20 20:32 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-02-20
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from anlauf at gcc dot gnu.org ---
The dump-tree shows for the assignment i = -i :

  {
    struct __class__STAR_t val.7;

    val.7._data = (void *) neg (&i)._data;
    val.7._vptr = (struct __vtype__STAR * {ref-all}) neg (&i)._vptr;
    val.7._len = 0;
    i = {CLOBBER};
    assign (&i, &val.7);
  }

We should evaluate neg (&i) only once.

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

* [Bug fortran/114012] overloaded unary operator called twice
  2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
  2024-02-20 20:32 ` [Bug fortran/114012] " anlauf at gcc dot gnu.org
@ 2024-02-25 19:44 ` anlauf at gcc dot gnu.org
  2024-02-25 20:36 ` anlauf at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-02-25 19:44 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |anlauf at gcc dot gnu.org
           Assignee|unassigned at gcc dot gnu.org      |anlauf at gcc dot gnu.org

--- Comment #2 from anlauf at gcc dot gnu.org ---
The following patch fixes the redundant procedure invocation:

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 118dfd7c9b2..581b3786dea 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -6691,6 +6692,10 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
                            {
                              tree efield;

+                             /* Evaluate arguments just once.  */
+                             if (e->expr_type != EXPR_VARIABLE)
+                               parmse.expr = save_expr (parmse.expr);
+
                              /* Set the _data field.  */
                              tmp = gfc_class_data_get (var);
                              efield = fold_convert (TREE_TYPE (tmp),

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

* [Bug fortran/114012] overloaded unary operator called twice
  2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
  2024-02-20 20:32 ` [Bug fortran/114012] " anlauf at gcc dot gnu.org
  2024-02-25 19:44 ` anlauf at gcc dot gnu.org
@ 2024-02-25 20:36 ` anlauf at gcc dot gnu.org
  2024-02-26 17:50 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-02-25 20:36 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED

--- Comment #3 from anlauf at gcc dot gnu.org ---
Submitted: https://gcc.gnu.org/pipermail/fortran/2024-February/060267.html

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

* [Bug fortran/114012] overloaded unary operator called twice
  2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
                   ` (2 preceding siblings ...)
  2024-02-25 20:36 ` anlauf at gcc dot gnu.org
@ 2024-02-26 17:50 ` cvs-commit at gcc dot gnu.org
  2024-02-27  9:42 ` alexandre.poux at coria dot fr
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-02-26 17:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:2f71e801ad0bb1f620334aadbd7c99cc4efe6309

commit r14-9186-g2f71e801ad0bb1f620334aadbd7c99cc4efe6309
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sun Feb 25 21:18:23 2024 +0100

    Fortran: do not evaluate polymorphic functions twice in assignment
[PR114012]

            PR fortran/114012

    gcc/fortran/ChangeLog:

            * trans-expr.cc (gfc_conv_procedure_call): Evaluate non-trivial
            arguments just once before assigning to an unlimited polymorphic
            dummy variable.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/pr114012.f90: New test.

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

* [Bug fortran/114012] overloaded unary operator called twice
  2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
                   ` (3 preceding siblings ...)
  2024-02-26 17:50 ` cvs-commit at gcc dot gnu.org
@ 2024-02-27  9:42 ` alexandre.poux at coria dot fr
  2024-03-06 21:03 ` cvs-commit at gcc dot gnu.org
  2024-03-06 21:06 ` anlauf at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: alexandre.poux at coria dot fr @ 2024-02-27  9:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Alexandre Poux <alexandre.poux at coria dot fr> ---
Thanks for the quick fix !

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

* [Bug fortran/114012] overloaded unary operator called twice
  2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
                   ` (4 preceding siblings ...)
  2024-02-27  9:42 ` alexandre.poux at coria dot fr
@ 2024-03-06 21:03 ` cvs-commit at gcc dot gnu.org
  2024-03-06 21:06 ` anlauf at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-06 21:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Harald Anlauf
<anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:1f5787e4b803a4294eeb80e048f56ccdb99c1b3b

commit r13-8407-g1f5787e4b803a4294eeb80e048f56ccdb99c1b3b
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sun Feb 25 21:18:23 2024 +0100

    Fortran: do not evaluate polymorphic functions twice in assignment
[PR114012]

            PR fortran/114012

    gcc/fortran/ChangeLog:

            * trans-expr.cc (gfc_conv_procedure_call): Evaluate non-trivial
            arguments just once before assigning to an unlimited polymorphic
            dummy variable.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/pr114012.f90: New test.

    (cherry picked from commit 2f71e801ad0bb1f620334aadbd7c99cc4efe6309)

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

* [Bug fortran/114012] overloaded unary operator called twice
  2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
                   ` (5 preceding siblings ...)
  2024-03-06 21:03 ` cvs-commit at gcc dot gnu.org
@ 2024-03-06 21:06 ` anlauf at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-03-06 21:06 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.3
             Status|ASSIGNED                    |RESOLVED
           Priority|P3                          |P4
         Resolution|---                         |FIXED

--- Comment #7 from anlauf at gcc dot gnu.org ---
Fixed on mainline for gcc-14, and backported to 13-branch.  Closing.

Thanks for the report!

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

end of thread, other threads:[~2024-03-06 21:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-20 14:14 [Bug fortran/114012] New: overloaded unary operator called twice alexandre.poux at coria dot fr
2024-02-20 20:32 ` [Bug fortran/114012] " anlauf at gcc dot gnu.org
2024-02-25 19:44 ` anlauf at gcc dot gnu.org
2024-02-25 20:36 ` anlauf at gcc dot gnu.org
2024-02-26 17:50 ` cvs-commit at gcc dot gnu.org
2024-02-27  9:42 ` alexandre.poux at coria dot fr
2024-03-06 21:03 ` cvs-commit at gcc dot gnu.org
2024-03-06 21:06 ` anlauf at gcc dot gnu.org

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