llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.99k stars 11.95k forks source link

[Flang] write statement in subroutine specified in FINAL statement is not executed correctly #108895

Closed ohno-fj closed 1 month ago

ohno-fj commented 1 month ago
Version of flang-new : 20.0.0(1cc4c87198ec0c6febca3f6ed03357e9e091cfd4)/AArch64

Execution error occurs because subroutine specified in final-subroutine-name-list of FINAL statement is not executed correctly.
Write statement in the subroutine creates a file named file.2 with no data.
Then the empty file is read, resulting in a runtime error.

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

sngg632d_.f90:

module m0
  type :: ty0
     integer :: ii
   contains
     FINAL::destructor
  end type ty0
  type   :: ty1
     integer :: jj
     integer :: kk
     type(ty0),allocatable :: ty0_obj
  end type ty1
contains
  subroutine destructor(dmy)
    IMPLICIT NONE
    TYPE(ty0), INTENT(INOUT):: dmy
    write(2,*)  dmy%ii
  end subroutine destructor
end module m0

program main
  use m0
  call sub
  rewind 2
  read(2,*) n
  if (n/=2) print *,701
  read(2,*,end=9) n
  if (n/=2) print *,702
9 print*, "pass"
contains
  subroutine s1(d1)
    type(ty1),INTENT(in)  :: d1
    integer :: v1, v2
    v1=d1%ty0_obj%ii
    v2=d1%jj
    v1=v1+v2
  end subroutine s1
  subroutine sub()
    type(ty0),save :: ty0_var
    ty0_var%ii=2
    call s1(ty1(10,10, ty0_var))
  end subroutine sub
end program main
$ flang-new sngg632d_.f90; ./a.out; ls -al fort.2; cat fort.2

fatal Fortran runtime error(/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/FFE_2019_generic_04/sngg632d_.f90:24): End of file during input
Aborted (core dumped)
-rw-------. 1 ohno ohno 0 Sep 10 13:39 fort.2
$
$ gfortran sngg632d_2.f90; ./a.out; ls -al fort.2; cat fort.2
 sngg632d : pass
-rw-r--r-- 1 ohno users 13 Sep 10  2024 fort.2
           2
$
$ ifort -diag-disable=10448 sngg632d_2.f90; ./a.out; ls -al fort.2; cat fort.2; cat fort.2
forrtl: No such file or directory
forrtl: severe (29): file not found, unit 2, file /home/ohno/CT/test/fort/tp/reproducerJ/Metro/FFE_2019_generic_04/fort.2
Image              PC                Routine            Line        Source
sngg632d_.f90.out  000000000040A08C  Unknown               Unknown  Unknown
sngg632d_.f90.out  000000000040420D  Unknown               Unknown  Unknown
sngg632d_.f90.out  000000000040416D  Unknown               Unknown  Unknown
libc.so.6          00007C89BEA29D90  Unknown               Unknown  Unknown
libc.so.6          00007C89BEA29E40  __libc_start_main     Unknown  Unknown
sngg632d_.f90.out  0000000000404085  Unknown               Unknown  Unknown
ls: cannot access 'fort.2': No such file or directory
cat: fort.2: No such file or directory
$
llvmbot commented 1 month ago

@llvm/issue-subscribers-flang-runtime

Author: None (ohno-fj)

``` Version of flang-new : 20.0.0(1cc4c87198ec0c6febca3f6ed03357e9e091cfd4)/AArch64 ``` Execution error occurs because `subroutine` specified in `final-subroutine-name-list` of `FINAL` statement is not executed correctly. `Write` statement in the `subroutine` creates a file named `file.2` with no data. Then the empty file is read, resulting in a runtime error. The following are the test program, Flang-new, Gfortran and ifort compilation/execution result. sngg632d_.f90: ```fortran module m0 type :: ty0 integer :: ii contains FINAL::destructor end type ty0 type :: ty1 integer :: jj integer :: kk type(ty0),allocatable :: ty0_obj end type ty1 contains subroutine destructor(dmy) IMPLICIT NONE TYPE(ty0), INTENT(INOUT):: dmy write(2,*) dmy%ii end subroutine destructor end module m0 program main use m0 call sub rewind 2 read(2,*) n if (n/=2) print *,701 read(2,*,end=9) n if (n/=2) print *,702 9 print*, "pass" contains subroutine s1(d1) type(ty1),INTENT(in) :: d1 integer :: v1, v2 v1=d1%ty0_obj%ii v2=d1%jj v1=v1+v2 end subroutine s1 subroutine sub() type(ty0),save :: ty0_var ty0_var%ii=2 call s1(ty1(10,10, ty0_var)) end subroutine sub end program main ``` ``` $ flang-new sngg632d_.f90; ./a.out; ls -al fort.2; cat fort.2 fatal Fortran runtime error(/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/FFE_2019_generic_04/sngg632d_.f90:24): End of file during input Aborted (core dumped) -rw-------. 1 ohno ohno 0 Sep 10 13:39 fort.2 $ ``` ``` $ gfortran sngg632d_2.f90; ./a.out; ls -al fort.2; cat fort.2 sngg632d : pass -rw-r--r-- 1 ohno users 13 Sep 10 2024 fort.2 2 $ ``` ``` $ ifort -diag-disable=10448 sngg632d_2.f90; ./a.out; ls -al fort.2; cat fort.2; cat fort.2 forrtl: No such file or directory forrtl: severe (29): file not found, unit 2, file /home/ohno/CT/test/fort/tp/reproducerJ/Metro/FFE_2019_generic_04/fort.2 Image PC Routine Line Source sngg632d_.f90.out 000000000040A08C Unknown Unknown Unknown sngg632d_.f90.out 000000000040420D Unknown Unknown Unknown sngg632d_.f90.out 000000000040416D Unknown Unknown Unknown libc.so.6 00007C89BEA29D90 Unknown Unknown Unknown libc.so.6 00007C89BEA29E40 __libc_start_main Unknown Unknown sngg632d_.f90.out 0000000000404085 Unknown Unknown Unknown ls: cannot access 'fort.2': No such file or directory cat: fort.2: No such file or directory $ ```
klausler commented 1 month ago

The FINAL subroutine in that program should never be called. Since the FINAL subroutine is the only place in the code that writes to unit 2, unit 2 is never written, and the READ statement later correctly fails.

(GNU Fortran does finalize the local variable ty0_var in subroutine sub, but that variable has the SAVE attribute and so the finalization is incorrect.)