User account creation filtered due to spam.

Bug 41580 - [OOP] SAME_TYPE_AS and EXTENDS_TYPE_OF - add compile-time simplifcation
Summary: [OOP] SAME_TYPE_AS and EXTENDS_TYPE_OF - add compile-time simplifcation
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: 4.6.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2009-10-05 12:41 UTC by Tobias Burnus
Modified: 2016-11-16 13:20 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Draft patch - not fully working (1.74 KB, patch)
2011-01-05 17:19 UTC, Tobias Burnus
Details | Diff
Draft patch - should be OK (2.08 KB, patch)
2011-01-06 17:40 UTC, Tobias Burnus
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2009-10-05 12:41:23 UTC
For SAME_TYPE_AS and EXTENDS_TYPE_OF one should add a compile-time simplification to simplify.c

Cf. also review comments at http://gcc.gnu.org/ml/fortran/2009-09/msg00298.html
Comment 1 Dominique d'Humieres 2010-04-18 15:56:52 UTC
I cannot reproduce the error at http://gcc.gnu.org/ml/fortran/2009-09/msg00298.html neither with trunk nor with fortran-dev. Could this PR be more explicit about the problem?
Comment 2 Tobias Burnus 2010-04-18 16:21:29 UTC
> Could this PR be more explicit about the problem?

No it cannot; as written: For SAME_TYPE_AS and EXTENDS_TYPE_OF one should add a compile-time simplification to simplify.c.
Currently, only the run-time version is implemented.
Comment 3 Dominique d'Humieres 2010-04-18 16:47:09 UTC
> Currently, only the run-time version is implemented.

So could the pr marked as NEW?
Comment 4 janus 2011-01-05 12:16:33 UTC
(In reply to comment #2)
> For SAME_TYPE_AS and EXTENDS_TYPE_OF one should add a
> compile-time simplification to simplify.c.
> Currently, only the run-time version is implemented.

Well, currently the translation of SAME_TYPE_AS is done by gfc_conv_same_type_as (trans-intrinsic.c), for both TYPE- and CLASS-valued arguments.

Note: If both arguments are TYPE-valued, the result will be a compile-time constant, otherwise it has to be evaluated at runtime, according to the _hash values in the vtabs.

In which situations would we actually gain something by treating the TYPE-TYPE case in simplify.c?
Comment 5 Tobias Burnus 2011-01-05 17:19:45 UTC
Created attachment 22907 [details]
Draft patch - not fully working

(In reply to comment #4)
> Note: If both arguments are TYPE-valued, the result will be a compile-time
> constant, otherwise it has to be evaluated at runtime, according to the _hash
> values in the vtabs.

Well, extends_type_of can also be (sometimes) be evaluated at compile time for CLASS.

> In which situations would we actually gain something

I assume that in practice it only matters for automatically generated code - but there it might reduce the code size and improve the performance.

By the way: I think that's a 4.7 item.

 * * *

I attached a draft patch. Note, however, that it does not work (cf. bottom of patch/test case). For

   if (extends_type_of(a11,b11) .neqv. .true.) call abort()

one enters gfc_simplify_extends_type_of twice: Once the second argument (mold) is BT_CLASS (which give the correct result: NULL, i.e. not compile-time simplifiable). But then again with mold being BT_DERIVED - which is then compile-time simplified to .TRUE. as "a11" and "b11" are the same type.

I have not dug into this, but it seems to occur in two steps - first a normal simplification happens, then a specific interface is used via gfc_intrinsic_func_interface, which is then again simplified - but with BT_DERIVED argument (base type). I think all gfc_simplify_* functions could be affected.

TODO:
- Fix the double-simplification problem
- Add more test cases - I think the double-simplification also affects same_type, but it is currently not tested for.
Comment 6 Tobias Burnus 2011-01-06 17:40:26 UTC
Created attachment 22914 [details]
Draft patch - should be OK

Updated patch, fixed the issue (but did not add more tests). On the way, I encountered PR 47194.
Comment 7 Tobias Burnus 2011-01-07 12:59:06 UTC
Submitted patch: http://gcc.gnu.org/ml/fortran/2011-01/msg00051.html
Comment 8 Tobias Burnus 2011-01-07 16:19:34 UTC
Author: burnus
Date: Fri Jan  7 16:19:29 2011
New Revision: 168579

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=168579
Log:
2011-01-07  Tobias Burnus  <burnus@net-b.de>

        PR fortran/41580
        * class.c (gfc_build_class_symbol): Mark __vtab as attr.vtab.
        * intrinsic.c (add_functions): Use simplify functions for
        EXTENDS_TYPE_OF and SAME_TYPE_AS.
        * intrinsic.h (gfc_simplify_extends_type_of,
        gfc_simplify_same_type_as): New prototypes.
        * simplify.c (is_last_ref_vtab, gfc_simplify_extends_type_of,
        gfc_simplify_same_type_as): New functions.

2011-01-07  Tobias Burnus  <burnus@net-b.de>

        PR fortran/41580
        * gfortran.dg/extends_type_of_3.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/extends_type_of_3.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/intrinsic.c
    trunk/gcc/fortran/intrinsic.h
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 Tobias Burnus 2011-01-07 16:20:14 UTC
... and FIXED (on the 4.6 trunk).