Bug 50919 - [OOP] Don't use vtable for NON_OVERRIDABLE TBP
Summary: [OOP] Don't use vtable for NON_OVERRIDABLE TBP
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2011-10-30 10:09 UTC by Tobias Burnus
Modified: 2011-11-07 18:46 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-10-31 00:00:00


Attachments
patch (983 bytes, patch)
2011-10-31 12:31 UTC, janus
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2011-10-30 10:09:03 UTC
Current, gfortran uses always a vtable for type-bound procedures. However, a vtable only makes sense if the type-bound procedure can be overridden (C++: if it is "virtual").

Expected:
a) gfortran call the procedure directly, which allows for inlining and avoids the pointer dereference.

b) When the ABI is changed, the procedure is no longer added to the vtable.
Hence, it should be added as item to http://gcc.gnu.org/wiki/LibgfortranAbiCleanup


Test case. The dump shows the following rather than a simple "test ()":

callit (struct __class_m_T & restrict x)
{
  x->_vptr->test ();



module m
type t
contains
  procedure, nopass, NON_OVERRIDABLE :: test
end type t
contains
  subroutine test()
    print *, "t's test"
  end subroutine test

  subroutine callit(x)
    class(t) :: x
    call x%test()
  end subroutine
end module m
tob@archimedes:/dev/shm> less poly.f90.*
tob@archimedes:/dev/shm> cat poly.f90
module m
type t
contains
  procedure, nopass, NON_OVERRIDABLE :: test
end type t
contains
  subroutine test()
    print *, "t's test"
  end subroutine test

  subroutine callit(x)
    class(t) :: x
    call x%test()
  end subroutine
end module m
Comment 1 janus 2011-10-31 12:08:31 UTC
(In reply to comment #0)
> Expected:
> a) gfortran call the procedure directly, which allows for inlining and avoids
> the pointer dereference.

Good point. I hadn't thought of that ...


> Test case. The dump shows the following rather than a simple "test ()":

Here is an extended test case involving a type-bound subroutine and a type-bound function (both of which have to be fixed):


module m

type t
contains
  procedure, nopass, NON_OVERRIDABLE :: testsub
  procedure, nopass, NON_OVERRIDABLE :: testfun
end type t

contains

  subroutine testsub()
    print *, "t's test"
  end subroutine

  integer function testfun()
    testfun = 1
  end function

end module m


  use m
  class(t), allocatable :: x
  allocate(x)
  call x%testsub()
  print *,x%testfun()
end
Comment 2 janus 2011-10-31 12:31:03 UTC
Created attachment 25672 [details]
patch

Here is a patch, which does two things:
1) The resolve.c part prevents the conversion to a PPC call (for functions and subroutines).
2) The class.c parts prevents adding the non-overridable TBP to the vtable.

The second one breaks the ABI, while the first one does not. So, should I start by committing the first part only, and waiting with the second one for the ABI cleanup?
Comment 3 janus 2011-10-31 13:55:55 UTC
(In reply to comment #2)
> Created attachment 25672 [details]
> patch

... regtests cleanly!
Comment 4 Tobias Burnus 2011-10-31 14:41:19 UTC
(In reply to comment #2)
> Created attachment 25672 [details]
> patch

Thanks for the patch!

> The second one breaks the ABI, while the first one does not. So, should I start
> by committing the first part only, and waiting with the second one for the ABI
> cleanup?

I think that's sensible: Submitting/committing the first one. And for the second part: Add a link to this PR at http://gcc.gnu.org/wiki/LibgfortranAbiCleanup
Comment 5 janus 2011-11-07 18:41:16 UTC
Author: janus
Date: Mon Nov  7 18:41:12 2011
New Revision: 181107

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=181107
Log:
2011-11-07  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/50919
	* class.c (add_proc_comp): Don't add non-overridable procedures to the
	vtable.
	* resolve.c (resolve_typebound_function,resolve_typebound_subroutine):
	Don't generate a dynamic _vptr call for non-overridable procedures.

2011-11-07  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/50919
	* gfortran.dg/typebound_call_21.f03: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/typebound_call_21.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/class.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 janus 2011-11-07 18:46:27 UTC
Fixed with r181107 (including both parts of the patch in comment 2). Closing.