flang-compiler / flang

Flang is a Fortran language front-end designed for integration with LLVM.
Other
799 stars 134 forks source link

BigDFT: a rename list in the `USE` statement resulted in a link time error #1014

Closed pawosm-arm closed 3 years ago

pawosm-arm commented 3 years ago

Consider following code containing sub2=> sub rename list:

module a
  implicit none
  interface sub
    module procedure sub
  end interface

contains

  subroutine sub(i)
    implicit none
    integer, intent(in) :: i

    print *, "Hello ", i
  end subroutine

end module

module b
  use a
  implicit none
  private
  public :: sub
end module

module c
  use b
  implicit none
end module

program x
  use c, sub2=> sub
  use b
  implicit none

  call sub(10)
end program

This causes the following link-time issue:

$ flang -c rename.f90
$ flang -o rename rename.o
ld: rename.o: in function `MAIN_':
rename.f90:(.text+0xec): undefined reference to `sub_'
ld: rename.f90:(.text+0xf0): undefined reference to `sub_'
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

$ nm rename.o|grep sub
0000000000000008 T a_sub_
0000000000000010 r .C283_a_sub_
0000000000000018 r .C284_a_sub_
0000000000000024 r .C300_a_sub_
0000000000000030 r .C302_a_sub_
0000000000000020 r .C303_a_sub_
0000000000000008 r .C306_a_sub_
0000000000000004 r .C309_a_sub_
0000000000000000 r .C311_a_sub_
                 U sub_

The removal of the rename list as such:

program x
  use c
  use b
  implicit none

  call sub(10)
end program

...results in a successful linking and a correct sub symbol resolution:

$ nm rename|grep sub
00000000004009ec T a_sub_
0000000000400be8 r .C283_a_sub_
0000000000400bf0 r .C284_a_sub_
0000000000400bfc r .C300_a_sub_
0000000000400c08 r .C302_a_sub_
0000000000400bf8 r .C303_a_sub_
0000000000400be0 r .C306_a_sub_
0000000000400bdc r .C309_a_sub_
0000000000400bd8 r .C311_a_sub_

To make this problem even more complicated, just the removal of the private clause in the module b also solves the link time problem and results in a correct sub symbol resolution.

There was also somewhat symmetric issue observed. The removal of 1) the private clause in the module b and 2) the use b line (while leaving the use c, sub2=> sub line intact and still calling sub instead of sub2) results in link-time error with gfortran, yet it results in a correct compilation and linkage (and a correct sub symbol resolution) with classic flang! This only suggests that something is wrong with processing the rename lists in the calssic flang.

michalpasztamobica commented 3 years ago

Just to keep you posted, @pawosm-arm : removing this line fixes the issue and does not break any of the publicly available tests. I will try to dig deeper and come up with a less intrusive work-around and some explanation of what is going on in that part of apply_use() function.