https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127728
Bug ID: 127728
Summary: Fortran: arrays of derived types with allocatable
components accidentally quadratic
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: henri at henrimenke dot de
Target Milestone: ---
If a derived type `trunk` has an array component of another derived type `leaf`
that has allocatable components, the generated finalizer of `trunk` will have
every instance of the finalizer of `leaf` inlined resulting in a quadratic
number of free calls. In the example below the type `leaf` has 8 allocatable
components resulting in 8^2 + 2 = 66 calls to `free` in the finalizer of
`trunk`. This causes enormous code bloat and excessive compile times in
real-world codes.
Looks very related, maybe even duplicate:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81827
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87352
module m
type :: leaf
integer, allocatable :: a(:), b(:), c(:), d(:), e(:), f(:), g(:), h(:)
end type leaf
type :: trunk
type(leaf) :: component(2)
end type trunk
contains
subroutine use(arg)
class(trunk) :: arg
end subroutine use
end module m
$ gfortran --version
GNU Fortran (GCC) 16.2.1 20260819 (Red Hat 16.2.1-2)
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gfortran --shared -o test.so test.f90
$ objdump --disassemble=__m_MOD___final_m_Trunk test.so | grep free | wc -l
66