Bug 100132 - Optimization breaks pointer association
Summary: Optimization breaks pointer association
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: 10.5
Assignee: José Rui Faustino de Sousa
URL:
Keywords: patch
: 87659 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-04-17 23:38 UTC by José Rui Faustino de Sousa
Modified: 2022-10-17 20:36 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-04-18 00:00:00


Attachments
Fortran code showing problem (460 bytes, text/plain)
2021-04-17 23:38 UTC, José Rui Faustino de Sousa
Details

Note You need to log in before you can comment on or make changes to this bug.
Description José Rui Faustino de Sousa 2021-04-17 23:38:00 UTC
Created attachment 50622 [details]
Fortran code showing problem

Hi All!

Pointer association is lost when compiling with -O1 or -O2, curiously not with -O3 unless -fcheck=recursion is used

Seen on:

GNU Fortran (GCC) 11.0.1 20210417 (experimental)
GNU Fortran (GCC) 10.3.1 20210417
GNU Fortran (GCC) 9.3.1 20210417

Thank you very much.

Best regards,
José Rui
Comment 1 José Rui Faustino de Sousa 2021-04-18 00:20:25 UTC
Patch posted

https://gcc.gnu.org/pipermail/fortran/2021-April/055946.html
Comment 2 Dominique d'Humieres 2021-04-18 09:43:28 UTC
Confirmed since at least GCC7.
Comment 3 anlauf 2022-09-18 19:11:59 UTC
(In reply to José Rui Faustino de Sousa from comment #1)
> Patch posted
> 
> https://gcc.gnu.org/pipermail/fortran/2021-April/055946.html

The patch is technically fine and regtests ok, but was unfortunately never
reviewed.

The basic fix can be slightly shortened to:

diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc
index 0ea7c74a6f1..c062a5b29d7 100644
--- a/gcc/fortran/trans-types.cc
+++ b/gcc/fortran/trans-types.cc
@@ -3054,12 +3054,23 @@ create_fn_spec (gfc_symbol *sym, tree fntype)
   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
     if (spec_len < sizeof (spec))
       {
-       if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
+       bool is_class = false;
+       bool is_pointer = false;
+
+       if (f->sym)
+         {
+           is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym)
+             && f->sym->attr.class_ok;
+           is_pointer = is_class ? CLASS_DATA (f->sym)->attr.class_pointer
+                                 : f->sym->attr.pointer;
+         }
+
+       if (f->sym == NULL || is_pointer || f->sym->attr.target
            || f->sym->attr.external || f->sym->attr.cray_pointer
            || (f->sym->ts.type == BT_DERIVED
                && (f->sym->ts.u.derived->attr.proc_pointer_comp
                    || f->sym->ts.u.derived->attr.pointer_comp))
-           || (f->sym->ts.type == BT_CLASS
+           || (is_class
                && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
                    || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
            || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))

I can take care of it for you.
Comment 5 GCC Commits 2022-09-20 18:31:44 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:be60aa5b608b5f09fadfeff852a46589ac311a42

commit r13-2743-gbe60aa5b608b5f09fadfeff852a46589ac311a42
Author: José Rui Faustino de Sousa <jrfsousa@gmail.com>
Date:   Mon Sep 19 22:00:45 2022 +0200

    Fortran: Fix function attributes [PR100132]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100132
            * trans-types.cc (create_fn_spec): Fix function attributes when
            passing polymorphic pointers.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100132
            * gfortran.dg/PR100132.f90: New test.
Comment 6 GCC Commits 2022-10-01 18:17:29 UTC
The releases/gcc-12 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:9de83c0939dabdbefcb88dfef3ad7caf932951ec

commit r12-8802-g9de83c0939dabdbefcb88dfef3ad7caf932951ec
Author: José Rui Faustino de Sousa <jrfsousa@gmail.com>
Date:   Mon Sep 19 22:00:45 2022 +0200

    Fortran: Fix function attributes [PR100132]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100132
            * trans-types.cc (create_fn_spec): Fix function attributes when
            passing polymorphic pointers.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100132
            * gfortran.dg/PR100132.f90: New test.
    
    (cherry picked from commit be60aa5b608b5f09fadfeff852a46589ac311a42)
Comment 7 GCC Commits 2022-10-01 18:40:19 UTC
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:cd13cc012e0bca927f97f618ab54c8d365883bbc

commit r11-10286-gcd13cc012e0bca927f97f618ab54c8d365883bbc
Author: José Rui Faustino de Sousa <jrfsousa@gmail.com>
Date:   Mon Sep 19 22:00:45 2022 +0200

    Fortran: Fix function attributes [PR100132]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100132
            * trans-types.c (create_fn_spec): Fix function attributes when
            passing polymorphic pointers.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100132
            * gfortran.dg/PR100132.f90: New test.
    
    (cherry picked from commit be60aa5b608b5f09fadfeff852a46589ac311a42)
Comment 8 GCC Commits 2022-10-01 18:55:33 UTC
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:8c583871e70f5530907d22a21b0f1d1e7b90e224

commit r10-11015-g8c583871e70f5530907d22a21b0f1d1e7b90e224
Author: José Rui Faustino de Sousa <jrfsousa@gmail.com>
Date:   Mon Sep 19 22:00:45 2022 +0200

    Fortran: Fix function attributes [PR100132]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100132
            * trans-types.c (create_fn_spec): Fix function attributes when
            passing polymorphic pointers.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100132
            * gfortran.dg/PR100132.f90: New test.
    
    (cherry picked from commit be60aa5b608b5f09fadfeff852a46589ac311a42)
Comment 9 anlauf 2022-10-01 18:56:21 UTC
Fixed on all open branches.  Closing.

Thanks for the patch!
Comment 10 anlauf 2022-10-17 20:36:32 UTC
*** Bug 87659 has been marked as a duplicate of this bug. ***